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
|
#include <cstring>
#include "vararray.hpp"
#include "typo_editdist.hpp"
#include "config.hpp"
#include "language.hpp"
#include "file_data_util.hpp"
#include "getdata.hpp"
#include "cache-t.hpp"
#include "asc_ctype.hpp"
// edit_distance is implemented using a straight forward dynamic
// programming algorithm with out any special tricks. Its space
// usage AND running time is tightly asymptotically bounded by
// strlen(a)*strlen(b)
typedef unsigned char uchar;
namespace aspeller {
using namespace std;
short typo_edit_distance(ParmString word0,
ParmString target0,
const TypoEditDistanceInfo & w)
{
int word_size = word0.size() + 1;
int target_size = target0.size() + 1;
const uchar * word = reinterpret_cast<const uchar *>(word0.str());
const uchar * target = reinterpret_cast<const uchar *>(target0.str());
VARARRAY(short, e_d, word_size * target_size);
ShortMatrix e(word_size,target_size, e_d);
e(0,0) = 0;
for (int j = 1; j != target_size; ++j)
e(0,j) = e(0,j-1) + w.missing;
--word;
--target;
short te;
for (int i = 1; i != word_size; ++i) {
e(i,0) = e(i-1,0) + w.extra_dis2;
for (int j = 1; j != target_size; ++j) {
if (word[i] == target[j]) {
e(i,j) = e(i-1,j-1);
} else {
te = e(i,j) = e(i-1,j-1) + w.repl(word[i],target[j]);
if (i != 1) {
te = e(i-1,j ) + w.extra(word[i-1], target[j]);
if (te < e(i,j)) e(i,j) = te;
te = e(i-2,j-1) + w.extra(word[i-1], target[j])
+ w.repl(word[i] , target[j]);
if (te < e(i,j)) e(i,j) = te;
} else {
te = e(i-1,j) + w.extra_dis2;
if (te < e(i,j)) e(i,j) = te;
}
te = e(i,j-1) + w.missing;
if (te < e(i,j)) e(i,j) = te;
//swap
if (i != 1 && j != 1) {
te = e(i-2,j-2) + w.swap
+ w.repl(word[i], target[j-1])
+ w.repl(word[i-1], target[j]);
if (te < e(i,j)) e(i,j) = te;
}
}
}
}
return e(word_size-1,target_size-1);
}
static GlobalCache<TypoEditDistanceInfo> typo_edit_dist_info_cache("keyboard");
PosibErr<void> setup(CachePtr<const TypoEditDistanceInfo> & res,
const Config * c, const Language * l, ParmString kb)
{
PosibErr<TypoEditDistanceInfo *> pe = get_cache_data(&typo_edit_dist_info_cache, c, l, kb);
if (pe.has_err()) return pe;
res.reset(pe.data);
return no_err;
}
struct CharPair {
char d[2];
CharPair(char a, char b) {d[0] = a; d[1] = b;}
};
PosibErr<TypoEditDistanceInfo *>
TypoEditDistanceInfo::get_new(const char * kb, const Config * cfg, const Language * l)
{
FStream in;
String file, dir1, dir2;
fill_data_dir(cfg, dir1, dir2);
find_file(file, dir1, dir2, kb, ".kbd");
RET_ON_ERR(in.open(file.c_str(), "r"));
ConvEC iconv;
RET_ON_ERR(iconv.setup(*cfg, "utf-8", l->charmap(), NormFrom));
Vector<CharPair> data;
char to_stripped[256];
for (int i = 0; i <= 255; ++i)
to_stripped[i] = l->to_stripped(i);
String buf;
DataPair d;
while (getdata_pair(in, d, buf)) {
if (d.key == "key") {
PosibErr<char *> pe(iconv(d.value.str, d.value.size));
if (pe.has_err())
return pe.with_file(file, d.line_num);
char * v = pe.data;
char base = *v;
while (*v) {
to_stripped[(uchar)*v] = base;
++v;
while (asc_isspace(*v)) ++v;
}
} else {
PosibErr<char *> pe(iconv(d.key.str, d.key.size));
if (pe.has_err())
return pe.with_file(file, d.line_num);
char * v = pe.data;
if (strlen(v) != 2)
return make_err(invalid_string, d.key.str).with_file(file, d.line_num);
to_stripped[(uchar)v[0]] = v[0];
to_stripped[(uchar)v[1]] = v[1];
data.push_back(CharPair(v[0], v[1]));
}
}
TypoEditDistanceInfo * w = new TypoEditDistanceInfo();
w->keyboard = kb;
memset(w->to_normalized_, 0, sizeof(w->to_normalized_));
int c = 1;
for (int i = 0; i <= 255; ++i) {
if (l->is_alpha(i)) {
if (w->to_normalized_[(uchar)to_stripped[i]] == 0) {
w->to_normalized_[i] = c;
w->to_normalized_[(uchar)to_stripped[i]] = c;
++c;
} else {
w->to_normalized_[i] = w->to_normalized_[(uchar)to_stripped[i]];
}
}
}
for (int i = 0; i != 256; ++i) {
if (w->to_normalized_[i]==0) w->to_normalized_[i] = c;
}
w->max_normalized = c;
c = w->max_normalized + 1;
int cc = c * c;
w->data = (short *)malloc(cc * 2 * sizeof(short));
w->repl .init(c, c, w->data);
w->extra.init(c, c, w->data + cc);
for (int i = 0; i != c; ++i) {
for (int j = 0; j != c; ++j) {
w->repl (i,j) = w->repl_dis2;
w->extra(i,j) = w->extra_dis2;
}
}
for (unsigned i = 0; i != data.size(); ++i) {
const char * d = data[i].d;
w->repl (w->to_normalized(d[0]),
w->to_normalized(d[1])) = w->repl_dis1;
w->repl (w->to_normalized(d[1]),
w->to_normalized(d[0])) = w->repl_dis1;
w->extra(w->to_normalized(d[0]),
w->to_normalized(d[1])) = w->extra_dis1;
w->extra(w->to_normalized(d[1]),
w->to_normalized(d[0])) = w->extra_dis1;
}
for (int i = 0; i != c; ++i) {
w->repl(i,i) = 0;
w->extra(i,i) = w->extra_dis1;
}
return w;
}
}
|