repl.it
Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from collections import Counter
from ascii_graph import Pyasciigraph
import requests

URL_BASE = "https://raw.githubusercontent.com/words/"

WORDS_ENGLISH = URL_BASE + "an-array-of-english-words/master/words.json"
WORDS_FRENCH  = URL_BASE + "an-array-of-french-words/master/mots.json"

# Retrieve words from online file and lowercase them
r = requests.get(WORDS_FRENCH)
words = [word.lower() for word in r.json()]


########################
# Function definitions #
########################

def matches_template(word: str, template: str) -> (bool, list):
  """Returns True and unknown letters for word if it matches template. E.g:
  matches_template("Hello", "*e**o") == True, ['h', 'l']"""
  if len(word) != len(template):
    return False, None

  unknown = []

  for i,char in enumerate(word):
    if template[i] != char:
      if template[i] not in ['*', '_']:
        return False, None
      else:
        unknown.append(char)

  return True, unknown

def inter(word1: str, word2: str) -> bool:
  """Returns True if one character of one word in in the other one"""
  for char in word1:
    if char in word2:
      return True
      
  return False

def filter_word(words: list, template: str, forbidden: str):
  filtered = []
  unknown = []

  for word in words:
    # If one character is forbidden, skip loop iteration
    if (inter(forbidden, word)):
      continue

    matches, u = matches_template(word, template)
    if matches:
      filtered.append(word)
      unknown.extend(u)
  
  return filtered, unknown


########################
###### Main code #######
########################

# Basic info
print(str(len(words)) + " words loaded...")
print()

# User input
template = input("Template: ").lower()
forbidden = input("Used chars: ").lower()
print()

# Filter words that matches template and count occurences of unknown letters
filtered, remaining_letters = filter_word(words, template, forbidden)
remaining_letters = Counter(remaining_letters).most_common()

# Print list of words that match template
print('Possible words')
for word in filtered:
  print(' * ' + word)

print()

# Draw graph of advised letters
graph = Pyasciigraph()
for line in graph.graph('Letters you should try', remaining_letters):
  print(line)
Fetching token
?