-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathords_extract_vocabulary.py
167 lines (132 loc) · 5.38 KB
/
ords_extract_vocabulary.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# Attempts to extract a vocabulary from the data for each ORDS category
# from either the problem text or by deriving the `product`.
# ToDo: Describe vocabulary use-cases, e.g. "find fault types" and "verify category".
import re
import polars as pl
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize
from funcs import *
# This can leave a number of 2-char words in the vocabulary but some are useful e.g. "tv", "cd", "pc".
# Numbers may be useful for some purposes, e.g. "mp3", "ps1"
class LemmaTokenizer:
def __init__(self):
self.wnl = WordNetLemmatizer()
# Remove numbers and punctuation.
self.rx = re.compile("[\W\d_]")
def __call__(self, doc):
return [
self.wnl.lemmatize(t) for t in word_tokenize(doc) if (not self.rx.search(t))
]
def get_vectorizer():
tokenizer = LemmaTokenizer()
# [Using stop words](https://scikit-learn.org/stable/modules/feature_extraction.html#stop-words)
# [Stop Word Lists in Free Open-source Software Packages](https://aclanthology.org/W18-2502/)
# [Stopword Lists for 19 Languages](https://www.kaggle.com/datasets/rtatman/stopword-lists-for-19-languages)
stopfile1 = open(f"{cfg.DATA_DIR}/stopwords-english.txt", "r")
# ORDS corpus custom stopwords.
stopfile2 = open(f"{cfg.DATA_DIR}/stopwords-english-repair.txt", "r")
stoplist = stopfile1.read().replace("\n", " ") + stopfile2.read().replace("\n", " ")
stopfile1.close()
stopfile2.close()
# Use same tokenizer on stopwords as used in the vectorizer.
stop_tokens = tokenizer(stoplist)
tv = TfidfVectorizer(stop_words=stop_tokens, tokenizer=tokenizer)
logger.debug("*** STOPWORDS ***")
logger.debug(tv.get_stop_words())
return tv
# Split the partner_product_category string.
# Using English language records (assumed by country).
def get_products(category):
df = ordsfuncs.get_data(cfg.get_envvar("ORDS_DATA")).filter(
pl.col("product_category") == category,
pl.col("country").is_in(["USA", "GBR", "AUS", "IRL", "JEY", "NZL"]),
)
return list(
ordsfuncs.extract_products(df)
.select(pl.col("product"))
.drop_nulls()
.unique()["product"]
)
def get_problem_text(category):
df = ordsfuncs.get_data(cfg.get_envvar("ORDS_DATA")).filter(
pl.col("product_category") == category,
pl.col("country").is_in(["USA", "GBR", "AUS", "IRL", "JEY", "NZL"]),
)
return list(df.select(pl.col("problem")).drop_nulls().unique()["problem"])
# Using `product` values derived from the `partner_product_category` column.
# The format of `partner_product_category` values depends on how many category levels existed for each record.
# Format when 2 category levels : [partner lvl-1 category] ~ [partner lvl-2 category]
# Format when either lvl-1 or lvl-2 category: [partner lvl-n category]
def fit_products():
logger.debug("*** ITEM TYPE ***")
# Changes to the ORDS categories will require updates to the regexes.
categories = ordsfuncs.get_categories(cfg.get_envvar("ORDS_CATS"))
tv = get_vectorizer()
df_out = pl.DataFrame(
schema={
"product_category": pl.String,
"term": pl.String,
"idx": pl.Int64,
},
)
for id, category in categories.iter_rows():
logger.debug(f"**** {category} ****")
strings = get_products(category)
if len(strings) == 0:
continue
tv.fit_transform(strings)
logger.debug("** Vocabulary **")
logger.debug(tv.vocabulary_)
df_tmp = pl.DataFrame(
data={
"product_category": category,
"term": tv.vocabulary_.keys(),
"idx": tv.vocabulary_.values(),
}
)
logger.debug(df_tmp.height)
df_out.extend(df_tmp)
logger.debug(df_out.height)
df_out.write_csv(f"{cfg.OUT_DIR}/ords_vocabulary_products.csv")
df_out.group_by(["term"]).len(name="records").sort(
"records", descending=True
).write_csv(f"{cfg.OUT_DIR}/ords_vocabulary_products_freq.csv")
def fit_problem_text():
logger.debug("*** PROBLEM ***")
categories = ordsfuncs.get_categories(cfg.get_envvar("ORDS_CATS"))
tv = get_vectorizer()
df_out = pl.DataFrame(
schema={
"product_category": pl.String,
"term": pl.String,
"idx": pl.Int64,
},
)
for id, category in categories.iter_rows():
logger.debug(f"**** {category} ****")
strings = get_problem_text(category)
if len(strings) == 0:
continue
tv.fit_transform(strings)
logger.debug("** Vocabulary **")
logger.debug(tv.vocabulary_)
df_tmp = pl.DataFrame(
data={
"product_category": category,
"term": tv.vocabulary_.keys(),
"idx": tv.vocabulary_.values(),
}
)
logger.debug(df_tmp.height)
df_out.extend(df_tmp)
logger.debug(df_out.height)
df_out.write_csv(f"{cfg.OUT_DIR}/ords_vocabulary_problem.csv")
df_out.group_by(["term"]).len(name="records").sort(
"records", descending=True
).write_csv(f"{cfg.OUT_DIR}/ords_vocabulary_problem_freq.csv")
if __name__ == "__main__":
logger = cfg.init_logger(__file__)
fit_products()
fit_problem_text()