itsjube

Untitled

Nov 19th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.Math;
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8.  
  9.  
  10.  
  11. public class HW4_2 {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         //---- Definitions ----//
  15.  
  16.         HashMap<String, List<String>> indexDocID = new HashMap<String, List<String>>();
  17.         HashMap<String, List<String>> indexTF = new HashMap<String, List<String>>();
  18.         HashMap<String, String> indexTC = new HashMap<String, String>();
  19.  
  20.         String indexFileLocation = "assignment_3_index//unigram_task2.txt";
  21.         String tokenMapFileLocation = "assignment_3_index//token_map_task2.txt";
  22.  
  23.         //---- Code ----//
  24.  
  25.         try (BufferedReader br = new BufferedReader(new FileReader(indexFileLocation))) {
  26.             String line;
  27.             int position = 0;
  28.             while ((line = br.readLine()) != null) {
  29.                 List<List<String>> valueList = new ArrayList<List<String>>();
  30.  
  31.                 String[] term = line.split(" -~- ");
  32.  
  33.                 //Grab Key
  34.                 String key = term[0];
  35.                 key = key.replace(" ", "");
  36.  
  37.                 //Grab values and put them in a list of lists
  38.                 String indexValue = term[1];
  39.                 indexValue = indexValue.replace("\n"," ");
  40.                 String[] values = indexValue.split(" ");
  41.  
  42.                 List<String> tfList = new ArrayList<String>();
  43.                 List<String> docIDList = new ArrayList<String>();
  44.                 for(int i = 0; i < values.length; ++i) {
  45.                     String temporary = values[i].replaceAll("[()]","");
  46.                     String[] individualValues = temporary.split(",");
  47.                     String docid = individualValues[0];
  48.                     docIDList.add(docid);
  49.                     String tf = individualValues[1];
  50.                     tfList.add(tf);
  51.  
  52.  
  53.                 }
  54.                 position =+ 1;
  55.                 indexDocID.put(key, docIDList);
  56.                 indexTF.put(key, tfList);
  57.             }
  58.  
  59.         } catch (IOException e) {
  60.             e.printStackTrace();
  61.         }
  62.  
  63.         try (BufferedReader br = new BufferedReader(new FileReader(tokenMapFileLocation))) {
  64.             String line;
  65.             int position = 0;
  66.             while ((line = br.readLine()) != null) {
  67.                 String[] entries = line.split("--");
  68.                 String docID = entries[0];
  69.                 docID = docID.replace(" ", "");
  70.                 String tokenCount = entries[1];
  71.                 indexTC.put(docID, tokenCount);
  72.  
  73.             }
  74.  
  75.         } catch (IOException e) {
  76.             e.printStackTrace();
  77.         }
  78.  
  79.  
  80.         //System.out.print(indexDocID.size());
  81.  
  82.         Scanner in = new Scanner(System.in);
  83.  
  84.         System.out.println("Please enter a query:");
  85.         String query_input = in.nextLine();
  86.         query_input = query_input.toLowerCase();
  87.  
  88.  
  89.         try {
  90.  
  91.             List<String> listDocumentsForTerm = new ArrayList<>();
  92.             listDocumentsForTerm = indexDocID.get(query_input);
  93.  
  94.             List<String> listOfTFForTerm = new ArrayList<>();
  95.             listOfTFForTerm = indexDocID.get(query_input);
  96.  
  97.             //Calculate normalized TF, DF and IDF for term
  98.             for(int i = 0; i < listDocumentsForTerm.size(); ++i) {
  99.                 String documentid = listDocumentsForTerm.get(i);
  100.                 double tokenCount = Double.parseDouble(indexTC.get(documentid));
  101.                 double tfCount = Double.parseDouble(listOfTFForTerm.get(i));
  102.  
  103.  
  104.                 double normalizedTF = calcNormalizeTF(tfCount, tokenCount);
  105.                 System.out.println("Normalized TF is " + normalizedTF);
  106.  
  107.  
  108.                 Double IDF = calcIDF(listDocumentsForTerm.size(), indexTC.size());
  109.                 System.out.println("IDF  is " + IDF);
  110.  
  111.                 Double TFIDF = calcTFIDF(normalizedTF, IDF);
  112.  
  113.                 System.out.println("TF-IDF  is " + IDF);
  114.  
  115.             }
  116.  
  117.  
  118.  
  119.  
  120.         } catch (Throwable e) {
  121.  
  122.         }
  123.  
  124.     }
  125.  
  126.  
  127.     public static double calcIDF(int documentFrequency, int totalDocuments) {
  128.         double IDF = 0;
  129.         IDF = 1 + Math.log(totalDocuments / documentFrequency);
  130.         return IDF;
  131.  
  132.     }
  133.  
  134.     public static double calcNormalizeTF(double tfCount, double tokenCount) {
  135.         double normalizedTF = 0;
  136.         normalizedTF = 1 + Math.log(tfCount / tokenCount);
  137.         return normalizedTF;
  138.     }
  139.  
  140.     public static double calcTFIDF(double normalizedTF, double IDF) {
  141.         return normalizedTF * IDF;
  142.     }
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment