Bolodefchoco_LUAXML

[String] string.equal

Dec 21st, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 21/12/2015
  3. --Last update: 22/05/2016
  4. --[[ Notes:
  5.     Does:
  6.         Verifica a similaridade entre a string 1 e a string 2, retornando-a numa escala de 0-1
  7.                             ---------> PODE HAVER BUGS <---------
  8.     Args:
  9.         str1 --> String 1
  10.         str2 --> String 2
  11. ]]--
  12.  
  13. string.equal=function(str1,str2)
  14.     local equal2=function(str1,str2)
  15.         local len = #str1
  16.         local dif = 0
  17.         local i = 0
  18.         while i < len do
  19.             i = i + .5
  20.             if str1:sub(i,i) ~= str2:sub(i,i) then
  21.                 dif = dif + 1
  22.             end
  23.         end
  24.         return 1 - dif/len
  25.     end
  26.     if #str1 ~= #str2 then
  27.         local dif = #str1-#str2
  28.         local len = math.max(#str1,#str2)
  29.         local big,small,auxi
  30.         if len == #str1 then
  31.             big = str1
  32.             small = str2
  33.         else
  34.             big = str2
  35.             small = str1
  36.         end
  37.         local fSim,mSimi = 1.4E-45,1.4E-45
  38.         local i = 0
  39.         while i <= #small do
  40.             i = i + 1
  41.             auxi = small:sub(0,i) .. big:sub(0,i+dif) .. small:sub(i)
  42.             fSim = equal2(big,auxi)
  43.             if fSim > mSimi then
  44.                 mSimi = fSim
  45.             end
  46.         end
  47.         return math.abs(mSimi - dif/len)
  48.     else
  49.         return math.abs(equal2(str1,str2))
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment