Advertisement
CapsAdmin

Untitled

Apr 22nd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local sounddict = {}
  2.  
  3. for key, list in pairs(chatsounds.List) do
  4.     for key, data in pairs(list) do
  5.         sounddict[key] = ""
  6.     end
  7. end
  8.  
  9. local soundtree = {}
  10. local soundtree_filekey = newproxy and newproxy() or {}
  11. for sound_patt, sound_file in pairs(sounddict) do
  12.     local currnode = soundtree
  13.     for part in string.gmatch(sound_patt, "([a-z_]+)") do
  14.         -- TODO: preprocess part to ignore case and such
  15.         currnode[part] = currnode[part] or {}
  16.         currnode = currnode[part]
  17.     end
  18.     currnode[soundtree_filekey] = string.gsub(sound_patt, " ", "_")..".wav"
  19. end
  20.  
  21. function stuff(soundtree, phrase)
  22.     -- TODO: preprocess phrase to ignore case and such
  23.    
  24.     local foundlist = {}
  25.    
  26.     local stack_i = 0
  27.     local stack_eposlist = { [0] = 1 }
  28.     local stack_nodelist = { [0] = soundtree }
  29.     local complete, atend = false, false
  30.     local backtrack = false
  31.     repeat
  32.         local spos = stack_eposlist[stack_i]
  33.         local node = stack_nodelist[stack_i]
  34.         stack_i = stack_i+1
  35.         -- get next word
  36.         --local word_s, word, word_e, mods = string.match(phrase, "()([a-z_]+)()(%p%d)*", spos, true)
  37.         local word_s, word_e, mods = string.find(phrase, "([a-z_]+)", spos)
  38.         if word_s then
  39.             --print(stack_i..": "..string.sub(phrase, word_s, word_e))
  40.             -- we have a next word
  41.             local word = string.sub(phrase, word_s, word_e)
  42.             local subnode = node[word] -- (you could do pattern matching here)
  43.             if subnode then
  44.                 -- we have a pattern that matches this next word
  45.                 -- (could be first, middle or last word in pattern!)
  46.                 stack_eposlist[stack_i] = word_e+1
  47.                 stack_nodelist[stack_i] = subnode
  48.                 -- continue
  49.             else
  50.                 -- there is no continuing pattern that matches this word
  51.                 if stack_i == 1 then
  52.                     -- this is the first word or next unmatched word and no pattern starts with it
  53.                     -- so jump to next word
  54.                     stack_i = 0
  55.                     stack_eposlist[0] = word_e+1
  56.                 else
  57.                     -- we were matching a pattern, see what comes of it
  58.                     -- (this word did not continue any pattern, so it's not considered)
  59.                     backtrack = true
  60.                 end
  61.             end
  62.         else
  63.             --print(stack_i..": END")
  64.             -- there is no next word, check for patterns
  65.             -- (last few words may be rechecked if there is no sound for their nodes)
  66.             if stack_i == 1 then
  67.                 -- we weren't matching anything
  68.                 complete = true
  69.             else    
  70.                 atend = true
  71.                 backtrack = true
  72.             end    
  73.         end
  74.         if backtrack then
  75.             -- find the longest pattern on the stack that has a sound associated
  76.             for stack_j = stack_i-1, 1, -1 do
  77.                 -- work backwards through each word to see if it has a sound
  78.                 local node = stack_nodelist[stack_j]
  79.                 local soundfile = node[soundtree_filekey]
  80.                 if soundfile then
  81.                     -- this word has a sound
  82.                     table.insert(foundlist, soundfile)
  83.                     -- TODO: handle mods here
  84.                    
  85.                     if stack_j == stack_i-1 and atend then
  86.                         -- we consumed the last word
  87.                         complete = true
  88.                     else
  89.                         -- start searching for new patterns
  90.                         stack_i = 0
  91.                         -- resume from word after longest pattern with a sound
  92.                         stack_eposlist[0] = stack_eposlist[stack_j]
  93.                     end
  94.                     -- stop backtracking
  95.                     break
  96.                 end
  97.             end
  98.             backtrack, atend = false, false
  99.         end
  100.     until complete
  101.    
  102.     return foundlist
  103. end
  104.  
  105. PrintTable(stuff(soundtree, "how could one man have slipped through your forces fingers time and time again how is it possible this is not some agent provocateur or highly trained assassin we are discussing gordon freeman is a theoretical physicist who had hardly earned the distinction of his ph d at the time of the black mesa incident i have good reason to believe that in the intervening years he was in a state that precluded further development of covert skills the man you have consistently failed to slow let alone capture is by all standards simply that an ordinary man how can you have failed to apprehend"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement