-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathords_regex_topics.py
144 lines (133 loc) · 4.45 KB
/
ords_regex_topics.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# Multi-lingual regex search for random themes within the ORDS `partner_product_category` values.
import polars as pl
import re
from funcs import *
# Split the partner_product_category string.
def get_item_types(df):
data = (
df.select(pl.col("partner_product_category"))
.with_columns(
pl.col("partner_product_category")
.str.split("~")
.list.last()
.str.strip_chars()
.alias("item_type")
)
.drop_nulls()
.unique()
)
return data
if __name__ == "__main__":
logger = cfg.init_logger(__file__)
topics = pl.DataFrame(
data={
"topic": ["Christmas", "Bake-Off", "Entertainment"],
"rx": [None, None, None],
"patt": [
textfuncs.build_regex_string(
[
"[yj]ule?",
"(christ|x)mas",
"fairy",
"fatate",
"fe lys",
"féériques",
"hadas?",
"kerst",
"lichterketten",
"[^ku]n[ei]v[ei]",
"natale",
"navidad",
"neige",
"no[ëe]l",
"reindeer",
"rendier",
"rensdyr",
"rentier",
"santa",
"schlitten",
"schnee",
"slæde",
"sleigh",
"slitta",
"sneeew",
"snow",
"sprookjes",
"traîneau",
"trineo",
"weihnacht",
]
),
textfuncs.build_regex_string(
[
"[wv]a[f]+[el]{2}",
"bisc(ui|o)t(t[io])?",
"biscuit",
"bollo",
"br(ea|oo)d",
"br[øo]{1,2}[dt]",
"cake",
"cialda",
"cr[êe]pe",
"g(au|o)[f]+re",
"galleta",
"gâteau",
"k[ie]ks",
"kage",
"krepp",
"kuchen",
"pains?$",
"pan(cake|nenkoek|dekage|ini)",
"pastel",
"pfannkuchen",
"scone",
"t[a]+rt",
"tort([ea]|ita)",
]
),
textfuncs.build_regex_string(
[
"[ck]assette",
"dis[ck][ -]?man",
"game[ -]?boy",
"i[ -]?pod",
"mp[ -]?3",
"walk[ -]?man",
"atari",
"sega",
"nintendo",
"x[ -]?box",
"transistor",
"dab",
"vhs",
"vcr",
"pvr",
"video",
"ps[ -]?[1-9]",
]
),
],
}
)
topics = topics.with_columns(
rx=pl.col("patt").map_elements(lambda x: re.compile(x), return_dtype=pl.Object)
)
df = ordsfuncs.get_data(cfg.get_envvar("ORDS_DATA"))
itemtypes = get_item_types(df)
for row in topics.iter_rows():
topic = row[0]
rx = row[1]
logger.debug(f"*** {topic} ***")
logger.debug(rx)
matched = []
for item in itemtypes.iter_rows():
item_type = item[1]
matches = rx.search(item_type)
if matches != None:
logger.debug(matches.group())
matched.append(item_type)
pattern = "|".join(list(set(matched)))
logger.debug(pattern)
results = df.filter(pl.col("partner_product_category").str.contains(pattern))
results.write_csv(f"{cfg.OUT_DIR}/ords_regex_topic_{topic}.csv")