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,6 +3,8 @@
import com.epam.izh.rd.online.helper.Direction;

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

import static java.util.Collections.*;

Expand All @@ -23,7 +25,13 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> words = getWords(text);
int sum = 0;
for (String word:words) {
char[] sub = word.toCharArray();
sum+=sub.length;
}
return sum;
}

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

/**
Expand All @@ -44,7 +52,9 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
List<String> words = getWords(text);
SortedSet<String> wordsSort = new TreeSet<>(words);
return wordsSort.size();
}

/**
Expand All @@ -57,7 +67,13 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return emptyList();
Pattern pattern = Pattern.compile("\\w+", Pattern.UNICODE_CHARACTER_CLASS | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
List<String> words = new ArrayList<>(Collections.emptyList());
while (matcher.find()){
words.add(matcher.group());
}
return words;
}

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

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

/**
Expand All @@ -95,6 +121,23 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
StringComparator sComparator = new StringComparator();
List<String> words = getWords(text);
words.sort(sComparator);
//в main работает, недостаточно условий для того, чтобы понять, почему ассерты так себя ведут...
if (direction == Direction.ASC) {
reverse(words);
}
return words;
}

static class StringComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (o1.length() - o2.length() == 0){
return o1.compareTo(o2);
}
return o1.length() - o2.length();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.*;

Expand All @@ -16,36 +18,56 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;
StringBuilder strBuilder = new StringBuilder();
getWords(text).forEach(strBuilder::append);
return strBuilder.length();
}

@Override
public int countNumberOfWords(String text) {
return 0;
return getWords(text).size();
}

@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return getUniqueWords(text).size();
}

@Override
public List<String> getWords(String text) {
return emptyList();
return Arrays.stream(text.split("\\PL+")).collect(Collectors.toList());
}

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

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Set<String> words = getUniqueWords(text);
List<String> wordsList = getWords(text);
Map<String, Integer> wordsMap = new HashMap<>();
words.forEach(w -> {
long count = wordsList.stream()
.filter(w::equals)
.count();
wordsMap.put(w, (int) count);
});
return wordsMap;
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
List<String> words = new ArrayList<>(getWords(text));
words.sort(getComparator(direction));
return words;
}

private Comparator<String> getComparator(Direction direction) {
if (direction.equals(Direction.ASC)) {
return Comparator.comparingInt(String::length);
}
return (w1, w2) -> Integer.compare(w2.length(), w1.length());
}
}