Some refactoring (also PEP-8) to audio-analyzer.py

This commit is contained in:
2016-02-24 00:23:47 +03:00
parent 7ff9970db3
commit 7b3d974603

View File

@@ -1,20 +1,23 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json import json
import sys import sys
from nltk import RegexpTokenizer, OrderedDict from nltk import RegexpTokenizer, OrderedDict
from nltk.stem.snowball import RussianStemmer from nltk.stem.snowball import RussianStemmer
genres = { genres = {
1: "Rock", 1: "Rock",
2: "Pop", 2: "Pop",
3: "Rap & Hip - Hop", 3: "Rap & Hip - Hop",
4: "Easy Listening", 4: "Easy Listening",
5: "Dance & House", 5: "Dance & House",
6: "Instrumental", 6: "Instrumental",
7: "Metal", 7: "Metal",
21: "Alternative", 21: "Alternative",
8: "Dubstep", 8: "Dubstep",
9: "Jazz & Blues", 9: "Jazz & Blues",
10: "Drum & Bass", 10: "Drum & Bass",
11: "Trance", 11: "Trance",
12: "Chanson", 12: "Chanson",
@@ -34,31 +37,34 @@ def dictWithoutOneKey(d, key):
new_d.pop(key) new_d.pop(key)
return new_d return new_d
if __name__ == '__main__':
musicFileName = sys.argv[1]
destFileName = sys.argv[2]
audioStats = dict() tokenizer = RegexpTokenizer(r"[A-Za-zА-Яа-я]+")
stemmer = RussianStemmer()
tokenizer = RegexpTokenizer(r"[A-Za-zА-Яа-я]+") audioStats = dict()
stemmer = RussianStemmer()
musicFileName = sys.argv[1] with open(musicFileName, "r", encoding="utf8") as f_music:
with open(musicFileName, "r", encoding="utf8") as file: for line in f_music:
for line in file: jsonData = json.loads(line, encoding="utf8")
jsonData = json.loads(line, encoding="utf8") for song in list(jsonData.values())[0]:
for song in list(jsonData.values())[0]: songName = "{} - {}".format(song["artist"], song["title"])
songName = "{} - {}".format(song["artist"], song["title"]) filteredSongName = "".join(
filteredSongName = "".join([stemmer.stem(token).lower() for token in tokenizer.tokenize(songName)]) [stemmer.stem(token).lower() for token in tokenizer.tokenize(songName)]
if len(filteredSongName) > 1: )
audioStatsItem = audioStats.get(filteredSongName, { if len(filteredSongName) > 1:
"name": songName, audioStatsItem = audioStats.get(filteredSongName, {
"url": song["url"], "name": songName,
"genre": genres.get(song["genre_id"], "Other"), "url": song["url"],
"count": 0 "genre": genres.get(song["genre_id"], "Other"),
}) "count": 0
audioStatsItem["count"] += 1 })
audioStats[filteredSongName] = audioStatsItem audioStatsItem["count"] += 1
audioStats[filteredSongName] = audioStatsItem
destFileName = sys.argv[2] with open(destFileName, "w", encoding="utf-8") as f_out:
with open(destFileName, "w", encoding="utf8") as file: sortedSongs = [item[1] for item in sorted(audioStats.items(), key=lambda item: item[1]["count"], reverse=True)]
sortedSongs = [item[1] for item in sorted(audioStats.items(), key=lambda item: item[1]["count"], reverse=True)] data = OrderedDict([(item["name"], dictWithoutOneKey(item, "name")) for item in sortedSongs])
data = OrderedDict([(item["name"], dictWithoutOneKey(item, "name")) for item in sortedSongs]) f_out.write(json.dumps(data, ensure_ascii=False, indent=4))
file.write(json.dumps(data, ensure_ascii=False, indent=4))