itsjube

Untitled

Nov 18th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. public static void main(String[] args) throws IOException {
  2.         //---- Definitions ----//
  3.  
  4.         HashMap<String, List<String>> indexDocID = new HashMap<String, List<String>>();
  5.         HashMap<String, List<String>> indexTF = new HashMap<String, List<String>>();
  6.         HashMap<String, String> indexNormalizedTF = new HashMap<String, String>();
  7.  
  8.         String indexFileLocation = "path";
  9.         String tokenMapFileLocation = "path";
  10.  
  11.         //---- Code ----//
  12.  
  13.         try (BufferedReader br = new BufferedReader(new FileReader(indexFileLocation))) {
  14.             String line;
  15.             int position = 0;
  16.             while ((line = br.readLine()) != null) {
  17.                 List<List<String>> valueList = new ArrayList<List<String>>();
  18.  
  19.                 String[] term = line.split("-~- ");
  20.  
  21.                 //Grab Key
  22.                 String key = term[0];
  23.  
  24.                 //Grab values and put them in a list of lists
  25.                 String indexValue = term[1];
  26.                 indexValue = indexValue.replace("\n"," ");
  27.                 String[] values = indexValue.split(" ");
  28.  
  29.                 List<String> tfList = new ArrayList<String>();
  30.                 List<String> docIDList = new ArrayList<String>();
  31.                 for(int i = 0; i < values.length; ++i) {
  32.                     if(values[i] != null || values[i] != " ") {
  33.                         String temporary = values[i].replaceAll("[()]","");
  34.                         String[] individualValues = temporary.split(",");
  35.                         String docid = individualValues[0];
  36.                         docIDList.add(docid);
  37.                         String tf = individualValues[1];
  38.                         tfList.add(tf);
  39.                     }
  40.  
  41.                 }
  42.                 position =+ 1;
  43.                 indexDocID.put(key, docIDList);
  44.                 indexTF.put(key, tfList);
  45.             }
  46.  
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.  
  51.         try (BufferedReader br = new BufferedReader(new FileReader(tokenMapFileLocation))) {
  52.             String line;
  53.             int position = 0;
  54.             while ((line = br.readLine()) != null) {
  55.                 String[] entries = line.split("--");
  56.                 String docID = entries[0];
  57.                 String tokenCount = entries[1];
  58.                 indexNormalizedTF.put(docID, tokenCount);
  59.             }
  60.  
  61.         } catch (IOException e) {
  62.             e.printStackTrace();
  63.         }
  64.  
  65.  
  66.         //System.out.print(indexDocID.size());
  67.  
  68.         Scanner in = new Scanner(System.in);
  69.  
  70.         System.out.println("Please enter a query:");
  71.         String query_input = in.nextLine();
  72.         query_input = query_input.toLowerCase();
  73.  
  74.         List<String> list = new ArrayList<>();
  75.         list = indexDocID.get(query_input);
  76.  
  77.         System.out.print(list.size());
  78.  
  79.  
  80.  
  81.         try {
  82.  
  83.  
  84.  
  85.         } catch (Throwable e) {
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.     }
  97.  
  98.     public static double calcTF(int termFrequency, int totalTokens) {
  99.         double normalizedTF = 0;
  100.         normalizedTF = termFrequency / totalTokens;
  101.         return normalizedTF;
  102.     }
  103.  
  104.     public static double calcIDF(int documentFrequency, List totalDocuments) {
  105.         double normalizedTF = 0;
  106.         normalizedTF = 1 + Math.log(totalDocuments.size() / documentFrequency);
  107.         return normalizedTF;
  108.  
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment