-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathords_regex_category_tester.py
82 lines (63 loc) · 2.25 KB
/
ords_regex_category_tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# Test compiled regular expressions agains a list of common item types.
import re
import polars as pl
from funcs import *
# Just a little debug helper.
def log_info(category, lang, term):
logger.debug(f"*** {category} {lang} {term} ***")
print(f"*** {category} {lang} {term} ***")
# Return regex matching information for a given term.
def get_matches(category, term, lang, rx):
match = ""
if rx:
matches = rx.search(term)
if matches:
match = matches.group()
result = [category, term, lang, bool(match), match]
else:
result = [category, term, lang, False, "no regex"]
return result
if __name__ == "__main__":
logger = cfg.init_logger(__file__)
rexes = pl.read_csv(f"{cfg.DATA_DIR}/product_category_regexes.csv")
# Pre-compile the regexes
rexes = rexes.with_columns(
obj=pl.col("regex").map_elements(
lambda x: re.compile(x, re.I), return_dtype=pl.Object
)
)
# Changes to the ORDS categories will require updates to the regexes.
categories = ordsfuncs.get_categories(cfg.get_envvar("ORDS_CATS"))
# A set of real-world product term translations.
allterms = pl.read_csv(
f"{cfg.DATA_DIR}/ords_testdata_multilingual_products.csv"
)
langs = ["en", "nl", "fr", "de"]
results = []
for id, category in categories.iter_rows():
regex = rexes.filter((pl.col("product_category") == category)).select(
pl.col("obj")
)
if regex.height != 1:
continue
rx = regex["obj"].item()
for lang in langs:
terms = allterms.filter((pl.col("product_category") == category)).select(
pl.col(lang)
)
for term in terms.iter_rows():
log_info(category, lang, term[0])
matches = get_matches(category, term[0], lang, rx)
results.append(matches)
results = pl.DataFrame(
data=results,
schema={
"product_category": pl.String,
"term": pl.String,
"lang": pl.String,
"matched": pl.Boolean,
"match": pl.String,
},
)
results.write_csv(f"{cfg.OUT_DIR}/product_category_regex_test_results.csv")