com.mythesis.userbehaviouranalysis.Utils.java Source code

Java tutorial

Introduction

Here is the source code for com.mythesis.userbehaviouranalysis.Utils.java

Source

/* 
 * Copyright 2015 Kostas Papangelou.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mythesis.userbehaviouranalysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
 * a class for auxiliary functions
 * @author Kostas Papangelou
 */
public class Utils {

    protected HashMap<String, ArrayList<String>> wordvectors;
    protected HashMap<String, String> crawlerOutputPaths;

    /**
     * a method that reads a txt file with the necessary parameters
     * @param input the file that includes the necessary parameters
     */
    public void readInput(File input) {
        FileInputStream inputStream = null;
        Scanner sc = null;
        wordvectors = new HashMap<>();
        crawlerOutputPaths = new HashMap<>();

        HashMap<String, String> wordvectorsPaths = new HashMap<>();
        try {
            inputStream = new FileInputStream(input);
            sc = new Scanner(inputStream);

            if (sc.hasNextLine()) {
                int numProfiles = Integer.parseInt(sc.nextLine().split(";")[1].trim().toLowerCase());

                int j = 0;
                while (j < numProfiles) {
                    String profile = "";
                    if (sc.hasNextLine()) {
                        profile = sc.nextLine().split(";")[1].trim();
                    }
                    if (sc.hasNextLine()) {
                        String path = sc.nextLine().split(";")[1].trim();
                        crawlerOutputPaths.put(profile, path);
                    }
                    if (sc.hasNextLine()) {
                        String path = sc.nextLine().split(";")[1].trim();
                        wordvectorsPaths.put(profile, path);
                    }
                    j++;
                }
            }

            for (String profile : wordvectorsPaths.keySet()) {
                File file = new File(wordvectorsPaths.get(profile));
                try {
                    List<String> wordvector = FileUtils.readLines(file);
                    wordvectors.put(profile, (ArrayList<String>) wordvector);
                } catch (IOException ex) {
                    Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (sc != null) {
                sc.close();
            }
        }

    }
}