0

ユーザーが文字のグループを入力し、文字が少しごちゃ混ぜになった後、データベースに保存した単語リストと比較される、スクラブル詐欺師のようなプログラムに取り組んでいます。文字の組み合わせごとにデータベースを検索するのは非常に時間がかかるため、最初に単語をリストに保存して、メモリ内で使用できるようにしました。これにより、パフォーマンスが大幅に向上しました。しかし、キーが単語の最初の文字である Hashtable の List に単語リストを格納することで、さらに高速化したいと考えていますが、どうすればよいかについて少し固執しています。

私が持っている(そして明らかにうまくいかない)のは次のとおりです。

public Hashtable populateWordTable()
    {
        Hashtable wordTable = new Hashtable();
        List<string> wordTableWordList = new List<string>();
        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;
        int found = 0;
        try
        {
            connect.Open();
            dr = find.ExecuteReader();
            string key = string.Empty;
            string keyStartingPosition = "a";
            while (dr.Read())
            {
                // if word is present
                if (!dr.IsDBNull(0))
                {
                    found = Convert.ToInt32(dr[0]);
                    key = dr[1].ToString().Substring(0, 1);                        
                }
                if (found > 0)
                {
                    // if we have transitioned to the next letter in the alphabet
                    if (key != keyStartingPosition)
                    {
                        wordTable.Add(keyStartingPosition, wordTableWordList);
                        List<string> newList = new List<string>();
                        newList.Add(dr[1].ToString());
                        keyStartingPosition = key;
                    }
                    // still in the same letter in the alphabet
                    else
                    {
                        wordTableWordList.Add(dr[1].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            connect.Close();
        }

        return wordTable;
    }
4

2 に答える 2

2

以前にこのようなことをしたことがありますが、最良の構造は DAWG (有向非循環ワードグラフ) であることがわかりました。昨年、Steve Hanov の Python DAWG ビルダーを C#に翻訳しましたが、RAID ドライブが故障したときに失われました。C# に変換するのはそれほど難しいことではありません。

于 2013-07-24T18:04:43.110 に答える
1

Dictionary<char, IEnumerable<string>>( Reference )を使用する必要があります。その使用法はかなり簡単です。単語を追加するときは、最初にそれがあるかどうかを確認しContainsKey(word[0])、そうでない場合はそれを確認しますAdd。最初に空のリストを割り当て、反復ごとに埋めるか、最初にリストを作成してからキーに割り当てることができます。それはあなた次第です。

于 2013-07-24T18:05:11.093 に答える