string jdmExiste(string s){ string url="http://www.jeuxdemots.org/autocompletion/autocompletion.php?completionarg=proposition&proposition="; url+=s; url = transformer(&url," ","%20"); int i=0; string result; if(lireMot(&i, &s, "/")) result = ouvrirPageForce(url); else result = ouvrirPage(url); i=0; string candidat; lireMot(&i, &result, "[\""); string maxs=""; int maxi=10; while(lireMot(&i, &result, &candidat, "\"")){ if( maxi<(poidJDM(candidat)/pow(LevenshteinDistance(s, candidat),3)) ){ maxi=poidJDM(candidat)/pow(LevenshteinDistance(s, candidat),3); maxs=s; } candidat.clear(); lireMot(&i, &result, "\""); } return maxs; }
int LevenshteinDistance(char* s, int len_s, char* t, int len_t) { int cost = 0; if (len_s == 0) return len_t; if (len_t == 0) return len_s; if (s[len_s-1] == t[len_t-1]) cost = 0; else cost = 1; return minimum(LevenshteinDistance(s, len_s - 1, t, len_t ) + 1, LevenshteinDistance(s, len_s , t, len_t - 1) + 1, LevenshteinDistance(s, len_s - 1, t, len_t - 1) + cost); }
char* correct_word(No* a_root, char* a_word){ if(a_root == NULL)//Caso a arvore esteja nula, só ocorre na inicialização return NULL; else { int i; if(a_root->exists!=NULL){ gi_custo_aux = LevenshteinDistance(a_word,gs_palavra); if(gi_custo_aux<gi_custo){ gi_custo = gi_custo_aux; gs_sugestion_word = a_root->exists; } if(gi_custo==1) return gs_sugestion_word; } for(i = 0 ; i<c_alphabet_length; i++)//Continue procurando em cada filho if(a_root->sheet[i]!= NULL) if(correct_word(a_root->sheet[i],append(a_word,'a'+i))!=NULL) return gs_sugestion_word; } return NULL; }
virtual double compare(const QString& s1, const QString& s2) const { double result = 0.0; QString e1 = LevenshteinDistance().toEnglish(s1); QString e2 = LevenshteinDistance().toEnglish(s2); // if there is an exact string match, then PLACES calls it a match. if (s1 == s2) { result = 1.0; } // if the length is less than 5 and there are 0 or 1 differences, then PLACES calls it a match. else if (s1.length() < 5 || s2.length() < 5) { int d = LevenshteinDistance().distance(s1, s2); d = min<int>(LevenshteinDistance().distance(e1, e2), d); if (d <= 1) { result = 1.0; } } // If the length is >= 5 and there is an 80% or greater match, then PLACES calls it a match. else { double s = LevenshteinDistance().score(s1, s2); s = max(LevenshteinDistance().score(e1, e2), s); if (s >= 0.8) { result = 1.0; } } return result; }
wxString GetBestMatch(const wxArrayString& a, const wxString& s, double* distance ) { const unsigned int count = a.GetCount(); double minDistance = 1.0; int minDistanceIndex = -1; for (unsigned int i = 0; i < count; ++i) { const double distance_ = LevenshteinDistance(a[i], s); if (distance_ < minDistance) { minDistance = distance_; minDistanceIndex = i; } } if (distance != NULL) *distance = minDistance; if (minDistanceIndex == -1) return wxEmptyString; return a[minDistanceIndex]; }
static PyObject * astrcmp(PyObject *self, PyObject *args) { PyObject *s1, *s2; float d; const Py_UNICODE *us1, *us2; int len1, len2; PyThreadState *_save; if (!PyArg_ParseTuple(args, "UU", &s1, &s2)) return NULL; us1 = PyUnicode_AS_UNICODE(s1); us2 = PyUnicode_AS_UNICODE(s2); len1 = PyUnicode_GetSize(s1); len2 = PyUnicode_GetSize(s2); Py_UNBLOCK_THREADS d = LevenshteinDistance(us1, len1, us2, len2); Py_BLOCK_THREADS return Py_BuildValue("f", d); }
void Layer7::LoadSlot(QNetworkReply *reply) { switch (op) { case lsoStart: emit UpdateSignal("Loading started..."); Logger::ins.Header() << "Loading registry data from " << http.Url() << "data/ started...\r\n"; http.Propfind(http.Url() + "data/"); op = lsoListDomains; break; case lsoListDomains: if (reply->error() == QNetworkReply::NoError) { QString replyString(reply->readAll()); sl::log << "lsoListDomains: " << SQ(replyString) << sl::end; Propfinds elements = http.ParsePropfindReply(replyString); for (Propfinds::iterator e = elements.begin(); e != elements.end(); ++e) if (e->href != http.Url()) { domains.push_back(GtDomain(SQ(e->href), SQ(e->name))); Logger::ins.Info() << "Service domain: " << e->name << "\r\n"; emit UpdateSignal("Service Domain: " + e->name); } if (SelectFirstDomain()) { loaded = true; // check if all domains match domains from the db for (unsigned int d = 0; d < domains.size(); ++d) if (!sdoms.contains(QS(domains[d].name))) { loaded = false; Logger::ins.Error() << "Repository does not contain service domain named " << QS(domains[d].name) << "\r\n"; emit DoneSignal("Repository does not contain service domain named " + QS(domains[d].name)); } if (loaded) { op = lsoListServices; http.Propfind(QS(SelectedDomain().href) + "web/"); } } } else emit UpdateSignal(reply->errorString()); break; case lsoListServices: if (reply->error() == QNetworkReply::NoError) { QString replyString(reply->readAll()); sl::log << "lsoListServices: " << SQ(replyString) << sl::end; Propfinds elements = http.ParsePropfindReply(replyString); for (Propfinds::iterator e = elements.begin(); e != elements.end(); ++e) if (e->href != http.Url() && e->name != "OLD" && e->name != "REMOVED") { endpoints.push_back(GtEndpoint(item++ * 100 + REPO_TB_IDX, SelectedDomain().name, SQ(e->href), SQ(e->name))); Logger::ins.Info() << "Service: " << e->name << "\r\n"; emit UpdateSignal("Service: " + e->name); } } else emit UpdateSignal(reply->errorString()); if (SelectNextDomain()) http.Propfind(QS(SelectedDomain().href) + "web/"); else if (SelectFirstEndpoint()) { Logger::ins.Info() << "Parsing index files...\r\n"; emit UpdateSignal("Parsing index files..."); op = lsoGetIndexXml; Logger::ins.Info() << "Parsing " << QS(SelectedEndpoint().urlRegistryFolder) << "index.xml...\r\n"; http.Get(QS(SelectedEndpoint().urlRegistryFolder) + "index.xml"); } break; case lsoGetIndexXml: if (reply->error() == QNetworkReply::NoError) { QString replyString(reply->readAll()); sl::log << "lsoGetIndexXml: " << SQ(replyString) << sl::end; QDomDocument doc; doc.setContent(replyString); for (QDomElement r = doc.firstChildElement(); !r.isNull(); r = r.nextSiblingElement()) { if (r.nodeName() == "service") { for (QDomElement e = r.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) { if (e.nodeName() == "files") { for (QDomElement f = e.firstChildElement(); !f.isNull(); f = f.nextSiblingElement()) { QDomAttr r = f.attributeNode("root"), t = f.attributeNode("type"); if (!r.isNull() && r.nodeValue() == "true" && !t.isNull() && t.value() == "WSDL") SelectedEndpoint().urlWSDL = SQ(f.text()); } } else if (e.nodeName() == "name") SelectedEndpoint().intGivenName = SQ(e.text()); else if (e.nodeName() == "version") SelectedEndpoint().intGivenVersion = SQ(e.text()); else if (e.nodeName() == "policyVersion") SelectedEndpoint().policyVersion = SQ(e.text()); else if (e.nodeName() == "enabled") SelectedEndpoint().enabled = SQ(e.text()); else if (e.nodeName() == "soap") SelectedEndpoint().soap = SQ(e.text()); else if (e.nodeName() == "resolutionPath") SelectedEndpoint().resolutionPath = SQ(e.text()); else if (e.nodeName() == "id") SelectedEndpoint().intGivenId = SQ(e.text()); else if (e.nodeName() == "protectedEndpoint") SelectedEndpoint().protectedEndpoint = SQ(e.text()); } break; } } if (SelectedEndpoint().urlWSDL == "") { Logger::ins.Warning() << "WSDL file url not found for endpoint '" << QS(SelectedEndpoint().intGivenName) << "'\r\n"; emit UpdateSignal("WSDL file url not found for endpoint '" + QS(SelectedEndpoint().intGivenName) + "'"); SelectedEndpoint().serviceName = SelectedEndpoint().intGivenName; } if (SelectedEndpoint().protectedEndpoint == "") { Logger::ins.Warning() << "Ignoring non-protected endpoint '" << QS(SelectedEndpoint().intGivenName) << "'\r\n"; emit UpdateSignal("Ignoring non-protected endpoint '" + QS(SelectedEndpoint().intGivenName) + "'"); RemoveSelectedEndpoint(); } } else { Logger::ins.Error() << reply->errorString() << "\r\n"; emit UpdateSignal(reply->errorString()); } if (SelectNextEndpoint()) { Logger::ins.Info() << "Parsing " << QS(SelectedEndpoint().urlRegistryFolder) << "index.xml...\r\n"; http.Get(QS(SelectedEndpoint().urlRegistryFolder) + "index.xml"); } else if (SelectFirstEndpoint()) { Logger::ins.Info() << "Parsing WSDL files...\r\n"; emit UpdateSignal("Parsing WSDL files..."); op = lsoGetWsdl; Logger::ins.Info() << "Parsing " << QS(SelectedEndpoint().urlRegistryFolder + SelectedEndpoint().urlWSDL) << "\r\n"; http.Get(QS(SelectedEndpoint().urlRegistryFolder + SelectedEndpoint().urlWSDL)); } break; case lsoGetWsdl: if (reply->error() == QNetworkReply::NoError) { QString replyString(reply->readAll()); sl::log << "lsoGetWsdl: " << SQ(replyString) << sl::end; QDomDocument doc; doc.setContent(replyString); for (QDomElement r = doc.firstChildElement(); !r.isNull(); r = r.nextSiblingElement()) { if (r.nodeName() == "definitions" || r.nodeName() == "wsdl:definitions") { QDomAttr n = r.attributeNode("targetNamespace"); if (!n.isNull()) SelectedEndpoint().targetNamespace = SQ(n.nodeValue()); n = r.attributeNode("name"); if (!n.isNull()) SelectedEndpoint().definitionName = SQ(n.nodeValue()); // find all service names std::vector<std::string> serviceNames; for (QDomElement e = r.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) { if (e.nodeName() == "service" || e.nodeName() == "wsdl:service") { QDomAttr a = e.attributeNode("name"); if (!a.isNull()) serviceNames.push_back(SQ(a.nodeValue())); } } // assign service name if (serviceNames.size() == 1) SelectedEndpoint().serviceName = serviceNames[0]; else if (serviceNames.size() > 1) { // search service node name with best fit to protected endpoint string std::map<int, std::string> m; for (std::vector<std::string>::iterator s = serviceNames.begin(); s != serviceNames.end(); ++s) m.insert(std::pair<int, std::string>(LevenshteinDistance(SelectedEndpoint().protectedEndpoint, *s, true), *s)); SelectedEndpoint().serviceName = m.begin()->second; } else { Logger::ins.Warning() << "Service name not found for endpoint '" << QS(SelectedEndpoint().intGivenName) + "'\r\n"; emit UpdateSignal("Service name not found for endpoint '" + QS(SelectedEndpoint().intGivenName) + "'"); SelectedEndpoint().serviceName = SelectedEndpoint().intGivenName; } break; } } } else { Logger::ins.Error() << reply->errorString() << "\r\n"; emit UpdateSignal(reply->errorString()); } if (SelectNextEndpoint()) { Logger::ins.Info() << "Parsing " << QS(SelectedEndpoint().urlRegistryFolder + SelectedEndpoint().urlWSDL) << "\r\n"; http.Get(QS(SelectedEndpoint().urlRegistryFolder + SelectedEndpoint().urlWSDL)); } else { Logger::ins.Info() << "Finished loading registry.\r\n"; emit DoneSignal("Finished loading."); } break; default: ; } }
char * compareWords(char *word){ FILE *words; long elapsed_seconds; char filename[20] = "words2.txt"; char line[200]; int newLine = 0; int space = 0; int k = 0; if(word[0] == '\n') return word; while(k <= 200){ if(word[k] == 0) break; if(word[k] == 32 ){ word[k] = 0; space = 1; break; } if(word[k] == '\n'){ word[k] = 0; newLine = 1; break; } k++; } for(k = 0;k < 200; k++) line[k] = 0; words = fopen(filename, "r"); while(fgets(line,200,words)){ sscanf (line, "%ld", &elapsed_seconds); k = 0; while(k <= 200){ if(line[k] == 0) break; if(line[k] == 32 || line[k] == '\n') line[k] = 0; k++; } if(strcasecmp(word,line) == 0){ if(space){ strcat(word," "); fclose(words); return word; } fclose(words); return word; } for(k = 0;k < 200; k++) line[k] = 0; } if(word[0] >= 65 && word[0] <= 90){ if(space){ strcat(word," "); fclose(words); return word; } fclose(words); return word; } words = fopen(filename, "r"); while(fgets(line,200,words)){ sscanf (line, "%ld", &elapsed_seconds); k = 0; while(k <= 200){ if(line[k] == 0) break; if(line[k] == 32 || line[k] == '\n') line[k] = 0; k++; } if(strlen(word) == strlen(line) && !(line[0] >= 65 && line[0] <= 90)) if(LevenshteinDistance(word,strlen(word),line,strlen(line)) == 1){ if(space){ strcat(line," "); fclose(words); return line; } } for(k = 0;k < 200; k++) line[k] = 0; } if(space){ strcat(word," "); fclose(words); return word; } return word; }
/* * Input: A uncorrect English word * Return: A list of strings represent 5 words which may replace the input word. */ QStringList QMyHashMap::correctWord(QString wordtofind) { int len=wordtofind.length(); QStringList candidate, best; QHash<QString, QString>::iterator it=dic.begin(); //find all candidate for(; it!=dic.end(); it++) { int l=it.key().length(); if(l>=len-1 && l<=len+1) candidate.append(it.key()); } //for all candidates, calculate their edit distance int max=4, midmax=3, mid=2, midmin=1, min=0; struct dis{int distance;QString word;}minidis[5]={{96,""},{97,""},{98,""},{99,""},{100,""}}; for(int i=0; i<candidate.size(); i++) { if(wordtofind==candidate.at(i)) continue; int curdis=LevenshteinDistance(wordtofind, candidate.at(i)); if(curdis<minidis[min].distance) { minidis[max].distance=curdis; minidis[max].word=candidate.at(i); int tmp=max; max=midmax; midmax=mid; mid=midmin; midmin=min; min=tmp; //qDebug()<<candidate.at(i)<<min<<midmin<<mid<<midmax<<max; } else if(curdis<minidis[midmin].distance) { minidis[max].distance=curdis; minidis[max].word=candidate.at(i); int tmp=max; max=midmax; midmax=mid; mid=midmin; midmin=tmp; //qDebug()<<candidate.at(i)<<min<<midmin<<mid<<midmax<<max; } else if(curdis<minidis[mid].distance) { minidis[max].distance=curdis; minidis[max].word=candidate.at(i); int tmp=max; max=midmax; midmax=mid; mid=tmp; //qDebug()<<candidate.at(i)<<min<<midmin<<mid<<midmax<<max; }else if(curdis<minidis[midmax].distance) { minidis[max].distance=curdis; minidis[max].word=candidate.at(i); int tmp=max; max=midmax; midmax=tmp; //qDebug()<<candidate.at(i)<<min<<midmin<<mid<<midmax<<max; }else if(curdis<minidis[max].distance) { minidis[max].distance=curdis; minidis[max].word=candidate.at(i); //qDebug()<<candidate.at(i)<<min<<midmin<<mid<<midmax<<max; } } // for(int i=0; i<5; i++) // { // best.append(minidis[i].word); // qDebug()<<minidis[i].word; // } // qDebug()<<best; return best; }