Class: Fuzzy

Inherits:
Object
  • Object
show all
Defined in:
ext/fuzzy_search.c,
ext/bitap_fuzzy_search.c

Class Method Summary collapse

Class Method Details

.equalObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/fuzzy_search.c', line 76

static VALUE
fuzzy_equal (VALUE self, VALUE text, VALUE pattern, VALUE errors_percent)
{
  const char *t = StringValuePtr (text);
  const char *p = StringValuePtr (pattern);
  int errors = (errors_percent * maximun (strlen (t), strlen (p))) / 100;
  int distance = levenshtein_distance (t, p);
  // printf ("Allowed errors: %d - Levenshtein's distance: %d\n", errors, distance);
  if (distance <= errors)
    return INT2NUM (errors - distance);
  return Qfalse;
}

.searchObject



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
# File 'ext/bitap_fuzzy_search.c', line 21

static VALUE
bitap_fuzzy_search (VALUE self, VALUE text, VALUE pattern, VALUE errors_percent)
{
  char *p = downcase2 (StringValuePtr (pattern));
  if (p[0] == '\0') return Qnil;
  char *t = downcase2 (StringValuePtr (text));
  int n = strlen (t);
  int m = strlen (p);
  if (abs (n - m) > 2) return Qnil;
  const char *result = NULL;
  unsigned long *R;
  unsigned long bitmasks[CHAR_MAX + 1];
  int i, d;

  int errors = (FIX2INT (errors_percent) * m) / 100;
  if (errors == 0) errors = 1;

  /* Initialize the bit array R */
  R = malloc ((errors + 1) * sizeof (*R));
  for (i = 0; i <= errors; ++i)
    R[i] = ~1;

  /* Initialize the pattern bitmasks */
  for (i = 0; i <= CHAR_MAX; ++i)
    bitmasks[i] = ~0;
  for (i = 0; i < m; ++i)
    bitmasks[p[i]] &= ~(1UL << i);

  for (i = 0; t[i] != '\0'; ++i)
    {
      /* Update the bit arrays */
      unsigned long old_Rd1 = R[0];

      R[0] |= bitmasks[t[i]];
      R[0] <<= 1;

      for (d = 1; d <= errors; ++d)
        {
          unsigned long tmp = R[d];
          /* Substitution is all we care about */
          R[d] = (old_Rd1 & (R[d] | bitmasks[t[i]])) << 1;
          old_Rd1 = tmp;
        }

    if (0 == (R[errors] & (1UL << m)) && (i - m + 1) == 0)
      {
        result = (t + i - m) + 1;
        break;
      }
    }

  free (R);
  free (p);
  free (t);

  if (result)
    return rb_str_new2 (result);
  return Qnil;
}