-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (55 loc) · 1.82 KB
/
utils.py
File metadata and controls
71 lines (55 loc) · 1.82 KB
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
import os
import re
# import nltk
from sklearn.externals import joblib
from sklearn.pipeline import Pipeline
def dump(classifier: Pipeline, file_name: str):
"""
Dumps a classifier pipeline into the cache folder under the given file_name.pkl
:param classifier: Classifier to be pickled
:param file_name: Name of file to be saved
:return: Nothing
"""
assert file_name is not None
if not file_name.endswith(".pkl"):
file_name = file_name + ".pkl"
joblib.dump(classifier, os.path.join("cache", file_name))
def load_from_file(file_name):
"""
Retrieves a classifier from the cache folder with provided name.
:param file_name: Name of file to be retrieved as classifier
:return: The classifier pipeline, if one exists. None if nothing found
"""
assert file_name is not None
if not file_name.endswith(".pkl"):
file_name = file_name + ".pkl"
try:
return joblib.load(os.path.join("cache", file_name))
except FileNotFoundError as e:
return None
def preprocess_text(text):
"""
Default pre-processor
:param text:
:return:
"""
# one case
text = text.lower()
# remove various whitespaces
text = text.strip()
text = re.sub(r"what's", "what is ", text)
text = re.sub(r"\'s", " ", text)
text = re.sub(r"\'ve", " have ", text)
text = re.sub(r"can't", "can not ", text)
text = re.sub(r"n't", " not ", text)
text = re.sub(r"i'm", "i am ", text)
text = re.sub(r"\'re", " are ", text)
text = re.sub(r"\'d", " would ", text)
text = re.sub(r"\'ll", " will ", text)
text = re.sub(r"\'scuse", " excuse ", text)
text = re.sub(r'\W', ' ', text)
text = re.sub(r'\s+', ' ', text)
# # stem it all
# sno = nltk.SnowballStemmer('english')
# text = sno.stem(text)
return text