File: fuzzy.awk

package info (click to toggle)
zplug 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: sh: 1,504; awk: 235; makefile: 26
file content (58 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
{
    # calculates the degree of similarity
    if ( (1 - leven_dist($NF, search_string) / (length($NF) + length(search_string))) * 100 >= 70 ) {
        # When the degree of similarity of search_string is greater than or equal to 70%,
        # to display the candidate path
        print $0
    }
}

# leven_dist returns the Levenshtein distance two text string
function leven_dist(a, b) {
    lena = length(a);
    lenb = length(b);

    if (lena == 0) {
        return lenb;
    }
    if (lenb == 0) {
        return lena;
    }

    for (row = 1; row <= lena; row++) {
        m[row,0] = row
    }
    for (col = 1; col <= lenb; col++) {
        m[0,col] = col
    }

    for (row = 1; row <= lena; row++) {
        ai = substr(a, row, 1)
        for (col = 1; col <= lenb; col++) {
            bi = substr(b, col, 1)
            if (ai == bi) {
                cost = 0
            } else {
                cost = 1
            }
            m[row,col] = min(m[row-1,col]+1, m[row,col-1]+1, m[row-1,col-1]+cost)
        }
    }

    return m[lena,lenb]
}

# min returns the smaller of x, y or z
function min(a, b, c) {
    result = a

    if (b < result) {
        result = b
    }

    if (c < result) {
        result = c
    }

    return result
}