Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

классы SimpleTextStatisticsAnalyzer, StreapApiTextStatisticsAnalyzer … #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {

public static void main(String[] args) throws IOException, URISyntaxException {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> arrayListWords = getWords(text);
int sumLengthOfWords = 0;
for (String word:arrayListWords) {
sumLengthOfWords += word.length();
}
return sumLengthOfWords;
}

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

/**
Expand All @@ -44,7 +49,8 @@ public int countNumberOfWords(String text) {
*/
@Override
public int countNumberOfUniqueWords(String text) {
return 0;

return getUniqueWords(text).size();
}

/**
Expand All @@ -57,7 +63,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 +76,8 @@ public List<String> getWords(String text) {
*/
@Override
public Set<String> getUniqueWords(String text) {
return emptySet();

return new HashSet<>(getWords(text)) ;
}

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

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

List<String> listWordsByLength = getWords(text);
listWordsByLength.sort(new ComparatorWordsByLength());
if (direction == Direction.DESC) {
Collections.reverse(listWordsByLength);
}
return listWordsByLength;
}
static class ComparatorWordsByLength implements Comparator<String> {

@Override
public int compare(String a, String b) {
if(a.length() > b.length()) {
return 1;
} else if(a.length()< b.length()) {
return -1;
} else {
return 0;
}
}

}
}
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,48 @@
public class StreamApiTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
@Override
public int countSumLengthOfWords(String text) {
return 0;

return getWords(text).stream().mapToInt((w) -> w.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<String> getWords(String text) {
return emptyList();

return Arrays.stream(text.split("\\W+")).collect(Collectors.toList());
}

@Override
public Set<String> getUniqueWords(String text) {
return emptySet();

return getWords(text).stream().collect(Collectors.toSet());
}

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();

return getUniqueWords(text).stream().collect(Collectors.toMap(wk->wk,
wc-> Collections.frequency(getWords(text),wc)));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();

return direction == Direction.ASC ?
getWords(text).stream().sorted((o1, o2) -> o1.length() - o2.length()).collect(Collectors.toList()) :
getWords(text).stream().sorted((o1, o2) -> o2.length() - o1.length()).collect(Collectors.toList());
}

}