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..87cf3e5 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,7 +1,10 @@ package com.epam.izh.rd.online.service; import com.epam.izh.rd.online.helper.Direction; +import com.epam.izh.rd.online.helper.FileReaderService; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.*; import static java.util.Collections.*; @@ -21,9 +24,15 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer { * * @param text текст */ + @Override public int countSumLengthOfWords(String text) { - return 0; + ArrayList arrayList = (ArrayList) getWords(text); + int sum = 0; + for (String s : arrayList) { + sum += s.length(); + } + return sum; } /** @@ -32,9 +41,10 @@ public int countSumLengthOfWords(String text) { * * @param text текст */ + @Override public int countNumberOfWords(String text) { - return 0; + return getWords(text).size(); } /** @@ -42,9 +52,10 @@ public int countNumberOfWords(String text) { * Например для текста "One, two, three, three - one, tWo, tWo!!" - данный метод должен вернуть 5. * param text текст */ + @Override public int countNumberOfUniqueWords(String text) { - return 0; + return getUniqueWords(text).size(); } /** @@ -55,9 +66,13 @@ public int countNumberOfUniqueWords(String text) { * * @param text текст */ + @Override public List getWords(String text) { - return emptyList(); + String[] wordsStringArray = text.split("\\W"); + ArrayList words = new ArrayList<>(Arrays.asList(wordsStringArray)); + words.removeIf(String::isEmpty); + return words; } /** @@ -68,9 +83,10 @@ public List getWords(String text) { * * @param text текст */ + @Override public Set getUniqueWords(String text) { - return emptySet(); + return new TreeSet<>(getWords(text)); } /** @@ -80,9 +96,22 @@ public Set getUniqueWords(String text) { * * @param text текст */ + @Override public Map countNumberOfWordsRepetitions(String text) { - return emptyMap(); + Map hashMapForRepetitionCounting = new HashMap<>(); + ArrayList list = (ArrayList) getWords(text); + int count; + for (int i = 0; i < list.size(); i++) { + count = 0; + for (String s : list) { + if (list.get(i).equals(s)) { + count++; + } + } + hashMapForRepetitionCounting.put(list.get(i), count); + } + return hashMapForRepetitionCounting; } /** @@ -93,8 +122,15 @@ public Map countNumberOfWordsRepetitions(String text) { * * @param text текст */ + @Override public List sortWordsByLength(String text, Direction direction) { - return emptyList(); + ArrayList arrayList = (ArrayList) getWords(text); + if (direction == Direction.ASC) { + arrayList.sort(Comparator.comparingInt(String::length)); + } else { + arrayList.sort(Comparator.comparingInt(String::length).reversed()); + } + return arrayList; } } 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..a871b45 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,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 java.util.stream.Stream; import static java.util.Collections.*; @@ -16,36 +15,55 @@ public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer { @Override public int countSumLengthOfWords(String text) { - return 0; + return getWords(text).stream() + .mapToInt(String::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")) + .filter(x -> !x.equals("")) + .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 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 (direction == Direction.ASC) { + return getWords(text).stream() + .sorted(Comparator.comparingInt(String::length)) + .collect(Collectors.toList()); + } else { + return getWords(text).stream() + .sorted(Comparator.comparingInt(String::length).reversed()) + .collect(Collectors.toList()); + + } } } + diff --git a/src/main/java/com/epam/izh/rd/online/service/TextStatisticsAnalyzer.java b/src/main/java/com/epam/izh/rd/online/service/TextStatisticsAnalyzer.java index 5037bc5..a84e591 100644 --- a/src/main/java/com/epam/izh/rd/online/service/TextStatisticsAnalyzer.java +++ b/src/main/java/com/epam/izh/rd/online/service/TextStatisticsAnalyzer.java @@ -2,6 +2,8 @@ import com.epam.izh.rd.online.helper.Direction; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.List; import java.util.Map; import java.util.Set; @@ -14,7 +16,7 @@ public interface TextStatisticsAnalyzer { int countNumberOfUniqueWords(String text); - List getWords(String text); + List getWords(String text) throws IOException, URISyntaxException; Set getUniqueWords(String text); diff --git a/src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java b/src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java index a64df2f..084f2c4 100644 --- a/src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java +++ b/src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -78,7 +80,7 @@ void testSortWordsByLengthDesc() { @Test @DisplayName("Тест метода TextStatisticsAnalyzer.getWords(String text)") - void testGetWords() { + void testGetWords() throws IOException, URISyntaxException { assertListsContainSameElements(wordsList, simpleTextStatisticsAnalyzer.getWords(text)); } @@ -126,7 +128,7 @@ void testSortWordsByLengthDescStream() { @Test @DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.getWords(String text)") - void testGetWordsStream() { + void testGetWordsStream() throws IOException, URISyntaxException { assertListsContainSameElements(wordsList, streamApiTextStatisticsAnalyzer.getWords(text)); }