From 3cb31c437f8d7bde709459a4368f9fa045b9cfe6 Mon Sep 17 00:00:00 2001 From: BogdanFito Date: Wed, 17 Oct 2018 16:00:54 +0300 Subject: [PATCH 1/2] task3 --- .../cet/javabasics/WarAndPeaceExercise.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 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..290bad9 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.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.*; +import java.util.stream.Collectors; +import static java.util.Arrays.asList; 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"); + final Charset charset = Charset.forName("windows-1251"); + List lines = Files.readAllLines(tome12Path, charset); + lines.addAll(Files.readAllLines(tome34Path, charset)); + List content = new ArrayList<>(); + lines.stream().map(s->s.toLowerCase()).map(s -> s.replaceAll("[^a-zа-я]", " ")).map(replace -> asList(replace.split(" "))).forEach(list-> content.addAll(list)); + List content1=content.stream().filter(s->s.length()>=4).collect(Collectors.toList()); + Map words=new LinkedHashMap<>(); + content1.forEach(key -> { + words.put(key, words.containsKey(key) ? words.get(key) + 1 : 1); + }); words.entrySet().removeIf(entires->entires.getValue()<10); + List> sort_words = new ArrayList(words.entrySet()); + sort_words.sort(Map.Entry.comparingByKey()); + sort_words.sort(Map.Entry.comparingByValue().reversed()); + String res=sort_words.stream().map(entry->entry.getKey() + " - " + entry.getValue()).collect(Collectors.joining("\n")); + return res; + + + + + + - // 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 b51f27dcbf7cfe93acc1c46e46a51db09bc9f8fc Mon Sep 17 00:00:00 2001 From: BogdanFito Date: Wed, 17 Oct 2018 16:04:04 +0300 Subject: [PATCH 2/2] task3 --- src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 290bad9..5120c72 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -13,7 +13,7 @@ public class WarAndPeaceExercise { - public static String warAndPeace() throws IOException { + 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"); final Charset charset = Charset.forName("windows-1251");