1

我正在尝试将英语词典(这个特定词典中大约 109,000 个单词)存储到 Dictionary> 数据结构中,但我很难弄清楚如何去做。我目前的方法想法是将单词的第一个字符存储为键值,然后将单词存储在列表(“wordlist”)中。当键从“a”变为“b”时(或“b”变为“c”等)就是我卡住的地方,因为我无法弄清楚如何处理列表。这是我的努力。非常感谢任何帮助。

public Dictionary<char, IEnumerable<string>> populateWordDictionary()
    {
        Dictionary<char, IEnumerable<string>> wordDictionary = new Dictionary<char, IEnumerable<string>>();
        //List<string> wordList;
        connect = new SqlConnection(connectionString);
        SqlCommand find = new SqlCommand("Select * FROM English order by Word", connect);

        // starting with first record, store each word into the List<string> wordlist
        SqlDataReader dr = null;
        try
        {
            connect.Open();
            dr = find.ExecuteReader();
            char key;
            char keyStartingPosition = 'a';
            List<string> wordList = new List<string>();
            while (dr.Read())
            {
                // if a word is present
                if (!dr.IsDBNull(0))
                {
                    // set Dictionary key value to the first letter in the word being evaluated
                    key = Convert.ToChar(dr[1].ToString().Substring(0, 1));

                    // if the key value is not the same as the starting position, clear the list
                    // i.e., we've advanced to the next letter in the alphabet
                    if (key != keyStartingPosition)
                    {   
                        // add the current list to the dictionary
                        wordDictionary.Add(key, wordList);
                        // advance key starting position to the new key value
                        keyStartingPosition = key;
                        // and clear current content of wordList
                        wordList.Clear();
                    }

                    // if the first letter of the word list hasn't advanced in the alphebet
                    // simply store the word to the current list.
                    if (key == keyStartingPosition)
                    {
                        wordList.Add(dr[1].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            connect.Close();
        }

        return wordDictionary;

    }
4

2 回答 2

1

您的一般方法很好,除了wordList.Clear()部分:您继续重复使用相同的列表,并将其副本插入所有键。结果,所有键都以与最后一个键相同的单词列表结束。

要解决此问题,请替换wordList.Clear()

wordList = new List<string>();

并将行移到行前wordDictionary.Add

if (key != keyStartingPosition)
{   
    wordList = new List<string>();
    // add the current list to the dictionary
    wordDictionary.Add(key, wordList);
    // advance key starting position to the new key value
    keyStartingPosition = key;
}

另请注意,由于添加是在遇到给定字母中的第一个单词时发生的,因此您需要替换keyStartingPosition = 'a'keyStartingPosition = '@'或另一个不能开始真实单词的字母。

于 2013-07-26T00:58:02.993 回答
0

这可以使用 LINQ 更简单地完成:

...
SqlCommand find = ...
var words = this.ToEnumerable(find);
// returns ILookup<char, string>
var wordDictionary = words.ToLookup(w => w[0]);
// wordDictionary['a'] gives an IEnumerable<string> for the words starting with a

// if you really want to use a dictionary, do:
var wordDictionary = works.GroupBy(w => w[0])
    .ToDictionary(g => g.Key, g => g.ToList());

// the ToEnumerable implementation
private IEnumerable<string> ToEnumerable(SqlCommand find)
{
    using (var reader = find.ExecuteReader) {
        while (reader.Read()) {
           if (!reader.IsDBNull(0)) { yield return reader[1].ToString(); }
        }
    }
}
于 2013-07-26T01:09:57.620 回答