示例#1
0
void test_levenshtein_distance(){
    char *string_one, *string_two;

    asprintf(&string_one, "impute/recodes");
    asprintf(&string_two, "impute/recdes");

    assert(levenshtein_distance(string_one, string_two) == 1);

    asprintf(&string_one, "input/input table");
    asprintf(&string_two, "niput/input table");

    assert(levenshtein_distance(string_one, string_two) == 2);

    asprintf(&string_one, "database");
    asprintf(&string_two, "database");
    assert(levenshtein_distance(string_one, string_two) == 0); 

    asprintf(&string_one, "mthod");
    asprintf(&string_two, "method");
    assert(levenshtein_distance(string_one, string_two) == 1);

    free(string_one);
    free(string_two);
    printf("Leaving test_levenshtein_distance successfully!\n");
}
示例#2
0
static constvalue *find_automaton(const char *name,int *last,char *closestmatch)
{
  constvalue *ptr;
  int dist,closestdist=INT_MAX;

  assert(last!=NULL);
  *last=0;
  if (closestmatch!=NULL)
    *closestmatch='\0';
  ptr=sc_automaton_tab.next;
  while (ptr!=NULL) {
    if (strcmp(name,ptr->name)==0)
      return ptr;
    if (closestmatch!=NULL && strlen(ptr->name)>0) {
      dist=levenshtein_distance(name,ptr->name);
      if (dist<closestdist && dist<=MAX_EDIT_DIST) {
        strcpy(closestmatch,ptr->name);
        closestdist=dist;
      } /* if */
    } /* if */
    if (ptr->index>*last)
      *last=ptr->index;
    ptr=ptr->next;
  } /* while */
  return NULL;
}
示例#3
0
void RecipeActionsHandler::mergeSimilar()
{
	QList<Q3ListViewItem> items = parentListView->selectedItems();
	if ( items.count() > 1 )
		KMessageBox::sorry( kapp->mainWidget(), i18nc("@info", "Please select only one category."), QString() );
	else if ( items.count() == 1 && items.at(0)->rtti() == 1001 ) {
		CategoryListItem * cat_it = ( CategoryListItem* ) items.at(0);
		QString name = cat_it->categoryName();
		const double max_allowed_distance = 0.60;
		const int length = name.length();
		ElementList categories;
		database->loadCategories( &categories );

		ElementList matches;
		for ( ElementList::const_iterator it = categories.begin(); it != categories.end(); ++it ) {
			#if 0
			if ( levenshtein_distance(name.toLatin1(),(*it).name.toLatin1())/double(qMax(length,(*it).name.length())) >= max_allowed_distance ) {
			#else
			if ( compareStrings(name,(*it).name) >= max_allowed_distance ) {
			#endif
				kDebug()<<(*it).name<<" matches";
				if ( cat_it->categoryId() != (*it).id )
					matches.append(*it);
			}
		}


		for ( ElementList::const_iterator it = categories.begin(); it != categories.end(); ++it ) {
			database->mergeCategories(cat_it->categoryId(),(*it).id);
		}

	}
	else //either nothing was selected or a recipe was selected
示例#4
0
文件: sc5.c 项目: dmschwartz/QuadPawn
static int find_closestsymbol_table(const char *name,const symbol *root,int symboltype,symbol **closestsym)
{
  int dist,closestdist=INT_MAX;
  char symname[2*sNAMEMAX+16];
  symbol *sym=root->next;
  int ident,critdist;

  assert(closestsym!=NULL);
  *closestsym=NULL;
  assert(name!=NULL);
  critdist=strlen(name)/2;  /* for short names, allow only a single edit */
  if (critdist>MAX_EDIT_DIST)
    critdist=MAX_EDIT_DIST;
  while (sym!=NULL) {
    funcdisplayname(symname,sym->name);
    ident=sym->ident;
    if (symboltype==iARRAY && ident==iREFARRAY)
      ident=iARRAY;     /* reference arrays match arrays */
    else if (symboltype==iVARIABLE && (sym->ident==iCONSTEXPR || sym->ident==iREFERENCE || sym->ident==iARRAY || sym->ident==iREFARRAY))
      ident=iVARIABLE;  /* when requesting variables, constants are also ok */
    if (symboltype==ident || (symboltype==iVARIABLE && ident==iFUNCTN)) {
      dist=levenshtein_distance(name,symname);
      if (dist<closestdist && dist<=critdist) {
        *closestsym=sym;
        closestdist=dist;
      } /* if */
    } /* if */
    sym=sym->next;
  } /* while */
  return closestdist;
}
示例#5
0
文件: main.cpp 项目: EFanZh/EFanZh
int main()
{
  const char *a = "hgfdhfgdhgf";
  const char *b = "hgfhgfhgfdh";

  printf("%d\n", levenshtein_distance(a, b));
}
示例#6
0
int benchmark()
{
  int i, j;
  volatile unsigned sum = 0;

  for(i = 0; i < 5; ++i)
    for(j = 0; j < 5; ++j)
      sum += levenshtein_distance(strings[i], strings[j]);

  return sum;

}
示例#7
0
void calculate_results_relevance (alpm_list_t *targets)
{
	for (const alpm_list_t *t = targets; t; t = alpm_list_next (t)) {
		const char *target = t->data;
		for (const alpm_list_t *r = results; r; r = alpm_list_next(r)) {
			const char *result = results_name (r->data);
			const size_t lev_dst = levenshtein_distance (target, result);
			if (lev_dst < ((results_t *) r->data)->rel) {
				((results_t *) r->data)->rel = lev_dst;
			}
		}
	}
}
示例#8
0
void recurse_levenshtein(int idx,int nwords)
{
    int loop,loop2,dist;
    char tmpword[MAX_WORD_LENGTH];
    static int depth,found;
    int locfound = 0;

    if (depth > 3){
	depth--;
	return;
    }

    if (depth == 0)
	found = 0;

    depth++;
    //    printf("depth = %d\n",depth);

    for (loop = 0; loop < nwords; loop++){
	if (word[loop]==NULL)
	    continue;
	dist = levenshtein_distance(word[idx],word[loop],1);
	if (dist == 1){
	    if (depth == 1)
		printf("%s\n",word[idx]);

	    printf("%s\n",word[loop]);
	    free(word[idx]);
	    word[idx] = NULL;
	    found++;
	    locfound = loop;
	    recurse_levenshtein(loop,nwords);
	}
    }
    depth--;
    
    if (locfound){
	    free(word[locfound]);
	    word[locfound] = NULL;
    }

    if (depth == 0){
	if (found == 0){
	    printf("%s\n",word[idx]);
	    free(word[idx]);
	    word[idx] = NULL;
	}
	printf("------------------\n");
    }
}
示例#9
0
static PyObject* jellyfish_levenshtein_distance(PyObject *self, PyObject *args)
{
    const char *s1, *s2;
    int result;

    if (!PyArg_ParseTuple(args, "ss", &s1, &s2)) {
        return NULL;
    }

    result = levenshtein_distance(s1, s2);
    if (result == -1) {
        // levenshtein_distance only returns failure code (-1) on
        // failed malloc
        PyErr_NoMemory();
        return NULL;
    }

    return Py_BuildValue("i", result);
}
示例#10
0
int check_levenshtein_distances(int max_lev_distance){
    int typo_counter=0;
    int min_distance;
    char *closest;
    if (!apop_table_exists("keys")) return 0;
    apop_data *userkeys = apop_query_to_text("select key from keys");
    for (int i=0; i < *userkeys->textsize; i++){
        min_distance = 100;
        for (char **keyptr=ok_keys; strlen(*keyptr); keyptr++){
            int ld = levenshtein_distance(*keyptr, *userkeys->text[i]);

            if (ld < min_distance){
                if(ld == 0) {min_distance=0; break;}
                min_distance=ld;
                closest = *keyptr;    
            }
        }
        Apop_stopif(min_distance > 0 && min_distance <= max_lev_distance, typo_counter++ , 0, 
                            "You wrote %s for one of the keys in your spec file. Did you "
                            "mean to write %s?", *userkeys->text[i], closest);
    }
    return typo_counter;
}
示例#11
0
文件: sc5.c 项目: dmschwartz/QuadPawn
SC_FUNC int error_suggest_list(int number,const char *name,constvalue *list)
{
  assert(name!=NULL);
  assert(list!=NULL);
  if (sc_status==statWRITE) {
    constvalue *closest=NULL;
    if (strlen(name)>0) {
      int dist,closestdist=INT_MAX;
      while (list->next!=NULL) {
        list=list->next;
        dist=levenshtein_distance(list->name,name);
        if (dist<closestdist && dist<=MAX_EDIT_DIST) {
          closest=list;
          closestdist=dist;
        } /* if */
      } /* while */
    } /* if */
    if (closest!=NULL && strcmp(name,closest->name)!=0)
      error(makelong(number,1),name,closest->name);
    else
      error(number,name);
  } /* if */
  return 0;
}
示例#12
0
int main (int argc, char **argv) {
    if (argc < 3) return -1;
    printf("%.1f%%\n", levenshtein_distance(argv[1],argv[2]) * 100);
    return 0;
}
示例#13
0
static VALUE lev_dist(VALUE self, VALUE str1, VALUE str2) {
	return LONG2FIX(levenshtein_distance( str1, str2 ));
}
示例#14
0
int main(int argc, char *argv[])
{
    if (argc == 3)
	printf("\n%s %s : %d\n",argv[1],argv[2],levenshtein_distance(argv[1],argv[2]));
}
示例#15
0
文件: spellcheck.c 项目: pjump/gcc
edit_distance_t
levenshtein_distance (const char *s, const char *t)
{
  return levenshtein_distance (s, strlen (s), t, strlen (t));
}
示例#16
0
/* "/home/abhinav/projects/fb-puzzles/breathalyzer/levenshtein.pyx":4
 *      int levenshtein_distance(char *s,char *t)
 * 
 * def levenshtein(char* s1, char* s2):             # <<<<<<<<<<<<<<
 *     return levenshtein_distance(s1, s2)
 */

static PyObject *__pyx_pf_11levenshtein_levenshtein(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pf_11levenshtein_levenshtein(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  char *__pyx_v_s1;
  char *__pyx_v_s2;
  PyObject *__pyx_r = NULL;
  PyObject *__pyx_t_1 = NULL;
  static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__s1,&__pyx_n_s__s2,0};
  __Pyx_RefNannySetupContext("levenshtein");
  __pyx_self = __pyx_self;
  if (unlikely(__pyx_kwds)) {
    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
    PyObject* values[2] = {0,0};
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
      case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      case  0: break;
      default: goto __pyx_L5_argtuple_error;
    }
    switch (PyTuple_GET_SIZE(__pyx_args)) {
      case  0:
      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__s1);
      if (likely(values[0])) kw_args--;
      else goto __pyx_L5_argtuple_error;
      case  1:
      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__s2);
      if (likely(values[1])) kw_args--;
      else {
        __Pyx_RaiseArgtupleInvalid("levenshtein", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    }
    if (unlikely(kw_args > 0)) {
      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "levenshtein") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    }
    __pyx_v_s1 = __Pyx_PyBytes_AsString(values[0]); if (unlikely((!__pyx_v_s1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    __pyx_v_s2 = __Pyx_PyBytes_AsString(values[1]); if (unlikely((!__pyx_v_s2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
    goto __pyx_L5_argtuple_error;
  } else {
    __pyx_v_s1 = __Pyx_PyBytes_AsString(PyTuple_GET_ITEM(__pyx_args, 0)); if (unlikely((!__pyx_v_s1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
    __pyx_v_s2 = __Pyx_PyBytes_AsString(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((!__pyx_v_s2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("levenshtein", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("levenshtein.levenshtein");
  return NULL;
  __pyx_L4_argument_unpacking_done:;

  /* "/home/abhinav/projects/fb-puzzles/breathalyzer/levenshtein.pyx":5
 * 
 * def levenshtein(char* s1, char* s2):
 *     return levenshtein_distance(s1, s2)             # <<<<<<<<<<<<<<
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyInt_FromLong(levenshtein_distance(__pyx_v_s1, __pyx_v_s2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("levenshtein.levenshtein");
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}