2016-07-17 6 views
2

pythonT9辞書を実装しようとしています。私はそれを実装するためにTrieを使用しています。私はこれを実行すると、私は、出力権利として[hello,hess]を取得する必要があり、辞書内の単語からTrieを作成し、このコードを持って、その後、パターンPythonでT9辞書を実装すると間違った出力があります

import string 

PHONE_LETTERS = 'abcdefghijklmnopqrstuvwxyz' 
PHONE_NUMBERS = '22233344455566677778889999' 
PHONE_TRANS = string.maketrans(PHONE_LETTERS, PHONE_NUMBERS) 

class Node: 

    def __init__(self, key): 
     self.children = {} 
     self.key = key 
     self.values = [] 


def append_word(node, sequence, completeword): 
    if not sequence: 
     return 
    key = sequence[0] 
    try: 
     child = node.children[key] 
    except KeyError: 
     child = Node(key) 
     node.children[key] = child 
    if len(sequence) == 1: 
     child.values.append(completeword) 
    else: 
     append_word(child, sequence[1:], completeword) 


def lookup(node, sequence=None): 
    if sequence: 
     # there are still numbers in the sequence: follow them in the trie 
     try: 
      child = node.children[sequence[0]] 
      return lookup(child, sequence[1:]) 
     except KeyError: 
      return [] 
    else: 
     # the sequence is empty: explore the trie using a DFS 
     result = node.values[:] 
     for child in node.children.values(): 
      result.extend(lookup(child)) 
     return result 


def main(): 
    root = Node(None) 

    words = ['hello','hess','home','abhi','busy','disturb'] 
    for wlist in words: 
     print wlist 
     map(lambda l: append_word(root, l.strip().translate(PHONE_TRANS), l.strip()), wlist) 

    words = sorted(lookup(root, '43')) 
    print "Words: %s" % words 


if __name__ == '__main__': 
    main() 

のための検索を行いますか?しかし、私は空のリストを取得します。私はここで何の間違いをしていますか?あなたはappend機能をmap

答えて

1

あなたは本当に全体wlist文字列の上にマッピングするために意味されていますか?

あなたが代わりにあなたが得る個々の文字のwlist列にtranslateを呼び出す場合:

import string 

PHONE_LETTERS = 'abcdefghijklmnopqrstuvwxyz' 
PHONE_NUMBERS = '22233344455566677778889999' 
PHONE_TRANS = string.maketrans(PHONE_LETTERS, PHONE_NUMBERS) 

class Node: 

    def __init__(self, key): 
     self.children = {} 
     self.key = key 
     self.values = [] 


def append_word(node, sequence, completeword): 
    if not sequence: 
     return 
    key = sequence[0] 
    try: 
     child = node.children[key] 
    except KeyError: 
     child = Node(key) 
     node.children[key] = child 
    if len(sequence) == 1: 
     child.values.append(completeword) 
    else: 
     append_word(child, sequence[1:], completeword) 


def lookup(node, sequence=None): 
    if sequence: 
     # there are still numbers in the sequence: follow them in the trie 
     try: 
      child = node.children[sequence[0]] 
      return lookup(child, sequence[1:]) 
     except KeyError: 
      return [] 
    else: 
     # the sequence is empty: explore the trie using a DFS 
     result = node.values[:] 
     for child in node.children.values(): 
      result.extend(lookup(child)) 
     return result 


def main(): 
    root = Node(None) 

    words = ['hello','hess','home','abhi','busy','disturb'] 
    for wlist in words: 
     print wlist 
     # XXX here, you shouldn't be mapping the `translate` call onto the entire string 
     append_word(root, wlist.strip().translate(PHONE_TRANS), wlist) 

    words = sorted(lookup(root, '43')) 
    print "Words: %s" % words 


if __name__ == '__main__': 
    main() 
+0

感謝:

hello hess home abhi busy disturb Words: ['hello', 'hess'] 

あなたがしなければならないのは、map呼び出しを削除です!私はmap()が間違った出力を引き起こしていることに気付かなかった。 – user2916886

関連する問題