-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_word.py
100 lines (73 loc) · 2.63 KB
/
valid_word.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
89
90
91
92
93
94
95
96
97
98
99
import enchant, itertools,os,sys
from threading import Thread
class MyThread (Thread):
pass
class MyProgram():
def __init__(self):
self.d = enchant.Dict("en_US")
self.listOfLetters = []
self.allWords = []
def CreateFilename(self):
temp = input("What is the name of this file (without the extension)? ")
filename = temp + ".txt"
print("Here is your filename:",filename)
return filename
def CreateListOfLetters(self, theInput):
letters = []
for l in theInput:
letters.append(l)
return letters
def CheckForFile(self, theFilename):
if (os.path.exists(theFilename)):
theFilename = theFilename.replace(".","-01.")
return theFilename
else:
return theFilename
def CreatePossibleWords(self, listOfLetters,filename):
x = 2
while x < len(listOfLetters):
print("Creating",x,"letter words")
tempList = [''.join(comb) for comb in itertools.product(listOfLetters, repeat=x)]
self.allWords += tempList
x +=1
# Remove Duplicates
self.allWords = sorted(self.RemoveDuplicates(self.allWords))
# Create a text file with all real words according to enchant
for word in self.allWords:
if self.d.check(word):
self.WriteWordToFile(word,filename)
return self.allWords
def RemoveDuplicates(self, theList):
theList = list(dict.fromkeys(theList))
return theList
def ReadFromFile(self, filename):
listOfWords = []
with open(filename,'r') as f:
listOfWords = f.readlines()
x = 0
while (x < len(listOfWords)):
listOfWords[x] = listOfWords[x].replace('\n','')
x +=1
return listOfWords
def CheckIfValid(self, word):
if(self.d.check(word)):
print(word)
def CheckListForValid(self, wordList):
count = 0
for word in wordList:
if(self.d.check(word)):
count +=1
print(word)
print(count,"valid words")
def WriteWordToFile(self, word, filename):
with open(filename,'a+') as f:
f.writelines(word+"\n")
theInput = input("Please type the letters with no spaces: ")
prog = MyProgram()
filename = prog.CreateFilename()
listOfLetters = prog.CreateListOfLetters(theInput)
prog.CreatePossibleWords(listOfLetters,filename)
possibleWords = prog.ReadFromFile(filename)
print(possibleWords)
print("Complete")
print("Found",str(len(possibleWords)),"words.")