Java BufferedReader Read loadTestUserEventRelation(String eventFilePath, HashMap> TestUser2EventIndexSetMap)

Here you can find the source of loadTestUserEventRelation(String eventFilePath, HashMap> TestUser2EventIndexSetMap)

Description

Build TestUser2EventIndexSetMap and TestUserIndices.

License

Apache License

Parameter

Parameter Description
eventFilePath a parameter
TestUser2EventIndexSetMap TestUser2EventIndexSetMap[i] = {indexOf(i, j) | (i, j) \in C}

Return

TestUserIndices TestUserIndices[k] is the user index for the k-th event

Declaration

public static int[] loadTestUserEventRelation(String eventFilePath,
        HashMap<Integer, LinkedList<Integer>> TestUser2EventIndexSetMap) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/*from w  w  w.  j  a va2 s . c  o m*/
     * Build TestUser2EventIndexSetMap and TestUserIndices.
     * 
     * @param eventFilePath
     * 
     * @param TestUser2EventIndexSetMap TestUser2EventIndexSetMap[i] = {indexOf(i, j) | (i, j) \in C}
     * 
     * @return TestUserIndices TestUserIndices[k] is the user index for the k-th event
     */
    public static int[] loadTestUserEventRelation(String eventFilePath,
            HashMap<Integer, LinkedList<Integer>> TestUser2EventIndexSetMap) {
        if (!new File(eventFilePath).exists()) {
            System.err.println(String.format("Event file %s doesn't exist.\n", eventFilePath));
            exit(1);
        }
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(eventFilePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            exit(1);
        }
        String line = "";
        List<Double> YijList = new LinkedList<Double>();
        // List<Vector> XijVectorList = new LinkedList<Vector>();
        List<double[]> XijList = new LinkedList<double[]>();
        List<Integer> userIdxList = new LinkedList<Integer>();
        List<Integer> itemIdxList = new LinkedList<Integer>();
        String[] container = null;
        double label = 0;
        int userIdx = -1;
        int itemIdx = -1;
        double gmp = 0;
        double freshness = 0;
        int eventIdx = -1;
        int maxUserIdx = -1;
        int maxItemIdx = -1;
        try {
            while ((line = br.readLine()) != null) {
                if (line.isEmpty())
                    continue;
                container = line.split("\t");
                label = Double.parseDouble(container[0]);
                userIdx = Integer.parseInt(container[1]);
                itemIdx = Integer.parseInt(container[2]);
                // Modified on Oct. 16th, 2014
                // {
                if (maxUserIdx < userIdx)
                    maxUserIdx = userIdx;
                if (maxItemIdx < itemIdx)
                    maxItemIdx = itemIdx;
                // }
                gmp = Double.parseDouble(container[3]);
                freshness = Double.parseDouble(container[4]);
                YijList.add(label);
                userIdxList.add(userIdx);
                itemIdxList.add(itemIdx);
                // XijVectorList.add(new DenseVector(new double[]{gmp, freshness}));
                XijList.add(new double[] { gmp, freshness });
                eventIdx += 1;
                if (TestUser2EventIndexSetMap.containsKey(userIdx)) {
                    TestUser2EventIndexSetMap.get(userIdx).add(eventIdx);
                } else {
                    LinkedList<Integer> eventSet = new LinkedList<Integer>();
                    eventSet.add(eventIdx);
                    TestUser2EventIndexSetMap.put(userIdx, eventSet);
                }
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int eventCnt = YijList.size();
        int[] TestUserIndices = new int[eventCnt];
        int cnt = 0;
        /*cnt = 0;
        for (double element : YijList) {
           Yij[cnt++] = element;
        }*/
        cnt = 0;
        for (int element : userIdxList) {
            TestUserIndices[cnt++] = element;
        }
        return TestUserIndices;
    }

    public static void exit(int status) {
        System.exit(status);
    }
}

Related

  1. loadTableList(List list, String fileName)
  2. loadTemplate(final String resource)
  3. loadTemplate(String templateUrl)
  4. loadTemplateParametersFile(File f)
  5. loadTestFixture(String path)
  6. loadToStringListFromFile(String fullFileName)
  7. loadTrustDistrustSets(String trust_data_set)
  8. loadTrustSet(String trustSet)
  9. loadTwoColumnResource(InputStream resource)