-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathords_quest_training_vacuums.py
198 lines (157 loc) · 6.43 KB
/
ords_quest_training_vacuums.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
"""
Another rudimentary attempt at using Quest data to train an NLP model using scikit-learn.
This script is a slimmed-down version of the quest training script for a single quest - DustUp.
It uses the language training model to filter the input for English only,
as only English is supported by the stopwords and tokenizer.
It cleans the input and uses a pipeline and manually created validation dataset.
"""
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn import metrics
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize
from joblib import dump
from joblib import load
import nltk
import re
import polars as pl
from funcs import *
nltk.download("wordnet")
def get_datasets(product_category_id, language="en"):
# This is the latest entire ORDS dataset.
logger.debug("*** ALL ORDS DATA ***")
alldata = (
ordsfuncs.get_data(cfg.get_envvar("ORDS_DATA"))
.filter(pl.col("product_category_id") == product_category_id)
.drop_nulls(subset="problem")
.select(pl.col("id", "problem"))
)
# This file holds the results of the quest.
logger.debug("*** QUEST DATA ***")
questdata = pl.read_csv(ordsfuncs.csv_path_quests("vacuums/dustup")).rename(
{"id_ords": "id"}
)
logger.debug(questdata)
# This file was created by manual review of some records not included in the quest.
logger.debug("*** VALIDATION DATA ***")
valdata = pl.read_csv(ordsfuncs.csv_path_quests("vacuums/dustup_validate_en"))
logger.debug(valdata)
# Join the datasets.
logger.debug("*** JOINED DATA ***")
jdata = (
alldata.join(questdata, on="id", how="left")
.join(valdata, on="id", how="left", suffix="_v")
.rename({"fault_type": "fault_type_q"})
)
logger.debug(jdata.select(pl.col("id", "fault_type_q", "fault_type_v")))
logger.debug("*** NEW DATA ***")
foo = textfuncs.clean_text(
jdata.filter(
(pl.col("fault_type_q").is_null()) & (pl.col("fault_type_v").is_null())
),
"problem",
)
newdata = detect_language(foo).filter(pl.col("language") == language)
logger.debug(newdata)
logger.debug("*** VAL DATA ***")
valdata = (
detect_language(
textfuncs.clean_text(jdata.filter((pl.col("fault_type_v").is_not_null())))
)
.filter(pl.col("language") == language)
.rename({"fault_type_v": "fault_type"})
.drop("fault_type_q")
)
logger.debug(valdata)
logger.debug("*** QUEST DATA ***")
questdata = (
detect_language(
textfuncs.clean_text(jdata.filter(pl.col("fault_type_q").is_not_null()))
)
.filter(pl.col("language") == language)
.rename({"fault_type_q": "fault_type"})
.drop("fault_type_v")
)
logger.debug(questdata)
return {
"questdata": questdata,
"valdata": valdata,
"data": newdata,
}
# Use a pre-trained model to detect and set the language.
# This should be more accurate than DeepL's language detection, though model still being refined.
# Requires that `ords_lang_training.py` has created the model object.
def detect_language(data):
lang_obj_path = f"{cfg.OUT_DIR}/ords_lang_obj_tfidf_cls_sentence.joblib"
if not pathfuncs.check_path(lang_obj_path):
print(f"LANGUAGE DETECTOR ERROR: MODEL NOT FOUND at {lang_obj_path}")
print("TO FIX THIS EXECUTE: ords_lang_training_sentence.py")
data = data.with_columns(language=pl.lit("??"))
else:
model = load(lang_obj_path)
data = data.with_columns(language=model.predict(data["problem"]))
return data
class LemmaTokenizer:
def __init__(self):
self.wnl = WordNetLemmatizer()
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_stopwords():
stopfile1 = open(f"{cfg.DATA_DIR}/stopwords-english.txt", "r")
stopfile2 = open(f"{cfg.DATA_DIR}/stopwords-english-repair.txt", "r")
stoplist = stopfile1.read().replace("\n", " ") + stopfile2.read().replace("\n", " ")
stopfile1.close()
stopfile2.close()
return stoplist
def do_training(questdata):
tokenizer = LemmaTokenizer()
pipe = Pipeline(
[
(
"tfidf",
TfidfVectorizer(
tokenizer=tokenizer, stop_words=tokenizer(get_stopwords())
),
),
("clf", MultinomialNB(force_alpha=True, alpha=0.01)),
]
)
pipe.fit(questdata["problem"], questdata["fault_type"])
dump(pipe, pipefile)
predictions = pipe.predict(questdata["problem"])
score = metrics.f1_score(questdata["fault_type"], predictions, average="macro")
logger.debug(f"** TRAINING : F1 SCORE: {score}")
questdata = questdata.with_columns(prediction=predictions)
questdata.write_csv(f"{cfg.OUT_DIR}/ords_quest_vacuum_training_results.csv")
logger.debug("** TRAINING MISSES **")
misses = questdata.filter(pl.col("language") != pl.col("prediction"))
logger.debug(misses)
misses.write_csv(f"{cfg.OUT_DIR}/ords_quest_vacuum_training_misses.csv")
def do_validation(valdata):
pipe = load(pipefile)
predictions = pipe.predict(valdata["problem"])
score = metrics.f1_score(valdata["fault_type"], predictions, average="macro")
logger.debug(f"** VALIDATION : F1 SCORE: {score}")
logger.debug(metrics.classification_report(valdata["fault_type"], predictions))
valdata = valdata.with_columns(prediction=predictions)
valdata.write_csv(f"{cfg.OUT_DIR}/ords_quest_vacuum_validation_results.csv")
logger.debug("** VALIDATION MISSES **")
misses = valdata.filter(pl.col("fault_type") != pl.col("prediction"))
logger.debug(misses)
misses.write_csv(f"{cfg.OUT_DIR}/ords_quest_vacuum_validation_misses.csv")
def do_test(data):
pipe = load(pipefile)
data = data.with_columns(prediction=pipe.predict(data["problem"]))
data.write_csv(f"{cfg.OUT_DIR}/ords_quest_vacuum_test_results.csv")
if __name__ == "__main__":
logger = cfg.init_logger(__file__)
pipefile = f"{cfg.OUT_DIR}/ords_quest_vacuum_obj_tfidf_cls.joblib"
datasets = get_datasets(product_category_id=34, language="en")
do_training(datasets["questdata"])
do_validation(datasets["valdata"])
do_test(datasets["data"])