-
Notifications
You must be signed in to change notification settings - Fork 79
Feature #1
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
base: master
Are you sure you want to change the base?
Feature #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -32,7 +37,7 @@ public int countSumLengthOfWords(String text) { | |
| */ | ||
| @Override | ||
| public int countNumberOfWords(String text) { | ||
| return 0; | ||
| return getWords(text).size(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -42,7 +47,7 @@ public int countNumberOfWords(String text) { | |
| */ | ||
| @Override | ||
| public int countNumberOfUniqueWords(String text) { | ||
| return 0; | ||
| return getUniqueWords(text).size(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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+"))); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У тебя же это будет вызываться для каждой копии слова в листе слов?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, получается решение более медленное с точки зрения времени выполнения, но более быстрое для программиста :)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Наверное, надо было использовать список уникальных слов, а частоту их употребления собирать из списка всех слов. |
||
| } | ||
| return countWordsRepetitionsMap; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем явно указывать |
||
| 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 |
|---|---|---|
|
|
@@ -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. Функциональность должна быть идентична | ||
|
|
@@ -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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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+"))