Python RegEx-hangman Algorithm

I am trying to write an executioner algorithm. My idea for this is as follows:

  • Pre-process a dictionary that contains the relative frequencies of the letters of words depending on their length. The step is completed.

Example:

#Each key corresponds to length of the word.   

frequencyDict = {2: ['a', 'o', 'e', 'i', 'm', 'h', 'n', 'u', 's', 't', 'y', 'b', 'd', 'l', 'p', 'x', 'f', 'r', 'w', 'g', 'k', 'j'], 
  3: ['a', 'e', 'o', 'i', 't', 's', 'u', 'p', 'r', 'n', 'd', 'b', 'm', 'g', 'y', 'l', 'h', 'w', 'f', 'c', 'k', 'x', 'v', 'j', 'z', 'q'], 
  4: ['e', 'a', 's', 'o', 'i', 'l', 'r', 't', 'n', 'u', 'd', 'p', 'm', 'h', 'b', 'c', 'g', 'k', 'y', 'f', 'w', 'v', 'j', 'z', 'x', 'q'],
  5: ['s', 'e', 'a', 'o', 'r', 'i', 'l', 't', 'n', 'd', 'u', 'c', 'p', 'y', 'm', 'h', 'g', 'b', 'k', 'f', 'w', 'v', 'z', 'x', 'j', 'q'],
  6: ['e', 's', 'a', 'r', 'i', 'o', 'l', 'n', 't', 'd', 'u', 'c', 'p', 'm', 'g', 'h', 'b', 'y', 'f', 'k', 'w', 'v', 'z', 'x', 'j', 'q'],
  7: ['e', 's', 'a', 'i', 'r', 'n', 'o', 't', 'l', 'd', 'u', 'c', 'g', 'p', 'm', 'h', 'b', 'y', 'f', 'k', 'w', 'v', 'z', 'x', 'j', 'q'],
  8: ['e', 's', 'i', 'a', 'r', 'n', 'o', 't', 'l', 'd', 'c', 'u', 'g', 'p', 'm', 'h', 'b', 'y', 'f', 'k', 'w', 'v', 'z', 'x', 'q', 'j']}

I also have a word generator in the dictionary:

dictionary = word_reader('C:\\Python27\\dictionary.txt', len(letters))

Based on this feature

#Strips dictionary of words that are too big or too small from the list
def word_reader(filename, L):
  L2 = L+2
  return (word.strip() for word in open(filename) \
          if len(word) < L2 and len(word) > 2)
  • This particular game will give you the latest vowel for free. For example, if the word was clay, the user will be presented with the following board: e ---- e- to guess. So, I want to find a way to create a new generator or list with all the words devoid of this, which do not match the pattern e ---- e-.

p = re.compile('^e\D\D\D\De\D$', re.IGNORECASE) will do this, but can find words that contain 'e in places other than the first letter and the second to the last letter.

So my first question is:

  • , 'e' -
  • , , ?

, "", ---- e- , 6 , , "---- e-" . ?

NEW frequencyDict, , NewList.

:

   cnt = Counter()
   for words in dictionary:
      for letters in words:
         cnt[letters]+=1

?

newfrequencyDict , , . , () .

? ?

+3
2

, - O (n) . , , , .

:

def matches_template(word, template):
  found_chars = set(x for x in template if x != '-')
  for char, template_char in zip(word, template):
    if template_char == '-':
      if char in found_chars: return False
    else:
      if template_char != char: return False
  return True

, , , . , 50% , , . - , , , , .

+2

. .

  • : '^e[^e][^e][^e][^e]e[^e]$'. [^e] , " , " e ". : , , , . , , " ". , , " ", " ea --- e-", '^ea[^ae][^ae][^ae]e[^ae]$'.
  • , , ​​ "ea --- e-", . : a) , ( , {'a', 'e'}), b) "match-all-but-this" ([^ae]) - , , , c) (ea[^ae][^ae][^ae]e[^ae]) d), , '^' "$" .
  • , dict - , . , . , , , , , . , , "" 2 "e"? , Hangman , , "eeeeeeee" "the" "e" (). .
+3

All Articles