アットウィキロゴ

csharp_ngram

Ngramでテキスト比較


public class Ngram
{
    /// <summary>
    /// Bigramで比較
    /// </summary>
    public static float Compare(string strA, string strB)
    {
        return Compare(2, strA, strB);
    }
 
    public static float Compare(int n, string strA, string strB)
    {
        if (strA == null) throw new ArgumentNullException("strA");
        if (strB == null) throw new ArgumentNullException("strB");
 
        List<string> blistA = new List<string>();
        for (int i = 0; i < strA.Length - (n - 1); i++)
        {
            string ngitem = strA.Substring(i, n);
            if (!blistA.Contains(ngitem)) { blistA.Add(ngitem); }
        }
        if (blistA.Count == 0) return 0;
 
        int found = 0;
        List<string> blistB = new List<string>();
        for (int i = 0; i < strB.Length - (n - 1); i++)
        {
            string ngitem = strB.Substring(i, n);
            if (blistB.Contains(ngitem)) continue;
            if (blistA.Contains(ngitem)) { found++; }
            blistB.Add(ngitem);
        }
        if (blistB.Count == 0) return 0;
 
        return (float)found * 2 / (blistA.Count + blistB.Count);
    }
}
 
最終更新:2010年11月22日 13:29