Skip to content

Commit

Permalink
Reto mouredev#19 - ANÁLISIS DE TEXTO [Media]
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenavarroenamoradotokio authored Sep 7, 2023
1 parent d48e4e0 commit 4bd4bfe
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.retos.ej19;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class jorgenavarroenamoradotokio {

public static void main(String[] args) {
String texto = "Nos encontramos en un periodo de guerra civil. "
+ "Las naves espaciales rebeldes, atacando desde una base oculta, "
+ "han logrado su primera victoria contra el malvado Imperio Galáctico";

System.out.println(obtenerNumeroPalabras(texto));
System.out.println(obtenerMediaLongitudPalabras(texto));
System.out.println(obtenerNumeroOraciones(texto));
System.out.println(obtenerPalabraMayorLongitud(texto));
}

private static long obtenerNumeroPalabras(String texto) {
return Stream.of(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty()).count();
}

private static int obtenerMediaLongitudPalabras(String texto) {
List<String> palabras = Arrays.stream(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty())
.collect(Collectors.toList());

int longitudTotal = palabras.stream().mapToInt(String::length).sum();

return palabras.isEmpty() ? 0 : (int) longitudTotal / palabras.size();
}

private static long obtenerNumeroOraciones(String texto) {
return Stream.of(texto.split("\\.")).filter(palabra -> !palabra.isEmpty()).count();
}

private static String obtenerPalabraMayorLongitud(String texto) {
List<String> palabras = Arrays.stream(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty())
.collect(Collectors.toList());

Optional<String> max = palabras.stream()
.max((palabra1, palabra2) -> Integer.compare(palabra1.length(), palabra2.length()));

return max.orElse("");
}
}

0 comments on commit 4bd4bfe

Please sign in to comment.