-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_words.py
185 lines (149 loc) · 5.14 KB
/
test_words.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
# coding: utf8
""" Test cases """
import pickle
from urduhack import normalize
from urduhack.urdu_characters import URDU_ALPHABETS
file_name: str = "words.txt"
file_bigram_name: str = "bigram_words.txt"
file_trigram_name: str = "trigram_words.txt"
location_file: str = "ner/locations.txt"
person_file: str = "ner/persons.txt"
organization_file: str = "ner/organizations.txt"
date_file: str = "ner/dates.txt"
def test_words():
""" Test case"""
handler = open(file_name, encoding="utf8")
for word in handler:
for character in word.strip():
if character is '_':
continue
assert character in URDU_ALPHABETS, F"Incorrect word: {word} and char: {character}"
handler.close()
def test_bigram_words():
""" Test case"""
handler = open(file_bigram_name, encoding="utf8")
for word in handler:
for character in word.strip():
if character is '_':
continue
assert character in URDU_ALPHABETS, F"Incorrect word: {word} and char: {character}"
handler.close()
def test_trigram_words():
""" Test case"""
handler = open(file_trigram_name, encoding="utf8")
for word in handler:
for character in word.strip():
if character is '_':
continue
assert character in URDU_ALPHABETS, F"Incorrect word: {word} and char: {character}"
handler.close()
def sorted_words():
"""Sorted the words.txt file"""
handler = open(file_name, encoding="utf8")
words_set = set()
for word in handler:
word = normalize(word.strip())
word = '_'.join(word.split())
words_set.add(word.strip())
handler.close()
print(F"Total words: {len(words_set)}")
words_set = sorted(words_set)
with open(file_name, 'w', encoding="utf8") as the_file:
for word in words_set:
the_file.write(word + '\n')
def duplicate_words():
""" find duplicate words"""
words_dict = {}
handler = open(file_name, encoding="utf8")
for word in handler:
inner_handler = open(file_name, encoding="utf8")
inner_list = []
for inner_word in inner_handler:
if word.strip() in inner_word.strip():
inner_list.append(inner_word.strip())
inner_handler.close()
if inner_list:
words_dict[word.strip()] = inner_list
handler.close()
with open('words.dict', 'wb') as fp:
pickle.dump(words_dict, fp)
def load_pkl():
"""Load the save dict file"""
with open('words.dict', 'rb') as fp:
word_dict = pickle.load(fp)
for key, values in word_dict.items():
print("=" * 50)
print(key)
print(values)
def test_locations():
"""Test Case"""
with open(location_file, "r") as file:
for word in file:
word = word.strip()
for char in word:
if char is " ":
continue
assert char in URDU_ALPHABETS, f"Incorrect word: {word} and char: {char}"
def test_persons():
"""Test Case"""
with open(person_file, "r") as file:
for word in file:
word = word.strip()
for char in word:
if char is " ":
continue
assert char in URDU_ALPHABETS, f"Incorrect word: {word} and char: {char}"
def test_organizations():
"""Test Case"""
with open(organization_file, "r") as file:
for word in file:
word = word.strip()
for char in word:
if char is " ":
continue
assert char in URDU_ALPHABETS, f"Incorrect word: {word} and char: {char}"
def test_dates():
"""Test Case"""
with open(date_file, "r") as file:
for word in file:
word = word.strip()
for char in word:
if char is " ":
continue
assert char in URDU_ALPHABETS, f"Incorrect word: {word} and char: {char}"
def test_ner_duplicates():
"""Test Case"""
locations = []
persons = []
organizations = []
dates = []
with open(location_file, "r") as loc_file, open(person_file, "r") as per_file, open(
organization_file, "r") as org_file, open(date_file, "r") as dat_file:
for word in loc_file:
word = word.strip()
locations.append(word)
for word in per_file:
word = word.strip()
persons.append(word)
for word in org_file:
word = word.strip()
organizations.append(word)
for word in dat_file:
word = word.strip()
dates.append(word)
for word in locations:
assert word not in organizations
assert word not in persons
assert word not in dates
for word in persons:
assert word not in locations
assert word not in organizations
assert word not in dates
for word in organizations:
assert word not in persons
assert word not in locations
assert word not in dates
for word in dates:
assert word not in persons
assert word not in locations
assert word not in organizations