Advertisement
Guest User

InvertedIndex

a guest
Aug 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.*;
  7.  
  8.  
  9.  
  10. public class InvertedIndex {
  11.  
  12.     public static Map<String, List<Coordinates>> invertedIndex = new TreeMap<>();
  13.     public static Map<String, Double> IDF = new TreeMap<>();
  14.     public static String text;
  15.     public static Map<String, List<String>> words=new TreeMap<>();
  16.     public static File[] files;
  17.  
  18.  
  19.     public static void setValue (String var, int value, String name) {
  20.  
  21.         if (invertedIndex.containsKey(var)) {
  22.  
  23.             if (invertedIndex.get(var).get(invertedIndex.get(var).size()-1).getName().equals(name)){
  24.                 invertedIndex.get(var).get(invertedIndex.get(var).size()-1).add(value);
  25.             }
  26.             else {
  27.                 Coordinates otherCoordinates = new Coordinates();
  28.                 otherCoordinates.fileName=name;
  29.                 otherCoordinates.add(value);
  30.                 invertedIndex.get(var).add(otherCoordinates);
  31.             }
  32.  
  33.         } else {
  34.             List<Coordinates> newList = new ArrayList<>();
  35.             Coordinates newCoordinates = new Coordinates();
  36.             newCoordinates.fileName=name;
  37.             newCoordinates.add(value);
  38.             newList.add(newCoordinates);
  39.             invertedIndex.put(var, newList);
  40.         }
  41.     }
  42.  
  43.     public static void main() throws IOException {
  44.  
  45.         File file = new File("E:\\SomeDir");
  46.  
  47.         if(file.isDirectory()) {
  48.            files = file.listFiles();
  49.  
  50.             if (files != null) {
  51.                 for (File f : files) {
  52.                     try {
  53.                         text = new String(Files.readAllBytes(Paths.get(new File(f.getCanonicalPath()).toURI())));
  54.                         List<String> text2 = Arrays.asList(
  55.                                 text
  56.                                         .replaceAll("[^a-zA-Zа-яА-Я1-9-]", " ")
  57.                                         .toLowerCase()
  58.                                         .split("\\s+")
  59.                         );
  60.  
  61.                             words.put(f.getName(),text2);
  62.  
  63.                        for (int number = 0; number < words.get(f.getName()).size(); number++) {
  64.                             setValue(words.get(f.getName()).get(number), number, f.getName());
  65.                         }
  66.  
  67.                     } catch (IOException e) {
  68.                         e.printStackTrace();
  69.                     }
  70.                     }
  71.                 for(Map.Entry<String, List<Coordinates>> item: invertedIndex.entrySet()){
  72.                     System.out.print(item.getKey()+" ");
  73.                     for (Coordinates coordinate : item.getValue()){
  74.                         coordinate.get();
  75.                     }
  76.                     System.out.println();
  77.                 }
  78.                 System.out.println("IDF: ");
  79. // Заполняем IDF
  80.                 for(String item2: invertedIndex.keySet()) {
  81.                     IDF.put(item2, Math.log10(words.size() / invertedIndex.get(item2).size()));
  82.                     System.out.println(IDF.get(item2));
  83.                 }
  84.             }
  85.             else {
  86.                 System.out.println("Здесь нет файлов");
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement