diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..eb6a95d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..0b89140 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml b/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml new file mode 100644 index 0000000..63b7b35 --- /dev/null +++ b/.idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_errorprone_error_prone_annotations_2_0_18.xml b/.idea/libraries/Gradle__com_google_errorprone_error_prone_annotations_2_0_18.xml new file mode 100644 index 0000000..eb64dcb --- /dev/null +++ b/.idea/libraries/Gradle__com_google_errorprone_error_prone_annotations_2_0_18.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_guava_guava_22_0.xml b/.idea/libraries/Gradle__com_google_guava_guava_22_0.xml new file mode 100644 index 0000000..4c947ec --- /dev/null +++ b/.idea/libraries/Gradle__com_google_guava_guava_22_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__com_google_j2objc_j2objc_annotations_1_1.xml b/.idea/libraries/Gradle__com_google_j2objc_j2objc_annotations_1_1.xml new file mode 100644 index 0000000..ab45264 --- /dev/null +++ b/.idea/libraries/Gradle__com_google_j2objc_j2objc_annotations_1_1.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_apache_commons_commons_lang3_3_4.xml b/.idea/libraries/Gradle__org_apache_commons_commons_lang3_3_4.xml new file mode 100644 index 0000000..f99c825 --- /dev/null +++ b/.idea/libraries/Gradle__org_apache_commons_commons_lang3_3_4.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml b/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml new file mode 100644 index 0000000..1a15019 --- /dev/null +++ b/.idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_codehaus_mojo_animal_sniffer_annotations_1_14.xml b/.idea/libraries/Gradle__org_codehaus_mojo_animal_sniffer_annotations_1_14.xml new file mode 100644 index 0000000..72ee118 --- /dev/null +++ b/.idea/libraries/Gradle__org_codehaus_mojo_animal_sniffer_annotations_1_14.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_2_0.xml b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_2_0.xml new file mode 100644 index 0000000..922f74f --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_2_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_2_0.xml b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_2_0.xml new file mode 100644 index 0000000..467a446 --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_2_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_2_0.xml b/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_2_0.xml new file mode 100644 index 0000000..dc731cc --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_2_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_2_0.xml b/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_2_0.xml new file mode 100644 index 0000000..22ae452 --- /dev/null +++ b/.idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_2_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_1_0.xml b/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_1_0.xml new file mode 100644 index 0000000..f54f419 --- /dev/null +++ b/.idea/libraries/Gradle__org_opentest4j_opentest4j_1_1_0.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fd2f27a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fcb603d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/osis_main.iml b/.idea/modules/osis_main.iml new file mode 100644 index 0000000..1ee7d3e --- /dev/null +++ b/.idea/modules/osis_main.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/osis_test.iml b/.idea/modules/osis_test.iml new file mode 100644 index 0000000..2da15c8 --- /dev/null +++ b/.idea/modules/osis_test.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/osis.iml b/osis.iml new file mode 100644 index 0000000..2f089cd --- /dev/null +++ b/osis.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ 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..4cba3af 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -2,20 +2,49 @@ import java.nio.file.Path; import java.nio.file.Paths; +import java.util.*; +import java.nio.charset.Charset; +import static java.nio.file.Files.readAllLines; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.io.IOException; 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"); + 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 + //throw new UnsupportedOperationException(); + Map dictionary = new HashMap<>(); + Charset set = Charset.forName("windows-1251"); + List book = readAllLines(tome12Path, set); + book.addAll(readAllLines(tome34Path, set)); + String text = book.toString(); + text = text.replaceAll("[^a-zA-Zа-яА-Я]", " ").toLowerCase(); + Stream.of(text.split(" ")) + .map(String::toString) + .filter(word -> word.length() > 3) + .forEach(word -> dictionary.put(word, dictionary.getOrDefault(word, 0) + 1)); + ArrayList> words = new ArrayList<>(dictionary.entrySet()); + words.sort(Comparator.comparing(Map.Entry::getKey)); + words.sort(Map.Entry.comparingByValue().reversed()); + String res = ""; + res = words.stream() + .filter(entry -> entry.getValue() > 9) + .map(entry -> entry.getKey() + " - " + entry.getValue()) + .collect(Collectors.joining("\n")); + + //throw new UnsupportedOperationException(); + return res.trim(); - // 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 - throw new UnsupportedOperationException(); } } \ No newline at end of file