From cc7ad6ea4879fe44baa564073271435c5b386ee5 Mon Sep 17 00:00:00 2001 From: Lyubov Date: Mon, 8 Oct 2018 14:30:09 +0300 Subject: [PATCH 1/2] to fix --- .../cet/javabasics/WarAndPeaceExercise.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index db16f2f..9c885d3 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -2,20 +2,43 @@ 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(){ - 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 + Map dictionary = new HashMap<>(); + + throw new UnsupportedOperationException(); + + String book = String.join(" ",readAllLines(tome12Path, Charset.forName("windows-1251"))); + book+=String.join(" ",readAllLines(tome34Path, Charset.forName("windows-1251"))); + book=book.replaceAll("[^a-zA-Zа-яА-Я]", " ").toLowerCase(); + Stream words = Stream.of(book.split(" ")); + words.filter(word->word.length()>=4); + words.forEach((String key) -> dictionary.put(key, dictionary.containsKey(key) ? dictionary.get(key) + 1 : 1)); + List> res = new ArrayList<>(dictionary.entrySet()); + res.sort((entry1, entry2)->(entry1.getValue().equals(entry2.getValue())) ? + entry1.getKey().compareTo(entry2.getKey()) : entry2.getValue().compareTo(entry1.getValue())); + String result = res.stream().filter(c->c.getValue()>=10) + .map(a -> a.getKey() + " - " + a.getValue()).map(Object::toString); + return result; - // 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 From fa84f61fa56fd802880a1dfcf994b2e92a9bee1d Mon Sep 17 00:00:00 2001 From: Lyubov Date: Wed, 23 Jan 2019 22:42:04 +0300 Subject: [PATCH 2/2] new --- .idea/compiler.xml | 9 +++++ .idea/gradle.xml | 18 +++++++++ ..._com_google_code_findbugs_jsr305_1_3_9.xml | 9 +++++ ...orprone_error_prone_annotations_2_0_18.xml | 11 ++++++ .../Gradle__com_google_guava_guava_22_0.xml | 11 ++++++ ...m_google_j2objc_j2objc_annotations_1_1.xml | 11 ++++++ ...__org_apache_commons_commons_lang3_3_4.xml | 11 ++++++ ..._org_apiguardian_apiguardian_api_1_0_0.xml | 11 ++++++ ...s_mojo_animal_sniffer_annotations_1_14.xml | 11 ++++++ ..._junit_jupiter_junit_jupiter_api_5_2_0.xml | 11 ++++++ ...nit_jupiter_junit_jupiter_engine_5_2_0.xml | 11 ++++++ ..._platform_junit_platform_commons_1_2_0.xml | 11 ++++++ ...t_platform_junit_platform_engine_1_2_0.xml | 11 ++++++ ...radle__org_opentest4j_opentest4j_1_1_0.xml | 11 ++++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 10 +++++ .idea/modules/osis_main.iml | 19 ++++++++++ .idea/modules/osis_test.iml | 26 +++++++++++++ .idea/vcs.xml | 6 +++ osis.iml | 13 +++++++ .../cet/javabasics/WarAndPeaceExercise.java | 38 +++++++++++-------- 21 files changed, 259 insertions(+), 16 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/libraries/Gradle__com_google_code_findbugs_jsr305_1_3_9.xml create mode 100644 .idea/libraries/Gradle__com_google_errorprone_error_prone_annotations_2_0_18.xml create mode 100644 .idea/libraries/Gradle__com_google_guava_guava_22_0.xml create mode 100644 .idea/libraries/Gradle__com_google_j2objc_j2objc_annotations_1_1.xml create mode 100644 .idea/libraries/Gradle__org_apache_commons_commons_lang3_3_4.xml create mode 100644 .idea/libraries/Gradle__org_apiguardian_apiguardian_api_1_0_0.xml create mode 100644 .idea/libraries/Gradle__org_codehaus_mojo_animal_sniffer_annotations_1_14.xml create mode 100644 .idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_api_5_2_0.xml create mode 100644 .idea/libraries/Gradle__org_junit_jupiter_junit_jupiter_engine_5_2_0.xml create mode 100644 .idea/libraries/Gradle__org_junit_platform_junit_platform_commons_1_2_0.xml create mode 100644 .idea/libraries/Gradle__org_junit_platform_junit_platform_engine_1_2_0.xml create mode 100644 .idea/libraries/Gradle__org_opentest4j_opentest4j_1_1_0.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/modules/osis_main.iml create mode 100644 .idea/modules/osis_test.iml create mode 100644 .idea/vcs.xml create mode 100644 osis.iml 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 9c885d3..4cba3af 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -11,9 +11,9 @@ 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"); @@ -21,22 +21,28 @@ public static String warAndPeace() throws IOException { // 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<>(); - - throw new UnsupportedOperationException(); - - String book = String.join(" ",readAllLines(tome12Path, Charset.forName("windows-1251"))); - book+=String.join(" ",readAllLines(tome34Path, Charset.forName("windows-1251"))); - book=book.replaceAll("[^a-zA-Zа-яА-Я]", " ").toLowerCase(); - Stream words = Stream.of(book.split(" ")); - words.filter(word->word.length()>=4); - words.forEach((String key) -> dictionary.put(key, dictionary.containsKey(key) ? dictionary.get(key) + 1 : 1)); - List> res = new ArrayList<>(dictionary.entrySet()); - res.sort((entry1, entry2)->(entry1.getValue().equals(entry2.getValue())) ? - entry1.getKey().compareTo(entry2.getKey()) : entry2.getValue().compareTo(entry1.getValue())); - String result = res.stream().filter(c->c.getValue()>=10) - .map(a -> a.getKey() + " - " + a.getValue()).map(Object::toString); - return result; + 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(); }