Some refactoring (also PEP-8) to audio-analyzer.py
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@@ -34,19 +37,23 @@ def dictWithoutOneKey(d, key):
|
|||||||
new_d.pop(key)
|
new_d.pop(key)
|
||||||
return new_d
|
return new_d
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
audioStats = dict()
|
musicFileName = sys.argv[1]
|
||||||
|
destFileName = sys.argv[2]
|
||||||
|
|
||||||
tokenizer = RegexpTokenizer(r"[A-Za-zА-Яа-я]+")
|
tokenizer = RegexpTokenizer(r"[A-Za-zА-Яа-я]+")
|
||||||
stemmer = RussianStemmer()
|
stemmer = RussianStemmer()
|
||||||
|
|
||||||
musicFileName = sys.argv[1]
|
audioStats = dict()
|
||||||
with open(musicFileName, "r", encoding="utf8") as file:
|
|
||||||
for line in file:
|
with open(musicFileName, "r", encoding="utf8") as f_music:
|
||||||
|
for line in f_music:
|
||||||
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([stemmer.stem(token).lower() for token in tokenizer.tokenize(songName)])
|
filteredSongName = "".join(
|
||||||
|
[stemmer.stem(token).lower() for token in tokenizer.tokenize(songName)]
|
||||||
|
)
|
||||||
if len(filteredSongName) > 1:
|
if len(filteredSongName) > 1:
|
||||||
audioStatsItem = audioStats.get(filteredSongName, {
|
audioStatsItem = audioStats.get(filteredSongName, {
|
||||||
"name": songName,
|
"name": songName,
|
||||||
@@ -57,8 +64,7 @@ with open(musicFileName, "r", encoding="utf8") as file:
|
|||||||
audioStatsItem["count"] += 1
|
audioStatsItem["count"] += 1
|
||||||
audioStats[filteredSongName] = audioStatsItem
|
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])
|
||||||
file.write(json.dumps(data, ensure_ascii=False, indent=4))
|
f_out.write(json.dumps(data, ensure_ascii=False, indent=4))
|
||||||
|
|||||||
Reference in New Issue
Block a user