int editDistance( char *X, char *Y, int m, int n ) { if( m == 0 || n == 0 ) //wrong return 0; else { if ( X[m] == X[n]) return editDistance(X,Y,m-1,n-1); else return min(editDistance(X,Y,m,n-1), editDistance(X,Y,m-1,n)) + 1; } }
int ydpDictionary::FuzzyFindWord(const char *wordin) { int i, numFound, best, score, hiscore; if ((wordCount<0) || (words == NULL)) return -1; if (strlen(wordin)==0) return -1; char *word = ConvertFromUtf(wordin); ClearFuzzyWordList(); numFound = 0; best = 0; hiscore = cnf->distance; for (i=0;i<wordCount;i++) if ((score=editDistance(word,words[i])) < cnf->distance) { fuzzyWords[numFound] = new char [strlen(words[i])+1]; strcpy(fuzzyWords[numFound],words[i]); wordPairs[numFound] = i; numFound++; if (score<hiscore) { best = i; hiscore = score; } } fuzzyWordCount = numFound; dictList->NewData(fuzzyWordCount,fuzzyWords,best); return best; }
QString nearestName( const QString& actual, const QSet<QString>& candidates ) { if (actual.isEmpty()) return QString(); int deltaBest = 10000; int numBest = 0; QString best; QSet<QString>::ConstIterator c = candidates.constBegin(); while ( c != candidates.constEnd() ) { if ( (*c)[0] == actual[0] ) { int delta = editDistance( actual, *c ); if ( delta < deltaBest ) { deltaBest = delta; numBest = 1; best = *c; } else if ( delta == deltaBest ) { numBest++; } } ++c; } if ( numBest == 1 && deltaBest <= 2 && actual.length() + best.length() >= 5 ) { return best; } else { return QString(); } }
int main() { while(scanf("%s%s",s1,s2)!=EOF) { editDistance(strlen(s1),strlen(s2)); } }
int main(int argc, char** argv) { FILE* fp; if(argc != 2) { printf("USAGE: DnaAlignment <fileContainingTestVectors>\n"); return 1; } fp = fopen(argv[1], "r"); if(fp == NULL) { printf("Failed to open the input file '%s' for reading!\n", argv[1]); return 2; } while(!feof(fp)) { char currentLine[MAX_LINE]; currentLine[0] = '\0'; fgets(currentLine, MAX_LINE, fp); int len = stringLen(currentLine, MAX_LINE); if(len <= 0) { continue; } int pos = findBar(currentLine, 0, len); printf("%d\n", editDistance(currentLine, pos-1, currentLine+pos+2, len-pos-2)); } fclose(fp); return 0; }
QString nearestName(const QString& actual, const QStringList& candidates) { int deltaBest = 10000; int numBest = 0; int tolerance = gCoreContext->GetNumSetting("MetadataLookupTolerance", 5); QString best; QStringList::ConstIterator i = candidates.begin(); while ( i != candidates.end() ) { if ( (*i)[0] == actual[0] ) { int delta = editDistance( actual, *i ); if ( delta < deltaBest ) { deltaBest = delta; numBest = 1; best = *i; } else if ( delta == deltaBest ) { numBest++; } } ++i; } if ( numBest == 1 && deltaBest <= tolerance && actual.length() + best.length() >= 5 ) return best; else return QString(); }
/* Driver program to test above function */ int main() { char X[] = "SATURDAY"; char Y[] = "SUNDAY"; int m = strlen(X); int n = strlen(Y); std::cout << "Edit Distance is "<< editDistance(X,Y,m,n) << std::endl; }
int main(int argc, char ** argv) { char str1[] = "GGATCGA"; char str2[] = "GAATTCAGTTA"; int len1 = strlen(str1); int len2 = strlen(str2); int ret = editDistance(str1, len1, str2, len2); std::cout<<ret<<std::endl; system("pause"); return 0; }
int main ( int argc, char *argv[] ) { FILE *fh; int linelen; char *line; int i,j; fh = fopen( argv[1], "r" ); if ( fh == NULL ) { fprintf( stderr, "Couldn't open file.\n" ); exit( EXIT_FAILURE ); } char *x = (char *)calloc( 24, sizeof( char ) ); strncpy( x, " Como pode um peixe vivo", 24 ); char *y = (char *)calloc( 16, sizeof( char ) ); strncpy( y, " Love of my life", 16 ); int **c = (int **)calloc( 24, sizeof( int * ) ); for ( i = 0; i <= 23; i++ ) c[i] = (int *)calloc( 16, sizeof( int ) ); operation_t **op = (operation_t **)calloc( 24, sizeof( operation_t * ) ); for ( i = 0; i <= 23; i++ ) op[i] = (operation_t *)calloc( 16, sizeof( operation_t ) ); editDistance( x, y, 23, 15, &c, &op ); for ( i = 0; i <= 23; i++ ) { for ( j = 0; j <= 15; j++ ) { printf( "\t%d", c[i][j] ); } printf( "\n" ); } printf( "\n\n" ); for ( i = 0; i <= 23; i++ ) { for ( j = 0; j <= 15; j++ ) { printf( "\t%c", op[i][j].o ); } printf( "\n" ); } opSequence( op, 23, 15 ); fclose( fh ); return 0; }
void buscarMenor(Parvore node, char *palavra){ int i; int variavel; if(node == NULL) return; for(i = 0; i < TAMANHO; i++){ if(node->flag == 1){ variavel = editDistance(node->palavra, palavra); if(variavel < menor){ menor = variavel; strcpy(menorpalavra, node->palavra); } } else buscarMenor(node->v[i], palavra); } }
int Application::numApproximateStringMatching(string bookName, string toSearch, int &numWords) { string line1, word1; int num = 0; int min = 20; stringstream s1(bookName); while (!s1.eof()) { s1 >> word1; numWords++; num = editDistance(toSearch, word1); if (num < min){ min = num; } } return min; }
int main() { int i, n = 0, m = 0; char str1[MAX], str2[MAX]; printf("enter 2 strings\n"); scanf("%s", str1); scanf("%s", str2); for (i = 0; str1[i] != '\0'; ++i) n++; for (i = 0; str2[i] != '\0'; ++i) m++; printf("%d\n", editDisRecur(str1, str2, n, m)); printf("%d\n", editDistance(str1, str2, n, m)); }
std::string Checker::findMostSimilarWord(const std::string &target) { int minIndex = 0, index = 0; int minDistance = 50000; for(Iter iter = _words_vec.begin(); iter != _words_vec.end(); ++iter) { int tempDistance = editDistance(*iter, target); if(tempDistance < minDistance) { minIndex = index; minDistance = tempDistance; } else if (tempDistance == minDistance) { if(_words_counter[*iter] > _words_counter[_words_vec[minIndex]]) { minIndex = index; minDistance = tempDistance; } } index++; } return _words_vec[minIndex]; }
std::string Checker::findTopKSimilarWords(const std::string &target, int k) { std::priority_queue<WordInfo, std::vector<WordInfo> ,Compare> _pri_queue_words; //编辑距离优先级队列 std::string result; int index = 0; for(Iter iter = _words_vec.begin(); iter != _words_vec.end(); ++iter) { WordInfo info; info.index = index; info.frequence = _words_counter[*iter]; info.editdis = editDistance(*iter, target); _pri_queue_words.push(info); index++; } while(k--) { result = result + _words_vec[_pri_queue_words.top().index]; result += " "; _pri_queue_words.pop(); } return result; }
const std::string& mostRepresentative(const std::vector<std::string>& strs) { int bestScore; int besti; int n = strs.size(); for(int i = 0; i < n; i++) { int curScore = 0; for(int j = 0; j < n; j++) { if(i != j) { int curDistance = editDistance(strs[i], strs[j]); curScore += curDistance * curDistance; } } if(curScore < bestScore || i == 0) { bestScore = curScore; besti = i; } } return strs[besti]; }
int outputSingleGrade(const Verb& v, const std::string& propertyName, const std::string& propertyValue, const std::string& guess) { int distance = editDistance(propertyValue, guess); std::cout << guess << " is "; if(propertyValue == guess) { std::cout << "<font color=\"green\"><strong>correct! +5 points!</strong></font>\n"; return +5; } else if(distance == 1) { std::cout << "<font color=\"#728C00\"><strong>almost correct, +3 points</strong></font> correct answer: " << propertyValue << "\n"; return +3; } else if(distance == 2) { std::cout << "<font color=\"orange\"><strong>right direction, +1 point</strong></font>, correct answer: " << propertyValue << "\n"; return +1; } else { std::cout << "<font color=\"red\">wrong! half of your score is lost!</font> :( right answer: " << propertyValue << "\n"; return -1; } }
void outputFindWord(const std::vector<Verb>& verbs, const std::string& findStr) { int minDistance = 100000000; int verbIndex = -1; for(size_t i=0; i<verbs.size(); ++i) { for(auto it = verbs[i].values.begin(); it != verbs[i].values.end(); ++it) { int dist = editDistance(findStr, it->second); if(dist < minDistance) { minDistance = dist; verbIndex = i; } } } std::cout << "<div style=\"padding-top:25px\">\n"; std::cout << "<table>\n"; Verb v = verbs[verbIndex]; for(auto it = v.values.begin(); it != v.values.end(); ++it) { std::cout << "<tr><th align=\"left\">" << outputProperty(it->first) << "</th><td align=\"center\" width=\"50px\">" << outputPronom(it->first) << "</td><td align=\"center\">" << it->second << "</td></tr>\n"; } std::cout << "</table>\n"; std::cout << "</div>\n"; }
std::vector<std::string> DidYouMean::getMatch(std::string input) { /** Magic numbers */ const int similarityThreshold = 7; const unsigned numMatchesThreshold = 10; typedef std::set<std::pair<int, std::string> > ScoreSet; ScoreSet scores; std::vector<std::string> ret; for (Words::const_iterator it = d_words.begin(); it != d_words.end(); ++it) { std::string s = (*it); if (s == input) { // if input matches AS-IS just return that ret.push_back(s); return ret; } int score; if (s.compare(0, input.size(), input) == 0) { score = 0; } else { score = editDistance(input, s) + 1; } scores.insert(make_pair(score, s)); } int min_score = scores.begin()->first; for (ScoreSet::const_iterator i = scores.begin(); i != scores.end(); ++i) { // add if score is overall not too big, and also not much close to // the score of the best suggestion if (i->first < similarityThreshold && i->first <= min_score + 1) { ret.push_back(i->second); #ifdef DIDYOUMEAN_DEBUG cout << i->second << ": " << i->first << std::endl; #endif } } if (ret.size() > numMatchesThreshold) { ret.resize(numMatchesThreshold); } return ret; }
float numApproximateStringMatching(string filename,string toSearch) { ifstream fich(filename.c_str()); if (!fich) { cout << "Erro a abrir ficheiro de leitura\n"; return 0; } string line1, word1; int num=0, nwords=0; while (!fich.eof()) { getline(fich,line1); stringstream s1(line1); while (!s1.eof()) { s1 >> word1; num += editDistance(toSearch,word1); nwords++; } } fich.close(); float res=(float)num/nwords; return res; }
/* static */ float AutocorrectionThresholdUtils::calcNormalizedScore(const int *before, const int beforeLength, const int *after, const int afterLength, const int score) { if (0 == beforeLength || 0 == afterLength) { return 0.0f; } const int distance = editDistance(before, beforeLength, after, afterLength); int spaceCount = 0; for (int i = 0; i < afterLength; ++i) { if (after[i] == KEYCODE_SPACE) { ++spaceCount; } } if (spaceCount == afterLength) { return 0.0f; } if (score <= 0 || distance >= afterLength) { // normalizedScore must be 0.0f (the minimum value) if the score is less than or equal to 0, // or if the edit distance is larger than or equal to afterLength. return 0.0f; } // add a weight based on edit distance. const float weight = 1.0f - static_cast<float>(distance) / static_cast<float>(afterLength); // TODO: Revise the following logic thoroughly by referring to... if (true /* USE_SUGGEST_INTERFACE_FOR_TYPING */) { return (static_cast<float>(score) / SUGGEST_INTERFACE_OUTPUT_SCALE) * weight; } // ...this logic. const float maxScore = score >= S_INT_MAX ? static_cast<float>(S_INT_MAX) : static_cast<float>(MAX_INITIAL_SCORE) * powf(static_cast<float>(TYPED_LETTER_MULTIPLIER), static_cast<float>(std::min(beforeLength, afterLength - spaceCount))) * static_cast<float>(FULL_WORD_MULTIPLIER); return (static_cast<float>(score) / maxScore) * weight; }
void ydpFuzzySearch::doSearch() { if ((wordCount<0) || (wordList == NULL)) return; listBox->clear(); int i; int distance = distSlider->value(); cfg->fuzzyDistance = distance; cfg->save(); int j=0, best=0, hiscore = distance, score; QCString tekst = cvt->fromUnicode(wordEdit->text()); for (i=0;i<wordCount;i++) if ((score = editDistance(tekst,wordList[i])) < distance) { listBox->insertItem(cvt->toUnicode(wordList[i])); j++; if (score<hiscore) { best = j-1; hiscore = score; } } listBox->setCurrentItem(best); }
void editTest(const std::string& a, const std::string& b) { std::cout << a << " <-> " << b << " : " << editDistance(a, b) << std::endl; }
/// Método que encontra as corrspondências entre linhas que possam ter mudado de posição entre os 2 ficheiros void Diff::findCorrespondentLines() { for (size_t originalTextLineIndex = 0; originalTextLineIndex < originalFileData.size(); ++originalTextLineIndex) { int bestMatch = INT_MAX; size_t bestMatchLineIndex = 0; bool exactMatch = false; for (size_t modifiedTextLineIndex = 0; modifiedTextLineIndex < modifiedFileData.size(); ++modifiedTextLineIndex) { exactMatch = false; EditDistance editDistance(originalFileData[originalTextLineIndex], modifiedFileData[modifiedTextLineIndex]); int editDistanceBetweenLines = editDistance.calcEditDistanceOptimized(); //exact match if (editDistanceBetweenLines == 0) { //se a linha encontrada ainda não foi associada a outra linha no file original if (!modifiedFileLinesPresentedOnOriginal[modifiedTextLineIndex]) { exactMatch = true; modifiedFileLinesPresentedOnOriginal[modifiedTextLineIndex] = true; if (originalTextLineIndex != modifiedTextLineIndex) { originalFileLinesStatus[originalTextLineIndex].setLinesStatus(LINE_MOVED); } else { originalFileLinesStatus[originalTextLineIndex].setLinesStatus(LINE_EQUAL); } originalFileMovedLinesIndexsInModifiedFile[originalTextLineIndex] = (int)modifiedTextLineIndex; modifiedFileMovedLinesIndexsInOriginalFile[modifiedTextLineIndex] = (int)originalTextLineIndex; break; } } //aproximate match if (editDistanceBetweenLines < (PERCENTAGE_TO_CONSIDER_DIFERENTE_LINE * originalFileData[originalTextLineIndex].size())) { if (editDistanceBetweenLines < bestMatch) { bestMatch = editDistanceBetweenLines; bestMatchLineIndex = modifiedTextLineIndex; } } } //se for exact match as actualizações já estão feitas em cima if (exactMatch) continue; //actualizaçao das dados com o melhor match if (bestMatch != INT_MAX) { //encontrou match modifiedFileLinesPresentedOnOriginal[bestMatchLineIndex] = true; if (originalTextLineIndex != bestMatchLineIndex) { originalFileLinesStatus[originalTextLineIndex].setLinesStatus(LINE_MOVED_AND_MODIFIED); } else { originalFileLinesStatus[originalTextLineIndex].setLinesStatus(LINE_MODIFIED); } originalFileMovedLinesIndexsInModifiedFile[originalTextLineIndex] = (int)bestMatchLineIndex; modifiedFileMovedLinesIndexsInOriginalFile[bestMatchLineIndex] = (int)originalTextLineIndex; } else { //não encontrou originalFileLinesStatus[originalTextLineIndex].setLinesStatus(LINE_DELETED); } } }
int main(void) { int m, n; scanf("%d %d",&m,&n); char *x, *y; int *seq; if (m <= 0 || n <= 0){ printf("Tamanho invalido\n"); return 0; } x = (char*)malloc(sizeof(char)*m); y = (char*)malloc(sizeof(char)*n); seq = (int*)malloc(sizeof(int)*(m+n+2)); scanf("%s",x); scanf("%s",y); int cost[SIZE]; cost[CPY] = -1; // STRINGS BATEM cost[REP] = 1; // Caracteres diferentes cost[DEL] = 2; // Por espaco em Y cost[INS] = 2; // Por espaco em X int i; int c = editDistance(x,y,cost,seq); printf("%d\n",-c); for(i=0; seq[i] >= 0; i++){} i--; int j = i; int a; for(a=0; i >=0; i--){ switch(seq[i]){ case CPY: case REP: case DEL: printf("%c", x[a]); a++; break; case INS: printf(" "); break; } } puts(""); i = j; for(a=0; i >=0; i--){ switch(seq[i]){ case CPY: case REP: case INS: printf("%c", y[a]); a++; break; case DEL: printf(" "); break; } } puts(""); i = j; for(a=0; i >=0; i--){ switch(seq[i]){ case CPY: printf("+"); break; case REP: printf("-"); break; case DEL: case INS: printf("*"); break; } } // free(x); //free(y); //free(seq); return 0; }
int main() { std::cout << editDistance("sitting", "kitten") << std::endl; return 0; }