当前位置: 首页>>代码示例>>Python>>正文


Python Dictionary.getPossibleWords方法代码示例

本文整理汇总了Python中dictionary.Dictionary.getPossibleWords方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.getPossibleWords方法的具体用法?Python Dictionary.getPossibleWords怎么用?Python Dictionary.getPossibleWords使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dictionary.Dictionary的用法示例。


在下文中一共展示了Dictionary.getPossibleWords方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import getPossibleWords [as 别名]

#.........这里部分代码省略.........
    #import two letter words
    self.twoLetteredWords = [] 
    f = open('2letter.txt', 'r')
    for line in f:
      for word in line.strip().split():
        self.twoLetteredWords.append(word)

    self.reset()

  def reset(self):
    #build list of letters sorted by decreasing frequency
    self.lettersLeft= list(self.letters)
    
    #list of next guesses
    self.next = list();

  def makeGuess(self, state):
    #handle apostrophes
    if "'_" in state:
      for char in self.post_apostrophe:
        if char in self.lettersLeft:
          self.lettersLeft.remove(char)
          return char

    state = state.lower()
    words = state.split()
    
    #handle single letter words
    if "_" in words:
      for char in self.one_letter:
        if char in self.lettersLeft:
          self.lettersLeft.remove(char)
          return char

    #handle special cases 
    for word in words:
      #should have some letters guessed first
      if '_' in word and getPriority(word) > minPriority:
        if len(word) == 2:
          #use common two letter list
          index = 1 - word.find('_') 
          char = word[index]

          for twoLetteredWord in self.twoLetteredWords:
            if twoLetteredWord[index] == char:
              retChar = twoLetteredWord[1-index]
              if retChar in self.lettersLeft:
                self.lettersLeft.remove(retChar)
                return twoLetteredWord[1-index]

        #handle 'and' and 'the'
        if len(word) == 3:
          for str in ['and', 'the']:
            valid = True
            index = -1

            #check that characters match position
            for i in range(len(word)):
              if word[i] == '_' and str[i] in self.lettersLeft:
                index = i
              elif word[i] != str[i]:
                valid = False

            if valid == True and index != -1:
              char = str[index]
              self.lettersLeft.remove(char)
              return char
 
    #sort words by priority (by % of word solved)
    #only care about words that have been solved to some degree,
    #defined by minPriority
    priority = list()
    for word in words:
      if '_' not in word or getPriority(word) < minPriority:
        continue

      if len(priority) == 0:
        priority.append(word)
      else:
        i = 0
        while i < len(priority) and getPriority(word) < getPriority(priority[i]):
          i += 1
        priority.insert(i, word)

    #handle by priority (highest % solved)
    for word in priority:
      possibleWords = self.dict.getPossibleWords(word) 
      possibleLetters = set()
      for possibleWord in possibleWords:
        for char in possibleWord:
          if char in self.lettersLeft:
            possibleLetters.add(char)
      for char in self.lettersLeft:
        if char in possibleLetters:
          self.lettersLeft.remove(char)
          return char

    #if all else fails, guess letters by frequency
    char = self.lettersLeft.pop(0)
    return char
开发者ID:wienleung,项目名称:Hangman_hulu,代码行数:104,代码来源:guesser.py


注:本文中的dictionary.Dictionary.getPossibleWords方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。