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) WURFL-Pro srl
30 """
31
32 __author__ = "Armand Lynch <lyncha@users.sourceforge.net>"
33 __copyright__ = "Copyright 2011, Armand Lynch"
34 __license__ = "LGPL"
35 __url__ = "http://celljam.net/"
36 __version__ = "1.2.1"
37
38 import Levenshtein
39
41
42
43
44 match = u""
45 needle_length = len(needle)
46
47 best_distance = -1
48 best_match_index = -1
49 low = 0
50 high = len(candidates) - 1
51
52
53
54
55
56 while (low <= high and best_distance < needle_length):
57 mid = (low + high) / 2
58 mid_candidate = candidates[mid]
59
60 distance = longest_common_prefix(needle, mid_candidate)
61
62 if distance > best_distance:
63 best_match_index = mid
64 best_distance = distance
65
66
67 if mid_candidate < needle:
68 low = mid + 1
69 elif mid_candidate > needle:
70 high = mid - 1
71 else:
72 break
73
74
75 if best_distance >= tolerance:
76 match = first_of_best_matches(needle, candidates, best_match_index,
77 best_distance)
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
95 best_match = candidates[best_match_index]
96 for candidate in reversed(candidates[:best_match_index-1]):
97 if best_distance == longest_common_prefix(needle, candidate):
98 best_match = candidate
99 else:
100 break
101 return best_match
102
103
104 -def ld_match(candidates, needle, tolerance):
105 needle_length = len(needle)
106 user_agent = u""
107 matches = [(Levenshtein.distance(needle, c), c) for c in candidates if
108 abs(needle_length - len(c)) <= tolerance]
109 if matches:
110 score, user_agent = min(matches)
111 if score > tolerance:
112 user_agent = u""
113 return user_agent
114