- class Dictionary_Anagrams:
- def __init__(self, path):
- self.dictionary={}
- self.load(path)
- #load the dictionary from the file path
- def load(self, path):
- f=open(path,'r')#open the dictionary file in read mode
- for line in f:#for each line of the file
- line=line.replace('\n','')#remove the endofline character
- if len(line) in self.dictionary:#if a word of the same length have already been appened to our dictionary
- self.dictionary[len(line)].append(line.upper())#just add the word
- else:
- self.dictionary[len(line)]=[line.upper()]#else, construct a new list with the word
- #tell if w1 & w2 are anagrams. Do not check their length. Need both words to at the same case level
- def areAna(self, w1, w2):
- for c in w1:#for each char in the first word
- if c not in w2 and c != '?':#if this char is not in word2 and is not a '?'
- return False#then return false
- else:
- w2=w2.replace(c,'',1)#else, remove the char from word2
- return True#if false have never been returned, return true!
- #find the word anagrams: return a list containing them
- def find (self, word):
- if len(word) not in self.dictionary:#if the word is too small or to big for the dictionnary: return an empty list
- return []
- word=word.upper()#put the word to the upper case
- ana=[]
- for word2 in self.dictionary[len(word)]:#for each word of the same length in the dictionnary
- if (self.areAna(word, word2)):#check if they are anagrams: if true
- ana.append(word2)#add the word to the anagram list
- return ana#finally, return the anagram list
- from Tkinter import *
- #called when the user press the keyboard in the entry
- def key(event):
- listbox.delete(0, END)#delete the listobx lines
- word=entry.get()
- ana=ana_finder.find(word)#find the anagrams for the word
- for anagram in ana:#for each word in the anagram list
- anagram=anagram.lower()#put it to lower case
- for char in anagram:#for each char in each anagram
- if char not in word:#if char is not in the original word
- anagram=anagram.replace(char,char.upper(),1)#put the char to upper case
- listbox.insert(END, anagram)#put the anagram at the end of the listbox
- ana_finder=Dictionary_Anagrams("ods5.txt")#initialize the dictionary_anagrams object with the dictionary file
- root=Tk()#create the main window
- root.title("VyCHNou'S aNaGRaMS")
- entry = Entry(root,bg="black", fg="lightgrey", font=14)#create the entry
- entry.pack(anchor=NW, side=TOP, fill=X)#use the pack layout to attach the entry to the windows
- entry.bind("<KeyRelease>", key)#bind the entry to the callback "key"
- entry.focus()#give the entry the focusscrollbar = Scrollbar(root)#add a scrollbar for the listbox
- scrollbar.pack(side=RIGHT, fill=Y)
- listbox = Listbox(root,height=30,font=14)#add a listbox
- listbox.pack(anchor=SW, side=LEFT, fill=BOTH)
- scrollbar.config(command=listbox.yview)#configure the scrollbar
- root.mainloop()#let's run the window
class Dictionary_Anagrams:
def __init__(self, path):
self.dictionary={}
self.load(path)
#load the dictionary from the file path
def load(self, path):
f=open(path,'r')#open the dictionary file in read mode
for line in f:#for each line of the file
line=line.replace('\n','')#remove the endofline character
if len(line) in self.dictionary:#if a word of the same length have already been appened to our dictionary
self.dictionary[len(line)].append(line.upper())#just add the word
else:
self.dictionary[len(line)]=[line.upper()]#else, construct a new list with the word
#tell if w1 & w2 are anagrams. Do not check their length. Need both words to at the same case level
def areAna(self, w1, w2):
for c in w1:#for each char in the first word
if c not in w2 and c != '?':#if this char is not in word2 and is not a '?'
return False#then return false
else:
w2=w2.replace(c,'',1)#else, remove the char from word2
return True#if false have never been returned, return true!
#find the word anagrams: return a list containing them
def find (self, word):
if len(word) not in self.dictionary:#if the word is too small or to big for the dictionnary: return an empty list
return []
word=word.upper()#put the word to the upper case
ana=[]
for word2 in self.dictionary[len(word)]:#for each word of the same length in the dictionnary
if (self.areAna(word, word2)):#check if they are anagrams: if true
ana.append(word2)#add the word to the anagram list
return ana#finally, return the anagram list
from Tkinter import *
#called when the user press the keyboard in the entry
def key(event):
listbox.delete(0, END)#delete the listobx lines
word=entry.get()
ana=ana_finder.find(word)#find the anagrams for the word
for anagram in ana:#for each word in the anagram list
anagram=anagram.lower()#put it to lower case
for char in anagram:#for each char in each anagram
if char not in word:#if char is not in the original word
anagram=anagram.replace(char,char.upper(),1)#put the char to upper case
listbox.insert(END, anagram)#put the anagram at the end of the listbox
ana_finder=Dictionary_Anagrams("ods5.txt")#initialize the dictionary_anagrams object with the dictionary file
root=Tk()#create the main window
root.title("VyCHNou'S aNaGRaMS")
entry = Entry(root,bg="black", fg="lightgrey", font=14)#create the entry
entry.pack(anchor=NW, side=TOP, fill=X)#use the pack layout to attach the entry to the windows
entry.bind("<KeyRelease>", key)#bind the entry to the callback "key"
entry.focus()#give the entry the focusscrollbar = Scrollbar(root)#add a scrollbar for the listbox
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(root,height=30,font=14)#add a listbox
listbox.pack(anchor=SW, side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)#configure the scrollbar
root.mainloop()#let's run the window