diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 1ff35c2..d79041f 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,21 +1,65 @@ package ru.ifmo.cet.javabasics; +import java.io.IOException; + +import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; - +import java.util.*; +import static java.nio.file.Files.readAllLines; public class WarAndPeaceExercise { - public static String warAndPeace() { - final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt"); - final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt"); + private static class Node implements Comparable{ + + private String key; + private Integer value; - // TODO map lowercased words to its amount in text and concatenate its entries. - // TODO If word "котик" occurred in text 23 times then its entry would be "котик - 23\n". - // TODO Entries in final String should be also sorted by amount and then in alphabetical order if needed. - // TODO Also omit any word with lengths less than 4 and frequency less than 10 + public Node(String key, Integer value) { + this.key = key; + this.value = value; + } - throw new UnsupportedOperationException(); + @Override + public int compareTo(Node node) { + if(node.value.equals(this.value)) return this.key.compareTo(node.key); + return node.value - this.value; + } + } + + public static String warAndPeace() throws IOException { + + final Path tome12Path = Paths.get("src", "main", "resources", "WAP12.txt"); + final Path tome34Path = Paths.get("src", "main", "resources", "WAP34.txt"); + HashMap map = new HashMap<>(); + Charset set = Charset.forName("windows-1251"); + List lines = readAllLines(tome12Path, set); + lines.addAll(readAllLines(tome34Path, set)); + for (String line : lines) { + line = line.replaceAll("[^a-zA-Zа-яА-Я]", " "); + for (String word : line.split(" ")) { + if (word.length() >= 4) { + word = word.toLowerCase(); + if (map.containsKey(word)) { + map.put(word, map.get(word) + 1); + } else { + map.put(word, 1); + } + } + } + } + ArrayList nodes = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()){ + nodes.add(new Node(entry.getKey(), entry.getValue())); + } + StringBuilder res = new StringBuilder(); + Collections.sort(nodes); + for (Node node : nodes){ + if(node.value >= 10) { + res.append(node.key).append(" - ").append(node.value).append("\n"); + } + } + return res.toString().substring(0, res.length()-1); } } \ No newline at end of file