-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_lyrics_data.py
78 lines (66 loc) · 2.59 KB
/
create_lyrics_data.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
73
74
75
76
77
78
import glob, codecs
import mutagen
from mutagen.id3 import ID3
from mutagen import MutagenError
import train_model
import spacy
from spacy_fastlang import LanguageDetector
root_dir = 'Y:\\'
data_file_full = 'data/lyrics_data_full.csv'
data_file = 'data/lyrics_data.csv'
badwords=['nigga', 'fuck', 'shit', 'bitch']
goodwords=['nokoshite', 'shitai', 'kesshite', 'shite', 'kaskushiteta', 'yurushite', 'sarakedashite', 'afuredashite']
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('language_detector')
with codecs.open(data_file_full, 'w', encoding='utf-8') as output:
output.write('is_offensive,text\r\n')
count=0
for location in glob.iglob(root_dir + '**/*.flac', recursive=True):
count += 1
with codecs.open(data_file_full, 'a', encoding='utf-8') as output:
lyrics = ''
#print(location)
try:
file = mutagen.File(location)
if location.endswith('.mp3'):
if 'audio/mp3' in file.mime:
audio = ID3(location)
lyrics_tag = audio.getall('USLT')
lyrics = lyrics_tag.text
if location.endswith('.flac'):
file = mutagen.File(location)
if 'audio/flac' in file.mime:
for tag in file.tags:
if tag[0] == 'LYRICS':
lyrics = tag[1]
break
except MutagenError as e:
print(f'file not found: {location}')
lyrics = lyrics.replace('Tekst piosenki:\n', '')
lyrics = lyrics.replace('Tekst piosenki:', '')
lyrics = lyrics.replace('Historia edycji tekstu\n', '')
lyrics = lyrics.replace('Historia edycji tekstu', '')
doc = nlp(lyrics.replace('\n', ' '))
if doc._.language == 'en':
lyrics = lyrics.replace("\"", "\"\"\"\"")
lyrics = lyrics.split('\n')
for line in lyrics:
if len(line) > 0:
if any(badword in line.lower() for badword in badwords):
score = 1
if any(goodword in line.lower() for goodword in goodwords):
score = 0
else:
score = 0
output.write(f'{score},\"{line}\"\r\n')
else:
print(doc._.language, location)
#dedup csv
lines_seen = set() # holds lines already seen
outfile = open(data_file, "w")
for line in open(data_file_full, "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
train_model.train()