Skip to content

Commit

Permalink
fixed balancing parenthesis error
Browse files Browse the repository at this point in the history
  • Loading branch information
kerighan committed Feb 8, 2021
1 parent 1f17ac5 commit 837a639
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 28 additions & 4 deletions eldar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="eldar",
version="0.0.5",
version="0.0.6",
author="Maixent Chenebaux",
author_email="[email protected]",
description="Boolean text search in Python",
Expand All @@ -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",
Expand Down

0 comments on commit 837a639

Please sign in to comment.