【问题标题】:Python spell checker using a trie使用 trie 的 Python 拼写检查器
【发布时间】:2013-07-13 00:30:12
【问题描述】:

我正在尝试使用 trie 数据结构实现拼写检查器。我目前对Node 有以下大纲:

class Node:
    def __init__(self):
        self.next = {}
        self.word_marker = False

    def add_item(self, string):
        #if the length of the string is 0 then return. When the end of the word 
        #comes set the word_marker to true
        if len(string) == 0:
            self.word_marker = True
            return
        #set the key to the first letter of the string and reformat the string to reflect the first letter taken out
        #ultimately going to store the letters for each node as the key
        key = string[0]
        string = string[1:]

        #if there is a key in the dictionary, then recursively call add_item with new string
        if key in self.next:
            self.next[key].add_item(string)

        else:
            node = Node()
            self.next[key] = node
            node.add_item(string)

接下来我要做的是编写函数来搜索string,然后返回建议的拼写。 (def correct(self, string))。我将如何通过这个尝试来实现搜索?假设我已经通过定义一个根节点root 向树中添加了一个单词列表,然后对列表中的每个单词使用add_item

【问题讨论】:

  • d.has_key(k) 真的是老帽子了;使用k in d
  • 很酷的感谢会做出改变

标签: python trie


【解决方案1】:

如果您还没有,您可能想查看 Norvig 的“How to Write a Spelling Corrector

【讨论】:

  • 是的,看到了这个,但我不想依赖概率。我希望能够解决过滤 aaaappppllleee => apple 和 jella => jello 的能力。第一个是区分重复字母,第二个是替换元音。
  • cfarm54,阅读关于编辑距离的部分。听起来这就是你要找的东西。其次,不要被概率论的使用吓到。 Norvig 知道他在做什么,并且使用它是有原因的。如果您想要一个好的通用拼写校正器,那么这就是要走的路。手工编织的启发式方法只会让你走这么远..
  • 我看到了这个,但是我想尝试查看每个案例,看看是否有我们可以通过编程方式做的事情。
  • 为什么jella 映射到jello?元音有什么特别之处?字母 a 在 qwerty 键盘上紧挨着 s,所以jells 肯定是更可能的候选者。
  • 我认为需要涵盖许多情况,但我只想一次了解一种情况。元音,然后是辅音,然后是键盘距离。显然这会很快变得复杂,所以我想先做简单的。
【解决方案2】:

这里有与您的问题相关的答案:https://stackoverflow.com/a/11016430/793956

还可以考虑使用库,例如 https://github.com/kmike/marisa-trie#readmehttps://github.com/kmike/datrie#readme

【讨论】:

    【解决方案3】:

    虽然不是直接的答案,但您可能希望从更成熟的 Trie 实现开始,如下所示:

    class Trie:
    
        def __init__(self):
            self.__final = False
            self.__nodes = {}
    
        def __repr__(self):
            return 'Trie<len={}, final={}>'.format(len(self), self.__final)
    
        def __getstate__(self):
            return self.__final, self.__nodes
    
        def __setstate__(self, state):
            self.__final, self.__nodes = state
    
        def __len__(self):
            return len(self.__nodes)
    
        def __bool__(self):
            return self.__final
    
        def __contains__(self, array):
            try:
                return self[array]
            except KeyError:
                return False
    
        def __iter__(self):
            yield self
            for node in self.__nodes.values():
                yield from node
    
        def __getitem__(self, array):
            return self.__get(array, False)
    
        def create(self, array):
            self.__get(array, True).__final = True
    
        def read(self):
            yield from self.__read([])
    
        def update(self, array):
            self[array].__final = True
    
        def delete(self, array):
            self[array].__final = False
    
        def prune(self):
            for key, value in tuple(self.__nodes.items()):
                if not value.prune():
                    del self.__nodes[key]
            if not len(self):
                self.delete([])
            return self
    
        def __get(self, array, create):
            if array:
                head, *tail = array
                if create and head not in self.__nodes:
                    self.__nodes[head] = Trie()
                return self.__nodes[head].__get(tail, create)
            return self
    
        def __read(self, name):
            if self.__final:
                yield name
            for key, value in self.__nodes.items():
                yield from value.__read(name + [key])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-05
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多