/
test_tfidf.py
executable file
·157 lines (116 loc) · 4.92 KB
/
test_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import unittest
import os
import re
from dotenv import load_dotenv
import sklearn.datasets
import nlpaug.augmenter.word as naw
import nlpaug.model.word_stats as nmw
from nlpaug.util import Action
class TestTfIdf(unittest.TestCase):
@classmethod
def setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
def test_train(self):
def _tokenizer(text, token_pattern=r"(?u)\b\w\w+\b"):
token_pattern = re.compile(token_pattern)
return token_pattern.findall(text)
train_data = sklearn.datasets.fetch_20newsgroups(subset='train', remove=('headers', 'footers', 'quotes'))
train_x = train_data.data
train_x_tokens = [_tokenizer(x) for x in train_x]
tfidf_model = nmw.TfIdf()
tfidf_model.train(train_x_tokens)
tfidf_model.save('.')
aug = naw.TfIdfAug(model_path='.', tokenizer=_tokenizer)
texts = [
'The quick brown fox jumps over the lazy dog',
'asdasd test apple dog asd asd'
]
for text in texts:
augmented_text = aug.augment(text)
self.assertNotEqual(text, augmented_text)
self.assertLess(0, len(texts))
def test_empty_input_for_insert(self):
texts = [' ']
aug = naw.TfIdfAug(model_path=os.environ.get("MODEL_DIR"), action=Action.SUBSTITUTE)
for text in texts:
augmented_text = aug.augment(text)
self.assertEqual('', augmented_text)
self.assertEqual(1, len(texts))
def test_oov(self):
unknown_token = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
texts = [
unknown_token,
unknown_token + ' the'
]
augmenters = [
naw.TfIdfAug(model_path=os.environ.get("MODEL_DIR"), action=Action.INSERT),
naw.TfIdfAug(model_path=os.environ.get("MODEL_DIR"), action=Action.SUBSTITUTE)
]
for aug in augmenters:
for text in texts:
self.assertLess(0, len(text))
augmented_text = aug.augment(text)
if aug.action == Action.INSERT:
self.assertLess(len(text.split(' ')), len(augmented_text.split(' ')))
self.assertNotEqual(text, augmented_text)
elif aug.action == Action.SUBSTITUTE:
self.assertEqual(len(text.split(' ')), len(augmented_text.split(' ')))
if unknown_token == text:
self.assertEqual(text, augmented_text)
else:
self.assertNotEqual(text, augmented_text)
else:
raise Exception('Augmenter is neither INSERT or SUBSTITUTE')
def test_insert(self):
texts = [
'The quick brown fox jumps over the lazy dog'
]
aug = naw.TfIdfAug(model_path=os.environ.get("MODEL_DIR"), action=Action.INSERT)
for text in texts:
self.assertLess(0, len(text))
augmented_text = aug.augment(text)
self.assertLess(len(text.split(' ')), len(augmented_text.split(' ')))
self.assertNotEqual(text, augmented_text)
self.assertLess(0, len(texts))
def test_substitute(self):
texts = [
'The quick brown fox jumps over the lazy dog'
]
aug = naw.TfIdfAug(model_path=os.environ.get("MODEL_DIR"), action=Action.SUBSTITUTE)
for text in texts:
self.assertLess(0, len(text))
augmented_text = aug.augment(text)
self.assertNotEqual(text, augmented_text)
self.assertLess(0, len(texts))
def test_substitute_stopwords(self):
texts = [
'The quick brown fox jumps over the lazy dog'
]
stopwords = [t.lower() for t in texts[0].split(' ')[:3]]
aug_n = 3
aug = naw.TfIdfAug(
model_path=os.environ.get("MODEL_DIR"),
action=Action.SUBSTITUTE,
stopwords=stopwords)
for text in texts:
self.assertLess(0, len(text))
augmented_text = aug.augment(text)
augmented_tokens = aug.tokenizer(augmented_text)
tokens = aug.tokenizer(text)
augmented_cnt = 0
for token, augmented_token in zip(tokens, augmented_tokens):
if token.lower() in stopwords and len(token) > aug_n:
self.assertEqual(token.lower(), augmented_token)
else:
augmented_cnt += 1
self.assertGreater(augmented_cnt, 0)
self.assertLess(0, len(texts))
def test_skip_punctuation(self):
text = '. . . . ! ? # @'
aug = naw.TfIdfAug(
model_path=os.environ.get("MODEL_DIR"),
action=Action.SUBSTITUTE)
augmented_text = aug.augment(text)
self.assertEqual(text, augmented_text)