Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. def build_trie(strs, prefix=''):
  2.     strs = list(strs)
  3.     if not prefix: print strs
  4.     width = sys.maxint
  5.     leaf = True
  6.  
  7.     for s in strs:
  8.         if s != prefix:
  9.             width = min(len(s), width)
  10.             leaf = False
  11.  
  12.     if leaf:
  13.         return None
  14.  
  15.     prefix = dict()
  16.     for s in strs:
  17.         prefix.setdefault(s[:width], []).append(s)
  18.  
  19.     trie = dict((k, build_trie(v, k)) for k, v in prefix.iteritems())
  20.     trie['_width'] = width
  21.     return trie
  22.  
  23.  
  24. def match_trie(trie, text):
  25.     key = text[:trie['_width']]
  26.     if key in trie:
  27.         if trie[key]:
  28.             result = match_trie(trie[key], text)
  29.             if result:
  30.                 return result
  31.             elif key in trie[key]:
  32.                 return key
  33.         else:
  34.             return key
  35.     return None
  36.  
  37. ---SNIP---
  38.     def render(self, output):
  39.         new_output = []
  40.         exclude = set()
  41.  
  42.         idx = 0
  43.         last_match = 0
  44.         while idx < len(output):
  45.             for def_list in self.def_lists:
  46.                 match = match_trie(def_list['trie'], output[idx:].lower())
  47.                 try:
  48.                     if match and match not in exclude:
  49.                         # Exclude partial-word matches
  50.                         if output[idx + len(match)] not in string.letters:
  51.                             new_output.append(output[last_match:idx])
  52.  
  53.                             # vvv This part does the "highlighting" (actually linkifying in this case)
  54.                             replace = dict(original=output[idx:idx + len(match)])
  55.                             replace.update(def_list['items'][match])
  56.                             new_output.append(def_list['replace'] % replace)
  57.  
  58.                             idx = idx + len(match)
  59.                             last_match = idx
  60.                             if self.first_only:
  61.                                 exclude.add(match)
  62.                             break
  63.                 except IndexError:
  64.                     pass
  65.  
  66.             try:
  67.                 # Skip the rest of the word
  68.                 while output[idx] in string.letters:
  69.                     idx += 1
  70.  
  71.                 # Skip to the next word (also skip tags)
  72.                 tag = False
  73.                 while tag or output[idx] not in string.letters:
  74.                     if output[idx] == '<':
  75.                         tag = True
  76.                     elif output[idx] == '>':
  77.                         tag = False
  78.                     idx += 1
  79.             except IndexError:
  80.                 pass
  81.        
  82.         if new_output:
  83.             new_output.append(output[last_match:])
  84.             return ''.join(new_output)
  85.         else:
  86.             return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement