~ubuntu-branches/ubuntu/saucy/libcolumbus/saucy

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2012 Canonical, Ltd.
 *
 * Authors:
 *    Jussi Pakkanen <jussi.pakkanen@canonical.com>
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of version 3 of the GNU Lesser General Public License as published
 * by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "columbus.h"
#include "Word.hh"
#include "Document.hh"
#include "Matcher.hh"
#include "MatchResults.hh"
#include "Corpus.hh"
#include "ErrorValues.hh"
#include "IndexWeights.hh"
#include <stdexcept>
#include <cstdio>

using namespace Columbus;
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

ColWord col_word_new(const char *utf8_word) {
    try {
        Word *w = new Word(utf8_word);
        return reinterpret_cast<ColWord>(w);
    } catch(exception &e) {
        fprintf(stderr, "Error creating Word: %s\n", e.what());
    }
    return nullptr;
}

void col_word_delete(ColWord w) {
    try {
        delete reinterpret_cast<Word*>(w);
    } catch(exception &e) {
        fprintf(stderr, "Error deleting Word: %s\n", e.what());
    }
}

size_t col_word_length(ColWord w) {
    try {
        return reinterpret_cast<Word*>(w)->length();
    } catch(exception &e) {
        fprintf(stderr, "Error getting Word length: %s\n", e.what());
    }
    return 0;
}

void col_word_as_utf8(ColWord w, char *buf, unsigned int bufSize) {
    try {
        reinterpret_cast<Word*>(w)->toUtf8(buf, bufSize);
    } catch(exception &e) {
        fprintf(stderr, "Error converting to Utf-8: %s\n", e.what());
    }
}

ColDocument col_document_new(DocumentID id) {
    try {
        return reinterpret_cast<ColDocument>(new Document(id));
    } catch(exception &e) {
        fprintf(stderr, "Error creating Document: %s\n", e.what());
    }
    return nullptr;
}

void col_document_delete(ColDocument doc) {
    try {
        delete reinterpret_cast<Document*>(doc);
    } catch(exception &e) {
        fprintf(stderr, "Error deleting Document: %s\n", e.what());
    }
}

DocumentID col_document_get_id(ColDocument doc) {
    try {
        return reinterpret_cast<Document*>(doc)->getID();
    } catch(exception &e) {
        fprintf(stderr, "Error getting Document ID %s\n", e.what());
    }
    return INVALID_DOCID;
}

void col_document_add_text(ColDocument doc, ColWord field_name, const char *text_as_utf8) {
    try {
        Document *d = reinterpret_cast<Document*>(doc);
        Word *w = reinterpret_cast<Word*>(field_name);
        d->addText(*w, text_as_utf8);
    } catch(exception &e) {
        fprintf(stderr, "Error adding text: %s\n", e.what());
    }
}

ColMatcher col_matcher_new() {
    try {
        return reinterpret_cast<ColMatcher>(new Matcher());
    } catch(exception &e) {
        fprintf(stderr, "Error creating Matcher: %s\n", e.what());
    }
    return nullptr;
}

void col_matcher_delete(ColMatcher m) {
    try {
        delete reinterpret_cast<Matcher*>(m);
    } catch(exception &e) {
        fprintf(stderr, "Error deleting Matcher: %s\n", e.what());
    }
}

void col_matcher_index(ColMatcher m, ColCorpus c) {
    try {
        Matcher *matcher = reinterpret_cast<Matcher*>(m);
        Corpus *corp = reinterpret_cast<Corpus*>(c);
        matcher->index(*corp);
    } catch(exception &e) {
        fprintf(stderr, "Exception when indexing: %s\n", e.what());
    }
}

ColMatchResults col_matcher_match(ColMatcher m, const char *query_as_utf8) {
    try {
        Matcher *matcher = reinterpret_cast<Matcher*>(m);
        MatchResults *results =
                new MatchResults(matcher->match(query_as_utf8));
        return reinterpret_cast<ColMatchResults>(results);
    } catch(exception &e) {
        fprintf(stderr, "Exception when matching: %s\n", e.what());
        return nullptr;
    }
}

ColErrorValues col_matcher_get_error_values(ColMatcher m) {
    try {
        Matcher *matcher = reinterpret_cast<Matcher*>(m);
        return reinterpret_cast<ColErrorValues>(&matcher->getErrorValues());
    } catch(exception &e) {
        fprintf(stderr, "Error getting ErrorValues: %s\n", e.what());
    }
    return nullptr;
}

ColIndexWeights col_matcher_get_index_weights(ColMatcher m) {
    try {
        Matcher *matcher = reinterpret_cast<Matcher*>(m);
        return reinterpret_cast<ColIndexWeights>(&matcher->getIndexWeights());
    } catch(exception &e) {
        fprintf(stderr, "Error getting IndexWeights: %s\n", e.what());
    }
    return nullptr;
}

ColMatchResults col_match_results_new() {
    try {
        return reinterpret_cast<ColMatchResults>(new MatchResults());
    } catch(exception &e) {
        fprintf(stderr, "Error creating MatchResults: %s\n", e.what());
    }
    return nullptr;
}
void col_match_results_delete(ColMatchResults mr) {
    try {
        delete reinterpret_cast<MatchResults*>(mr);
    } catch(exception &e) {
        fprintf(stderr, "Error deleting MatchResults: %s\n", e.what());
    }
}

size_t col_match_results_size(ColMatchResults mr) {
    try {
        return reinterpret_cast<MatchResults*>(mr)->size();
    } catch(exception &e) {
        fprintf(stderr, "Error getting match size: %s\n", e.what());
    }
    return 0;
}

DocumentID col_match_results_get_id(ColMatchResults mr, size_t i) {
    try {
        MatchResults *results = reinterpret_cast<MatchResults*>(mr);
        return results->getDocumentID(i);
    } catch(exception &e) {
        fprintf(stderr, "Exception when getting result document ID: %s\n", e.what());
    }
    return INVALID_DOCID;
}

double col_match_results_get_relevancy(ColMatchResults mr, size_t i) {
    try {
        MatchResults *results = reinterpret_cast<MatchResults*>(mr);
        return results->getDocumentID(i);
    } catch(exception &e) {
        fprintf(stderr, "Exception when getting result document ID: %s\n", e.what());
    }
    return -1.0;
}

ColCorpus col_corpus_new() {
    try {
        return reinterpret_cast<ColCorpus>(new Corpus());
    } catch(exception &e) {
        fprintf(stderr, "Error creating Corpus: %s\n", e.what());
    }
    return nullptr;
}


void col_corpus_delete(ColCorpus c) {
    try {
        delete reinterpret_cast<Corpus*>(c);
    } catch(exception &e) {
        fprintf(stderr, "Error deleting Corpus: %s\n", e.what());
    }
}

void col_corpus_add_document(ColCorpus c, ColDocument d) {
    try {
        Corpus *corp = reinterpret_cast<Corpus*>(c);
        Document *doc = reinterpret_cast<Document*>(d);
        corp->addDocument(*doc);
    } catch(exception &e) {
        fprintf(stderr, "Error adding document: %s\n", e.what());
    }
}

void col_error_values_add_standard_errors(ColErrorValues ev) {
    try {
        ErrorValues *results = reinterpret_cast<ErrorValues*>(ev);
        results->addStandardErrors();
    } catch(exception &e) {
        fprintf(stderr, "Error adding standard errors: %s\n", e.what());
    }
}

void col_error_values_set_substring_mode(ColErrorValues ev) {
    try {
        ErrorValues *results = reinterpret_cast<ErrorValues*>(ev);
        results->setSubstringMode();
    } catch(exception &e) {
        fprintf(stderr, "Error setting substring mode: %s\n", e.what());
    }
}

void col_index_weights_set_weight(ColIndexWeights weights, const ColWord field, const double new_weight) {
    try {
        IndexWeights *cweight = reinterpret_cast<IndexWeights*>(weights);
        Word *w = reinterpret_cast<Word*>(field);
        cweight->setWeight(*w, new_weight);
    } catch(exception &e) {
        fprintf(stderr, "Error setting weight: %s\n", e.what());
    }
}

double col_index_weights_get_weight(ColIndexWeights weights, const ColWord field) {
    try {
        IndexWeights *cweight = reinterpret_cast<IndexWeights*>(weights);
        Word *w = reinterpret_cast<Word*>(field);
        return cweight->getWeight(*w);
    } catch(exception &e) {
        fprintf(stderr, "Error getting weight: %s\n", e.what());
    }
    return 1.0;
}


#ifdef __cplusplus
}
#endif