-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtfidf_model.py
43 lines (31 loc) · 1.07 KB
/
tfidf_model.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
"""
Term Frequency Inverse Document Vectorizer
------------------------------------------
Wrapper around SciPy TFIDF Vectorizer
Public Methods
~~~~~~~~~~~~~~
train_model
"""
from __future__ import print_function
from sklearn.feature_extraction.text import TfidfVectorizer
class CustomPreprocessor(object):
def __call__(self, doc):
return doc
class CustomTokenizer(object):
def __call__(self, doc):
return doc
def train_model(data_samples, test_dataset):
df = (5, 0.90)
tfidf_vectorizer = TfidfVectorizer(
min_df=df[0], max_df=df[1],
# found in 90% and found onnly 5 documents are ignored
tokenizer=CustomTokenizer(),
preprocessor=CustomPreprocessor())
tfidf_vectorizer.fit(data_samples)
tf_trained = tfidf_vectorizer.transform(test_dataset)
return {
'features': tfidf_vectorizer.get_feature_names(),
'transformations': tf_trained,
'_model': tfidf_vectorizer,
'_name': 'tfidf'
}