Skip to content

Commit 4dd25eb

Browse files
committed
fix
1 parent 5f0b19d commit 4dd25eb

4 files changed

Lines changed: 51 additions & 3 deletions

File tree

fuzzymatcher/data_getter_sqlite.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,29 @@ def _tokens_to_matches(self, tokens, misspelling = False):
177177
"""
178178

179179
# This fails if the special tokens 'and' or 'or' are in fts string! See issue 35!
180-
tokens_to_remove = ["AND", "OR"]
181-
tokens = [t for t in tokens if t not in tokens_to_remove]
180+
tokens_to_escape = ["AND", "OR", "NEAR"]
181+
182+
def escape_token(t):
183+
# return t
184+
if t in tokens_to_escape:
185+
return '"' + t + '"'
186+
else:
187+
return t
188+
189+
190+
tokens = [escape_token(t) for t in tokens]
191+
182192
fts_string = " ".join(tokens)
183193

194+
184195
if misspelling:
185196
table_name = "_concat_all_alternatives"
186197
else:
187198
table_name = "_concat_all"
188199

189200
sql = get_records_sql.format(table_name, fts_string, self.return_records_limit)
190201

202+
191203
cur = self.con.cursor()
192204
cur.execute(sql)
193205
results = cur.fetchall()

tests/data/left_token_escape.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id,fname,mname,lname,dob,another_field
2+
1,or,or and,and,20/05/1980,other data
3+
2,or,or,or smith or,15/06/1990,more data
4+
3,near,and,near,20/05/1960,another thing
5+

tests/data/right_token_escape.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,name,middlename,surname,date,other
2+
1,or,or,or smith or,15/06/1990,more data
3+
2,near,and,near,20/05/1960,another thing
4+
3,or,or and,and,20/05/1980,other data

tests/test_misc.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,31 @@ def test_nulls_no_errors(self):
1717

1818
on = ["first_name", "surname", "dob", "city"]
1919

20-
flj = link_table(df_left, df_right, on, on)
20+
flj = link_table(df_left, df_right, on, on)
21+
22+
23+
class TestNulls(unittest.TestCase):
24+
"""
25+
Test what happens when the user provides input data with
26+
fts4 match expression keyworks like AND, OR, NEAR
27+
"""
28+
29+
def test_nulls_no_errors(self):
30+
"""
31+
32+
"""
33+
34+
35+
df_left = pd.read_csv("tests/data/left_token_escape.csv")
36+
df_right = pd.read_csv("tests/data/right_token_escape.csv")
37+
38+
# Columns to match on from df_left
39+
left_on = ["fname", "mname", "lname"]
40+
41+
# Columns to match on from df_right
42+
right_on = ["name", "middlename", "surname"]
43+
44+
on = ["first_name", "surname", ]
45+
46+
flj = link_table(df_left, df_right, left_on, right_on,
47+
left_id_col="id", right_id_col="id")

0 commit comments

Comments
 (0)