-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathquery_lyrics.py
72 lines (58 loc) · 1.69 KB
/
query_lyrics.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
67
68
69
70
71
72
# -*- coding: utf-8 -*-
import csv
import json
import sys
# retrieve input
if len(sys.argv) < 2:
print ("Usage: "+sys.argv[0]+" <body part> <artist>")
sys.exit(1)
body_part = sys.argv[1]
artist = sys.argv[2]
# files
LYRICS_FILE = "data/lyrics.json"
WORDS_FILE = "data/words.csv"
songs = []
words = []
song_matches = []
# Read words
with open(WORDS_FILE, 'rb') as f:
r = csv.DictReader(f)
for row in r:
words.append(row)
# Read songs/lyrics
with open(LYRICS_FILE) as f:
songs = json.load(f)
# Filter songs for this artist
songs = [s for s in songs if artist.lower() in s['artist'].lower()]
if len(songs) <= 0:
print "No matches for artist: " + artist
sys.exit(1)
def addSongMatch(song):
global song_matches
matches = [s for s in song_matches if s['url']==song['url']]
# if already exists, just increment score
if len(matches) > 0:
match = matches[0]
song_matches[match['index']]['score'] += 1
# add if not already exist
else:
song['score'] = 1
song['index'] = len(song_matches)
song_matches.append(song)
print "Analyzing..."
# Analyze each song
for song in songs:
lWords = song['lyrics'].split(' ')
for lw in lWords:
matches = [w for w in words if w['word']==lw and w['region']==body_part]
if len(matches) > 0:
addSongMatch(song)
# Sort song matches by score
song_matches = sorted(song_matches, key=lambda k: k['score'], reverse=True)
# Report on matches
if len(song_matches) > 0:
print str(len(song_matches)) + ' matches found: '
else:
print 'No results found'
for s in song_matches:
print str(s['score']) + '. ' + s['song'] + ' - ' + s['album'] + ' [' + s['url'] + ']'