1

そのため、テキスト マイニング プロジェクトに取り組んでおり、現在情報取得を実装しようとしています。各行がドキュメントを表すデータがあります。したがって、改行文字は異なるドキュメントを分割します。

列がすべてのドキュメントのすべての異なる単語であり、行が異なるドキュメントであるマトリックスを生成する必要があります。このテーブルの各セルは、単語がそのドキュメントに存在するかどうかを示す 1 (真) または 0 (偽) です。987 個のドキュメントがあり、合計単語数は 22860 であり、合計個別単語数は 3680 です。したがって、3680 単語が 22860 と比較されます。これは実行が遅くなりますが、問題ありません。時間がかかるループは、単語のリストのオブジェクトをトラバースして行列を生成するときです。下記参照

注: ドキュメント内で繰り返されるすべての単語を既に削除しました。

 class word_list
        {
            public string word;
            public List<bool> doc= new List<bool>();
        };//class ends

        private void button2_Click(object sender, EventArgs e)
        {
            //Convert the string into an array of words 
            string[] w1 = richTextBox1.Text.Trim().Split('\n',' ').Select(x => x.Trim().ToLower()).Distinct().ToArray(); //all distinct words
            string[] rich_doc = richTextBox1.Text.Trim().Split('\n'); //all documents array
            List<word_list> words = new List<word_list>();

            richTextBox2.Text+=("no. of distict words: " + w1.Length + ", no. of docs " + rich_doc.Length);
            for (int i = 0; i < w1.Length; i++)
            {
                word_list temp = new word_list();
                temp.word = w1[i]; //temp has the current distict word as class object

                for(int j=0;j<rich_doc.Length;j++)//traverse all doc array
                {
                    temp.doc.Add(false);
                    List<string> doc_word = Regex.Split(rich_doc[j], @"\b").Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
                    //richTextBox2.Text += ("\n no. of words in this doc: " + doc_word.Count);
                    //richTextBox2.SelectionStart = richTextBox1.Text.Length;
                    //richTextBox2.Focus();
                    int doc_count = doc_word.Count; // number of docs
                    for (int k = 0; k < doc_count; k++)//All words in a doc are compared
                    {
                        if(doc_word[k].ToLower() == w1[i].ToLower())
                        {
                            temp.doc[temp.doc.Count-1]=true;                            
                            break;
                        }
                    }                      
                }
                if ((words.Count - 1)>=0)
                    richTextBox2.Text += ("\n word(" + words.Count + "/" + w1.Length + "): " + words[words.Count - 1].word);
                richTextBox2.SelectionStart = richTextBox1.Text.Length;
                richTextBox2.Focus();
                words.Add(temp);
            }
            //generate matrix
            int t = rich_doc.Length; //no. of docs
            int word_count = words.Count;
            richTextBox1.Text = "Doc";
            foreach (word_list w in words)
            {
                richTextBox1.Text += "\t" + w.word;
            }
            richTextBox1.Text += "\n";
//This loop is slow
            for (int i = 0; i < t; i++) //traverse through number of docs
            {
                richTextBox1.Text += i + 1;
                for (int h = 0; h < word_count; h++)//traverse through each distinct word in the list
                {
                    if (words[h].doc[i])
                        richTextBox1.Text += "\t1";
                    else
                        richTextBox1.Text += "\t0";
                }
                richTextBox1.Text += "\n";
            }
        }//end of button 2
4

1 に答える 1