-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicDataCollection.
216 lines (190 loc) · 8.21 KB
/
MusicDataCollection.
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package musicdatacollection;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import de.umass.lastfm.*;
import java.util.Vector;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collection;
/**
*
* @author Rawan
*/
public class MusicDataCollection {
/**
* @param args the command line arguments
*/
static int numofpages = 800;
public static void main(String[] args) {
Caller.getInstance().setUserAgent("RawanMG");
Caller.getInstance().setDebugMode(true);
String key = "74e2bf6d43f85e4a5d3a89e6eee3fd7b"; //this is the key used in the Last.fm API examples
String user = "RawanMG";
Vector<String> uniquetracks = new Vector<String>(); //to store the track name
Vector<String> artists = new Vector<String>(); //to store the artist name
Vector<Integer> uniqueplaycount = new Vector<Integer>(); //to store the playcount
Vector<Integer> ulovedtracks = new Vector<Integer>(); //to store whether the track is loved or not
Vector<String> uniqueartist = new Vector<String>(); //to store the artist vs id(index)
//Testing data
/* Vector<String> uniquetrackstest = new Vector<String>(); //to store the track name
Vector<String> artiststest = new Vector<String>(); //to store the artist name
Vector<Integer> uniqueplaycounttest = new Vector<Integer>(); //to store the playcount
Vector<Integer> ulovedtrackstest = new Vector<Integer>(); //to store whether the track is loved or not
Vector<String> uniqueartisttest = new Vector<String>(); //to store the artist vs id(index)
*/
//Collect Loved tracks
Vector<String> lovedtracks = new Vector<String>(); //to store all the loved tracks
PaginatedResult<Track> loved;
int c = 0;
for(int x=1; x<=numofpages; x++){
loved = User.getLovedTracks(user, x, key);
for(Track track: loved){
lovedtracks.add(track.getName());
c++;
}
}
/* loved = User.getLovedTracks(user, 2, key);
for(Track track: loved){
lovedtracks.add(track.getName());
c++;
}*/
System.out.println(c);
PaginatedResult<Track> tracks;
for(int j=1; j<=numofpages; j++)
{ //First 200 tracks (page 1)
tracks = User.getRecentTracks(user, j, 100, key);
for(Track track: tracks){
String t= track.getName();
//if the trakc is added to the vector increment the playcount
if(uniquetracks.contains(t))
{
int loc = uniquetracks.indexOf(t);//location of the track in the Vector
uniqueplaycount.set(loc, uniqueplaycount.elementAt(loc)+1); //increments the playcount
}
else //if the track is not in the vector add it with playcount = 1
{ uniquetracks.addElement(t);
String artist = track.getArtist();
artists.add(artist);
uniqueplaycount.addElement(1);
if(!uniqueartist.contains(artist))
uniqueartist.addElement(artist);
if (lovedtracks.contains(t))
ulovedtracks.add(1);
else
ulovedtracks.add(0);
}
}
}
//Second 200 tracks (page 2)
/* tracks = User.getRecentTracks(user, 2, 200, key);
for(Track track: tracks){
String t= track.getName();
//if the trakc is added to the vector increment the playcount
if(uniquetracks.contains(t))
{
int loc = uniquetracks.indexOf(t);//location of the track in the Vector
uniqueplaycount.set(loc, uniqueplaycount.elementAt(loc)+1); //increments the playcount
}
else //if the track is not in the vector add it with playcount = 1
{ uniquetracks.addElement(t);
String artist = track.getArtist();
artists.add(artist);
uniqueplaycount.addElement(1);
if(!uniqueartist.contains(artist))
uniqueartist.addElement(artist);
if (lovedtracks.contains(t))
ulovedtracks.add(1);
else
ulovedtracks.add(0);
}
}
//Third 200 tracks (page 3)
tracks = User.getRecentTracks(user, 3, 200, key);
for(Track track: tracks){
String t= track.getName();
//if the trakc is added to the vector increment the playcount
if(uniquetracks.contains(t))
{
int loc = uniquetracks.indexOf(t);//location of the track in the Vector
uniqueplaycount.set(loc, uniqueplaycount.elementAt(loc)+1); //increments the playcount
}
else //if the track is not in the vector add it with playcount = 1
{ uniquetracks.addElement(t);
String artist = track.getArtist();
artists.add(artist);
uniqueplaycount.addElement(1);
if(!uniqueartist.contains(artist))
uniqueartist.addElement(artist);
if (lovedtracks.contains(t))
ulovedtracks.add(1);
else
ulovedtracks.add(0);
}
} */
try{
FileWriter fw = new FileWriter("musicdata.txt"); //holds track data in the form ( track_title artist_index playcount loved_or_not)
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Music Data: " +uniquetracks.size());
for(int i =0; i<uniquetracks.size(); i++){
bw.write(uniquetracks.elementAt(i) + " " +uniqueartist.indexOf( artists.elementAt(i))+ " " + uniqueplaycount.elementAt(i) + " " + ulovedtracks.elementAt(i)+ "\n" );
}
bw.close();
//50% of the data for Training
fw = new FileWriter("trainingmusicdata.txt");
bw = new BufferedWriter(fw);
System.out.println("Training Data: " + uniquetracks.size()/2);
for(int i =0; i<uniquetracks.size()/2; i++){
bw.write(uniqueartist.indexOf( artists.elementAt(i))+ "," + uniqueplaycount.elementAt(i) + "," + ulovedtracks.elementAt(i) );
bw.write("\n");
}
bw.close();
//20% of the data for Validation
fw = new FileWriter("validationmusicdata.txt");
bw = new BufferedWriter(fw);
int startv = uniquetracks.size()/2 ; //starting index for testing data
System.out.println("startv index " + startv);
int sizev = (int) (uniquetracks.size()*(20.0/100.0)); //size of validation data
System.out.println("Validation Data: " + sizev); //the size of the data is
for(int i =startv; i<startv+ sizev; i++){
bw.write(uniqueartist.indexOf( artists.elementAt(i))+ "," + uniqueplaycount.elementAt(i) + "," + ulovedtracks.elementAt(i) );
bw.write("\n");
}
bw.close();
//30% of the data for Testing
fw = new FileWriter("testingmusicdata.txt");
bw = new BufferedWriter(fw);
int start = startv + sizev; //starting index for testing data
int sizet = (int) (uniquetracks.size()*(30.0/100.0)); //size of testing data
System.out.println("Testing Data: " + sizet); //the size of the data is
for(int i =start; i<uniquetracks.size(); i++){
bw.write(uniqueartist.indexOf( artists.elementAt(i))+ "," + uniqueplaycount.elementAt(i) + "," + ulovedtracks.elementAt(i) );
bw.write("\n");
}
bw.close();
//a new file to map artist id to artist name
FileWriter fw2 = new FileWriter("artistdata.txt");
bw = new BufferedWriter(fw2);
for(int i = 0; i<uniqueartist.size(); i++){
bw.write(i + " " + uniqueartist.elementAt(i));
bw.write("\n"); }
bw.close();
bw = new BufferedWriter(new FileWriter("filesize.txt"));
bw.write("Music Data: " +uniquetracks.size());
bw.write("\n");
bw.write("Training Data: " + uniquetracks.size()/2);
bw.write("\n");
bw.write("Testing Data: " + (uniquetracks.size()-start));
bw.close();
}catch(IOException e){
System.out.println(e.getMessage());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/