2017-03-08 149 views
1

如果一個字符串接近表中的字符串,它會用表中的字符串取代嗎?如何在lua中創建一個字符串字典函數?

像一個拼寫檢查函數,它搜索一個表,如果輸入接近於表中的一個,它將修復它,所以表中的一個和字符串是相同的?

+0

對不起,我是一個緩慢的學習者,關於處理字符串。 –

+0

您應該使用[Edit distance](https://en.wikipedia.org/wiki/Edit_distance) –

+0

將字符串與字典中的所有字符串進行比較在實踐中,[Jaro-Winkler distance](https://en.wikipedia。 org/wiki/Jaro%E2%80%93Winkler_distance)通常會提供更逼真的結果。 –

回答

1

您可以使用此代碼:)參考代碼是從這裏:https://github.com/badarsh2/Algorithm-Implementations/blob/master/Levenshtein_distance/Lua/Yonaba/levenshtein.lua

local function min(a, b, c) 
    return math.min(math.min(a, b), c) 
end 

local function matrix(row,col) 
    local m = {} 
    for i = 1,row do m[i] = {} 
    for j = 1,col do m[i][j] = 0 end 
    end 
    return m 
end 

local function lev(strA,strB) 
    local M = matrix(#strA+1,#strB+1) 
    local i, j, cost 
    local row, col = #M, #M[1] 
    for i = 1, row do M[i][1] = i - 1 end 
    for j = 1, col do M[1][j] = j - 1 end 
    for i = 2, row do 
    for j = 2, col do 
     if (strA:sub(i - 1, i - 1) == strB:sub(j - 1, j - 1)) then cost = 0 
     else cost = 1 
     end 
    M[i][j] = min(M[i-1][j] + 1,M[i][j - 1] + 1,M[i - 1][j - 1] + cost) 
    end 
    end 
    return M[row][col] 
end 

local refTable = {"hell", "screen"} 

local function getClosestWord(pInput, pTable, threesold) 
    cDist = -1 
    cWord = "" 
    for key, val in pairs(pTable) do 
    local levRes = lev(pInput, val) 
    if levRes < cDist or cDist == -1 then 
     cDist = levRes 
     cWord = val 
    end 
    end 
    print(cDist) 
    if cDist <= threesold then 
    return cWord 
    else 
    return pInput 
    end 
end 

a = getClosestWord("hello", refTable, 3) 
b = getClosestWord("screw", refTable, 3) 
print(a, b) 

第三個參數是threesold,如果最小距離大於threesold高,字不被替換。

+0

謝謝,這將有助於我的luabot發展很多! –

+0

祝你機器人好運:) – Sygmei