FAQ
Hi..
I try a boolean parser in python since 1 weak.. But i can't do this
because this is very complicated :(
Do you know any blooean parser script in python or how do i write a
boolean parser ?
example query: ((google or yahoo) or (live msn)) not web
I'm sorry my bad english.
King Regards..

Search Discussions

  • Timaranz at Oct 4, 2007 at 8:00 pm

    On Oct 5, 7:29 am, Abandoned wrote:
    Hi..
    I try a boolean parser in python since 1 weak.. But i can't do this
    because this is very complicated :(
    Do you know any blooean parser script in python or how do i write a
    boolean parser ?
    example query: ((google or yahoo) or (live msn)) not web
    I'm sorry my bad english.
    King Regards..
    Try the pyparsing module (google it)
    They have several examples.
  • Paul McGuire at Oct 5, 2007 at 3:02 pm

    On Oct 4, 3:00 pm, timar... at gmail.com wrote:
    On Oct 5, 7:29 am, Abandoned wrote:

    Hi..
    I try a boolean parser in python since 1 weak.. But i can't do this
    because this is very complicated :(
    Do you know any blooean parser script in python or how do i write a
    boolean parser ?
    example query: ((google or yahoo) or (live msn)) not web
    I'm sorry my bad english.
    King Regards..
    Try the pyparsing module (google it)
    They have several examples.
    The pyparsing wiki has this online example:
    http://pyparsing.wikispaces.com/space/showimage/searchparser.py.

    -- Paul
  • Abandoned at Oct 5, 2007 at 11:29 am

    On 4 Ekim, 22:29, Abandoned wrote:
    Hi..
    I try a boolean parser in python since 1 weak.. But i can't do this
    because this is very complicated :(
    Do you know any blooean parser script in python or how do i write a
    boolean parser ?
    example query: ((google or yahoo) or (live msn)) not web
    I'm sorry my bad english.
    King Regards..
    This is a example written in perl "
    http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId50&lngWId=6"
    but i don't find any python example
  • Abandoned at Oct 5, 2007 at 12:00 pm
    http://pyparsing.wikispaces.com/space/showimage/searchparser.py this
    is searchparser but i can't understant to use this.
    Can anybody explain this how to use ?
  • Paul McGuire at Oct 5, 2007 at 3:27 pm

    On Oct 5, 7:00 am, Abandoned wrote:
    http://pyparsing.wikispaces.com/space/showimage/searchparser.pythis
    is searchparser but i can't understant to use this.
    Can anybody explain this how to use ?
    The code demonstrates a testing example, named TestParser, which
    inherits from SearchQueryParser. As mentioned in the docstrings at
    the top, to use this code, import the module and define your own class
    to inherit from SearchQueryParser. Then override the Get... methods,
    as is done in TestParser. Each Get method returns a set, and all sets
    are then ANDed and ORed according to the input search string.

    Here's a simple example, a database of fruit-based products. To keep
    things simple, each product will come from a single fruit, and the
    name of the fruit will be at the beginning of the product's name, for
    instance "grape juice" is made from the fruit named "grape".

    from searchparser import SearchQueryParser

    products = [ "grape juice", "grape jelly", "orange juice", "orange
    jujubees",
    "strawberry jam", "prune juice", "prune butter", "orange
    marmalade",
    "grapefruit juice" ]

    class FruitSearchParser(SearchQueryParser):
    def GetWord(self, word):
    return set( p for p in products if p.startswith(word + " ") )

    def GetWordWildcard(self, word):
    return set( p for p in products if p.startswith(word[:-1]) )

    def GetQuotes(self, search_string, tmp_result):
    result = Set()
    # I have no idea how to use this feature...
    return result

    def GetNot(self, not_set):
    return set( products ) - not_set


    parser = FruitSearchParser()

    tests = """\
    grape or orange
    grape*
    not(grape*)
    prune and grape""".splitlines()

    for t in tests:
    print t.strip()
    print parser.Parse(t)
    print


    Prints:

    grape or orange
    set(['orange juice', 'grape juice', 'orange marmalade', 'orange
    jujubees', 'grape jelly'])

    grape*
    set(['grapefruit juice', 'grape juice', 'grape jelly'])

    not(grape*)
    set(['orange jujubees', 'orange juice', 'prune butter', 'orange
    marmalade', 'strawberry jam', 'prune juice'])

    prune and grape
    set([])
  • Bruno Desthuilliers at Oct 4, 2007 at 3:06 am
    Paul McGuire a ?crit :
    (snip)

    <nitpicking>
    May I suggest a couple cleanifications for our newbie friends around ?
    from searchparser import SearchQueryParser

    products = [ "grape juice", "grape jelly", "orange juice", "orange
    jujubees",
    "strawberry jam", "prune juice", "prune butter", "orange
    marmalade",
    "grapefruit juice" ]
    # round 1 : extracting constants from iterations
    class FruitSearchParser(SearchQueryParser):
    def GetWord(self, word):
    return set( p for p in products if p.startswith(word + " ") )
    def GetWord(self, word):
    target = word + " "
    return set(p for p in products if p.startswith(target) )

    def GetWordWildcard(self, word):
    return set( p for p in products if p.startswith(word[:-1]) )
    def GetWordWildcard(self, word):
    target = word[:-1]
    return set( p for p in products if p.startswith(target) )


    # round 2 : factoring out common code

    class FruitSearchParser(SearchQueryParser):
    def _find_products_starting_with(self, target):
    return set(p for p in products if p.startswith(target))

    def GetWord(self, word):
    return self._find_products_starting_with(word + " ")

    def GetWordWildcard(self, word):
    return self._find_products_starting_with(word[:-1])

    # round 3: doing proper encapsulation:

    class FruitSearchParser(SearchQueryParser):
    def __init__(self, products):
    self._products = products

    def _find_products_starting_with(self, target):
    return set(p for p in self._products if p.startswith(target))

    def GetWord(self, word):
    return self._find_products_starting_with(word + " ")

    def GetWordWildcard(self, word):
    return self._find_products_starting_with(word[:-1])


    # round 4 : respecting pep08 (naming conventions):
    # heck ! I guess that this would need a rewrite of SearchQueryParser

    </nitpicking>
  • David at Oct 5, 2007 at 12:36 pm

    On 10/4/07, Abandoned wrote:
    Hi..
    I try a boolean parser in python since 1 weak.. But i can't do this
    because this is very complicated :(
    Do you know any blooean parser script in python or how do i write a
    boolean parser ?
    example query: ((google or yahoo) or (live msn)) not web
    I'm sorry my bad english.
    King Regards..

    --
    http://mail.python.org/pipermail/python-list/2006-August/399804.html

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 4, '07 at 3:06a
activeOct 5, '07 at 3:27p
posts8
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase