Advertisement
greedydev

HW11

May 1st, 2022 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. public class App {
  6.  
  7.     public static void main(String[] args) throws Exception {
  8.         File directoryPath = new File("in/");
  9.         String[] files = directoryPath.list();
  10.         assert files != null;
  11.  
  12.         if (files.length < 10) {
  13.             throw new NotEnoughFilesException("Not enough files in the directory");
  14.         }
  15.         for (var fileName : files) {
  16.             File file = new File("in/" + fileName);
  17.             if (file.length() < 150 * 1024) {
  18.                 throw new FileTooSmallException("File " + fileName + " is too small");
  19.             }
  20.         }
  21.  
  22.         LinkedHashMap<String, Integer> dict = new LinkedHashMap<>();
  23.  
  24.         for (var fileName : files) {
  25.             File file = new File("in/" + fileName);
  26.             BufferedReader reader = new BufferedReader(new FileReader(file));
  27.             String st;
  28.             StringBuilder contents = new StringBuilder();
  29.             while ((st = reader.readLine()) != null) {
  30.                 contents.append(st).append("\n");
  31.             }
  32.             String[] words = contents.toString().split("\\s");
  33.  
  34.             for (var word : words) {
  35.                 if (word.length() > 0) {
  36.                     String fmt = word.replaceAll("[^\\w\\p{sc=Cyrillic}]+", "").toLowerCase();
  37.                     int count;
  38.                     if (dict.containsKey(fmt)) {
  39.                         count = dict.get(fmt) + 1;
  40.                     } else {
  41.                         count = 1;
  42.                     }
  43.                     dict.put(fmt, count);
  44.                 }
  45.             }
  46.         }
  47.  
  48.         LinkedHashMap<String, Integer> sortedDict = dict.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  49.  
  50.         int wordCount = sortedDict.values().stream().mapToInt(i -> i).sum();
  51.         int termCount = sortedDict.size();
  52.  
  53.         File file = new File("dict.txt");
  54.         BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  55.         for (var entry : sortedDict.entrySet()) {
  56.             writer.write(entry.getKey() + " -- " + entry.getValue() + "\n");
  57.         }
  58.         writer.flush();
  59.         long size = file.length();
  60.  
  61.         System.out.printf("Dictionary size: %.2f KB\n", (double) size / 1024.0);
  62.         System.out.println("Word count: " + wordCount);
  63.         System.out.println("Term count: " + termCount);
  64.  
  65.         writer.close();
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement