Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Code Explaination: Spelling correction code

Reply
Thread Tools

Code Explaination: Spelling correction code

 
 
Drew
Guest
Posts: n/a
 
      04-12-2007
I recently saw this website: http://www.norvig.com/spell-correct.html

All the code makes sense to me save one line:

def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
NWORDS)

I understand (from seeing a ruby version of the code) that the goal
here is to rerun the edits1 method on each member of the set returned
by running edits1 on the initial word. However, I'm confused how the
for within a for works. Can anyone help shed some light on this for me?

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      04-12-2007
Drew wrote:
> I recently saw this website: http://www.norvig.com/spell-correct.html
>
> All the code makes sense to me save one line:
>
> def known_edits2(word):
> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
> NWORDS)


This is the same as:

result = set()
for e1 in edits1(word):
for e2 in edits1(e1):
if e2 in NWORDS:
result.add(e2)
return result

The thing between the ``set(`` and ``)`` is called a generator
comprehension if you'd like to look into it further.

STeVe
 
Reply With Quote
 
 
 
 
Drew
Guest
Posts: n/a
 
      04-12-2007
On Apr 11, 11:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
> Drew wrote:
> > I recently saw this website:http://www.norvig.com/spell-correct.html

>
> > All the code makes sense to me save one line:

>
> > def known_edits2(word):
> > return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
> > NWORDS)

>
> This is the same as:
>
> result = set()
> for e1 in edits1(word):
> for e2 in edits1(e1):
> if e2 in NWORDS:
> result.add(e2)
> return result
>
> The thing between the ``set(`` and ``)`` is called a generator
> comprehension if you'd like to look into it further.
>
> STeVe


Steve -

Thanks for the response. I'm somewhat familiar with generator/list
comprehension but was unsure how multiple statements were evaluated
when chained together. From your explanation, I'm assuming they are
evaluated from the "inside out" rather than left to right or right to
left.

Does the mean that the comprehension on the inside is always evaluated
first?

Thanks,
Drew

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      04-12-2007
Drew wrote:
> On Apr 11, 11:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
>> Drew wrote:
>>> def known_edits2(word):
>>> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
>>> NWORDS)

>>
>> This is the same as:
>>
>> result = set()
>> for e1 in edits1(word):
>> for e2 in edits1(e1):
>> if e2 in NWORDS:
>> result.add(e2)
>> return result
>>
>> The thing between the ``set(`` and ``)`` is called a generator
>> comprehension if you'd like to look into it further.

>
> Thanks for the response. I'm somewhat familiar with generator/list
> comprehension but was unsure how multiple statements were evaluated
> when chained together. From your explanation, I'm assuming they are
> evaluated from the "inside out" rather than left to right or right to
> left.
>
> Does the mean that the comprehension on the inside is always evaluated
> first?


Not really (at least for the most literal interpretation of ``evaluated
first``). I find it easiest to think of translating them into regular
for loops by adding the appropriate indentation.

Starting with:

(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

Adding newlines:

(e2
for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS)

Adding indentation:

(e2
for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS)

Moving the add/append to the bottom:

for e1 in edits1(word)
for e2 in edits1(e1)
if e2 in NWORDS
e2

Adding the remaining boiler-plate:

result = set()
for e1 in edits1(word):
for e2 in edits1(e1):
if e2 in NWORDS:
result.add(e2)


So multiple for- and if-expressions are evaluated in the same order that
they would normally be in Python, assuming the proper whitespace was added.

HTH,

STeVe
 
Reply With Quote
 
Drew
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 10:28 am, Steven Bethard <steven.beth...@gmail.com> wrote:
> Drew wrote:
> > On Apr 11, 11:27 pm, Steven Bethard <steven.beth...@gmail.com> wrote:
> >> Drew wrote:
> >>> def known_edits2(word):
> >>> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in
> >>> NWORDS)

>
> >> This is the same as:

>
> >> result = set()
> >> for e1 in edits1(word):
> >> for e2 in edits1(e1):
> >> if e2 in NWORDS:
> >> result.add(e2)
> >> return result

>
> >> The thing between the ``set(`` and ``)`` is called a generator
> >> comprehension if you'd like to look into it further.

>
> > Thanks for the response. I'm somewhat familiar with generator/list
> > comprehension but was unsure how multiple statements were evaluated
> > when chained together. From your explanation, I'm assuming they are
> > evaluated from the "inside out" rather than left to right or right to
> > left.

>
> > Does the mean that the comprehension on the inside is always evaluated
> > first?

>
> Not really (at least for the most literal interpretation of ``evaluated
> first``). I find it easiest to think of translating them into regular
> for loops by adding the appropriate indentation.
>
> Starting with:
>
> (e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
>
> Adding newlines:
>
> (e2
> for e1 in edits1(word)
> for e2 in edits1(e1)
> if e2 in NWORDS)
>
> Adding indentation:
>
> (e2
> for e1 in edits1(word)
> for e2 in edits1(e1)
> if e2 in NWORDS)
>
> Moving the add/append to the bottom:
>
> for e1 in edits1(word)
> for e2 in edits1(e1)
> if e2 in NWORDS
> e2
>
> Adding the remaining boiler-plate:
>
> result = set()
> for e1 in edits1(word):
> for e2 in edits1(e1):
> if e2 in NWORDS:
> result.add(e2)
>
> So multiple for- and if-expressions are evaluated in the same order that
> they would normally be in Python, assuming the proper whitespace was added.
>
> HTH,
>
> STeVe


Wow, thanks for having the patience to write that out. This makes
perfect sense now.

-Drew

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
spelling correction dairell bell Computer Support 36 05-22-2005 03:34 PM
Spelling checkers Michelangelo Verso Jr. Firefox 1 10-18-2004 10:10 AM
automatic spelling check Allen Thomson Firefox 2 06-20-2004 11:53 PM
Spelling Completion or Code completion in ASP? ziggs ASP General 2 04-21-2004 02:02 PM
Mozilla spelling check file? carlton hunter Firefox 8 02-07-2004 12:38 AM



Advertisments
 



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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57