-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_extractor.py
165 lines (129 loc) · 5.44 KB
/
feature_extractor.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Feature extractor for CS 229 Final Project: Whose Rap is it Anyways?
# Alex Wang, Robin Cheong, Vince Ranganathan
# jwang98, robinc20, akranga @ stanford.edu
# Updated 11/02/2017
from nltk.stem import PorterStemmer as ps
from vocabulary_builder import buildVocabulary
import pandas as pd
from sklearn.model_selection import train_test_split
import sys
import numpy as np
import regex as re
ps = ps()
VOCAB_FILE = "chosen_train.csv"
#----------------------------------#
def normalize(data):
bias = data[:, 0]
m, n = data.shape
means = (data.sum(axis=0) * 1.0 / m)
data = data - means
variance = np.square(data).sum(axis=0) * 1.0 / m
variance[variance == 0] = 1
data /= np.sqrt(variance)
data[:, 0] = bias
return data
def getArtists():
with open('./data_scraping/artists.txt') as f:
return f.read().splitlines()
def extract_artist_map():
artists = getArtists()
artist_map = {}
for i, artist in enumerate(artists):
artist_map[artist] = i
return artist_map
def calc_idf(data, vocab):
m, n = data.shape
appearances = []
for i, data_pt in enumerate(data):
vocab_dict = dict.fromkeys(vocab, 0)
words = data_pt[2].decode('utf-8').split()
lyrics = [ps.stem(word) for word in words]
for word in lyrics:
word = re.sub(r'\p{P}+', "", word)
if word in vocab_dict:
vocab_dict[word] = 1
counts = np.array(vocab_dict.values())
appearances.append(counts)
appearances = np.array(appearances).sum(axis=0)
idf = np.log(float(m) / appearances)
return idf
def featureExtractor(raw_data, filename, vocab, idf, lower=0, upper=20000, TF='regular', verbose=0):
# setup
# set of all words that appear in the song
processed_data = []
artist_map = extract_artist_map()
K = 0.5
for data_pt in raw_data:
artist = artist_map[data_pt[0]]
vocab_dict = dict.fromkeys(vocab, 0)
words = data_pt[2].decode('utf-8').split()
lyrics = [ps.stem(word) for word in words]
def wordFrequencies(vocab_dict, lyrics):
for word in lyrics:
if word in vocab_dict:
vocab_dict[word] += 1
if TF == 'binary':
for word in lyrics:
if word in vocab_dict:
vocab_dict[word] = 1
elif TF == 'regular':
wordFrequencies(vocab_dict, lyrics)
elif TF == 'log':
wordFrequencies(vocab_dict, lyrics)
for word in lyrics:
if word in vocab_dict:
vocab_dict[word] = np.log(1 + vocab_dict[word])
elif TF == 'norm':
wordFrequencies(vocab_dict, lyrics)
max_freq = max(vocab_dict.values())
for word in lyrics:
if word in vocab_dict:
vocab_dict[word] = K + ((1 - K) * (vocab_dict[word] / max_freq))
counts = np.array(vocab_dict.values())
phi = np.concatenate((np.array([1]), counts))
processed_data.append(np.concatenate((np.array([artist]), phi)))
if(TF != 'binary'):
processed_data = np.array(processed_data)
x = processed_data[:, 2:] * idf
y = processed_data[:, 0:2]
processed_data = np.append(y, x, axis=1)
processed_df = pd.DataFrame(processed_data)
print processed_df.head()
processed_df.to_csv(filename + '_' + TF + '.csv', index=False)
#----------------------------------#
def split_data(data, test_size):
artists = getArtists()
train_data = pd.DataFrame(columns=['Artist', 'Title', 'Lyrics'])
test_data = pd.DataFrame(columns=['Artist', 'Title', 'Lyrics'])
for artist in artists:
artist_data = data[data['Artist'] == artist]
artist_train, artist_test = train_test_split(artist_data, test_size=test_size, random_state=420)
train_data = train_data.append(artist_train)
test_data = test_data.append(artist_test)
return train_data, test_data
def initialize_data_sets(raw_data_path):
data = pd.read_csv(raw_data_path, delimiter='|')
train_data, test_data = split_data(data, .15)
train_data, dev_data = split_data(train_data, .125)
pd.DataFrame(train_data, columns=["Artist", "Title", "Lyrics"]).to_csv('chosen_train.csv', sep="|", index=False)
pd.DataFrame(dev_data, columns=["Artist", "Title", "Lyrics"]).to_csv('chosen_dev.csv', sep="|", index=False)
pd.DataFrame(test_data, columns=["Artist", "Title", "Lyrics"]).to_csv('chosen_test.csv', sep="|", index=False)
def main():
#initialize_data_sets('./data_scraping/finaldata.csv')
train_data = pd.read_csv('chosen_train.csv', delimiter='|').as_matrix()
dev_data = pd.read_csv('chosen_dev.csv', delimiter='|').as_matrix()
test_data = pd.read_csv('chosen_test.csv', delimiter='|').as_matrix()
if len(sys.argv) > 1:
lower = sys.argv[1]
upper = sys.argv[2]
TF = sys.argv[3] if len(sys.argv) > 3 else 'regular'
strain = 'train_' + str(lower) + '-' + str(upper)
sdev = 'dev_' + str(lower) + '-' + str(upper)
stest = 'test_' + str(lower) + '-' + str(upper)
vocab = buildVocabulary(lower, upper, VOCAB_FILE)
idf = calc_idf(train_data, vocab)
featureExtractor(train_data, strain, vocab, idf, lower, upper, TF)
featureExtractor(dev_data, sdev, vocab, idf, lower, upper, TF, vocab)
featureExtractor(test_data, stest, vocab, idf, lower, upper, TF, vocab)
if __name__ == "__main__":
main()