-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_analyzer.py
More file actions
198 lines (169 loc) · 6.2 KB
/
test_analyzer.py
File metadata and controls
198 lines (169 loc) · 6.2 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
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
"""Test module for analyzer.py"""
import pytest
import src.analyzer as az
import pandas as pd
import src.constants as cts
def test_tokenize():
"""Test tokenize break down str into list of str correctly with the porter
method from nltk package"""
input_text = "Test tokenize break down str into list of str correctly"
output = az.tokenize(input_text)
expected = ["test", "tokenize", "break", "str", "list", "str", "correctly"]
assert output == expected
@pytest.mark.parametrize(
"input_text, expected",
[
(
"The programer is programming many functional programs.",
["programer", "program", "functional", "program"],
),
(
"It is likely that many like words have liked liking other likes",
["likely", "like", "word", "like", "like", "like"],
),
(
"If you can't avoid it. We'll all use punctuation.",
["not", "avoid", "use", "punctuation"],
),
("can't don't won't", ["not", "not", "will", "not"]),
("... ! @ # $ *** ##", [],),
],
)
def test_tokenize_parametrize(input_text, expected):
"""parametrize test tokenize"""
output = az.tokenize(input_text)
assert output == expected
@pytest.mark.parametrize(
"input_text, expected",
[
("numbers 1 2 3 4 55", "numbers ",),
("a sentence\nin a new line", "a sentence in a new line",),
],
)
# pylint: disable=W0613
def test_normalize(input_text, expected):
"""parametrize test normalize"""
output = az.normalize(input_text)
assert output == expected
def test_compute_frequency():
"""Test if it return correct frequency result"""
token_lst = ["hello", "hello", "hello"]
output = az.compute_frequency(token_lst)
assert output == [("hello", 3)]
def test_word_frequency():
"""Test if it return correct frequency result from a file"""
text = "hello world hello world hello world"
output = az.word_frequency(text)
expected = [("hello", 3), ("world", 3)]
assert expected == output
def test_dir_frequency(tmp_path):
"""Test if it return correct frequency result from a directory"""
directory = tmp_path / "sub"
directory.mkdir()
para_1 = directory / "hello.md"
para_2 = directory / "world.md"
text = "# header\n hello world hello world hello world"
para_1.write_text(text)
para_2.write_text(text)
output = az.dir_frequency(directory)
expected = [("hello", 6), ("world", 6)]
assert expected == output
def test_part_of_speech():
"""Test if it return correct part of speech information"""
text = "The greatest technical challenge that I faced \
was getting the program to run"
output = az.part_of_speech(text)
assert output == [
("The", "DET"),
("greatest", "ADJ"),
("technical", "ADJ"),
("challenge", "NOUN"),
("that", "DET"),
("I", "PRON"),
("faced", "VERB"),
("was", "AUX"),
("getting", "VERB"),
("the", "DET"),
("program", "NOUN"),
("to", "PART"),
("run", "VERB"),
]
def test_named_entity_recognization():
"""Test if it return correct noun phrases"""
text = "Apple is looking at buying U.K. startup for $1 billion"
output = az.named_entity_recognization(text)
assert output == [
("Apple", "ORG"),
("U.K.", "GPE"),
("$1 billion", "MONEY"),
]
def test_noun_phrase():
"""test return correct noun phrase"""
text = "Apple is looking at buying U.K. startup for $1 billion"
output = az.noun_phrase(text)
assert output == ["Apple", "U.K. startup"]
def test_lemmatized_text():
"""Test lemmatized text works"""
text = "She loves dogs"
output = az.lemmatized_text(text)
expect = "love dog"
print(output)
assert output == expect
def test_sentence_tokenize():
"""Test sentence tokenizer works"""
text = "The greatest technical challenge that I faced \
was getting the program to run. I am looking forward to some \
poroblem even involing some challenging math to be more interesting."
output = az.sentence_tokenize(text)
assert output == [
"The greatest technical challenge that I faced was getting the program \
to run.",
"I am looking forward to some poroblem even involing some challenging \
math to be more interesting.",
]
def test_tfidf():
"""test tfidf return result"""
input_tokens = [
"test",
"tokenize",
"break",
"str",
"list",
"str",
"correctly",
]
term_frequency, vector = az.compute_tfidf(input_tokens)
assert term_frequency is not None
assert vector is not None
def test_category_frequency():
"test that professional skills, technical skills, and ethics are properly \
classified "
text = ["One professional skill that I practiced was communicating \
independently with a team. I did this by atttending all meetings, using \
Zenhub, and including everyone in the major decision making process. I \
also practiced the professional skill of resolving conflicts by talking \
through the conflict with my group members, coming to a resolution, and \
apologizing for the mishap that I caused."]
output = az.category_frequency(text)
print(output)
assert output["Professional Skills"] == 1
text = ["One technical skill that I practiced was installing Python \
packages and integrating these packages with my code."]
output = az.category_frequency(text)
print(output)
assert output["Technical Skills"] == 1
def test_top_polarized_word():
"""Tests if the positive/negative words columns are created"""
df = pd.DataFrame(columns=[cts.TOKEN, cts.POSITIVE, cts.NEGATIVE])
input_tokens = [
["incredible", "horrible", "terrific", "terrible"],
["amazing", "devastating", "boring", "cool"],
["alarming", "awesome", "beautiful", "ugly"],
]
df[cts.TOKEN] = pd.Series(input_tokens)
df[cts.POSITIVE], df[cts.NEGATIVE] = \
az.top_polarized_word(df[cts.TOKEN].values)
assert df[cts.POSITIVE] is not None
assert df[cts.NEGATIVE] is not None
assert df[cts.POSITIVE].size is df[cts.TOKEN].size
assert df[cts.NEGATIVE].size is df[cts.TOKEN].size