2
2
#include < zim/item.h>
3
3
#include < zim/error.h>
4
4
#include < zim/search.h>
5
+ #include < zim/suggestion.h>
5
6
#include < iostream>
6
7
#include < chrono>
7
8
#include < emscripten/bind.h>
@@ -69,6 +70,9 @@ class EntryWrapper{
69
70
std::string getPath () {
70
71
return m_entry.getPath ();
71
72
}
73
+ std::string getTitle () {
74
+ return m_entry.getTitle ();
75
+ }
72
76
bool isRedirect () {
73
77
return m_entry.isRedirect ();
74
78
}
@@ -80,6 +84,68 @@ class EntryWrapper{
80
84
zim::Entry m_entry;
81
85
};
82
86
87
+ // SuggestionSearcher wrapper
88
+ class SuggestionSearcherWrapper {
89
+ public:
90
+ SuggestionSearcherWrapper () {
91
+ if (!g_archive) {
92
+ throw std::runtime_error (" No archive loaded" );
93
+ }
94
+ searcher = std::make_unique<zim::SuggestionSearcher>(*g_archive);
95
+ }
96
+
97
+ SuggestionSearchWrapper suggest (const std::string& query);
98
+
99
+ private:
100
+ std::unique_ptr<zim::SuggestionSearcher> searcher;
101
+ };
102
+
103
+ // SuggestionSearch wrapper
104
+ class SuggestionSearchWrapper {
105
+ public:
106
+ SuggestionSearchWrapper (zim::SuggestionSearch search)
107
+ : search_(search) {}
108
+
109
+ unsigned int getEstimatedMatches () const {
110
+ try {
111
+ return search_.getEstimatedMatches ();
112
+ } catch (const std::exception& e) {
113
+ std::cout << " getEstimatedMatches error: " << e.what () << std::endl;
114
+ return 0 ;
115
+ }
116
+ }
117
+
118
+ std::vector<EntryWrapper> getResults (int start, int count) const {
119
+ try {
120
+ zim::SuggestionResultSet resultSet = search_.getResults (start, count);
121
+ std::vector<EntryWrapper> results;
122
+
123
+ for (const auto & entry : resultSet) {
124
+ results.push_back (EntryWrapper (entry));
125
+ }
126
+
127
+ return results;
128
+ } catch (const std::exception& e) {
129
+ std::cout << " getResults error: " << e.what () << std::endl;
130
+ return std::vector<EntryWrapper>();
131
+ }
132
+ }
133
+
134
+ private:
135
+ zim::SuggestionSearch search_;
136
+ };
137
+
138
+ // Implement the suggest method (needs to be after SuggestionSearchWrapper definition)
139
+ SuggestionSearchWrapper SuggestionSearcherWrapper::suggest (const std::string& query) {
140
+ try {
141
+ zim::SuggestionSearch search = searcher->suggest (query);
142
+ return SuggestionSearchWrapper (search);
143
+ } catch (const std::exception& e) {
144
+ std::cout << " suggest error: " << e.what () << std::endl;
145
+ throw ;
146
+ }
147
+ }
148
+
83
149
// Get an entry by its path
84
150
std::unique_ptr<EntryWrapper> getEntryByPath (std::string url) {
85
151
try {
@@ -107,19 +173,38 @@ std::vector<EntryWrapper> search(std::string text, int numResults) {
107
173
return ret;
108
174
}
109
175
176
+ // Suggestion search function (alternative to class-based approach)
177
+ std::vector<EntryWrapper> suggest (std::string text, int numResults) {
178
+ try {
179
+ auto suggestionSearcher = zim::SuggestionSearcher (*g_archive);
180
+ auto suggestionSearch = suggestionSearcher.suggest (text);
181
+ auto resultSet = suggestionSearch.getResults (0 , numResults);
182
+ std::vector<EntryWrapper> ret;
183
+ for (auto entry : resultSet) {
184
+ ret.push_back (EntryWrapper (entry));
185
+ }
186
+ return ret;
187
+ } catch (const std::exception& e) {
188
+ std::cout << " suggestion error: " << e.what () << std::endl;
189
+ return std::vector<EntryWrapper>();
190
+ }
191
+ }
192
+
110
193
// Binding code
111
194
EMSCRIPTEN_BINDINGS (libzim_module) {
112
195
emscripten::function (" loadArchive" , &loadArchive);
113
196
emscripten::function (" getEntryByPath" , &getEntryByPath);
114
197
emscripten::function (" getArticleCount" , &getArticleCount);
115
198
emscripten::function (" search" , &search);
199
+ emscripten::function (" suggest" , &suggest);
116
200
emscripten::register_vector<char >(" vector<char>" );
117
201
emscripten::register_vector<EntryWrapper>(" vector(EntryWrapper)" );
118
202
class_<EntryWrapper>(" EntryWrapper" )
119
203
.function (" getItem" , &EntryWrapper::getItem)
120
204
.function (" getPath" , &EntryWrapper::getPath)
121
205
.function (" isRedirect" , &EntryWrapper::isRedirect)
122
206
.function (" getRedirectEntry" , &EntryWrapper::getRedirectEntry)
207
+ .function (" getTitle" , &EntryWrapper::getTitle)
123
208
;
124
209
class_<ItemWrapper>(" ItemWrapper" )
125
210
.function (" getData" , &ItemWrapper::getData)
@@ -128,4 +213,12 @@ EMSCRIPTEN_BINDINGS(libzim_module) {
128
213
class_<BlobWrapper>(" BlobWrapper" )
129
214
.function (" getContent" , &BlobWrapper::getContent)
130
215
;
216
+ class_<SuggestionSearcherWrapper>(" SuggestionSearcher" )
217
+ .constructor <>()
218
+ .function (" suggest" , &SuggestionSearcherWrapper::suggest)
219
+ ;
220
+ class_<SuggestionSearchWrapper>(" SuggestionSearch" )
221
+ .function (" getEstimatedMatches" , &SuggestionSearchWrapper::getEstimatedMatches)
222
+ .function (" getResults" , &SuggestionSearchWrapper::getResults)
223
+ ;
131
224
}
0 commit comments