Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.epam.izh.rd.online.helper.Direction;

import java.util.*;

import static java.util.Collections.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Совет:
Expand All @@ -16,14 +16,20 @@
public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {

/**
* Необходимо реализовать функционал подсчета суммарной длины всех слов (пробелы, знаким препинания итд не считаются).
* Например для текста "One, I - tWo!!" - данный метод должен вернуть 7.
* Необходимо реализовать функционал подсчета суммарной длины всех слов (пробелы, знаки препинания и т.д. не считаются).
* Например, для текста "One, I - tWo!!" - данный метод должен вернуть 7.
*
* @param text текст
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
final List<String> listOfWords = getWords(text);
Iterator<String> iterateInListOfWords = listOfWords.iterator();
int lengthOfAllWordsInText = 0;
while (iterateInListOfWords.hasNext()) {
lengthOfAllWordsInText += iterateInListOfWords.next().length();
}
return lengthOfAllWordsInText;
}

/**
Expand All @@ -34,30 +40,39 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
final List<String> listOfWords = getWords(text);
return listOfWords.size();
}

/**
* Необходимо реализовать функционал подсчета количества уникальных слов в тексте (с учетом регистра).
* Например для текста "One, two, three, three - one, tWo, tWo!!" - данный метод должен вернуть 5.
* Например, для текста "One, two, three, three - one, tWo, tWo!!" - данный метод должен вернуть 5.
* param text текст
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
final List<String> listOfWords = getWords(text);
HashSet<String> onlyUniqueWords = new HashSet<>(listOfWords);
return onlyUniqueWords.size();
}

/**
* Необходимо реализовать функционал получения списка слов из текста.
* Пробелы, запятые, точки, кавычки и другие знаки препинания являются разделителями слов.
* Например для текста "One, two, three, three - one, tWo, tWo!!" должен вернуться список :
* Например для текста "One, two, three, three - one, tWo, tWo!!" должен вернуться список:
* {"One", "two", "three", "three", "one", "tWo", "tWo"}
*
* @param text текст
*/
@Override
public List<String> getWords(String text) {
return emptyList();
final Pattern patternFindWordForWord = Pattern.compile("\\w+");
final Matcher matcherFindWordForWord = patternFindWordForWord.matcher(text);
final List<String> listOfWords = new ArrayList<>();
while (matcherFindWordForWord.find()) {
listOfWords.add(matcherFindWordForWord.group());
}
return listOfWords;
}

/**
Expand All @@ -70,7 +85,9 @@ public List<String> getWords(String text) {
*/
@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
final List<String> listOfWords = getWords(text);
HashSet<String> onlyUniqueWords = new HashSet<>(listOfWords);
return onlyUniqueWords;
}

/**
Expand All @@ -82,7 +99,16 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
final List<String> listOfWords = getWords(text);
Map<String, Integer> resultMap = new HashMap<>();
for (String elementOfList : listOfWords) {
if (resultMap.containsKey(elementOfList)) {
resultMap.put(elementOfList, resultMap.get(elementOfList) + 1);
} else {
resultMap.put(elementOfList, 1);
}
}
return resultMap;
}

/**
Expand All @@ -95,6 +121,17 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
final List<String> listOfWords = getWords(text);
List<String> ascDesc = listOfWords;
Collections.sort(ascDesc, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
if (direction == Direction.DESC) {
Collections.reverse(ascDesc);
}
return ascDesc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.*;

/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
Expand All @@ -16,36 +15,54 @@
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 getWords(text)
.size();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return getWords(text)
.stream()
.distinct()
.collect(Collectors.toList())
.size();
}

@Override
public List<String> getWords(String text) {
return emptyList();
final List<String> listOfWords = Stream.of(text.split("\\W+"))
.collect(Collectors.toList());
return listOfWords;
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
return Stream.of(getWords(text))
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
return Stream.of(getWords(text))
.flatMap(Collection::stream)
.collect(Collectors.toMap(Function.identity(), v -> 1, Integer::sum));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
Comparator<String> comparator = Comparator.comparingInt(String::length);
return getWords(text)
.stream()
.sorted((direction.equals(Direction.ASC) ? comparator : comparator.reversed()))
.collect(Collectors.toList());
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static List<String> readWordsFromProperties() {
List<String> result = new ArrayList<>();

for (Map.Entry<Object, Object> entry : properties.entrySet()) {
int wordRepetition = Integer.valueOf(entry.getValue().toString());
int wordRepetition = Integer.parseInt(entry.getValue().toString());
String word = entry.getKey().toString();

if (wordRepetition == 1) {
Expand Down