int editDistance(const std::string& sstr,const std::string& tstr) { int n = sstr.length(); int m = tstr.length(); if(0 == n) return m; if(0 == m) return n; typedef std::vector<std::vector<int> > Tmatrix; Tmatrix matrix(n+1); //initial the matrix (n+1)*(m+1) for(int i = 0; i <= n; ++i) matrix[i].resize(m+1); for(int i = 1; i <= n; ++i) matrix[i][0] = i; for(int i = 1; i <= m; ++i) matrix[0][i] = i; //caculate for(int i = 1; i <= n; ++i) { const char si = sstr[i-1]; int cost; //penalty for(int j = 1; j <= m; ++j) { const char tj = tstr[j-1]; if(si == tj) { cost = 0; } else { cost = 1; } const int above = matrix[i-1][j] + 1; //above (i,j) const int left = matrix[i][j-1] + 1; //lesf (i.j) const int diag = matrix[i-1][j-1] + cost; //diagonal elements matrix[i][j] = std::min(above,std::min(left,diag)); } } if(isEnglish(sstr) && isEnglish(tstr)) return matrix[n][m]; else return matrix[n][m] / 3; }
int OVIMRomanNewContext::keyEvent( OVKeyCode* k, OVBuffer* b, OVCandidate* i, OVService* s) { if(!candi.isEmpty()) { if(k->code()==ovkEsc) return closeCandidateWindow(i); if (k->code()==ovkLeft || k->code()==ovkUp) { if(pagenumber > 0) pagenumber--; else pagenumber = pagetotal; return showcandi(i); } if (k->code()==ovkRight || k->code()==ovkDown) { if(pagenumber == pagetotal) pagenumber = 0; else pagenumber++; return showcandi(i); } if(k->code()==ovkTab){ if(!candi.item(temp+pagenumber*10)) return 0; b->clear() ->append(keyseq.buf) ->append(candi.item(temp+pagenumber*10) + keyseq.len) ->update(); if(temp++ > 8) temp = 0; if(temp+pagenumber*10 > candi.count()) temp = 0; return 1; } if(is_selkey(k->code())) { murmur("SelectKey Pressed: %c",k->code()); int n = (k->code() - '1' + 10) % 10; if(n+pagenumber*10 >= candi.count()) return 0; const char* correctedWord = candi.item(n+pagenumber*10); b->clear()->append(correctedWord)->append(" ")->send(); if (i->onScreen()) i->hide(); keyseq.clear(); return closeCandidateWindow(i); } if (k->code()==ovkSpace || k->code()==ovkReturn || is_punc(k->code())) { if (!(strlen(keyseq.buf))) return 0; // empty buffer, do nothing b->append(" ")->send(); keyseq.clear(); return closeCandidateWindow(i); } if(!b->isEmpty() && isprint(k->code())) { b->append(" ")->send(); keyseq.clear(); keyseq.add(k->code()); b->clear()->append(keyseq.buf)->update(); return closeCandidateWindow(i); } } if(k->code() == ovkEsc) { b->clear()->update(); clear(); return 1; } if (k->code() == ovkReturn) { if(!(strlen(keyseq.buf))) return 0; if (keyseq.buf) { if(!(strlen(keyseq.buf))) return 0; keyseq.clear(); //keyseq.add(k->code()); b->append(keyseq.buf)->send(); keyseq.clear(); return 1; } } if (k->code()==ovkSpace || is_punc(k->code())) { if (!(strlen(keyseq.buf))) return 0; // empty buffer, do nothing if(keyseq.buf) { pagenumber = 0; if(!isEnglish(keyseq.buf) && spellCheckerByHunspell(keyseq.buf)) { showcandi(i); return 1; } if (!(strlen(keyseq.buf))) return 0; keyseq.clear(); keyseq.add(k->code()); b->append(keyseq.buf)->send(); keyseq.clear(); return closeCandidateWindow(i); } else { b->send(); return 0; } } if (k->code()==ovkDelete || k->code()==ovkBackspace) { if(!strlen(keyseq.buf)) { closeCandidateWindow(i); return 0;} keyseq.remove(); if(keyseq.len && i->onScreen()) { pagenumber = 0; if(!isEnglish(keyseq.buf) && spellCheckerByHunspell(keyseq.buf)) { showcandi(i); return 1; } else { closeCandidateWindow(i); } } else { closeCandidateWindow(i); } b->clear()->append(keyseq.buf)->update(); return 1; } if(k->code()==ovkTab){ if(keyseq.buf) { pagenumber = 0; if(!isEnglish(keyseq.buf) && spellCheckerByHunspell(keyseq.buf)) { showcandi(i); return 1; } else { closeCandidateWindow(i); } } else { return 0; } } if (!isprint(k->code()) || k->isFunctionKey()) { closeCandidateWindow(i); return 0; } if(strlen(keyseq.buf) >= ebMaxKeySeq) return 1; if (isprint(k->code())){ char s[2]; sprintf(s, "%c", k->code()); keyseq.add(k->code()); if(keyseq.buf && i->onScreen()) { pagenumber = 0; temp = 0; if(!isEnglish(keyseq.buf) && spellCheckerByHunspell(keyseq.buf)) { showcandi(i); return 1; } else { closeCandidateWindow(i); } } b->clear()->append(keyseq.buf)->update(); return 1; } return 0; }