From 861aac51da22b771543d8609234eecb732b45ac1 Mon Sep 17 00:00:00 2001 From: Roman <1992roman@list.ru> Date: Sun, 28 Jun 2020 21:06:29 +0300 Subject: [PATCH] all methods completed --- .../service/SimpleTextStatisticsAnalyzer.java | 54 ++++++++++++++++--- .../StreamApiTextStatisticsAnalyzer.java | 46 ++++++++++++---- 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java index 32f8e35..cad67bc 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleTextStatisticsAnalyzer.java @@ -3,6 +3,8 @@ import com.epam.izh.rd.online.helper.Direction; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.Collections.*; @@ -23,7 +25,14 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + int count = 0; + List words = getWords(text); + for (String word : words + ) { + count += word.length(); + + } + return count; } /** @@ -34,7 +43,7 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -44,7 +53,7 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + return getUniqueWords(text).size(); } /** @@ -57,7 +66,10 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + if (text.length() == 0) { + return emptyList(); + } + return Arrays.asList(text.split("\\W+")); } /** @@ -70,7 +82,7 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + return new HashSet<>(getWords(text)); } /** @@ -82,7 +94,17 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + List words = getWords(text); + Map res = new HashMap<>(); + + for (String word : words) { + if (res.containsKey(word)) { + res.computeIfPresent(word, (k, v) -> v + 1); + } else { + res.put(word, 1); + } + } + return res; } /** @@ -95,6 +117,24 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + + Comparator comparator = new Comparator() { + @Override + public int compare(String o1, String o2) { + return o1.length() - o2.length(); + } + }; + + List res = getWords(text); + res.sort(comparator); + + if (direction.toString().equals("DESC")) { + res.sort(comparator.reversed()); + System.out.println(res); + } + + return res; + + } } diff --git a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java index e9b8957..6e04cae 100644 --- a/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/StreamApiTextStatisticsAnalyzer.java @@ -2,10 +2,8 @@ import com.epam.izh.rd.online.helper.Direction; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; import static java.util.Collections.*; @@ -16,36 +14,62 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + return getWords(text).stream().mapToInt(el -> el.length()).sum(); } @Override public int countNumberOfWords(String text) { - return 0; + return (int) getWords(text).stream().count(); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + return (int) getWords(text).stream().distinct().count(); } @Override public List getWords(String text) { - return emptyList(); + if (text.length() == 0) { + return emptyList(); + } + return Arrays.asList(text.split("\\W+")); } @Override public Set getUniqueWords(String text) { - return emptySet(); + return getWords(text).stream().distinct().collect(Collectors.toSet()); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + return getWords(text).stream().collect(Collectors.toMap(el -> el, el -> 1, Integer::sum)); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + Comparator comparator = new Comparator() { + @Override + public int compare(String o1, String o2) { + return o1.length() - o2.length(); + } + }; + + if (direction.toString().equals("DESC")) { + return getWords(text).stream().sorted(comparator.reversed()).collect(Collectors.toList()); + } + return getWords(text).stream().sorted(comparator).collect(Collectors.toList()); } } + + + + + + + + + + + + +