From 837a639db97f07c521e5ca5ea3c8024aa5255094 Mon Sep 17 00:00:00 2001 From: Maixent Chenebaux Date: Mon, 8 Feb 2021 23:21:23 +0100 Subject: [PATCH] fixed balancing parenthesis error --- eldar/__init__.py | 32 ++++++++++++++++++++++++++++---- setup.py | 3 +-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/eldar/__init__.py b/eldar/__init__.py index a64d2b8..437a70a 100644 --- a/eldar/__init__.py +++ b/eldar/__init__.py @@ -81,6 +81,11 @@ def __repr__(self): return f"({self.left}) AND NOT ({self.right})" +# class NOT(Binary): +# def evaluate(self, doc): + + + class OR(Binary): def evaluate(self, doc): if self.left.evaluate(doc): @@ -95,21 +100,30 @@ def __repr__(self): class Entry: def __init__(self, query): + self.not_ = False + if query[:4] == "not ": + self.not_ = True + query = query[4:] self.query = strip_quotes(query) def evaluate(self, doc): - return self.query in doc + res = self.query in doc + if self.not_: + return not res + return res def __repr__(self): - return self.query + if self.not_: + return f'NOT "{self.query}"' + return f'"{self.query}"' def parse_query(query, ignore_case=True, ignore_accent=True): # remove brackets around query - if query[0] == '(' and query[-1] == ')' and query.count('(') == 1: + if query[0] == '(' and query[-1] == ')': query = strip_brackets(query) # if there are quotes around query, make an entry - if query[0] == '"' and query[-1] == '"' and query.count('"') == 2: + if query[0] == '"' and query[-1] == '"' and query.count('"') == 1: if ignore_case: query = query.lower() if ignore_accent: @@ -163,6 +177,16 @@ def parse_query(query, ignore_case=True, ignore_accent=True): def strip_brackets(query): + count_left = 0 + for i in range(len(query) - 1): + letter = query[i] + if letter == "(": + count_left += 1 + elif letter == ")": + count_left -= 1 + if i > 0 and count_left == 0: + return query + if query[0] == "(" and query[-1] == ")": return query[1:-1] return query diff --git a/setup.py b/setup.py index a66c595..0993d2a 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name="eldar", - version="0.0.5", + version="0.0.6", author="Maixent Chenebaux", author_email="mchenebaux@reputationsquad.com", description="Boolean text search in Python", @@ -16,7 +16,6 @@ url="https://github.com/kerighan/eldar", packages=find_packages(), include_package_data=True, - install_requires=["unidecode>=1.1.1"], classifiers=[ "Programming Language :: Python :: 3.7", "Operating System :: OS Independent",