"anagrammeur"

Description

Ce programme propose une interface graphique en TK et permet de trouver les anagrammes d'un mot entré.

Il gère le caractère '?' comme "n'importe quel caractère"(par exemple le JOKER au Scrabble). Il affichera alors les lettres remplacées en majuscule.

Il utilise le dictionnaire "L'Officiel Du Scrabble 2008" qui se trouve dans le zip (ods5.txt).

Source / Exemple :


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

Conclusion :


Je serais ravi de lire vos commentaires.
Je me suis inspiré de l'algorithme qu'avait mis en place doudoubidou dans la source http://www.pythonfrance.com/codes/RECHERCHE-ANAGRAMME_37143.aspx
pour améliorer la vitesse de recherche.
En esperant que cela vous plaise ...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.