diff options
Diffstat (limited to 'examples/searchparser.py')
-rw-r--r-- | examples/searchparser.py | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/examples/searchparser.py b/examples/searchparser.py index 36e3cd7..d645a1f 100644 --- a/examples/searchparser.py +++ b/examples/searchparser.py @@ -2,7 +2,7 @@ version 2006-03-09
-This search query parser uses the excellent Pyparsing module
+This search query parser uses the excellent Pyparsing module
(http://pyparsing.sourceforge.net/) to parse search queries by users.
It handles:
@@ -30,7 +30,7 @@ are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
+ this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Estrate nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
@@ -41,10 +41,10 @@ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
-ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
CONTRIBUTORS:
@@ -72,12 +72,12 @@ class SearchQueryParser: 'wordwildcard': self.evaluateWordWildcard,
}
self._parser = self.parser()
-
+
def parser(self):
"""
This function returns a parser.
The grammar should be like most full text search engines (Google, Tsearch, Lucene).
-
+
Grammar:
- a query consists of alphanumeric words, with an optional '*' wildcard
at the end of a word
@@ -89,19 +89,19 @@ class SearchQueryParser: - if an operator is missing, use an 'and' operator
"""
operatorOr = Forward()
-
+
operatorWord = Group(Combine(Word(alphanums) + Suppress('*'))).setResultsName('wordwildcard') | \
Group(Word(alphanums)).setResultsName('word')
-
+
operatorQuotesContent = Forward()
operatorQuotesContent << (
(operatorWord + operatorQuotesContent) | operatorWord
)
-
+
operatorQuotes = Group(
Suppress('"') + operatorQuotesContent + Suppress('"')
).setResultsName("quotes") | operatorWord
-
+
operatorParenthesis = Group(
(Suppress("(") + operatorOr + Suppress(")"))
).setResultsName("parenthesis") | operatorQuotes
@@ -117,7 +117,7 @@ class SearchQueryParser: ).setResultsName("and") | Group(
operatorNot + OneOrMore(~oneOf("and or") + operatorAnd)
).setResultsName("and") | operatorNot)
-
+
operatorOr << (Group(
operatorAnd + Suppress(Keyword("or", caseless=True)) + operatorOr
).setResultsName("or") | operatorAnd)
@@ -158,7 +158,7 @@ class SearchQueryParser: def evaluateWordWildcard(self, argument):
return self.GetWordWildcard(argument[0])
-
+
def evaluate(self, argument):
return self._methods[argument.getName()](argument)
@@ -230,7 +230,7 @@ class ParserTest(SearchQueryParser): 7: 'nothing',
8: 'helper',
}
-
+
index = {
'help': {1, 2, 4, 5},
'me': {2},
@@ -264,7 +264,7 @@ class ParserTest(SearchQueryParser): if self.docs[item].count(search_string):
result.add(item)
return result
-
+
def GetNot(self, not_set):
all = set(list(self.docs.keys()))
return all.difference(not_set)
@@ -284,7 +284,7 @@ class ParserTest(SearchQueryParser): print('>>>>>>>>>>>>>>>>>>>>>>Test ERROR<<<<<<<<<<<<<<<<<<<<<')
print('')
return all_ok
-
+
if __name__=='__main__':
if ParserTest().Test():
print('All tests OK')
|