Code Explaination: Spelling correction code

Discussion in 'Python' started by Drew, Apr 12, 2007.

  1. Drew

    Drew Guest

    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?
    Drew, Apr 12, 2007
    #1
    1. Advertising

  2. 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
    Steven Bethard, Apr 12, 2007
    #2
    1. Advertising

  3. Drew

    Drew Guest

    On Apr 11, 11:27 pm, Steven Bethard <> 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
    Drew, Apr 12, 2007
    #3
  4. Drew wrote:
    > On Apr 11, 11:27 pm, Steven Bethard <> 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
    Steven Bethard, Apr 12, 2007
    #4
  5. Drew

    Drew Guest

    On Apr 12, 10:28 am, Steven Bethard <> wrote:
    > Drew wrote:
    > > On Apr 11, 11:27 pm, Steven Bethard <> 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
    Drew, Apr 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George

    spelling problem

    George, Aug 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    404
    George
    Aug 9, 2005
  2. ziggs
    Replies:
    2
    Views:
    176
    Henry Robinson
    Apr 21, 2004
  3. Dave Lenhardt

    Explaination of a bit of code

    Dave Lenhardt, Sep 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    102
    Brian Candler
    Sep 21, 2008
  4. kim

    Explaination of Javascript code

    kim, Jul 29, 2003, in forum: Javascript
    Replies:
    0
    Views:
    76
  5. Replies:
    6
    Views:
    95
    Michele Dondi
    May 4, 2007
Loading...

Share This Page