Advertisement
Bolodefchoco_LUAXML

[AI] Correção de palavras

Jan 27th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 28/01/2017
  3. --Last update: 28/01/2017
  4. --[[ Notes:
  5.     Verifica a melhor correção para texto (a primeira letra deve ser correta).
  6.     Exemplo: hpkr = hoje
  7. ]]--
  8.  
  9. text = "amyrms"
  10.  
  11. POTENCY=5
  12.  
  13. frequency = {
  14.     a =  0.08167,
  15.     b =  0.01492,
  16.     c =  0.02782,
  17.     d =  0.04253,
  18.     e =  0.12702,
  19.     f =  0.02228,
  20.     g =  0.02015,
  21.     h =  0.06094,
  22.     i =  0.06966,
  23.     j =  0.00153,
  24.     k =  0.00772,
  25.     l =  0.04025,
  26.     m =  0.02406,
  27.     n =  0.06749,
  28.     o =  0.07507,
  29.     p =  0.01929,
  30.     q =  0.00095,
  31.     r =  0.05987,
  32.     s =  0.06327,
  33.     t =  0.09056,
  34.     u =  0.02758,
  35.     v =  0.00978,
  36.     w =  0.02360,
  37.     x =  0.00150,
  38.     y =  0.01974,
  39.     z =  0.00074,
  40. }
  41. letters = {
  42.     q = {'a','w'},
  43.     w = {'q','a','s','e'},
  44.     e = {'w','s','d','r'},
  45.     r = {'e','d','f','t'},
  46.     t = {'r','f','g','y'},
  47.     y = {'t','g','h','u'},
  48.     u = {'y','h','j','i'},
  49.     i = {'u','j','k','o'},
  50.     o = {'i','k','l','p'},
  51.     p = {'o','l'},
  52.     a = {'q','w','s','z'},
  53.     s = {'a','w','e','d','z'},
  54.     d = {'s','e','r','f','x'},
  55.     f = {'d','r','t','g','c'},
  56.     g = {'f','t','y','h','v'},
  57.     h = {'g','y','u','j','b'},
  58.     j = {'h','u','i','k','n'},
  59.     k = {'j','i','o','l','m'},
  60.     l = {'k','o','p','m'},
  61.     z = {'a','s','x'},
  62.     x = {'z','d','c'},
  63.     c = {'x','f','v'},
  64.     v = {'c','g','b'},
  65.     b = {'v','h','n'},
  66.     n = {'b','j','m'},
  67.     m = {'n','k'}
  68. }
  69.  
  70. string.letterFrequency = function(str)
  71.     local freq = {}
  72.     for i = 1,#str do
  73.         local letter = str:sub(i,i)
  74.         if letter ~= " " then
  75.             freq[letter] = (freq[letter] and freq[letter]+1 or 1)
  76.         end
  77.     end
  78.     for k,v in next,freq do
  79.         freq[k] = v / #str
  80.     end
  81.     return freq
  82. end
  83.  
  84. string.compatibility = function(foo)
  85.     local d = 0
  86.     for k,v in next,frequency do
  87.         if foo.lfreq[k] then
  88.             d = d + math.abs(foo.lfreq[k] - v)
  89.         end
  90.     end
  91.     return 1-d
  92. end
  93.  
  94. string.rotate = function(str,t)
  95.     local newStr = ""
  96.     for k = 2,#str do
  97.         local l = str:lower():sub(k,k)
  98.         local i = letters[l]
  99.         i = i[#i>=t and t or t%#i+1]
  100.         newStr = newStr .. i
  101.     end
  102.     return str:sub(1,1) .. newStr
  103. end
  104.  
  105. io.write("Sentence: "..text.."\nPotency: "..POTENCY.."\n")
  106.  
  107. txtformat = "@Rotation: %d\n\t#Sentence: %s\n\t#Compatibility: %.18f%%\n"
  108.  
  109. solve = {}
  110. solution = {
  111.     rotation = 0,
  112.     compatibility = 0
  113. }
  114. for i = 1,POTENCY do
  115.     solve[i] = {}
  116.  
  117.     solve[i].txt = string.rotate(text,i)
  118.  
  119.     solve[i].lfreq = string.letterFrequency(solve[i].txt)
  120.  
  121.     solve[i].compatibility = string.compatibility(solve[i])
  122.  
  123.     if solution.compatibility < solve[i].compatibility then
  124.         io.write("\n$BEAT "..i.."\n")
  125.         solution = {
  126.             rotation = i,
  127.             compatibility = solve[i].compatibility
  128.         }
  129.     end
  130.  
  131.     io.write(txtformat:format(i,solve[i].txt,solve[i].compatibility))
  132. end
  133.  
  134. io.write("\n>> BEST\n\t" .. txtformat:format(solution.rotation,solve[solution.rotation].txt,solution.compatibility))
  135.  
  136. os.execute("pause >nul")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement