22
33import com .epam .izh .rd .online .helper .Direction ;
44
5+ import java .lang .reflect .Array ;
56import java .util .*;
67
78import static java .util .Collections .*;
@@ -23,7 +24,11 @@ public class SimpleTextStatisticsAnalyzer implements TextStatisticsAnalyzer {
2324 */
2425 @ Override
2526 public int countSumLengthOfWords (String text ) {
26- return 0 ;
27+ int countSumLengthOfWords = 0 ;
28+ for (String word : getWords (text )) {
29+ countSumLengthOfWords += word .length ();
30+ }
31+ return countSumLengthOfWords ;
2732 }
2833
2934 /**
@@ -34,7 +39,7 @@ public int countSumLengthOfWords(String text) {
3439 */
3540 @ Override
3641 public int countNumberOfWords (String text ) {
37- return 0 ;
42+ return getWords ( text ). size () ;
3843 }
3944
4045 /**
@@ -44,7 +49,7 @@ public int countNumberOfWords(String text) {
4449 */
4550 @ Override
4651 public int countNumberOfUniqueWords (String text ) {
47- return 0 ;
52+ return getUniqueWords ( text ). size () ;
4853 }
4954
5055 /**
@@ -57,7 +62,7 @@ public int countNumberOfUniqueWords(String text) {
5762 */
5863 @ Override
5964 public List <String > getWords (String text ) {
60- return emptyList ( );
65+ return Arrays . asList ( text . split ( "[ \\ W]+" ) );
6166 }
6267
6368 /**
@@ -70,7 +75,7 @@ public List<String> getWords(String text) {
7075 */
7176 @ Override
7277 public Set <String > getUniqueWords (String text ) {
73- return emptySet ( );
78+ return new HashSet <>( getWords ( text ) );
7479 }
7580
7681 /**
@@ -82,7 +87,15 @@ public Set<String> getUniqueWords(String text) {
8287 */
8388 @ Override
8489 public Map <String , Integer > countNumberOfWordsRepetitions (String text ) {
85- return emptyMap ();
90+ Map <String , Integer > repeatWords = new HashMap <>();
91+ for (String word : getWords (text )) {
92+ Integer q = repeatWords .put (word , 1 );
93+ if (q != null ) {
94+ repeatWords .put (word , q + 1 );
95+ }
96+
97+ }
98+ return repeatWords ;
8699 }
87100
88101 /**
@@ -95,6 +108,13 @@ public Map<String, Integer> countNumberOfWordsRepetitions(String text) {
95108 */
96109 @ Override
97110 public List <String > sortWordsByLength (String text , Direction direction ) {
98- return emptyList ();
111+ List <String > words = new ArrayList <>(getUniqueWords (text ));
112+ words .sort (Comparator .comparingInt (String ::length ));
113+ if (direction .name ().equals ("DESC" )) {
114+ Collections .reverse (words );
115+ }
116+ return words ;
99117 }
118+
119+
100120}
0 commit comments