Apkawa

Untitled

Sep 30th, 2021
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class WordStatCount {
  5.     public static boolean isLetterOrDashOrApostrophe(char sym) {
  6.         return Character.isLetter(sym) || Character.getType(sym) == Character.DASH_PUNCTUATION || sym == '\'';
  7.     }
  8.  
  9.     public static void main(String[] files) {
  10.         try {
  11.             Map<String, Integer> words = new LinkedHashMap<>();
  12.             BufferedReader in = null;
  13.             try {
  14.                 in = new BufferedReader(
  15.                     new InputStreamReader(
  16.                         new FileInputStream(new File(files[0])),
  17.                         "UTF-8"
  18.                     )
  19.                 );
  20.                 int textSize = 0;
  21.                 for (String line = in.readLine(); line != null; line = in.readLine()) {
  22.                     for (int begin = 0; begin < line.length(); ++begin) {
  23.                         int end = begin;
  24.                         while (end < line.length() && isLetterOrDashOrApostrophe(line.charAt(end))) {
  25.                             ++end;
  26.                         }
  27.                         if (begin != end) {
  28.                             String word = line.substring(begin, end).toLowerCase();
  29.                             words.put(word, words.getOrDefault(word, 0) + 1);
  30.                             begin = end;
  31.                         }
  32.                     }
  33.                 }
  34.             } finally {
  35.                 if (in != null)
  36.                     in.close();
  37.             }
  38.  
  39.             BufferedWriter out = null;
  40.             try {
  41.                 out = new BufferedWriter(
  42.                     new OutputStreamWriter(
  43.                         new FileOutputStream(new File(files[1])),
  44.                         "UTF-8"
  45.                     )
  46.                 );
  47.                 int[][] sortedByCount = new int[words.size()][2];
  48.                 String[] text = new String[words.size()];
  49.                 int current = 0;
  50.                 for (Map.Entry<String, Integer> orderedWord : words.entrySet()) {
  51.                     text[current] = orderedWord.getKey();
  52.                     sortedByCount[current][0] = orderedWord.getValue();
  53.                     sortedByCount[current][1] = current;
  54.                     ++current;
  55.                 }
  56.                 Arrays.sort(sortedByCount, new Comparator<int[]>() {
  57.                     @Override
  58.                     public int compare(int[] o1, int[] o2) {
  59.                         return o1[0] - o2[0];
  60.                     }
  61.                 });
  62.                 for (int[] i : sortedByCount) {
  63.                     out.write(text[i[1]]);
  64.                     out.write(" " + i[0]);
  65.                     out.newLine();
  66.                 }
  67.             } finally {
  68.                 if (out != null)
  69.                     out.close();
  70.             }
  71.         } catch (IOException e) {
  72.             System.out.println(e.getMessage());
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment