Advertisement
Danielto2000

Untitled

Oct 29th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.io.*;
  3.  
  4. public class WordStatIndex {
  5.     public static void main(String[] args) throws IOException {
  6.         LinkedHashMap<String, Integer> wordEntry = new LinkedHashMap<>();
  7.         LinkedHashMap<String, StringBuilder> wordIndex = new LinkedHashMap<>();
  8.         final int INITIAL_INDEX = 0;
  9.         int index = INITIAL_INDEX;
  10.         int INITIAL_ENTRY = 1;
  11.         try {
  12.             MyFileReader reader = new MyFileReader(args[0]);
  13.             while (reader.hasNextLine()) {
  14.                 String line = reader.nextLine();
  15.                 for (String curLine : line.toLowerCase().split("[^\\p{Pd}\\p{L}\\']")) {
  16.                     if (!curLine.isEmpty()) {
  17.                         index++;
  18.                         if (wordEntry.containsKey(curLine)) {
  19.                             wordEntry.put(curLine, wordEntry.get(curLine) + 1);
  20.                             wordIndex.put(curLine, wordIndex.get(curLine).append(" " + index));
  21.                         } else {
  22.                             wordEntry.put(curLine, INITIAL_ENTRY);
  23.                             wordIndex.put(curLine, new StringBuilder(" " + index));
  24.                         }
  25.                     }
  26.                 }
  27.             }
  28.  
  29.         } catch (IOException e) {
  30.             System.out.println(" Usage : <inputFile> <outputFile> ");
  31.         } finally {
  32.             PrintWriter writer = new PrintWriter(args[1], "UTF-8");
  33.             for (String keys : wordEntry.keySet()) {
  34.                 writer.print(keys + " " + wordEntry.get(keys) + wordIndex.get(keys) + "\n");
  35.             }
  36.             writer.close();
  37.         }
  38.     }
  39.  
  40.     public static class MyFileReader {
  41.         public InputStreamReader in;
  42.         private int size = 256;
  43.         private char[] buffer = new char[size];
  44.         private int curPos, curSize;
  45.  
  46.         public MyFileReader(String fileName) throws IOException {
  47.             in = new InputStreamReader(new FileInputStream(fileName), "utf-8");
  48.         }
  49.  
  50.         public void readChars() throws IOException {
  51.             curSize = in.read(buffer);
  52.             curPos = 0;
  53.         }
  54.  
  55.         public String nextLine() throws IOException {
  56.             if (curPos == curSize) {
  57.                 readChars();
  58.             }
  59.             StringBuilder line = new StringBuilder();
  60.             while (curPos < curSize && buffer[curPos] != '\n') {
  61.                 line.append(buffer[curPos]);
  62.                 curPos++;
  63.                 if (curPos == curSize) {
  64.                     readChars();
  65.                 }
  66.             }
  67.             curPos++;
  68.             return line.toString();
  69.         }
  70.  
  71.         public boolean hasNextLine() throws IOException {
  72.             if (curPos == curSize) {
  73.                 readChars();
  74.             }
  75.             return curSize > curPos;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement