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

final commit! #29

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
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.epam.izh.rd.online.service;

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

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

import static java.util.Collections.*;
Expand All @@ -21,9 +24,15 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
*
* @param text текст
*/

@Override
public int countSumLengthOfWords(String text) {
return 0;
ArrayList<String> arrayList = (ArrayList<String>) getWords(text);
int sum = 0;
for (String s : arrayList) {
sum += s.length();
}
return sum;
}

/**
Expand All @@ -32,19 +41,21 @@ public int countSumLengthOfWords(String text) {
*
* @param text текст
*/

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

/**
* Необходимо реализовать функционал подсчета количества уникальных слов в тексте (с учетом регистра).
* Например для текста "One, two, three, three - one, tWo, tWo!!" - данный метод должен вернуть 5.
* param text текст
*/

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

/**
Expand All @@ -55,9 +66,13 @@ public int countNumberOfUniqueWords(String text) {
*
* @param text текст
*/

@Override
public List<String> getWords(String text) {
return emptyList();
String[] wordsStringArray = text.split("\\W");
ArrayList<String> words = new ArrayList<>(Arrays.asList(wordsStringArray));
words.removeIf(String::isEmpty);
return words;
}

/**
Expand All @@ -68,9 +83,10 @@ public List<String> getWords(String text) {
*
* @param text текст
*/

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

/**
Expand All @@ -80,9 +96,22 @@ public Set<String> getUniqueWords(String text) {
*
* @param text текст
*/

@Override
public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
return emptyMap();
Map<String, Integer> hashMapForRepetitionCounting = new HashMap<>();
ArrayList<String> list = (ArrayList<String>) getWords(text);
int count;
for (int i = 0; i < list.size(); i++) {
count = 0;
for (String s : list) {
if (list.get(i).equals(s)) {
count++;
}
}
hashMapForRepetitionCounting.put(list.get(i), count);
}
return hashMapForRepetitionCounting;
}

/**
Expand All @@ -93,8 +122,15 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
*
* @param text текст
*/

@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
ArrayList<String> arrayList = (ArrayList<String>) getWords(text);
if (direction == Direction.ASC) {
arrayList.sort(Comparator.comparingInt(String::length));
} else {
arrayList.sort(Comparator.comparingInt(String::length).reversed());
}
return arrayList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

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 java.util.stream.Stream;

import static java.util.Collections.*;

Expand All @@ -16,36 +15,55 @@
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 emptyList();
return Arrays.stream(text.split("\\W"))
.filter(x -> !x.equals(""))
.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 getWords(text).stream()
.collect(Collectors.toMap(s -> s, s -> 1, (a, b) -> a + 1));
}


@Override
public List<String> sortWordsByLength(String text, Direction direction) {
return emptyList();
if (direction == Direction.ASC) {
return getWords(text).stream()
.sorted(Comparator.comparingInt(String::length))
.collect(Collectors.toList());
} else {
return getWords(text).stream()
.sorted(Comparator.comparingInt(String::length).reversed())
.collect(Collectors.toList());

}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -14,7 +16,7 @@ public interface TextStatisticsAnalyzer {

int countNumberOfUniqueWords(String text);

List<String> getWords(String text);
List<String> getWords(String text) throws IOException, URISyntaxException;

Set<String> getUniqueWords(String text);

Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/epam/izh/rd/online/TextAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -78,7 +80,7 @@ void testSortWordsByLengthDesc() {

@Test
@DisplayName("Тест метода TextStatisticsAnalyzer.getWords(String text)")
void testGetWords() {
void testGetWords() throws IOException, URISyntaxException {
assertListsContainSameElements(wordsList, simpleTextStatisticsAnalyzer.getWords(text));
}

Expand Down Expand Up @@ -126,7 +128,7 @@ void testSortWordsByLengthDescStream() {

@Test
@DisplayName("Тест метода StreamApiTextStatisticsAnalyzer.getWords(String text)")
void testGetWordsStream() {
void testGetWordsStream() throws IOException, URISyntaxException {
assertListsContainSameElements(wordsList, streamApiTextStatisticsAnalyzer.getWords(text));
}

Expand Down