Advertisement
WarPie90

AnagramSolver

Mar 1st, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. # simplified version
  2. class Node():
  3.   def __init__(self, value, children, last=False):
  4.     self.value = value
  5.     self.children = children
  6.     self.isword = last
  7.  
  8. class WordList(object):
  9.   def __init__(self, words):
  10.     self.root = Node(None,dict())
  11.    
  12.     ''' naive building of the tree '''
  13.     for word in wlist:
  14.       self.insert(word.lower())
  15.  
  16.   def insert(self, word):
  17.     root = self.root
  18.     size = len(word)-1
  19.     for i,char in enumerate(word):
  20.       item = root.children.get(char)
  21.       if item is None:
  22.         n = Node(char,dict(),i==size)
  23.         root.children[char] = n
  24.         root = root.children[char]
  25.       else:
  26.         if i==size:
  27.           item.isword = True
  28.         root = item
  29.  
  30.   def match_chars(self,chars,root=None, pre=''):
  31.     '''
  32.      recursivly walk through nodes and gather all the
  33.      words that fits the given characters
  34.    '''
  35.     res = set()
  36.     if root is None: root = self.root
  37.    
  38.     for i,char in enumerate(chars):
  39.       new_chars = chars[:]
  40.       del new_chars[i]
  41.      
  42.       item = root.children.get(char)
  43.       if item is None:
  44.         continue
  45.      
  46.       node = item
  47.       if node.isword:
  48.         res.add(pre+char)
  49.       res.update(self.match_chars(new_chars,node,pre+char))
  50.     return res
  51.  
  52.  
  53. class AnagramSolver(object):
  54.   def __init__(self,words):
  55.     self.dictionary = WordList(words)
  56.    
  57.   def __buildAnagrams(self, word, alts, agram=[]):
  58.     res = []
  59.     for alt in alts:
  60.       tmp = list(word)
  61.       imp = False
  62.       for c in alt:
  63.         try:
  64.           idx = tmp.index(c)
  65.           del tmp[idx]
  66.         except:
  67.           imp = True
  68.           break;
  69.       if imp: continue;
  70.       n = agram[:] + [alt]
  71.       if len(tmp) == 0: #only insert if all chars are used
  72.         res.append(n)
  73.       else:
  74.         res += self.__buildAnagrams(tmp, alts, n)
  75.      
  76.     return res
  77.    
  78.   def generateAnagrams(self, word, limit=0):
  79.     if limit < 0:
  80.       raise ValueError('Limit less than 0 not supported')
  81.      
  82.     chars = list(word.lower())
  83.     words = self.dictionary.match_chars(chars)
  84.     result = sorted(self.__buildAnagrams(chars,words))
  85.     if limit != 0:
  86.       result = [l for l in result if len(l) <= limit]
  87.     return result
  88.  
  89.  
  90.  
  91. if __name__ == '__main__':
  92.   from time import time
  93.   with open('includes/wlist.txt') as f:
  94.     wlist = f.read().splitlines()
  95.  
  96.   solver = AnagramSolver(wlist)
  97.   print solver.generateAnagrams('officekey')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement