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 @@ -23,7 +23,7 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
return text.replaceAll("\\W+", "").length();
}

/**
Expand All @@ -34,7 +34,7 @@ public int countSumLengthOfWords(String text) {
*/
@Override
public int countNumberOfWords(String text) {
return 0;
return getWords(text).size();
}

/**
Expand All @@ -44,7 +44,8 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
Set<String> stringSet = new HashSet<>(Arrays.asList(text.split("\\W+")));
return stringSet.size();
}

/**
Expand All @@ -57,7 +58,7 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
return Arrays.asList(text.split("\\W+"));
}

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

/**
Expand All @@ -82,7 +83,11 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map<String, Integer> countMap = new HashMap<>();
for (String word : getWords(text)) {
countMap.merge(word, 1, Integer::sum);
}
return countMap;
}

/**
Expand All @@ -95,6 +100,15 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> list = getWords(text);
switch (direction) {
case ASC:
list.sort(Comparator.comparingInt(String::length));
break;
case DESC:
list.sort(Comparator.comparingInt(String::length).reversed());
break;
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our static analysis engine detects that your pull request includes a change from an explicit import to a wildcard import. According to the best practice (http://www.javadude.com/articles/importondemandisevil.html), import star (import on demand) is a bad practice, wildcard import can sometimes cause name conflicts and ambiguities.

import java.util.stream.Collectors;

import static java.util.Collections.*;

Expand All @@ -16,36 +14,50 @@
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 new HashSet<>(getWords(text)).size();
}

@Override
public List<String> getWords(String text) {
return emptyList();
return Arrays.asList(text.split("\\W+"));
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
return new HashSet<>(getWords(text));
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
return getWords(text)
.stream()
.collect(Collectors.toMap(word -> word, word -> 1, Integer::sum));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
return getWords(text)
.stream()
.sorted(getCustomComparator(direction))
.collect(Collectors.toList());
}

private Comparator<String> getCustomComparator(Direction direction){
return direction==Direction.ASC ? Comparator.comparing(String::length)
: Comparator.comparing(String::length).reversed();
}
}