From d2168d5419111447bc79eb895f39c66592981a86 Mon Sep 17 00:00:00 2001 From: BogdanFito Date: Sun, 14 Oct 2018 21:34:51 +0300 Subject: [PATCH 1/3] task2 --- .../cet/javabasics/WarAndPeaceExercise.java | 73 ++++++++++++++++--- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 1ff35c2..5476910 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,21 +1,74 @@ package ru.ifmo.cet.javabasics; - + import java.io.IOException; + import java.nio.charset.Charset; + import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.*; 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"); - // 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 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"); - throw new UnsupportedOperationException(); - } + HashMap listing= new HashMap<>(); + + Charset charset = Charset.forName("windows-1251"); + + + List strings = Files.readAllLines(tome12Path, charset); + strings.addAll(Files.readAllLines(tome34Path, charset)); + for (String line: strings) { + line=line.toLowerCase(); + line=line.replaceAll("[^A-Za-z\u0410-\u042f\u0430-\u044f]", " "); + String[] parts=line.split(" "); + + + + for (String word:parts) { + if(word.length()<4) continue; + Integer Count; + Count=listing.get(word); + if (listing.get(word) == null) { + listing.put(word,1); + }else{ + listing.put(word,++Count); + } + } + } + for(Iterator> it=listing.entrySet().iterator();it.hasNext();) { + HashMap.Entry accession = it.next(); + if (accession.getValue() < 10) { + it.remove(); + } + } + //сортировка и отбор в алфавитном порядке + ArrayList Result= new ArrayList<>(); + while (!listing.isEmpty()){ + HashMap.Entry Max_entry=listing.entrySet().iterator().next(); + for (Map.Entry entry : listing.entrySet()) { + if (Max_entry.getValue() < entry.getValue()) { + Max_entry = entry; + } else if (Max_entry.getValue().equals(entry.getValue())) { + if (Max_entry.getKey().compareTo(entry.getKey()) > 0) { + Max_entry = entry; + } + } + } + String string=Max_entry.getKey()+ " - "+Max_entry.getValue(); + Result.add(string); + listing.remove(Max_entry.getKey()); + } + String result=""; + for (String s : Result) + { + result = result+s + "\n"; + } + result=result.substring(0,result.length()-1); + return result; + } } \ No newline at end of file From 3163e1c80302927546b7ac71163d361288317075 Mon Sep 17 00:00:00 2001 From: BogdanFito Date: Sun, 14 Oct 2018 21:57:03 +0300 Subject: [PATCH 2/3] task2 --- .../java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 5476910..69dd545 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -53,12 +53,13 @@ public static String warAndPeace() throws IOException { for (Map.Entry entry : listing.entrySet()) { if (Max_entry.getValue() < entry.getValue()) { Max_entry = entry; - } else if (Max_entry.getValue().equals(entry.getValue())) { - if (Max_entry.getKey().compareTo(entry.getKey()) > 0) { - Max_entry = entry; + } if ((Max_entry.getValue().equals(entry.getValue()))&&(Max_entry.getKey().compareTo(entry.getKey()) > 0)) + { + + Max_entry = entry; } } - } + String string=Max_entry.getKey()+ " - "+Max_entry.getValue(); Result.add(string); listing.remove(Max_entry.getKey()); From 4e7789892bf4f40e2fefb85ee6990bc8010170aa Mon Sep 17 00:00:00 2001 From: BogdanFito Date: Tue, 16 Oct 2018 21:37:08 +0300 Subject: [PATCH 3/3] task2 --- .../cet/javabasics/WarAndPeaceExercise.java | 95 +++++++------------ 1 file changed, 36 insertions(+), 59 deletions(-) diff --git a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java index 69dd545..6137a92 100644 --- a/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java +++ b/src/main/java/ru/ifmo/cet/javabasics/WarAndPeaceExercise.java @@ -1,75 +1,52 @@ package ru.ifmo.cet.javabasics; - import java.io.IOException; - import java.nio.charset.Charset; - import java.nio.file.Files; + import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.Files; +import java.io.IOException; +import java.nio.charset.Charset; import java.util.*; - 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"); - - HashMap listing= new HashMap<>(); - - Charset charset = Charset.forName("windows-1251"); - - - List strings = Files.readAllLines(tome12Path, charset); - strings.addAll(Files.readAllLines(tome34Path, charset)); - for (String line: strings) { - line=line.toLowerCase(); - line=line.replaceAll("[^A-Za-z\u0410-\u042f\u0430-\u044f]", " "); - String[] parts=line.split(" "); - - - - - for (String word:parts) { - if(word.length()<4) continue; - Integer Count; - Count=listing.get(word); - if (listing.get(word) == null) { - listing.put(word,1); - }else{ - listing.put(word,++Count); + final Charset charset = Charset.forName("windows-1251"); + List tome12 = Files.readAllLines(tome12Path, charset); + List tome34 = Files.readAllLines(tome34Path, charset); + List strings = new LinkedList(tome12); + Map map = new HashMap<>(); + strings.addAll(tome34); + for (String str : strings) { + str = str.replaceAll("[^a-zA-Zа-яА-Я]", " "); + for (String words : str.split(" ")) { + if (words.length() >= 4) { + if (map.containsKey(words.toLowerCase())) { + map.put(words.toLowerCase(), map.get(words.toLowerCase()) + 1); + } else { + map.put(words.toLowerCase(), 1); + } + } } } - } - for(Iterator> it=listing.entrySet().iterator();it.hasNext();) { - HashMap.Entry accession = it.next(); - if (accession.getValue() < 10) { - it.remove(); - } - } - //сортировка и отбор в алфавитном порядке - ArrayList Result= new ArrayList<>(); - while (!listing.isEmpty()){ - HashMap.Entry Max_entry=listing.entrySet().iterator().next(); - for (Map.Entry entry : listing.entrySet()) { - if (Max_entry.getValue() < entry.getValue()) { - Max_entry = entry; - } if ((Max_entry.getValue().equals(entry.getValue()))&&(Max_entry.getKey().compareTo(entry.getKey()) > 0)) - { - - Max_entry = entry; + Set> set = map.entrySet(); + List> list = new ArrayList>(set); + Collections.sort(list, new Comparator>() { + public int compare(Map.Entry record, Map.Entry record1) { + if (record.getValue().equals(record1.getValue())) { + return (record.getKey()).compareTo(record1.getKey()); + } else { + return (record1.getValue()).compareTo(record.getValue()); } } - - String string=Max_entry.getKey()+ " - "+Max_entry.getValue(); - Result.add(string); - listing.remove(Max_entry.getKey()); - } - String result=""; - for (String s : Result) - { - result = result+s + "\n"; + }); + String result = ""; + for (Map.Entry pairs : list) { + if (pairs.getValue() < 10) continue; + result += (pairs.getKey() + " - " + pairs.getValue() + "\n"); + } + return result.substring(0, result.length() - 1); } - result=result.substring(0,result.length()-1); - return result; } -} \ No newline at end of file