Advertisement
SchrodZzz

specificName

Nov 5th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.ArrayList;
  4. import java.util.Map;
  5. import java.util.TreeMap;
  6.  
  7. public class WordStatLineIndex {
  8.     public static void main(String[] args) throws IOException {
  9.  
  10.         Map <String, ArrayList <AdditionalPair <Integer, Integer>>> wordStatistic
  11.                 = new TreeMap <>();
  12.  
  13.         try (FastScanner reader = new FastScanner(
  14.                 new FileInputStream(args[0]), "UTF-8")) {
  15.  
  16.             int lineCounter = 0;
  17.             while (reader.hasNext()) {
  18.                 int inLineWordsCounter = 0;
  19.                 lineCounter++;
  20.                 while (reader.onLine()) {
  21.                     String currentWord = reader.nextWord();
  22.                     if (!currentWord.isEmpty()) {
  23.                         inLineWordsCounter++;
  24.                         if (!wordStatistic.containsKey(currentWord)) {
  25.                             wordStatistic.put(currentWord, new ArrayList <>());
  26.                         }
  27.                         wordStatistic.get(currentWord).add(new AdditionalPair <>(lineCounter, inLineWordsCounter));
  28.                     }
  29.                 }
  30.             }
  31.  
  32.             try (BufferedWriter writer = new BufferedWriter(
  33.                     new OutputStreamWriter(
  34.                             new FileOutputStream(args[1]),
  35.                             "UTF-8"))) {
  36.  
  37.                 for (Map.Entry <String, ArrayList <AdditionalPair <Integer, Integer>>> currentWordStatisticPair
  38.                         : wordStatistic.entrySet()) {
  39.  
  40.                     writer.write(currentWordStatisticPair.getKey() + " ");
  41.                     writer.write(currentWordStatisticPair.getValue().size() + "");
  42.  
  43.                     for (AdditionalPair <Integer, Integer> currentWordStatistic
  44.                             : currentWordStatisticPair.getValue()) {
  45.  
  46.                         writer.write(" " + currentWordStatistic.getFirstElement());
  47.                         writer.write(":" + currentWordStatistic.getSecondElement());
  48.                     }
  49.  
  50.                     writer.write("\n");
  51.                 }
  52.             } catch (IOException ex) {
  53.                 System.out.println("sorry, but program can't output result \n Try to check args correctness");
  54.             }
  55.         } catch (FileNotFoundException ex) {
  56.             System.out.println("sorry, but program can't find the input file OwO");
  57.         } catch (IndexOutOfBoundsException ex) {
  58.             System.out.println("pls, enter args correctly (Example: \"input.txt\" \"output.txt\")");
  59.         }
  60.     }
  61. }
  62.  
  63. // Also we can use String ArrayList instead of AdditionalPair, but AdditionalPair solution is more pretty, I think.
  64.  
  65. import java.io.FileInputStream;
  66. import java.io.IOException;
  67. import java.io.InputStreamReader;
  68.  
  69. public class FastScanner implements AutoCloseable {
  70.  
  71.     private char currentChar;
  72.     private final InputStreamReader stream;
  73.     private static final byte END_OF_FILE = -1;
  74.     private static final char NEW_LINE = '\n';
  75.     private static final char APOSTROPHE = '\'';
  76.  
  77.     FastScanner(FileInputStream stream, String charsetName) throws IOException {
  78.         this.stream = new InputStreamReader(stream, charsetName);
  79.     }
  80.  
  81.     private void readNextChar() throws IOException {
  82.         currentChar = (char) stream.read();
  83.     }
  84.  
  85.     boolean hasNext() throws IOException {
  86.         readNextChar();
  87.         return (byte) currentChar != END_OF_FILE;
  88.     }
  89.  
  90.     boolean onLine() {
  91.         return currentChar != NEW_LINE
  92.                 && (byte) currentChar != END_OF_FILE;
  93.     }
  94.  
  95.     String nextWord() throws IOException {
  96.         StringBuilder currentWord = new StringBuilder();
  97.         while (!charIsPartOfWord(currentChar)) {
  98.             readNextChar();
  99.             if (!onLine()) {
  100.                 break;
  101.             }
  102.         }
  103.         while (charIsPartOfWord(currentChar)) {
  104.             currentWord.append(currentChar);
  105.             readNextChar();
  106.         }
  107.         return currentWord.toString().toLowerCase();
  108.     }
  109.  
  110.     private boolean charIsPartOfWord(char symbol) {
  111.         return Character.isLetter(symbol)
  112.                 || Character.getType(symbol) == Character.DASH_PUNCTUATION
  113.                 || symbol == APOSTROPHE;
  114.     }
  115.  
  116.     public void close(){}
  117. }
  118.  
  119. class AdditionalPair<FirstDataType, SecondDataType> {
  120.  
  121.     private final FirstDataType first;
  122.     private final SecondDataType second;
  123.  
  124.     AdditionalPair(FirstDataType first, SecondDataType second) {
  125.         this.first = first;
  126.         this.second = second;
  127.     }
  128.  
  129.     FirstDataType getFirstElement() {
  130.         return first;
  131.     }
  132.  
  133.     SecondDataType getSecondElement() {
  134.         return second;
  135.     }
  136. }
  137.  
  138. //pair in javaFX 2.0 +
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement