|
begin process at 2008 07 25 09:57:02
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
"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-ANAGRAMME_37143.aspx pour améliorer la vitesse de recherche. En esperant que cela vous plaise ...
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
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 de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 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 | | | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|