make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / levenshtein-distance
blob5bcf5c0c96c4f983dbc8afbeb8f610e44da8ee40
1 #!/usr/bin/env python2.7
3 """
4 =pod
6 =head1 NAME
8 levenshtein-distance - Calculate the Levenshtein distance of given strings
10 jaro-metric - Calculate the Jaro metric of given strings
12 jaro-winkler-metric - Calculate the Jaro-Winkler metric of given strings
14 =cut
16 """
19 import Levenshtein
20 import sys
21 import os
23 try:
24         str1 = sys.argv[1]
25 except IndexError:
26         raise Exception("""
27 Usage:
28   levenshtein-distance <string-1> [<string-2>]
29   jaro-metric <string-1> [<string-2>]
30   jaro-winkler-metric <string-1> [<string-2>]
31 Environment:
32   JARO_WINKLER_PREFIX_WEIGHT
33 Reads strings to compare from stdin unless string-2 is given.
34 Prints the resulting metric, or the <metric> TAB <string-2> if read from stdin.
35 """)
38 if len(sys.argv) > 2:
39         str2 = sys.argv[2]
40         read_stdin = False
41 else:
42         str2 = None
43         read_stdin = True
45 jaro_winkler_prefix_weight = os.environ.get('JARO_WINKLER_PREFIX_WEIGHT')
46 if jaro_winkler_prefix_weight is not None: jaro_winkler_prefix_weight = float(jaro_winkler_prefix_weight)
49 if 'jaro' in sys.argv[0]:
50         if 'winkler' in sys.argv[0]:
51                 method = 'jaro_winkler'
52         else:
53                 method = 'jaro'
54 else:
55         method = 'distance'
58 if read_stdin:
59         while True:
60                 str2 = sys.stdin.readline()
61                 if str2 == '': break
62                 str2 = str2.rstrip('\r\n')
63                 
64                 args = [str1, str2]
65                 if jaro_winkler_prefix_weight is not None:
66                         args.append(jaro_winkler_prefix_weight)
67                 
68                 print "%s\t%s" % (str(getattr(Levenshtein, method)(*args)), str2)
69 else:
70         args = [str1, str2]
71         if jaro_winkler_prefix_weight is not None:
72                 args.append(jaro_winkler_prefix_weight)
73         print getattr(Levenshtein, method)(*args)