searchParserAppDemo.py 929 B

12345678910111213141516171819202122232425262728293031323334
  1. from searchparser import SearchQueryParser
  2. products = [ "grape juice", "grape jelly", "orange juice", "orange jujubees",
  3. "strawberry jam", "prune juice", "prune butter", "orange marmalade",
  4. "grapefruit juice" ]
  5. class FruitSearchParser(SearchQueryParser):
  6. def GetWord(self, word):
  7. return { p for p in products if p.startswith(word + " ") }
  8. def GetWordWildcard(self, word):
  9. return { p for p in products if p.startswith(word[:-1]) }
  10. def GetQuotes(self, search_string, tmp_result):
  11. result = set()
  12. # I have no idea how to use this feature...
  13. return result
  14. def GetNot(self, not_set):
  15. return set( products ) - not_set
  16. parser = FruitSearchParser()
  17. tests = """\
  18. grape or orange
  19. grape*
  20. not(grape*)
  21. prune and grape""".splitlines()
  22. for t in tests:
  23. print(t.strip())
  24. print(parser.Parse(t))
  25. print('')