-
Notifications
You must be signed in to change notification settings - Fork 0
/
HunspellPHP.cpp
161 lines (124 loc) · 3.86 KB
/
HunspellPHP.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
#include <iostream>
#include <hunspell/hunspell.h>
#include <phpcpp.h>
#include <stdio.h>
using namespace std;
inline bool file_exists(const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
class Hunspell : public Php::Base {
private:
Hunhandle * handle = nullptr;
const char * encoding = nullptr;
public:
Hunspell() = default;
virtual ~Hunspell() = default;
Php::Value getEncoding() const
{
return encoding;
}
void __construct(Php::Parameters ¶ms)
{
const char * aff_path = params[0];
const char * dic_path = params[1];
if (!file_exists(aff_path)) {
throw Php::Exception("Aff file does not found");
}
if (!file_exists(dic_path)) {
throw Php::Exception("Dic file does not found");
}
handle = Hunspell_create(aff_path, dic_path);
if (!handle) {
throw Php::Exception("Cannot open dictionary");
}
encoding = Hunspell_get_dic_encoding(handle);
}
Php::Value spell(Php::Parameters ¶ms)
{
const char * word = params[0];
return (bool)Hunspell_spell(handle, word);
}
Php::Value stem(Php::Parameters ¶ms)
{
Php::Value result;
char **slist;
const char * word = params[0];
int num_slist = Hunspell_stem(handle, &slist, word);
for (int i = 0; i < num_slist; i++) {
result[i] = slist[i];
}
Hunspell_free_list(handle, &slist, num_slist);
return result;
}
Php::Value suggest(Php::Parameters ¶ms)
{
Php::Value result;
char **slist;
const char * word = params[0];
int num_slist = Hunspell_suggest(handle, &slist, word);
for (int i = 0; i < num_slist; i++) {
result[i] = slist[i];
}
Hunspell_free_list(handle, &slist, num_slist);
return result;
}
Php::Value analyze(Php::Parameters ¶ms)
{
Php::Value result;
char **slist;
const char * word = params[0];
int num_slist = Hunspell_analyze(handle, &slist, word);
for (int i = 0; i < num_slist; i++) {
result[i] = slist[i];
}
Hunspell_free_list(handle, &slist, num_slist);
return result;
}
Php::Value add(Php::Parameters ¶ms)
{
const char * word = params[0];
return Hunspell_add(handle, word);
}
Php::Value remove(Php::Parameters ¶ms)
{
const char * word = params[0];
return Hunspell_remove(handle, word);
}
};
extern "C" {
PHPCPP_EXPORT void *get_module()
{
static Php::Extension HunspellExtension("hunspellphp", "1.0");
Php::Class<Hunspell> hunspell("Hunspell");
hunspell.method<&Hunspell::__construct>("__construct", {
Php::ByVal("affFilePath", Php::Type::String),
Php::ByVal("dicFilePath", Php::Type::String)
});
hunspell.method<&Hunspell::spell>("spell", {
Php::ByVal("word", Php::Type::String)
});
hunspell.method<&Hunspell::stem>("stem", {
Php::ByVal("word", Php::Type::String)
});
hunspell.method<&Hunspell::suggest>("suggest", {
Php::ByVal("word", Php::Type::String)
});
hunspell.method<&Hunspell::analyze>("analyze", {
Php::ByVal("word", Php::Type::String)
});
hunspell.method<&Hunspell::add>("add", {
Php::ByVal("word", Php::Type::String)
});
hunspell.method<&Hunspell::remove>("remove", {
Php::ByVal("word", Php::Type::String)
});
hunspell.property("encoding", &Hunspell::getEncoding);
HunspellExtension.add(std::move(hunspell));
return HunspellExtension.module();
}
}