-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_preprocessing.py
204 lines (150 loc) · 4.89 KB
/
test_preprocessing.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
199
200
201
202
203
import pytest
from preprocessing import KeywordCorpus
from preprocessing import KeywordCorpusFactory
from preprocessing import KeywordCorpusIterator
from preprocessing import SentenceIterator
from test_embedding import WiKiCorpusIterator
# 20181201 New Test
class TestSentenceIterator():
def test_init(self):
# Test input: list of str
test_sentences = [
'This is a test',
'This is a test again'
]
s = SentenceIterator(test_sentences)
for _s in s:
assert isinstance(_s, list)
# Test input: list of tokens
test_tokens = [
'This is a test'.split(' '),
'This is a test again'.split(' ')
]
s = SentenceIterator(test_tokens)
for _s in s:
assert isinstance(_s, list)
# Test input: Generator
test_gen = (str(i) for i in range(100))
s = SentenceIterator(test_gen)
count = 0
for _s in s:
count += 1
assert isinstance(_s, list)
if count == 0:
assert False
class TestKeywordCorpus():
def test_init(self):
kc = KeywordCorpus()
assert True
def test_set_and_get_item(self):
kc = KeywordCorpus()
kc['test'] = 'is good'
assert kc['test'] == 'is good'
class TestKeywordCorpusIterator():
def test_init(self):
kc = KeywordCorpus()
kc['test'] = set(['test is good', 'test is nice'])
kc_iter = KeywordCorpusIterator(kc)
count_token = 0
for tokens in kc_iter:
if not isinstance(tokens, list):
assert False
count_token += 1
if count_token != 2:
assert False
count_token = 0
kc_iter = KeywordCorpusIterator(kc, False)
for tokens in kc_iter:
if not isinstance(tokens, str):
assert False
count_token += 1
if count_token != 2:
assert False
class TestKeywordCorpusFactory():
def test_init(self):
keywords = ['test', 'test again']
kcf = KeywordCorpusFactory(keywords)
assert True
del kcf;
a_keyword = 'a keyword'
with pytest.raises(Exception):
kcf = KeywordCorpusFactory(a_keyword)
keywords = [1,2,3,4,5]
with pytest.raises(Exception):
kcf = KeywordCorpusFactory(keywords)
def test_keyword_corpus(self):
kcf = KeywordCorpusFactory(['hello', 'world'])
# 20181124 Hannah Chen, create keyword corpus
keyword_corpus = kcf.create(
['Hello World is our first program',
'Hello World is our last program']
)
assert keyword_corpus['hi'] == 'Corpus of Keyword hi does not exist.'
# assert kcf.keyword_corpus['hi'] == 'Corpus of Keyword hi does not exist.'
def test_create(self):
keywords = [
'anarchism', 'philosophy', 'autism', 'communication', 'autistic',
'family', 'studies', 'important', 'earth', 'incident']
wc = WiKiCorpusIterator(1000, return_str=True)
kcf = KeywordCorpusFactory(keywords)
keyword_corpus = kcf.create(wc)
for k in keywords:
if keyword_corpus[k] is None:
assert False
assert True
# def test_add_keyword_corpus(self):
# start_keywords = ['not', 'so', 'important']
# keyword = 'Hello'
# corpus = ['This is a Hello World Sample.', 'This is nothing']
# kcf = KeywordCorpusFactory(start_keywords)
# kcf.add_keyword_corpus(keyword, corpus)
# assert kcf.kc['Hello'] == set(corpus)
# new_corpus = ['This a new hello word sample']
# kcf.add_keyword_corpus(keyword, new_corpus)
# assert kcf.kc['Hello'] == set(corpus + new_corpus)
# # Test for list input which should be illegal
# with pytest.raises(Exception):
# kcf.add_keyword_corpus(['Hi again'], corpus)
def test_update(self):
first_keywords = ['Hello', 'World']
first_sentences = [
['Hello World is our first program'],
['Hello World is our last program']
]
kcf = KeywordCorpusFactory(first_keywords)
keyword_corpus = kcf.create(first_sentences)
# 20181201 LIN, Y.D. Test update sentences
second_sentences = [
['Hello World is a fantastic sample'],
['Hello World is a fantastic example']
]
kcf.update(sentences=second_sentences)
assert keyword_corpus['Hello'] == set([
'Hello World is our first program',
'Hello World is our last program',
'Hello World is a fantastic sample',
'Hello World is a fantastic example'
])
assert keyword_corpus['World'] == set([
'Hello World is our first program',
'Hello World is our last program',
'Hello World is a fantastic sample',
'Hello World is a fantastic example'
])
# 20181201 LIN, Y.D. Test update keyword
kcf.update(['program'])
assert keyword_corpus['program'] == set([
'Hello World is our first program',
'Hello World is our last program',
])
# assert keyword_corpus['program'] == [
# ['Hello', 'World', 'is', 'our', 'first', 'program'],
# ['Hello', 'World', 'is', 'our', 'last', 'program'],
# # ['Hello', 'World', 'is', 'a', 'fantastic', 'sample'],
# # ['Hello', 'World', 'is', 'a', 'fantastic', 'example']
# ]
# def test_create_keyword_corpus(self):
# kcf = KeywordCorpusFactory()
# test_sentences = ['I am cool.', 'I am handsome.']
# kcf.create_keyword_corpus('I', test_sentences)
# assert kcf.keyword_corpus['I'] == [['am', 'cool.'], ['am', 'handsome.']]