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
|
#include <string>
#include <stdexcept>
#include <vector>
#include <iomanip>
#include <stdlib.h>
#include <errno.h>
#include <iconv.h>
#include <simstring/simstring.h>
#include "export.h"
#define UTF16 "UTF-16LE"
#define UTF32 "UTF-32LE"
#ifdef USE_LIBICONV_GNU
#define iconv_open libiconv_open
#define iconv_convert libiconv_convert
#define iconv_close libiconv_close
#endif/*USE_LIBICONV_GNU*/
#ifndef ICONV_CONST
#define ICONV_CONST
#endif/*ICONV_CONST*/
template <class source_type, class destination_type>
bool iconv_convert(iconv_t cd, const source_type& src, destination_type& dst)
{
typedef typename source_type::value_type source_char_type;
typedef typename destination_type::value_type destination_char_type;
const char *inbuf = reinterpret_cast<const char *>(src.c_str());
size_t inbytesleft = sizeof(source_char_type) * src.length();
while (inbytesleft > 0) {
char buffer[1024];
char *p = buffer;
size_t outbytesleft = 1024;
int ret = iconv(cd, (ICONV_CONST char **)&inbuf, &inbytesleft, &p, &outbytesleft);
if (ret == -1 && errno != E2BIG) {
return false;
}
dst.append(
reinterpret_cast<const destination_char_type*>(buffer),
(1024 - outbytesleft) / sizeof(destination_char_type)
);
}
return true;
}
typedef simstring::ngram_generator ngram_generator_type;
typedef simstring::writer_base<std::string, ngram_generator_type> writer_type;
typedef simstring::writer_base<std::wstring, ngram_generator_type> uwriter_type;
typedef simstring::reader reader_type;
writer::writer(const char *filename, int n, bool be, bool unicode)
: m_dbw(NULL), m_gen(NULL), m_unicode(unicode)
{
ngram_generator_type *gen = new ngram_generator_type(n, be);
if (unicode) {
uwriter_type *dbw = new uwriter_type(*gen, filename);
if (dbw->fail()) {
std::string message = dbw->error();
delete dbw;
delete gen;
throw std::invalid_argument(message);
}
m_dbw = dbw;
m_gen = gen;
} else {
writer_type *dbw = new writer_type(*gen, filename);
if (dbw->fail()) {
std::string message = dbw->error();
delete dbw;
delete gen;
throw std::invalid_argument(message);
}
m_dbw = dbw;
m_gen = gen;
}
}
writer::~writer()
{
if (m_unicode) {
uwriter_type* dbw = reinterpret_cast<uwriter_type*>(m_dbw);
ngram_generator_type* gen = reinterpret_cast<ngram_generator_type*>(m_gen);
dbw->close();
if (dbw->fail()) {
std::string message = dbw->error();
delete dbw;
delete gen;
throw std::runtime_error(message);
}
delete dbw;
delete gen;
} else {
writer_type* dbw = reinterpret_cast<writer_type*>(m_dbw);
ngram_generator_type* gen = reinterpret_cast<ngram_generator_type*>(m_gen);
dbw->close();
if (dbw->fail()) {
std::string message = dbw->error();
delete dbw;
delete gen;
throw std::runtime_error(message);
}
delete dbw;
delete gen;
}
}
void writer::insert(const char *string)
{
if (m_unicode) {
uwriter_type* dbw = reinterpret_cast<uwriter_type*>(m_dbw);
std::wstring str;
iconv_t cd = iconv_open("WCHAR_T", "UTF-8");
iconv_convert(cd, std::string(string), str);
iconv_close(cd);
dbw->insert(str);
if (dbw->fail()) {
throw std::runtime_error(dbw->error());
}
} else {
writer_type* dbw = reinterpret_cast<writer_type*>(m_dbw);
dbw->insert(string);
if (dbw->fail()) {
throw std::runtime_error(dbw->error());
}
}
}
void writer::close()
{
if (m_unicode) {
uwriter_type* dbw = reinterpret_cast<uwriter_type*>(m_dbw);
dbw->close();
if (dbw->fail()) {
throw std::runtime_error(dbw->error());
}
} else {
writer_type* dbw = reinterpret_cast<writer_type*>(m_dbw);
dbw->close();
if (dbw->fail()) {
throw std::runtime_error(dbw->error());
}
}
}
reader::reader(const char *filename)
: m_dbr(NULL), measure(cosine), threshold(0.7)
{
reader_type *dbr = new reader_type;
if (!dbr->open(filename)) {
delete dbr;
throw std::invalid_argument("Failed to open the database");
}
m_dbr = dbr;
}
reader::~reader()
{
this->close();
delete reinterpret_cast<reader_type*>(m_dbr);
}
template <class insert_iterator_type>
void retrieve_thru(
reader_type& dbr,
const std::string& query,
int measure,
double threshold,
insert_iterator_type ins
)
{
switch (measure) {
case exact:
dbr.retrieve<simstring::measure::exact>(query, threshold, ins);
break;
case dice:
dbr.retrieve<simstring::measure::dice>(query, threshold, ins);
break;
case cosine:
dbr.retrieve<simstring::measure::cosine>(query, threshold, ins);
break;
case jaccard:
dbr.retrieve<simstring::measure::jaccard>(query, threshold, ins);
break;
case overlap:
dbr.retrieve<simstring::measure::overlap>(query, threshold, ins);
break;
}
}
template <class char_type, class insert_iterator_type>
void retrieve_iconv(
reader_type& dbr,
const std::string& query,
const char *encoding,
int measure,
double threshold,
insert_iterator_type ins
)
{
typedef std::basic_string<char_type> string_type;
typedef std::vector<string_type> strings_type;
// Translate the character encoding of the query string from UTF-8 to the target encoding.
string_type qstr;
iconv_t fwd = iconv_open(encoding, "UTF-8");
iconv_convert(fwd, query, qstr);
iconv_close(fwd);
strings_type xstrs;
switch (measure) {
case exact:
dbr.retrieve<simstring::measure::exact>(qstr, threshold, std::back_inserter(xstrs));
break;
case dice:
dbr.retrieve<simstring::measure::dice>(qstr, threshold, std::back_inserter(xstrs));
break;
case cosine:
dbr.retrieve<simstring::measure::cosine>(qstr, threshold, std::back_inserter(xstrs));
break;
case jaccard:
dbr.retrieve<simstring::measure::jaccard>(qstr, threshold, std::back_inserter(xstrs));
break;
case overlap:
dbr.retrieve<simstring::measure::overlap>(qstr, threshold, std::back_inserter(xstrs));
break;
}
// Translate back the character encoding of retrieved strings into UTF-8.
iconv_t bwd = iconv_open("UTF-8", encoding);
for (typename strings_type::const_iterator it = xstrs.begin();it != xstrs.end();++it) {
std::string dst;
iconv_convert(bwd, *it, dst);
*ins = dst;
}
iconv_close(bwd);
}
std::vector<std::string> reader::retrieve(const char *query)
{
reader_type& dbr = *reinterpret_cast<reader_type*>(m_dbr);
std::vector<std::string> ret;
switch (dbr.char_size()) {
case 1:
retrieve_thru(dbr, query, this->measure, this->threshold, std::back_inserter(ret));
break;
case 2:
retrieve_iconv<uint16_t>(dbr, query, UTF16, this->measure, this->threshold, std::back_inserter(ret));
break;
case 4:
retrieve_iconv<uint32_t>(dbr, query, UTF32, this->measure, this->threshold, std::back_inserter(ret));
break;
}
return ret;
}
void reader::close()
{
reader_type& dbr = *reinterpret_cast<reader_type*>(m_dbr);
dbr.close();
}
|