Python 如何考虑生成所有单词并为输入重新修改给定字母的最佳单词

Python 如何考虑生成所有单词并为输入重新修改给定字母的最佳单词,python,algorithm,priority-queue,trie,Python,Algorithm,Priority Queue,Trie,我决定用python编写一个小应用程序,帮助我学习使用dvorak键盘布局打字。在我的算法课上,我们讨论了树,尝试,并实现了一个自动完成函数 我抓起一个单词表。然后,我将其中的所有单词加载到trie中(令人惊讶的是,这只花了大约三分之一秒),现在我正试图找出如何制作相关单词 我目前正在维护一个优先级队列,以跟踪用户输入错误最多的字母,因此我从这个队列中删除了3个字母作为开始。如果我想要所有以这些字母开头的单词,我可以这样做,然后可能只是过滤掉所有没有用户输入错误最多的其他字母的单词 是否有可能高

我决定用python编写一个小应用程序,帮助我学习使用dvorak键盘布局打字。在我的算法课上,我们讨论了树,尝试,并实现了一个自动完成函数

我抓起一个单词表。然后,我将其中的所有单词加载到trie中(令人惊讶的是,这只花了大约三分之一秒),现在我正试图找出如何制作相关单词

我目前正在维护一个优先级队列,以跟踪用户输入错误最多的字母,因此我从这个队列中删除了3个字母作为开始。如果我想要所有以这些字母开头的单词,我可以这样做,然后可能只是过滤掉所有没有用户输入错误最多的其他字母的单词

是否有可能高效地(甚至可能不高效地)获取一个包含优先级队列中字母的所有单词的列表,然后过滤掉,这样我就能得到对打字机最大挑战的单词


我可以用字符来实现这一点,但单词带来了一个有趣的挑战,因为trie的本质是只获取前缀以我们在队列中的字母开头的单词。

您可以重新计算trie以容纳所有子字符串(在真实单词本身之上),其中子字符串的结尾指向TRIE中的真实单词


通过这种方式,您可以使用已有的代码并将其应用于子字符串。

此处是否需要trie?我认为你要么不需要任何高级结构,要么你需要其他东西


你想处理多少单词?如果只需三分之一秒就可以将它们加载到trie,那么只需浏览所有它们并选择您想要的任何内容,所需时间不会太长。你每次都必须这样做,但如果只有1/3秒,那就没问题了。

好的。我提出的解决方案将@shapiro yaacov的答案与我编写的代码结合起来。 我扔掉了trie,每封信都用了一个带箱子的东西。每个单词被放入每个字母的一个箱子中,然后算法将字母相加,找出哪些单词有最想要的字母。对于我不想要的每一个字母,我还从单词中减去十分之一,以鼓励我的程序给出合理的单词,因为如果我只是简单地将所有单词与最多的字母相加,我会得到大量的单词

这是我的话。py:

import string
import random
import operator
class Bin:
    """ 
    A bin is a container that stores words given in a dictionary file.
    It is designed to retrieve all words in this file with the given letters.
    The words are stored in this container in an array and when new words get added,
    the container automatically adds the word to the words list,
    and places them into as many bins as need be.
    For example,
    >>> bin=Bin("words.txt") #get all words from bin.txt
    >>>bin.addWord("about") 
    now, the bins for a, b, o, u, t  will have a pointer to "about".
    Now immagine the bin has the words "king", "fish", and "dish" in it.
    >>> d=bin.getWordWithLetters("sh")
    >>> print d
    ["fish", "dish"]
    """

    def __init__(self, wordsFile):
        """initialize the container from the given file,
        if None, just initialize an empty container.
        """
        self.bins={}
        for i in string.ascii_lowercase+".'&": #these are the letters I need.
            self.bins[i]=[] #initialize an empty list for each bin. 
        if wordsFile == None:
            return
        with open(wordsFile) as words:
            for i in words:
                self.addWord(i.strip("\n"))

    def addWord(self, word):
        for i in word:
            self.bins[i].append(word) #add the word to the bin for each letter in that word.

    def getWordsWithLetters(self, lrs):
        """Gets best word that has the letters lrs in it.         
        For example, if abcdef is given, and the words [has, babe, shame] are there,
        [babe] would be returned because it is the word with the maximum return,
        since it contains b,a,e."""
        words=[]
        for i in lrs:
            words+=self.bins[i]
        #Now we go through the words, and calculate the score of each word.
        #a score is calculated by adding up the number of times a letter from lrs appears in each word.
        # Then we will subtract out the number of 
        for index, item in enumerate(words):
            score=random.randint(0,10) #give some randomness for the typing thing.
            #print(score)
            #score = 0 #to make it deterministic.           
            base=score
            itCounts={}
            for i in lrs:
                itCounts[i]=False
            for letter in item:
                if letter in lrs and (not itCounts[letter]):
                    score+=1
                    itCounts[letter]= True
                else:
                    score-=.1
            words[index] = (item, score)
        words = sorted(words, key=operator.itemgetter(1), reverse=True)
        w=[]
        for i in words:
            if i[1] > base:
                w.append(i[0])
        return w[:50]

我将如何处理每一个问题?只是线性搜索?这难道不需要很长时间才能看出单词中是否有每个字母吗?@DerekRiemer,最简单的方法是对每个单词进行一次线性传递,你可以在一次传递中计算每个字母出现的总数。这将对每个单词进行一次传递,这与您将其添加到trie的时间相同。一个更快的方法是预先计算每一件作品中包含的字母。这给了我一个想法。