2012-10-24 61 views
0

可以說我有關鍵字它叫什麼 - 從關鍵詞列表中獲取最常見的單詞段?

free numerology compatibility 
numerology calculator free 
free numerology report 
numerology reading 
free numerology reading 
etc... 

列表通過什麼C#算法或什麼是叫這樣我就可以進一步研究它,當我想要得到下面的結果?

6 instances of "numerology" 
3 instances of "free numerology" 
2 instances of "numerology reading" 
1 instance of "numerology compatibility" 
1 instance of "numerology calculator" 
etc... 
+0

看看[Aho-Corasick字符串匹配算法] [1]維基百科條目。在C#中還有一個[CodeProject] [2]實現。 [1]:http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm [2]:http://www.codeproject.com/Articles/12383/Aho-Corasick-string-matching- in-C – McArthey

+1

搜索*詞頻分析 *或只是頻率分析 –

+0

我會稱之爲短語而不是關鍵詞,因爲您也在尋找訂單。 – Paparazzi

回答

0

你正在尋找的主題推移詞頻分析詞頻分析。下面的代碼可以給你的每個詞的出現次數的名稱。它也很容易找到給定短語的頻率,但是對整個文檔進行分析並且發現頻率高於1的項的序列有點複雜。

void Analyze(ref String InputText, ref Dictionary<string, int> WordFreq) 
{ 
    string []Words = InputText.Split(' '); 

    for (int i = 0; i < Words.Length; i++) 
    { 
     if (WordFreq.ContainsKey(Words[i]) == false) 
      WordFreq.Add(Words[i], 1); 
     else 
     { 
      WordFreq[Words[i]]++; 
     } 
    } 
} 

void DoWork() 
{ 
    string InputText = "free numerology compatibility numerology calculator free free numerology report numerology reading free numerology reading"; 
    Dictionary<string, int> WordFreq = new Dictionary<string,int>(); 

    Analyze(ref InputText,ref WordFreq); 

    string result = null; 
    foreach (KeyValuePair<string, int> pair in WordFreq) 
    { 
     result += pair.Value + " Instances of " + pair.Key + "\r\n"; 
    } 

    MessageBox.Show(result); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    DoWork(); 
} 
0

您可以遍歷單詞數組並使用字典存儲計數。

例如

Dictionary d = new Dictionary<string, int>(); 

foreach (string word in wordList) 
{ 
    if (d.ContainsKey(word)) 
    { 
     d[word]++; 
    } 
    else 
    { 
     d[word] = 1; 
    } 
} 
+0

他不只是在尋找單詞,他正在尋找短語。這使問題變得複雜。 – Servy