forked from phelrine/stakk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stakk_server.h
85 lines (78 loc) · 2.01 KB
/
stakk_server.h
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
#ifndef STAKK_SERVER_H
#define STAKK_SERVER_H
#include "common.h"
#include "util.h"
#include "trie.h"
#include "connection.h"
#include "stakk.h"
#include "converter.h"
#include "server.h"
#include "trie_server.h"
namespace stakk {
class StakkServer : public TrieServer {
protected:
Stakk &stakk;
Converter &converter;
int number;
string output;
public:
//init trie and stakk references
StakkServer(int port_, WideTrie &trie_, Stakk &stakk_, Converter &converter_)
: TrieServer(port_, trie_), stakk(stakk_), converter(converter_)
{
}
//parse request path
wstring parse_path(vector<string> &path) {
if (path.size() < 4)
return L"";
mode = path[2];
if (mode == "convert") {
if (path.size() > 4)
output = path[4];
else
output = "wakati";
} else if (mode == "predict") {
if (path.size() > 4)
number = atoi(path[4].c_str());
else
number = 50;
} else if (mode == "spell") {
if (path.size() > 4)
threshold = atoi(path[4].c_str());
else
threshold = 1;
if (path.size() > 5)
number = atoi(path[5].c_str());
else
number = 50;
}
return widen(urldecode(path[3]));
}
//implement spell and predict mode
virtual wstring get_result(vector<string> path) {
wstring input = parse_path(path);
//converter
if (mode == "convert") {
vector<Converter::Node> nodes = converter.convert(input);
return converter.format(nodes, output);
}
//prediction or spell correction
{
vector<Stakk::Entry> result;
if (mode == "spell")
stakk.correct(input, threshold, result);
else if (mode == "predict")
stakk.predict(input, result);
wstring response = L"";
for (int i = 0; i < min(number, (int)result.size()); i++) {
response += result[i].format() + L"\n";
}
if (result.size() > 0)
return response;
}
//otherwise raw trie operation
return TrieServer::get_result(path);
}
};
}
#endif