比較單詞相似度,必須首先切詞,英文單詞切詞法,combine排列組合的“組合”數學算法是關鍵。
下面代碼給出原子的一種思路:比如輸入單詞“spide”,輸出所有可能用來和其他單詞比較的原子,由於單詞長度是5,匹配度小於60%的沒意義,所以原子最小長度是3。
代碼是從C語言算法改過來的,同樣代碼也可以修改成任何編程語言。
ide
pde
sde
pie
sie
spe
pid
sid
spd
spi
pide
side
spde
spie
spid
spide
這裏用到了Combine 輸出全部組合,Combine 5,3就是得到在5個裏選3個的全部可能選法。
<SCRIPT LANGUAGE="vbScript">
str="spide"
''創建全局字典對象,用來存儲所有得到的原子結果
Set dict=CreateObject("Scripting.Dictionary")
Dim a(100)
strLength=Len(str)
''原子
atomyLength=round(strLength*0.6)
For x=atomyLength To strLength
a(0)=x
''計算5選3,5選4,5選5組合
combine strLength,x
next
sub combine(m, k)
''計算組合在m裏面選k個元素的全部組合情況,添加到字典對象裏
i=0
j=0
For i=m To k Step -1
a(k)=i
if (k>1) then
combine i-1,k-1
else
tempStr=""
for j=1 To a(0)
tempStr=tempStr & Mid(str,a(j),1)
Next
''排除重複的,加到字典裏
If Not dict. Exists(tempStr) then dict.add tempStr,Len(tempStr)
End if
next
End sub
Main()
Sub Main
''輸出顯示結果
For i=0 To dict.count-1
Document.write dict.keys()(i) & "<br/>"
next
End sub
</SCRIPT>