-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordList.cpp
More file actions
45 lines (43 loc) · 1.42 KB
/
Copy pathWordList.cpp
File metadata and controls
45 lines (43 loc) · 1.42 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
#include "WordList.h"
#include <cmath>
#include <iostream>
WordList::WordList(std::istream& stream) {
std::string buffer;
while (getline(stream, buffer)) {
bool ignore = false;
for (size_t i = 0; i < buffer.length(); i++) {
if (isspace(buffer[i])) {
buffer.erase(i,1);
} else if (!(isalpha(buffer[i]) && islower(buffer[i]))) {
ignore = true;
break;
}
}
if (!ignore) {
mWords.push_back(buffer);
}
}
}
Heap WordList::correct(const std::vector<Point>& points, size_t maxcount, float cutoff) const {
Heap candidates(maxcount);
for (std::string buffer : mWords) {
if (buffer.length() == points.size()) {
double s_sum = 0;
for (size_t i = 0; i < points.size(); i++) {
double d = pow(pow(points[i].x-QWERTY[buffer[i]-'a'].x, 2) + pow(points[i].y-QWERTY[buffer[i]-'a'].y, 2), .5); //pythagoran theorem
s_sum += 1/(10*pow(d,2)+1);
}
float s = (float)(s_sum/points.size());
try {
if (s >= cutoff) {
candidates.push(buffer, s);
}
} catch (std::overflow_error& e) {
if (s > candidates.top().score) {
candidates.pushpop(buffer, s);
}
}
}
}
return candidates;
}