Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.io.*;
- import java.util.*;
- import java.lang.Math;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- public class HW4_2 {
- public static void main(String[] args) throws IOException {
- //---- Definitions ----//
- HashMap<String, List<String>> indexDocID = new HashMap<String, List<String>>();
- HashMap<String, List<String>> indexTF = new HashMap<String, List<String>>();
- HashMap<String, String> indexTC = new HashMap<String, String>();
- String indexFileLocation = "assignment_3_index//unigram_task2.txt";
- String tokenMapFileLocation = "assignment_3_index//token_map_task2.txt";
- //---- Code ----//
- try (BufferedReader br = new BufferedReader(new FileReader(indexFileLocation))) {
- String line;
- int position = 0;
- while ((line = br.readLine()) != null) {
- List<List<String>> valueList = new ArrayList<List<String>>();
- String[] term = line.split(" -~- ");
- //Grab Key
- String key = term[0];
- key = key.replace(" ", "");
- //Grab values and put them in a list of lists
- String indexValue = term[1];
- indexValue = indexValue.replace("\n"," ");
- String[] values = indexValue.split(" ");
- List<String> tfList = new ArrayList<String>();
- List<String> docIDList = new ArrayList<String>();
- for(int i = 0; i < values.length; ++i) {
- String temporary = values[i].replaceAll("[()]","");
- String[] individualValues = temporary.split(",");
- String docid = individualValues[0];
- docIDList.add(docid);
- String tf = individualValues[1];
- tfList.add(tf);
- }
- position =+ 1;
- indexDocID.put(key, docIDList);
- indexTF.put(key, tfList);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try (BufferedReader br = new BufferedReader(new FileReader(tokenMapFileLocation))) {
- String line;
- int position = 0;
- while ((line = br.readLine()) != null) {
- String[] entries = line.split("--");
- String docID = entries[0];
- docID = docID.replace(" ", "");
- String tokenCount = entries[1];
- indexTC.put(docID, tokenCount);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- //System.out.print(indexDocID.size());
- Scanner in = new Scanner(System.in);
- System.out.println("Please enter a query:");
- String query_input = in.nextLine();
- query_input = query_input.toLowerCase();
- try {
- List<String> listDocumentsForTerm = new ArrayList<>();
- listDocumentsForTerm = indexDocID.get(query_input);
- List<String> listOfTFForTerm = new ArrayList<>();
- listOfTFForTerm = indexDocID.get(query_input);
- //Calculate normalized TF, DF and IDF for term
- for(int i = 0; i < listDocumentsForTerm.size(); ++i) {
- String documentid = listDocumentsForTerm.get(i);
- double tokenCount = Double.parseDouble(indexTC.get(documentid));
- double tfCount = Double.parseDouble(listOfTFForTerm.get(i));
- double normalizedTF = calcNormalizeTF(tfCount, tokenCount);
- System.out.println("Normalized TF is " + normalizedTF);
- Double IDF = calcIDF(listDocumentsForTerm.size(), indexTC.size());
- System.out.println("IDF is " + IDF);
- Double TFIDF = calcTFIDF(normalizedTF, IDF);
- System.out.println("TF-IDF is " + IDF);
- }
- } catch (Throwable e) {
- }
- }
- public static double calcIDF(int documentFrequency, int totalDocuments) {
- double IDF = 0;
- IDF = 1 + Math.log(totalDocuments / documentFrequency);
- return IDF;
- }
- public static double calcNormalizeTF(double tfCount, double tokenCount) {
- double normalizedTF = 0;
- normalizedTF = 1 + Math.log(tfCount / tokenCount);
- return normalizedTF;
- }
- public static double calcTFIDF(double normalizedTF, double IDF) {
- return normalizedTF * IDF;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment