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..cf3400f 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 @@ -1,9 +1,8 @@ package com.epam.izh.rd.online.service; import com.epam.izh.rd.online.helper.Direction; - import java.util.*; - +import static com.epam.izh.rd.online.helper.Direction.ASC; import static java.util.Collections.*; /** @@ -23,7 +22,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + int sumLengthOfWords = 0; + for (String word : getWords(text)) { + sumLengthOfWords += word.length(); + } + return sumLengthOfWords; } /** @@ -34,7 +38,8 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + return getWords(text).size(); } /** @@ -44,7 +49,8 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + return getUniqueWords(text).size(); } /** @@ -57,7 +63,14 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + if (text == null || text.length() == 0) return emptyList(); + List words = new ArrayList<>(); + for (String word : text.split("\\W")) { + if (!word.isEmpty()) { + words.add(word); + } + } + return words; } /** @@ -70,7 +83,8 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + if (text == null || text.length() == 0) return emptySet(); + return new HashSet<>(getWords(text)); } /** @@ -82,7 +96,13 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + if (text == null || text.length() == 0) return emptyMap(); + Map wordRepeats = new HashMap<>(); + for (String word :getWords(text)) { + wordRepeats.putIfAbsent(word, 0); + wordRepeats.put(word, wordRepeats.get(word) + 1); + } + return wordRepeats; } /** @@ -95,6 +115,9 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + if (text == null || text.length() == 0) return emptyList(); + List wordsByLength = new ArrayList<>(getWords(text)); + wordsByLength.sort((o1, o2) -> direction == ASC ? o1.length() - o2.length() : o2.length() - o1.length()); + return wordsByLength; } } 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..3cd828a 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,11 +2,9 @@ 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 com.epam.izh.rd.online.helper.Direction.ASC; import static java.util.Collections.*; /** @@ -14,38 +12,52 @@ * {@link SimpleTextStatisticsAnalyzer}. */ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { + @Override public int countSumLengthOfWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + return getWords(text).stream().mapToInt(String::length).reduce(Integer::sum).orElse(0); } @Override public int countNumberOfWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + return getWords(text).size(); } @Override public int countNumberOfUniqueWords(String text) { - return 0; + if (text == null || text.length() == 0) return 0; + return getUniqueWords(text).size(); } @Override public List getWords(String text) { - return emptyList(); + if (text == null || text.length() == 0) return emptyList(); + return Arrays.stream(text.split("\\W")).filter(s -> !s.isEmpty()).collect(Collectors.toList()); } @Override public Set getUniqueWords(String text) { - return emptySet(); + if (text == null || text.length() == 0) return emptySet(); + return new HashSet<>(getWords(text)); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + if (text == null || text.length() == 0) return emptyMap(); + return getWords(text).stream().collect(Collectors.toMap( + s -> s, + s -> 1, + (a, b) -> a + 1 + ) + ); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + if (text == null || text.length() == 0) return emptyList(); + return getWords(text).stream().sorted((o1, o2) -> direction == ASC ? o1.length() - o2.length() : o2.length() - o1.length()) + .collect(Collectors.toList()); } }