diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..1d23afa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..86dcfd2 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6bd5763 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/ru/ifmo/cet/javabasics/WAPResult.java b/src/main/java/ru/ifmo/cet/javabasics/WAPResult.java index 7d4c2a7..f696dd2 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WAPResult.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WAPResult.java @@ -10,6 +10,9 @@ public class WAPResult { public final String result; public WAPResult() throws IOException { + result = readAllLines(Paths.get("src", "main","resources", "WAPResult.txt")).stream().collect(Collectors.joining("\n")); + + } } \ No newline at end of file diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index db16f2f..7320493 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,21 +1,34 @@ 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 java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.nio.file.Files.readAllLines; public class WarAndPeaceExercise { - public static String warAndPeace() { + 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"); - // TODO map lowercased words to its amount in text and concatenate its entries. - // TODO Iff 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 iff needed. - // TODO Also omit any word with lengths less than 4 and frequency less than 10 + String txt = readAllLines(tome12Path, Charset.forName("windows-1251")).stream().collect(Collectors.joining(" ")); + txt += readAllLines(tome34Path, Charset.forName("windows-1251")).stream().collect(Collectors.joining(" ")); + txt = txt.replaceAll("[^a-zA-Zа-яА-Я]", " ").toLowerCase(); - throw new UnsupportedOperationException(); - } + Map dict = new HashMap<>(); + Stream textStr = Stream.of(txt.split(" ")); + textStr.forEach((String key) -> dict.put(key, dict.containsKey(key) ? dict.get(key) + 1 : 1)); + List> sortedDictionary = new ArrayList<>(dict.entrySet()); + Collections.sort(sortedDictionary, (o1, o2) -> (o1.getValue().equals(o2.getValue())) ? o1.getKey().compareTo(o2.getKey()) : o2.getValue().compareTo(o1.getValue())); + return sortedDictionary.stream().filter(m -> (m.getKey().length() >= 4) && (m.getValue() >= 10)) + .map(m -> m.getKey() + " - " + m.getValue()).collect(Collectors.joining("\n")); + } } \ No newline at end of file