【问题标题】:Creating an object in Python from multiple classes在 Python 中从多个类创建一个对象
【发布时间】:2022-11-15 23:32:33
【问题描述】:

我正在创建一个拼写检查器,我将在其中接受一个输入词,然后生成一个编辑距离为 1 的词列表,同时检查这些词是否可以在我将创建的三元树中找到。这棵树将使用有效单词列表制作。这段代码只能修改带有### TODO: YOUR CODE HERE ###的函数。

valid_words = ['the', 'of', 'and', 'to', 'a', 'in', 'for', 'is', 'on', 'that']

class Node:
   def __init__(self, value):
        self.left_child = None
        self.middle_child = None
        self.right_child = None
        self.value = value
        self.is_end = False

class TernarySearchTree:
    def __init__(self):
        self.root_node = None

    def insert(self, word, node=None):
        ### TODO: YOUR CODE HERE ###    
        if len(word) == 0:
            return node
        
        head = word[0]
        tail = word[1:]
        if node is None:
            node = Node(head)
    
        if head < node.value:
            node.left_child = self.insert(word, node.left_child)
        elif head > node.value:
            node.right_child = self.insert(word, node.right_child)
        else:
            if len(tail) == 0:
              node.is_end = True
            else:
              node.middle_child = self.insert(tail, node.middle_child)
                
        return node
            
    def contains(self, word, node=None):
        ### TODO: YOUR CODE HERE ###        

        if node is None or len(word) == 0:
          return False

        head = word[0]
        tail = word[1:]

        if (head < node.value) :
          return self.contains(word, node.left_child)
        elif (head > node.value) :
          return self.contains(word, node.right_child)
        else:
          if len(tail) == 0 and node.is_end:
            return True
          return self.contains(tail, node.middle_child)

class Spellchecker:
    def __init__(self, valid_words):
        ### TODO: YOUR CODE HERE ###

        tree = TernarySearchTree()

        for word in valid_words:
          tree.root_node = tree.insert(word, tree.root_node)
          
    def getNearbyStrings(self, word):
        letters    = 'abcdefghijklmnopqrstuvwxyz'
        splits     = [(word[:i], word[i:])    for i in range(len(word) + 1)]
        deletes    = [L + R[1:]               for L, R in splits if R]
        transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
        replaces   = [L + c + R[1:]           for L, R in splits if R for c in letters]
        inserts    = [L + c + R               for L, R in splits for c in letters]
        return list(set(deletes + transposes + replaces + inserts))

    def make_suggestions(self, word):
        ### TODO: YOUR CODE HERE ###

        nearby_strings_list = self.getNearbyStrings(word)
        edit_distance1_list = []
        tree = TernarySearchTree()

        for i in nearby_strings_list:
          if (tree.contains(i, tree.root_node)):
            edit_distance1_list.append(i)
                  
        return edit_distance1_list

spellchecker = Spellchecker(valid_words)
output = spellchecker.make_suggestions(input())
output.sort()
for word in output:
    print(word)

我的问题与make_suggestions 函数有关。我能够使用spellchecker = Spellchecker(valid_words) 创建一棵树,但是如何将它的内容分配给属于TernarySearchTree 类的对象,以便我可以从该类调用contains 函数? make_suggestions 检查单词是否在树中,并将其附加到它将返回的列表中。

【问题讨论】:

    标签: python tree spell-checking ternary-tree


    【解决方案1】:
    class Spellchecker:
        def __init__(self, valid_words):
            # […]
    
            tree = TernarySearchTree()
    
            for word in valid_words:
              tree.root_node = tree.insert(word, tree.root_node)
              
        # […]
    
        def make_suggestions(self, word):
            # […]
    
            nearby_strings_list = self.getNearbyStrings(word)
            edit_distance1_list = []
            tree = TernarySearchTree()
    
            for i in nearby_strings_list:
              if (tree.contains(i, tree.root_node)):
                edit_distance1_list.append(i)
                      
            return edit_distance1_list
    

    make_suggestions 中,您不应该创建另一个 TernarySearchTree。您已经在__init__ 中创建了一个并且它已经包含了有效的单词。

    __init__ 中,您应该将此树分配为实例变量,以便您可以从make_suggestions 使用它。

    就像是:

    class Spellchecker:
        def __init__(self, valid_words):
            # […]
    
            tree = TernarySearchTree()
    
            for word in valid_words:
              tree.root_node = tree.insert(word, tree.root_node)
    
            self.tree = tree  # add this
              
        # […]
    
        def make_suggestions(self, word):
            # […]
    
            nearby_strings_list = self.getNearbyStrings(word)
            edit_distance1_list = []
            tree = self.tree  # use this instead of creating a new one
    
            for i in nearby_strings_list:
              if (tree.contains(i, tree.root_node)):
                edit_distance1_list.append(i)
                      
            return edit_distance1_list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多