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,14 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
int count = 0;
List<String> words = getWords(text);
for (String word : words
) {
count += word.length();

}
return count;
}

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

/**
Expand All @@ -44,7 +53,7 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;
return getUniqueWords(text).size();
}

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

/**
Expand All @@ -70,7 +82,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 +94,17 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
List<String> words = getWords(text);
Map<String, Integer> res = new HashMap<>();

for (String word : words) {
if (res.containsKey(word)) {
res.computeIfPresent(word, (k, v) -> v + 1);
} else {
res.put(word, 1);
}
}
return res;
}

/**
Expand All @@ -95,6 +117,24 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();

Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
};

List<String> res = getWords(text);
res.sort(comparator);

if (direction.toString().equals("DESC")) {
res.sort(comparator.reversed());
System.out.println(res);
}

return res;


}
}
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.*;
import java.util.stream.Collectors;

import static java.util.Collections.*;

Expand All @@ -16,36 +14,62 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;
return getWords(text).stream().mapToInt(el -> el.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) getWords(text).stream().distinct().count();
}

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

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();
return getWords(text).stream().distinct().collect(Collectors.toSet());
}

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

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
};

if (direction.toString().equals("DESC")) {
return getWords(text).stream().sorted(comparator.reversed()).collect(Collectors.toList());
}
return getWords(text).stream().sorted(comparator).collect(Collectors.toList());
}
}