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
[ASP.NET] NE PAS SE FAIRE AVOIR PAR IHTTPMODULE ET SA MéTHODE INIT()[ASP.NET] NE PAS SE FAIRE AVOIR PAR IHTTPMODULE ET SA MéTHODE INIT() par tja
Beaucoup de développeurs pensent que lorsqu'on créé et enregistre un IHttpModule, il n'en existe qu'une seule instance et la méthode Init() sera appelée qu'une fois.
C'est faux
Cela peut vraiment créer des bugs subtils dont en ne se rend pas compte ...
Cliquez pour lire la suite de l'article par tja [MSTD10] SHAREPOINT 2010 ET TEAM FOUNDATION SERVER[MSTD10] SHAREPOINT 2010 ET TEAM FOUNDATION SERVER par phil
Un post rapide pour vous informer de la disponibilité de la vidéo de ma présentation sur SharePoint 2010 & Team Foundation Server. http://www.microsoft.com/france/vision/mstechdays10/Webcast.aspx?EID=20215d48-02e3-4d43-8c36-e53505c3b316 Dans la ...
Cliquez pour lire la suite de l'article par phil [MSTD10] SQL SERVER 2008 POUR LES DéVELOPPEURS ET VISUAL STUDIO TEAM SYSTEM DATABASE EDITION[MSTD10] SQL SERVER 2008 POUR LES DéVELOPPEURS ET VISUAL STUDIO TEAM SYSTEM DATABASE EDITION par Miiitch
Lors de cette session que j'ai animé avec Rui , j'ai eu l'occasion de présenter, en plus des nouveautés de SQL Server 2008, comment utiliser l'édition "database" de Visual Studio 2008. Le principe? Avoir les mêmes outils lors que l'on travaille avec une b...
Cliquez pour lire la suite de l'article par Miiitch [WF4] LOCALISER SIMPLEMENT UNE ACTIVITé ET SON DESIGNER WPF[WF4] LOCALISER SIMPLEMENT UNE ACTIVITé ET SON DESIGNER WPF par JeremyJeanson
La localisation . vous connaissez ce system miraculeux qui permet à votre application de s'afficher dans la langue de l'utilisateur (à condition de l'avoir intégrée dans votre programme). Comment mettre en place la localisation dans WF4? Cette question m'...
Cliquez pour lire la suite de l'article par JeremyJeanson [TECHDAYS 2010] WEBCASTS ET SLIDES[TECHDAYS 2010] WEBCASTS ET SLIDES par Audrey
Les webcasts des sessions des Techdays 2010 sont maintenant disponibles en ligne ! Me concernant, voici les slides et le webcast de la session Blend 3 / Skechtflow : Dessinez c'est gagné ! (animée avec Michel Perfetti ) Les slides et le webcast : Blend 3/...
Cliquez pour lire la suite de l'article par Audrey
Forum
MCD LYCéMCD LYCé par Wiliam123
Cliquez pour lire la suite par Wiliam123
Logiciels
Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor Xlite (v 3.0 build 41150)XLITE (V 3.0 BUILD 41150)c'est un logiciel qui permet de téléphoner par Internet à l'aide d'un compte SIP pratique pour le... Cliquez pour télécharger Xlite Academy System (10.9.3.0)ACADEMY SYSTEM (10.9.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
Comparez les prix

HTC Hero
Entre 550€ et 550€
|