diff --git a/src/main/java/com/epam/izh/rd/online/Main.java b/src/main/java/com/epam/izh/rd/online/Main.java index 3f661d6..e45564b 100644 --- a/src/main/java/com/epam/izh/rd/online/Main.java +++ b/src/main/java/com/epam/izh/rd/online/Main.java @@ -2,10 +2,10 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; public class Main { - public static void main(String[] args) throws IOException, URISyntaxException { - - } } 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..431bc96 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 @@ -23,7 +23,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { */ @Override public int countSumLengthOfWords(String text) { - return 0; + List arrayListWords = getWords(text); + int sumLengthOfWords = 0; + for (String word:arrayListWords) { + sumLengthOfWords += word.length(); + } + return sumLengthOfWords; } /** @@ -34,7 +39,7 @@ public int countSumLengthOfWords(String text) { */ @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -44,7 +49,8 @@ public int countNumberOfWords(String text) { */ @Override public int countNumberOfUniqueWords(String text) { - return 0; + + return getUniqueWords(text).size(); } /** @@ -57,7 +63,7 @@ public int countNumberOfUniqueWords(String text) { */ @Override public List getWords(String text) { - return emptyList(); + return Arrays.asList(text.split("\\W+")); } /** @@ -70,7 +76,8 @@ public List getWords(String text) { */ @Override public Set getUniqueWords(String text) { - return emptySet(); + + return new HashSet<>(getWords(text)) ; } /** @@ -82,7 +89,16 @@ public Set getUniqueWords(String text) { */ @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + Map mapNumberOfWordsRepetitions = new HashMap<>(); + List listWords = getWords(text); + for (String word: listWords) { + if (mapNumberOfWordsRepetitions.containsKey(word)) { + mapNumberOfWordsRepetitions.put(word, mapNumberOfWordsRepetitions.get(word)+1); + } else { + mapNumberOfWordsRepetitions.put(word, 1); + } + } + return mapNumberOfWordsRepetitions; } /** @@ -95,6 +111,26 @@ public Map countNumberOfWordsRepetitions(String text) { */ @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + + List listWordsByLength = getWords(text); + listWordsByLength.sort(new ComparatorWordsByLength()); + if (direction == Direction.DESC) { + Collections.reverse(listWordsByLength); + } + return listWordsByLength; + } + static class ComparatorWordsByLength implements Comparator { + + @Override + public int compare(String a, String b) { + if(a.length() > b.length()) { + return 1; + } else if(a.length()< b.length()) { + return -1; + } else { + return 0; + } + } + } } 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..6b2babb 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,48 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + + return getWords(text).stream().mapToInt((w) -> w.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) getUniqueWords(text).stream().count(); + } @Override public List getWords(String text) { - return emptyList(); + + return Arrays.stream(text.split("\\W+")).collect(Collectors.toList()); } @Override public Set getUniqueWords(String text) { - return emptySet(); + + return getWords(text).stream().collect(Collectors.toSet()); } @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + + return getUniqueWords(text).stream().collect(Collectors.toMap(wk->wk, + wc-> Collections.frequency(getWords(text),wc))); } @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + + return direction == Direction.ASC ? + getWords(text).stream().sorted((o1, o2) -> o1.length() - o2.length()).collect(Collectors.toList()) : + getWords(text).stream().sorted((o1, o2) -> o2.length() - o1.length()).collect(Collectors.toList()); } + }