レーベンシュタイン距離を使用した文字列の類似度を調べるモジュール。類似度が高いと0に近くなります。
詳しい説明とかは http://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%BC%E3%83%99%E3%83%B3%E3%82%B7%E3%83%A5%E3%82%BF%E3%82%A4%E3%83%B3%E8%B7%9D%E9%9B%A2
レーベンシュタイン距離を使用した文字列の類似度を調べるモジュール。類似度が高いと0に近くなります。
詳しい説明とかは http://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%BC%E3%83%99%E3%83%B3%E3%82%B7%E3%83%A5%E3%82%BF%E3%82%A4%E3%83%B3%E8%B7%9D%E9%9B%A2
// Wikipediaに記載の擬似コードから変換しています// http://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%BC%E3%83%99%E3%83%B3%E3%82%B7%E3%83%A5%E3%82%BF%E3%82%A4%E3%83%B3%E8%B7%9D%E9%9B%A2#module#defcfunc min3 int p1, int p2, int p3if p1 > p2{if p2 > p3{res = p3}else{res = p2}}else{if p1 > p3{res = p3}else{res = p1}}return resreturn#defcfunc LevenshteinDistance str _s1, str _s2s1 = _s1 : s2 = _s2dim d, strlen(s1)+1, strlen(s2)+1i1 = 0 : i2 = 0 : cost = 0repeat strlen(s1)i1 = cntd(i1, 0) = i1looprepeat strlen(s2)i2 = cntd(0, i2) = i2looprepeat strlen(s1), 1i1 = cntrepeat strlen(s2), 1i2 = cntif peek(s1, i1) == peek(s2, i2) : cost = 0 : else : cost =1d(i1, i2) = min3( d(i1-1, i2) + 1, d(i1, i2-1) + 1, d(i1-1, i2-1) + cost)looploopreturn d(strlen(s1), strlen(s2))#globalmes LevenshteinDistance("HotSoupProcessor","HotSoupProcessor")mes LevenshteinDistance("HotSoupProcessor","ColdSoupProcessor")mes LevenshteinDistance("熱いスープ","冷たいスープ")mes LevenshteinDistance("オニオン","たまねぎ")