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 @@ -21,7 +21,12 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*/
@Override
public int countSumLengthOfWords(String text) {
return 0;
List<String> allWords = getWords(text);
int sumLengthOfWords = 0;
for (String s : allWords) {
sumLengthOfWords += s.length();
}
return sumLengthOfWords;
}

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

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

/**
Expand All @@ -55,7 +60,7 @@ public int countNumberOfUniqueWords(String text) {
*/
@Override
public List<String> getWords(String text) {
return new ArrayList<>();
return new ArrayList<>(Arrays.asList(text.split("\\W+")));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Можно просто Arrays.asList(text.split("\\W+"))

}

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

/**
Expand All @@ -80,7 +85,13 @@ public Set<String> getUniqueWords(String text) {
*/
@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return Collections.emptyMap();
List<String> allWords = getWords(text);
Map<String, Integer> countWordsRepetitionsMap = new LinkedHashMap<>();
for (String s : allWords) {
int repetitionCounter = Collections.frequency(allWords, s);
countWordsRepetitionsMap.put(s, repetitionCounter);
Copy link
Collaborator

Choose a reason for hiding this comment

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

У тебя же это будет вызываться для каждой копии слова в листе слов?

Copy link
Author

Choose a reason for hiding this comment

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

Да, получается решение более медленное с точки зрения времени выполнения, но более быстрое для программиста :)

Copy link
Author

Choose a reason for hiding this comment

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

Наверное, надо было использовать список уникальных слов, а частоту их употребления собирать из списка всех слов.

}
return countWordsRepetitionsMap;
}

/**
Expand All @@ -93,6 +104,13 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*/
@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return new ArrayList<>();
ArrayList<String> allWords = (ArrayList<String>) getWords(text);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Зачем явно указывать ArrayList

allWords.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return direction == Direction.ASC ? o1.length() - o2.length() : o2.length() - o1.length();
}
});
return allWords;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.epam.izh.rd.online.helper.Direction;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

/**
* Данный класс обязан использовать StreamApi из функционала Java 8. Функциональность должна быть идентична
Expand All @@ -13,36 +12,43 @@
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 (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 null;
List<String> listOfWords = new ArrayList<>();
String [] arrayOfWords = text.split("\\W+");
Arrays.stream(arrayOfWords).forEach(listOfWords::add);
return listOfWords;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Stream.of(text.split("\W+").collect(Collectors.toList());

}

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

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return null;
return getUniqueWords(text).stream().collect(Collectors.toMap(e -> e,
e -> Collections.frequency(getWords(text), e)));
}

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return null;
return getWords(text).stream().sorted
((o1, o2) -> direction == Direction.ASC ? o1.length() - o2.length() : o2.length() - o1.length())
.collect(Collectors.toList());
}
}