CapsAdmin

Untitled

Apr 2nd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.20 KB | None | 0 0
  1. local tree = {}
  2. TREE = tree
  3.  
  4. local function insert(sentence, path)
  5.     local words = sentence:Split(" ")
  6.     local current = tree
  7.     for k, v in pairs(words) do
  8.         current[v] = current[v] or {}
  9.         current = current[v]
  10.     end
  11.     current[#current + 1] = path
  12. end
  13.  
  14. local function clean(text)
  15.     return text:gsub("[^%w ]+", ""):gsub(" +", " "):lower():Trim()
  16. end
  17.  
  18. local function index()
  19.     for k1, v1 in pairs(chatsounds.List) do
  20.         for k2, v2 in pairs(v1) do
  21.             if type(v2) == "table" then
  22.                 for k3, v3 in ipairs(v2) do
  23.                     insert(clean(k2), v3.path)
  24.                 end
  25.             end
  26.         end
  27.     end
  28. end
  29.  
  30. index()
  31.  
  32. function match(text)
  33.     local words = clean(text):Split(" ")
  34.     local node = tree
  35.     local current
  36.    
  37.     local found = {}
  38.     local sentence = {}
  39.    
  40.     for i = 1, #words+1 do
  41.         local word = words[i]
  42.        
  43.         ::SIGH::
  44.        
  45.         if node[word] then
  46.             table.insert(sentence, word)
  47.             node = node[word]
  48.         elseif #sentence > 0 then
  49.             sentence = table.concat(sentence, " ")
  50.             table.insert(found, {key = sentence, data = node})
  51.             sentence = {}
  52.             node = tree
  53.            
  54.             goto SIGH
  55.         end
  56.     end
  57.        
  58.     for k,v in pairs(found) do
  59.         chat.AddText(v.key)
  60.     end
  61. end
  62.  
  63. hook.Remove("OnPlayerChat", "a", function(a, b)
  64.     match(b)
  65. end)
Advertisement
Add Comment
Please, Sign In to add comment