Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.recommenders.rival.split.parser;
  
  import java.io.*;
  import java.util.HashMap;
  import java.util.Map;

Author(s):
Alejandro
 
 public class LastfmCelma1KParser extends AbstractLastfmCelmaParser implements ParserWithIdMapping {

    
The column index for the user id in the file.
 
     public static final int USER_TOK = 0;
    
The column index for the artist id in the file.
 
     public static final int ARTIST_TOK = 3;
    
The column index for the track id in the file.
 
     public static final int TRACK_TOK = 5;
    
The column index for the time in the file.
 
     public static final int TIME_TOK = 1;
 
     public LastfmCelma1KParser(boolean useArtists) {
         super(useArtists);
     }
 
 
     public DataModel<LongLongparseData(File fString mapIdsPrefixthrows IOException {
         DataModel<LongLongdataset = new DataModel<LongLong>();
 
         Map<StringLongmapUserIds = new HashMap<StringLong>();
         Map<StringLongmapItemIds = new HashMap<StringLong>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
 
         long curUser = getIndexMap(new File(mapIdsPrefix + "_userId.txt"), mapUserIds);
         long curItem = getIndexMap(new File(mapIdsPrefix + "_itemId.txt"), mapItemIds);
 
         BufferedReader br = new BufferedReader(new FileReader(f));
         String line = null;
         while ((line = br.readLine()) != null) {
             String[] toks = line.split("\t");
             // user
             String user = toks[];
             if (!mapUserIds.containsKey(user)) {
                 mapUserIds.put(usercurUser);
                 curUser++;
             }
             long userId = mapUserIds.get(user);
             // item
             String artist = toks[];
             String track = toks[];
             String item = null;
             if () {
                 item = artist;
             } else {
                 item = artist + "_" + track;
             }
             if (!mapItemIds.containsKey(item)) {
                 mapItemIds.put(itemcurItem);
                 curItem++;
             }
             long itemId = mapItemIds.get(item);
             // timestamp
             long timestamp = -1;
             if ( != -1) {
                 try {
                     timestamp = sdf.parse(toks[]).getTime();
                 } catch (ParseException e) {
                     e.printStackTrace();
                 }
             }
             // preference
             double preference = 1.0;
             //////
             // update information
             //////
             dataset.addPreference(userIditemIdpreference);
             if (timestamp != -1) {
                 dataset.addTimestamp(userIditemIdtimestamp);
             }
         }
         br.close();
 
         // save map ids?
         if (mapIdsPrefix != null) {
             // save user map
            PrintStream outUser = new PrintStream(mapIdsPrefix + "_userId.txt");
            for (Entry<StringLonge : mapUserIds.entrySet()) {
                outUser.println(e.getKey() + "\t" + e.getValue());
            }
            outUser.close();
            // save item map
            PrintStream outItem = new PrintStream(mapIdsPrefix + "_itemId.txt");
            for (Entry<StringLonge : mapItemIds.entrySet()) {
                outItem.println(e.getKey() + "\t" + e.getValue());
            }
            outItem.close();
        }
        return dataset;
    }
New to GrepCode? Check out our FAQ X