Advertisement
desislava_topuzakova

6. Word Count

Jan 22nd, 2022
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package StreamsFilesDirectories_Exercise;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. public class WordCount_06 {
  13.     public static void main(String[] args) throws IOException {
  14.         //words.txt -> думи, които ще броим в друг файл
  15.         String pathWords = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\words.txt";
  16.         //дума -> бр. срещанията
  17.         Map<String, Integer> countWords = new HashMap<>();
  18.  
  19.         //1. прочетем всички думи от файла и всяка една дума я съхраним в мап
  20.         List<String> allLinesWithWords = Files.readAllLines(Path.of(pathWords));
  21.         for (String lineWithWords : allLinesWithWords) {
  22.             //lineWithWords = "of which The".split("\\s+") -> ["of", "which", "The"]
  23.             Arrays.stream(lineWithWords.split("\\s+")).forEach(
  24.                     word -> {
  25.                         countWords.put(word, 0);
  26.                     }
  27.             );
  28.         }
  29.  
  30.         //countWords -> кои са думите, които ще търсим
  31.  
  32.         //2. прочитаме файла, в който ще търсим
  33.         String path = "C:\\Users\\I353529\\Desktop\\04. Java-Advanced-Files-and-Streams-Exercises-Resources\\text.txt";
  34.         //обхождаме всеки един ред -> взимаме всички думи на всеки ред -> проверяваме дали думата трябва да я преброим (само буквите, които са в мапа)
  35.         List<String> allLines = Files.readAllLines(Path.of(path));
  36.         for (String line : allLines) {
  37.             //line = "There are many variations".split("\\s+") -> []
  38.             Arrays.stream(line.split("\\s+")).forEach(word -> {
  39.                         if (countWords.containsKey(word)) {
  40.                             int currentCount = countWords.get(word);
  41.                             countWords.put(word, currentCount + 1);
  42.                         }
  43.                     }
  44.             );
  45.  
  46.         }
  47.  
  48.         //знаем коя буква колко пъти се среща
  49.         //3. принтираме резултата
  50.         //сортираме по value
  51.         //запис: ключ(дума) -> value(бр. срещанията)
  52.         PrintWriter writer = new PrintWriter("results.txt");
  53.         countWords.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
  54.                 .forEach(entry -> writer.println(entry.getKey() + " - " + entry.getValue()));
  55.         writer.close();
  56.         //compareTo -> 0 (първото == второто), 1 (първото > второто), -1 (второто > първото)
  57.         //sorted -> 0 (не разменя местата на записите), 1 (разменя местата на първото и второто), -1 (не разменя местата на записите)
  58.  
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement