itsjube

Untitled

Nov 18th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 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 test {
  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> indexNormalizedTF = new HashMap<String, String>();
  19.  
  20.         String indexFileLocation = "path";
  21.         String tokenMapFileLocation = "path";
  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.  
  36.                 //Grab values and put them in a list of lists
  37.                 String indexValue = term[1];
  38.                 indexValue = indexValue.replace("\n"," ");
  39.                 String[] values = indexValue.split(" ");
  40.  
  41.                 List<String> tfList = new ArrayList<String>();
  42.                 List<String> docIDList = new ArrayList<String>();
  43.                 for(int i = 0; i < values.length; ++i) {
  44.                     String temporary = values[i].replaceAll("[()]","");
  45.                     String[] individualValues = temporary.split(",");
  46.                     String docid = individualValues[0];
  47.                     docIDList.add(docid);
  48.                     String tf = individualValues[1];
  49.                     tfList.add(tf);
  50.  
  51.  
  52.                 }
  53.                 position =+ 1;
  54.                 indexDocID.put(key, docIDList);
  55.                 indexTF.put(key, tfList);
  56.             }
  57.  
  58.         } catch (IOException e) {
  59.             e.printStackTrace();
  60.         }
  61.  
  62.         try (BufferedReader br = new BufferedReader(new FileReader(tokenMapFileLocation))) {
  63.             String line;
  64.             int position = 0;
  65.             while ((line = br.readLine()) != null) {
  66.                 String[] entries = line.split("--");
  67.                 String docID = entries[0];
  68.                 String tokenCount = entries[1];
  69.                 indexNormalizedTF.put(docID, tokenCount);
  70.             }
  71.  
  72.         } catch (IOException e) {
  73.             e.printStackTrace();
  74.         }
  75.  
  76.  
  77.         //System.out.print(indexDocID.size());
  78.  
  79.         Scanner in = new Scanner(System.in);
  80.  
  81.         System.out.println("Please enter a query:");
  82.         String query_input = in.nextLine();
  83.         query_input = query_input.toLowerCase();
  84.  
  85.  
  86.         System.out.println(query_input);
  87.  
  88.         List<String> test = indexDocID.get(query_input);
  89.  
  90.         System.out.println(test.size());
  91.  
  92. /*
  93.         for (Map.Entry<String,List<String>> entry : indexDocID.entrySet()) {
  94.             String key = entry.getKey();
  95.             List<String> value = entry.getValue();
  96.             System.out.println(key);
  97.             System.out.println(value);
  98.         }
  99.         */
  100.  
  101.         List<String> list = new ArrayList<>();
  102.         list = indexDocID.get(query_input);
  103.  
  104.  
  105.  
  106.         try {
  107.  
  108.  
  109.  
  110.         } catch (Throwable e) {
  111.  
  112.         }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.     }
  122.  
  123.     public static double calcTF(int termFrequency, int totalTokens) {
  124.         double normalizedTF = 0;
  125.         normalizedTF = termFrequency / totalTokens;
  126.         return normalizedTF;
  127.     }
  128.  
  129.     public static double calcIDF(int documentFrequency, List totalDocuments) {
  130.         double normalizedTF = 0;
  131.         normalizedTF = 1 + Math.log(totalDocuments.size() / documentFrequency);
  132.         return normalizedTF;
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.     //System.out.println(terms);
  139.  
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment