-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliblen.py
66 lines (57 loc) · 1.91 KB
/
liblen.py
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
61
62
63
64
65
66
import xlrd
import operator
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
'''
lib - dic of releases [created by calling parse_stack]
possible search categories include: (just strings for now)
artst name
album title
label
genre
addedby
donatedby
keyword - value to be searched
datasetSize - amount of items to be returned
intensity - int from 0 to 100 of how narrow a search
'''
def search(lib, categories, keyword, datasetSize, intensity):
hits = []
hitRatios = []
releases = []
releases = lib
for release in releases:
for category in categories:
ratio = fuzz.token_sort_ratio(release[category], keyword)
if ratio >= intensity:
hits.append(release)
hitRatios.append(ratio)
hits = [k for k, v in sorted(zip(hits, hitRatios), key=operator.itemgetter(1))]
hits.reverse()
return hits[:datasetSize]
def parse_stack(path):
releases = []
book = xlrd.open_workbook(path)
for s in range(book.nsheets):
sheet = book.sheet_by_index(s)
# skip first row since it has the headings
for r in range(1, sheet.nrows):
row = sheet.row(r)
year = row[3].value
if type(year) == float:
year = int(year)
elif type(year) != int:
year = None
releases.append({
'artist': str(row[0].value).strip(),
'title': str(row[1].value).strip(),
'label': str(row[2].value).strip(),
'year': year,
'genres': [genre.strip() for genre in
str(row[4].value).split('/')],
'stack': str(row[5].value).strip(),
'missing': str(row[7].value).strip() != '',
'addedby': str(row[8].value).strip(),
'donatedby': str(row[9].value).strip()
})
return releases