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.0"
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
82 i = 0
83 for j, x in enumerate(t1):
84 try:
85 if x == t2[j]:
86 i += 1
87 except IndexError:
88 break
89 return i
90
91
92 -def ld_match(candidates, needle, tolerance):
93 needle_length = len(needle)
94 matches = [(Levenshtein.distance(needle, c), c) for c in candidates if
95 abs(needle_length - len(c)) <= tolerance]
96 score, user_agent = min(matches)
97 if score > tolerance:
98 user_agent = u""
99 return user_agent
100