Advertisement
daniel_079

Untitled

Dec 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.TreeMap;
  11.  
  12. public class WordStatLineIndex {
  13.     public static void main(String[] args) {
  14.         if (args == null || args.length < 2) {
  15.             System.out.println("Specify input and output files");
  16.             return;
  17.         }
  18.         //args = new String[2];
  19.         //args[1] = "output.txt";
  20.         //args[0] = "Test.txt";
  21.         try {
  22.             TreeMap<String, List<Pair>> map = readToMap(args[0]);
  23.             try (PrintWriter out = new PrintWriter(new File(args[1]), "utf8")) {
  24.                 for (Map.Entry<String, List<Pair>> pair : map.entrySet()) {
  25.                     List<Pair> f = pair.getValue();
  26.                     String word = pair.getKey();
  27.                     out.print(word + " " + f.size() + " ");
  28.                     for (int i = 0; i < f.size(); i++) {
  29.                         out.print(f.get(i).line + ":" + f.get(i).index);
  30.                         if (i != f.size() - 1) {
  31.                             out.print(" ");
  32.                         }
  33.                     }
  34.                     out.println();
  35.                 }
  36.             } catch (FileNotFoundException e) {
  37.                 System.out.println("Specify output file");
  38.             } catch (UnsupportedEncodingException e) {
  39.                 System.out.println("Unsupported encoding, please use utf8");
  40.             }
  41.         } catch (IOException e) {
  42.             System.out.println("Input file not found");
  43.         }
  44.     }
  45.    
  46.     private static TreeMap<String, List<Pair>> readToMap(String inputfile) throws IOException {
  47.         List<String> lines = new ArrayList<>();
  48.         FastScanner in = new FastScanner(new FileInputStream(inputfile));
  49.         while (in.hasNextLine()) {
  50.             lines.add(in.readLine());
  51.         }
  52.         in.close();
  53.         TreeMap<String, List<Pair>> map = new TreeMap<>();
  54.         for (int line = 0; line < lines.size(); line++) {
  55.             String[] words = lines.get(line).replaceAll("[^\\p{L}\\p{Pd}']", " ").trim().toLowerCase().split(" +"); // trim - удаляет пробелы по краям, split - делает массив по пробелам
  56.             for (int i = 0; i < words.length; i++) {
  57.                 String word = words[i];
  58.                 List<Pair> indices = map.getOrDefault(word, new ArrayList<>());
  59.                 indices.add(new Pair(line + 1, i + 1));
  60.                 map.put(word, indices);
  61.             }
  62.         }
  63.         return map;
  64.     }
  65.    
  66.     static class Pair {
  67.         int line;
  68.         int index;
  69.        
  70.         Pair(int line, int index) {
  71.             this.line = line;
  72.             this.index = index;
  73.         }
  74.     }
  75.    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement