1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 __doc__ = """
21 This module contains the supporting classes for the Two Step Analysis user agent
22 algorithm that is used as the primary way to match user agents with the Java API
23 for the WURFL.
24
25 A description of the way the following source is intended to work can be found
26 within the source for the original Java API implementation here:
27 http://sourceforge.net/projects/wurfl/files/WURFL Java API/
28
29 The original Java code is GPLd and Copyright (c) 2008-2009 WURFL-Pro srl
30 """
31
32 __author__ = "Armand Lynch <lyncha@users.sourceforge.net>"
33 __copyright__ = "Copyright 2010, Armand Lynch"
34 __license__ = "LGPL"
35 __url__ = "http://celljam.net/"
36 __version__ = "1.0.1"
37
38 import Levenshtein
39
41
42
43
44 match = u""
45 best_match = u""
46 needle_length = len(needle)
47
48 best_distance = -1
49 low = 0
50 high = len(candidates) - 1
51
52
53
54
55
56
57 while (low <= high and best_distance < needle_length):
58 mid = (low + high) / 2
59 mid_candidate = candidates[mid]
60
61 distance = get_ris_distance(needle, mid_candidate)
62
63 if distance > best_distance:
64 best_match = mid_candidate
65 best_distance = distance
66
67
68 if mid_candidate < needle:
69 low = mid + 1
70 elif mid_candidate > needle:
71 high = mid - 1
72 else:
73 break
74
75
76 if best_distance >= tolerance:
77 match = best_match
78
79 return match
80
81
83 i = 0
84 t = min(len(t1), len(t2))
85
86 for j in xrange(0, t):
87 if t1[j] == t2[j]:
88 i += 1
89 else:
90 break
91 return i
92
93
94 -def ld_match(candidates, needle, tolerance):
95 needle_length = len(needle)
96 user_agent = u""
97 matches = [(Levenshtein.distance(needle, c), c) for c in candidates if
98 abs(needle_length - len(c)) <= tolerance]
99 if matches:
100 score, user_agent = min(matches)
101 if score > tolerance:
102 user_agent = u""
103 return user_agent
104