-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
62 lines (49 loc) · 2.54 KB
/
Copy path__init__.py
File metadata and controls
62 lines (49 loc) · 2.54 KB
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
59
60
from operator import itemgetter
from .matcher import *
class Thing:
def __init__(self, search_string):
self.search_string = search_string
def same(self, record_string, result_ranking = 1):
"""
For checking the match confidence of a single record.
However, samething is more useful when comparing the
confidence scores of different records matching, as
opposed to returning a single confidence score in
isolation.
:param record_string:
:param result_ranking:
:return:
"""
return match_confidence(self.search_string, record_string, record_ranking= result_ranking)
def most_same(self, record_string_list, ordered_by_popularity=False):
"""
If only interesting in the best matching string, this could easily be used like so:
best_match = list1[thing.most_same(list1)] # (with thing already declared)
:param record_string_list: list of possible matching strings
:param ordered_by_popularity: determines whether the input list has already been ordered by popularity
:return: index of string the most same string
"""
result_list_ordered = self.most_same_listed(record_string_list, ordered_by_popularity)
most_same_index = result_list_ordered[0]['index']
return most_same_index
def most_same_listed(self, record_string_list, ordered_by_popularity=False):
"""
:param record_string_list: List of possible matching strings.
:param ordered_by_popularity: specifies whether the input list has already been ordered by popularity.
This could be the situation when searching a site like gsmarena: when searching "S8", it would list
"Samsung Galaxy S8" before "Blackview S8". If this value is true, the first strings in record_string_list
is given a small preference in matching confidence.
:return: List of match dictionaries ordered by confidence scores, descending. Each match dictionary
contains the index of the match, the string (labelled "name") and the confidence score.
"""
return_list = []
for i, record_string in enumerate(record_string_list):
record_ranking = (i + 1) if ordered_by_popularity else 0
same = self.same(record_string, record_ranking)
return_list.append({
'index': i,
'name': record_string,
'confidence': same
})
return_list.sort(key=itemgetter('confidence'), reverse=True)
return return_list