Accueil > > > "ANAGRAMMEUR"
"ANAGRAMMEUR"
Information sur la source
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
- 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
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-ANAGRA MME_37143.aspx pour améliorer la vitesse de recherche. En esperant que cela vous plaise ...
Historique
- 01 juillet 2005 02:21:39 :
- MAJ car lien pour le dictionnaire défaillant sous IE
- 01 juillet 2005 12:45:18 :
- Correction d'un bug d'ailleurs visible dans la capture d'écran
- 27 octobre 2006 12:46:02 :
- Grosse mise à jour: changement de l'algorithme de recherche des anagrammes (inspiré de celui de DoudouBidou)
Simplification du code (57 lignes environ)
Commentaires mis en anglais
- 28 octobre 2006 14:48:43 :
- Correction d'un léger bug de mise en forme des '?'
- 10 janvier 2007 11:15:58 :
- Changement de l'explication finale
- 02 janvier 2008 17:16:27 :
- Mise à jour pour être conforme avec l'ODS 5 (Version ODS 2008)
- 02 janvier 2008 17:32:32 :
- Changement du zip
- 15 janvier 2008 03:02:29 :
- Enfin, le vrai ods5 !
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
scrollbar dans scrollbox avec TKinter [ par MHI ]
Est-ce que quelqu'un sait comment ajouter les scrollbar à une scrollbox :J'ai essayé ceci :lstFile = Tkinter.Listbox(frmMain)lstFile.place(x = 20, y =
checkButton avec TKinter [ par MHI ]
comment faire pour tester si un checkButton est coché ?
Probleme avec TKinter [ par titasse ]
Bonjour, je debute en python. J'ai un probleme lorsque je veux importer TKinter avec la commande from TKinter import * j'ai le message suivant : Imp
au sujet de Tkinter et le module turtle [ par nico1900 ]
from turtle import *forward(120)left(90) color('red') forward(80)bon en fait je voulais tester le module turtle avec l e code ci-d
Un Canvas comme dans Tkinter, mais pour wxPython [ par samurize ]
Slt tout le monde. Voila tout est dans le titre (ou a peu pres ) : Je suis à la recherche d'un module pouvant s'integrer da
Vous feriez comment... ? [ par freeosca ]
Bonjour à tous, Pourriez-vous me confirmer que l'exemple qui suit est possible avec ce langage : Exemple : - J'installe une distrib linux sur un pc
Taille de widgets sous Tkinter [ par Uims ]
Bonjour, Quelqu"un saurait comment definir la taille d'un widgets sous tkinter??? Exemple: fen 1 = Tk(taille=600) J'espere que je me fait comprend
Ouverture d'un fichier windows (avec Tkinter) [ par Uims ]
Bonjour, Je travaille sous python et Tkinter et j'aurai voulu savoir comment dire a python de demarrer (comme on clique sur un fichier) une applicati
Tkinter et Python [ par Telimektar1er ]
Voila j'ai commencé e python il y a une semaine et jusqu à aujourd'hui aucun problème. Mais voilà, je viens de commencer la cr
help, faire un mastermind en python et en tkinter avant le 24 !!! [ par Crick132 ]
je suis étudiante en 2ème année, je dois réaliser un mastermind en python avec 8 couleurs et 5 combinaisons possibles.si quelqu'un
|
Derniers Blogs
[WP7] AJOUTER DES IMAGES DANS LA MEDIA LIBRARY D'UN WINDOWS PHONE 7[WP7] AJOUTER DES IMAGES DANS LA MEDIA LIBRARY D'UN WINDOWS PHONE 7 par Audrey
L'émulateur Windows Phone 7, fourni avec la version Beta des outils développeurs n'inclut aucune image dans sa bibliothèque. Pas très pratique de tester son application lorsque l'on souhaite que l'utilisateur puisse choisir une image présente dans le télé...
Cliquez pour lire la suite de l'article par Audrey VIVE LES MOCKS ET LES POCOSVIVE LES MOCKS ET LES POCOS par vLabz
J'observe régulièrement autour de moi de la confusion à propos de ces deux termes et j'aimerais juste rappeler ce qu'ils signifient. Je ne suis bien sûr pas le mieux placé pour faire une leçon mais je vais faire de mon mieux pour mettre en valeur ce q...
Cliquez pour lire la suite de l'article par vLabz [WF4] WORKFLOW AND CUSTOM ACTIVITIES - BEST PRACTICES (4/5)[WF4] WORKFLOW AND CUSTOM ACTIVITIES - BEST PRACTICES (4/5) par JeremyJeanson
Vendredi dernier Microsoft a publié le quatrième épisode des bonnes pratiques pour coder ses activités custom dans WF4 : endpoint.tv - Workflow and Custom Activities - Best Practices (Part 4) . Tout comme pour les précédents épisodes, j'ai pris le temps d...
Cliquez pour lire la suite de l'article par JeremyJeanson DéVELOPPEMENT MOBILE : .NET COMPACT FRAMEWORK & LIMITATIONSDéVELOPPEMENT MOBILE : .NET COMPACT FRAMEWORK & LIMITATIONS par Pi-R
Introduction :
Le développement d'applications mobiles est quelque peu différent du développement d'applications sous Windows. En effet, le développement d'applications mobiles se base sur le .NET Compact Fra...
Cliquez pour lire la suite de l'article par Pi-R IPHONE VERSUS WP7 CODINGIPHONE VERSUS WP7 CODING par Nicolas
Je relais une présentation sur slideshare.net, qui compare le développement sur Iphone et Windows Phone 7, qui ma fait sourire. I phone versus windows phone 7 coding View more presentations from www.donburnett.com. J'aurais bien aimé une comparai...
Cliquez pour lire la suite de l'article par Nicolas
Logiciels
Xilisoft HD Vidéo Convertisseur 6 (6.0.3.0421)XILISOFT HD VIDéO CONVERTISSEUR 6 (6.0.3.0421)Xilisoft HD Vidéo Convertisseur est un outil professionnel de conversion HDTV, conçu pour transfo... Cliquez pour télécharger Xilisoft HD Vidéo Convertisseur 6 Xilisoft MP4 Convertisseur 6 (6.0.2.0415)XILISOFT MP4 CONVERTISSEUR 6 (6.0.2.0415)Xilisoft MP4 Convertisseur est un outil puissant pour la conversion de vidéo MP4, qui peut conver... Cliquez pour télécharger Xilisoft MP4 Convertisseur 6 Vade Retro Desktop (3.03)VADE RETRO DESKTOP (3.03)Le logiciel antispam Vade Retro pour Microsoft Outlook®, Outlook Express® et Windows Mail®(Vista)... Cliquez pour télécharger Vade Retro Desktop Malwarebytes Anti Malwares (1.46)MALWAREBYTES ANTI MALWARES (1.46)Malwarebytes' Anti-Malware est un anti-malware qui peut éliminer même les plus avancés des logic... Cliquez pour télécharger Malwarebytes Anti Malwares
|