-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathords_demo_tfidf.py
143 lines (110 loc) · 3.96 KB
/
ords_demo_tfidf.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
#!/usr/bin/env python3
# Demonstration of extracting vocabulary, features and "bag of words" using TfidfVectorizer.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
import polars as pl
from funcs import *
# Using English language records (assumed by country).
def get_data():
return ordsfuncs.get_data(cfg.get_envvar("ORDS_DATA")).filter(
pl.col("country").is_in(["USA"]),
pl.col("problem").str.len_chars() > 24,
)
def get_problem_text(data, category):
return list(
data.filter(
pl.col("product_category") == category,
).select(
pl.col(
"problem",
)
)["problem"]
)
# Split the partner_product_category string.
def get_products(data, category):
return list(
ordsfuncs.extract_products(
data.filter(
pl.col("product_category") == category,
)
)
.select(pl.col("product"))
.drop_nulls()
.unique()["product"]
)
# Using derived `item_type` values.
def fit_products(data):
logger.debug("*** PRODUCT TfidfVectorizer ***")
categories = ordsfuncs.get_categories(cfg.get_envvar("ORDS_CATS"))
for id, category in categories.iter_rows():
logger.debug(f"**** {category} ****")
strings = get_products(data, category)
if len(strings) == 0:
continue
tv = TfidfVectorizer()
cv = CountVectorizer()
tv_fit = tv.fit_transform(strings).toarray()
cv_fit = cv.fit_transform(strings).toarray()
logger.debug("** TV vocabulary **")
logger.debug(tv.vocabulary_)
logger.debug("** CV vocabulary **")
logger.debug(cv.vocabulary_)
logger.debug("** TV feature names **")
logger.debug(tv.get_feature_names_out())
logger.debug("** CV feature names **")
logger.debug(cv.get_feature_names_out())
# logger.debug('** TV bag of words **')
# logger.debug(tv_fit)
# logger.debug('** CV bag of words **')
# logger.debug(cv_fit)
def fit_problem_text(data):
logger.debug("*** PROBLEM ***")
categories = ordsfuncs.get_categories(cfg.get_envvar("ORDS_CATS"))
logger.debug("*** TfidfVectorizer ***")
for id, category in categories.iter_rows():
logger.debug(f"**** {category} ****")
# strings = get_problem_text(data, category)
strings = list(
data.filter(
pl.col("product_category") == category,
).select(
pl.col(
"problem",
)
)["problem"]
)
if len(strings) == 0:
continue
tv = TfidfVectorizer()
cv = CountVectorizer()
tv_fit = tv.fit_transform(strings).toarray()
cv_fit = cv.fit_transform(strings).toarray()
foo = tv.vocabulary_
bar = cv.vocabulary_
if foo != bar:
logger.debug("** DIFF vocabulary **")
logger.debug("** TV vocabulary **")
logger.debug(tv.vocabulary_)
logger.debug("** CV vocabulary **")
logger.debug(cv.vocabulary_)
logger.debug("** TV stop words **")
logger.debug(tv.get_stop_words())
logger.debug("** CV stop words **")
logger.debug(cv.get_stop_words())
foo = tv.get_feature_names_out()
bar = cv.get_feature_names_out()
if (foo != bar).all():
logger.debug("** DIFF feature names **")
logger.debug("** TV feature names **")
logger.debug(tv.get_feature_names_out())
logger.debug("** CV feature names **")
logger.debug(cv.get_feature_names_out())
# logger.debug('** TV bag of words **')
# logger.debug(tv_fit)
# logger.debug('** CV bag of words **')
# logger.debug(cv_fit)
if __name__ == "__main__":
logger = cfg.init_logger(__file__)
data = get_data()
fit_products(data)
fit_problem_text(data)