/
languagestring.cpp
executable file
·93 lines (73 loc) · 2.66 KB
/
languagestring.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
#include "languagestring.h"
#include <QPair>
#include <QMap>
#include <QTextStream>
#include <cmath>
GuessLanguage *LanguageString::_dont_use_directly_languageGuesser = 0;
QString LanguageString::subString(int lenghtInWords, int start) {
if (start < 0) {
start = qrand() % text.length();
}
int end = start + lenghtInWords;
if (end <= text.length()) {
QStringList res = text.mid(start, lenghtInWords);
return res.join(" ");
} else {
QStringList res1 = text.mid(start, text.length()-start);
QStringList res2 = text.mid(0, end % text.length());
return (res1 + res2).join(" ");
}
}
void LanguageString::test() {
// for each string lenght maps the number of strings tested
QMap<int, int> triesMap;
// for each string length maps the number of correct test
QMap<int, int> correctMap;
// the following length variable indicates the length in words
for (int length = text.length() / 3; length > 0; --length) {
for (int i = 0; i < text.length(); ++i) {
QString substr = subString(length, i);
if (substr.length() < 3) continue;
QList<QPair<Language,double> > guessedLanguages =
languageGuesser()->identify(substr);
triesMap[substr.size()] += 1;
if (!guessedLanguages.empty()) {
correctMap[substr.size()] +=
Language(guessedLanguages.first().first) == language;
}
}
}
//QTextStream cin(stdin);
QTextStream cout(stdout);
cout << "Testing language " << language << "\n";
//int max = triesMap.keys().last();
int max = 71;
int tries = 0;
int correct = 0;
int startingFrom = 3;
for (int i = 3; i <= max; ++i) {
tries += triesMap.value(i);
correct += correctMap.value(i);
if ((i-2)%3 == 0 || i == max) {
// every 3 iterations and at last iteration
cout << startingFrom << "-" << i << ": " << correct << "/"
<< tries << "(" << double(correct) / tries * 100 << "%)\n";
startingFrom = i+1;
tries = 0;
correct = 0;
}
}
// foreach (int size, triesMap.keys()) {
// int tries = triesMap.value(size);
// int correct = correctMap.value(size);
// cout << size << ": " << correct << "/" << tries << "("
// << double(correct) / tries * 100 << "%)\n";
// }
cout << endl;
}
GuessLanguage *LanguageString::languageGuesser() {
if (_dont_use_directly_languageGuesser == 0) {
_dont_use_directly_languageGuesser = new GuessLanguage();
}
return _dont_use_directly_languageGuesser;
}