Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1491 connectés 

  FORUM HardWare.fr
  Programmation
  Python

  spell-checker : code de Peter Norvig [résolu]

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

spell-checker : code de Peter Norvig [résolu]

n°2093991
suizokukan
Posté le 08-08-2011 à 20:09:43  profilanswer
 

Je suis tombé par hasard sur un très beau morceau de code écrit par Peter Norvig, Director of Research chez Google. Le code fonctionne avec Python 2x et est incompatible avec les versions 3.x
 
source : http://norvig.com/spell-correct.html

Code :
  1. import re, collections
  2. def words(text): return re.findall('[a-z]+', text.lower())
  3. def train(features):
  4.     model = collections.defaultdict(lambda: 1)
  5.     for f in features:
  6.         model[f] += 1
  7.     return model
  8. NWORDS = train(words(file('big.txt').read()))
  9. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  10. def edits1(word):
  11.    splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
  12.    deletes    = [a + b[1:] for a, b in splits if b]
  13.    transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
  14.    replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
  15.    inserts    = [a + c + b     for a, b in splits for c in alphabet]
  16.    return set(deletes + transposes + replaces + inserts)
  17. def known_edits2(word):
  18.     return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
  19. def known(words): return set(w for w in words if w in NWORDS)
  20. def correct(word):
  21.     candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
  22.     return max(candidates, key=NWORDS.get)


 
J'ai du mal à comprendre comment fonctionne la ligne suivante :

Code :
  1. return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)


 
Où placer les parenthèses pour hiérarchiser le code ?
 
Merci d'avance !


Message édité par suizokukan le 08-08-2011 à 20:30:26

---------------
rule #1 : trust the python
mood
Publicité
Posté le 08-08-2011 à 20:09:43  profilanswer
 

n°2093997
suizokukan
Posté le 08-08-2011 à 20:30:08  profilanswer
 

Bon, je me réponds moi-même sans ajouter de parenthèses :
 

Code :
  1. return set(e2 for e1 in edits1(word)
  2.                  for e2 in edits1(e1)
  3.            if e2 in NWORDS)


---------------
rule #1 : trust the python

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

  spell-checker : code de Peter Norvig [résolu]

 

Sujets relatifs
Code htmlCode::Blocks et wxWidgets : propriétés Multiplate-forme
Code VB pour changer les jours samedi et lundi au vendrediExecution d'une Macro Access a partir d'un code C#
code IP fragmentationArbre de code XML
Vérification code Javascriptque fait ce code de ORDImage
Code Css erroné ?Erreur code java String[][]
Plus de sujets relatifs à : spell-checker : code de Peter Norvig [résolu]


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR