Skip to content

Instantly share code, notes, and snippets.

@wenming
Created May 20, 2012 23:53
Show Gist options
  • Select an option

  • Save wenming/2759963 to your computer and use it in GitHub Desktop.

Select an option

Save wenming/2759963 to your computer and use it in GitHub Desktop.
opening 3 files to convert matrix indexes to int from string hash
#!/usr/bin/python #http://labrosa.ee.columbia.edu/millionsong/sites/default/files/challenge/train_triplets.txt.zip
songs_file = open('songs.txt', 'r')
users_file = open('users.txt', 'r')
dataset_file = open('train_triplets.txt', 'r')
#dataset_file = open('a.txt', 'r')
songs_count = 0
users_count = 0
dataset_count = 0
users_dict = dict()
songs_dict = dict()
# reading songs into a dictionary object
lines = songs_file.readlines()
for line in lines:
line = line.strip()
songs_dict[line] = songs_count
songs_count = songs_count + 1
#print songs_count
#print songs_dict
songs_file.close()
# reading users into a dictionary object
lines = users_file.readlines()
for line in lines:
line = line.strip()
users_dict[line] = users_count
users_count = users_count + 1
#print users_count
#print users_dict.values()
users_file.close()
#reading dataset
# reading songs into a dictionary object
while 1:
lines = dataset_file.readlines(1000000)
if not lines:
break
for line in lines:
line = line.strip()
(user, song, freq) = line.split()
user = user.strip()
if (users_dict.haskey(user) == False):
users_dict[user] = users_count + 1;
song = song.strip()
if (songs_dict.haskey(song) == False):
songs_dict[song] = songss_count + 1;
freq = freq.strip()
print users_dict[user], songs_dict[song], freq
dataset_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment