-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.cpp
201 lines (176 loc) · 5.04 KB
/
main_test.cpp
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
/*
* Lequel?
*
* Copyright (C) 2022 Marc S. Ressl
*
* Tests
*/
#include <iostream>
#include <string>
#include "Lequel.h"
#include "Text.h"
using namespace std;
void print(string s)
{
cout << s;
}
int fail()
{
cout << "FAIL" << endl;
return 1;
}
int pass()
{
cout << "PASS" << endl;
return 0;
}
int main()
{
Text text;
TrigramProfile profile;
TrigramProfile profile2;
float value;
Languages languages;
Language language;
print("buildTrigramProfile() works when text length is shorter than trigram length... ");
getText("hi", text);
profile = buildTrigramProfile(text);
if (profile.size() != 0)
return fail();
pass();
print("buildTrigramProfile() works when text length matches trigram length... ");
getText("^_^", text);
profile = buildTrigramProfile(text);
if ((profile.size() != 1) ||
(profile["^_^"] != 1))
return fail();
pass();
print("buildTrigramProfile() works when text length is trigram length plus one... ");
getText("meco", text);
profile = buildTrigramProfile(text);
if ((profile.size() != 2) ||
(profile["mec"] != 1) ||
(profile["eco"] != 1))
return fail();
pass();
print("buildTrigramProfile() works with repeated characters... ");
getText("aaaa", text);
profile = buildTrigramProfile(text);
if ((profile.size() != 1) ||
(profile["aaa"] != 2))
return fail();
pass();
print("buildTrigramProfile() works with non-ASCII characters... ");
getText("áéí", text);
profile = buildTrigramProfile(text);
if ((profile.size() != 1) ||
(profile["áéí"] != 1))
return fail();
pass();
print("normalizeTrigramProfile() does not add or remove keys... ");
profile.clear();
profile["ABC"] = 1;
profile["BCD"] = 1;
profile["CDE"] = 1;
profile["DEF"] = 1;
normalizeTrigramProfile(profile);
if (profile.size() != 4)
return fail();
pass();
print("normalizeTrigramProfile() works with equal frequencies... ");
profile.clear();
profile["ABC"] = 1;
profile["BCD"] = 1;
profile["CDE"] = 1;
profile["DEF"] = 1;
normalizeTrigramProfile(profile);
if ((profile["ABC"] != 0.5F) ||
(profile["BCD"] != 0.5F) ||
(profile["CDE"] != 0.5F) ||
(profile["DEF"] != 0.5F))
return fail();
pass();
print("normalizeTrigramProfile() works with different frequencies... ");
profile.clear();
profile["ABC"] = 4;
profile["BCD"] = 3;
normalizeTrigramProfile(profile);
if ((profile["ABC"] != 0.8F) ||
(profile["BCD"] != 0.6F))
return fail();
pass();
print("normalizeTrigramProfile() works with large numbers... ");
profile.clear();
profile["ABC"] = 1E9;
normalizeTrigramProfile(profile);
if (profile["ABC"] != 1)
return fail();
pass();
print("getCosineSimilarity() works with one shared key... ");
profile.clear();
profile2.clear();
profile["ABC"] = 1;
profile2["ABC"] = 1;
value = getCosineSimilarity(profile, profile);
if (value != 1)
return fail();
pass();
print("getCosineSimilarity() works with non-overlapping keys... ");
profile.clear();
profile2.clear();
profile["ABC"] = 1;
profile2["BCD"] = 2;
value = getCosineSimilarity(profile, profile2);
if (value != 0)
return fail();
pass();
print("getCosineSimilarity() works with keys present in textProfile but not languageProfile... ");
profile.clear();
profile2.clear();
// These aren't normalized but we are just testing...
profile["ABC"] = 1;
profile["BCD"] = 2;
profile["CDE"] = 3;
profile2["BCD"] = 4;
profile2["CDE"] = 5;
value = getCosineSimilarity(profile, profile2);
if (value != 23)
return fail();
pass();
print("getCosineSimilarity() works with keys mising in both textProfile and languageProfile... ");
profile.clear();
profile2.clear();
profile["BCD"] = 3;
profile["CDE"] = 4;
profile2["ABC"] = 4;
profile2["BCD"] = 5;
value = getCosineSimilarity(profile, profile2);
if (value != 15)
return fail();
pass();
print("identifyLanguage() works with perfect similarity... ");
string languageCodes[3] = {"---", "---", "---"};
languages.clear();
language.languageCode = "L1";
language.trigramProfile.clear();
language.trigramProfile["ABC"] = 1;
languages.push_back(language);
language.languageCode = "L2";
language.trigramProfile.clear();
language.trigramProfile["BCD"] = 1;
languages.push_back(language);
language.languageCode = "L3";
language.trigramProfile.clear();
language.trigramProfile["CDE"] = 1;
languages.push_back(language);
language.languageCode = "L4";
language.trigramProfile.clear();
language.trigramProfile["DEF"] = 1;
languages.push_back(language);
getText("CDE", text);
identifyLanguage(text, languages, languageCodes);
if (languageCodes[0] != "L3")
return fail();
pass();
return 0;
}