Hangman

Log | Files | Refs | README

eng_dict.py (2321B)


      1 """
      2 Help file for hangman program.
      3 Read a dictionary then provide functions to get a random word.
      4 """
      5 
      6 import random
      7 
      8 
      9 class eng_dictionary():
     10     """
     11     Constructor function.
     12     Read all lines in a dictionary text file and store in dictonary.
     13     """
     14     def __init__(self, dictionary_path) -> None:
     15         self.dictionary = set(line.strip().lower() for line in open(dictionary_path, 'r'))
     16 
     17 
     18     """
     19     Return entire dictionary
     20     """
     21     def getDictionary(self):
     22         return self.dictionary
     23 
     24 
     25     """
     26     Return word from dictionary at specified position
     27     """
     28     def getWordAt(self, pos):
     29         return self.dictionary[pos]
     30 
     31 
     32     """
     33     Return one random word from the english dictionary
     34     """
     35     def getRandomWord(self):
     36         numberOfWords = len(self.dictionary)
     37         rand = random.randint(0, numberOfWords)
     38         
     39         count = 0
     40         for word in self.dictionary:
     41             count += 1
     42             if count == rand:
     43                 return word
     44 
     45 
     46     """
     47     Get all the possible words for example:
     48     -O-- would return A set containing {"good", "goal", "goon"} etc
     49     VAR: template = ex. "--u---" or d--g
     50     """
     51     def getPossibleWords(self, template):
     52         word_length = len(template)
     53         output = set()
     54 
     55         for word in self.dictionary:
     56             if self.__wordMatchesTemplate(template, word):
     57                 output.add(word)
     58         
     59         return output
     60 
     61 
     62     """
     63     Removes all words in the dictionary
     64     that are not of the specified length.
     65     """
     66     def trimDictionaryWordLength(self, length):
     67         new_dict = set()
     68 
     69         for word in self.dictionary:
     70             if len(word) == length:
     71                 new_dict.add(word)
     72 
     73         self.dictionary = new_dict
     74 
     75 
     76     """
     77     Help function for getPossibleWords()
     78     that returns if a given word matches the template
     79     returns true of false.
     80     """
     81     def __wordMatchesTemplate(self, template, word):
     82         for i in range(len(template)):
     83             if template[i] != "-":
     84                 if template[i] != word[i]:
     85                     return False
     86         
     87         return True
     88 
     89 
     90     """
     91     Manually set the dict to a new set
     92     """
     93     def setDict(self, new_dict):
     94         self.dictionary = new_dict