Skip to content

Commit

Permalink
Clase 08/02
Browse files Browse the repository at this point in the history
  • Loading branch information
jandion committed Feb 9, 2024
1 parent 789b274 commit 015edff
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 17 deletions.
12 changes: 12 additions & 0 deletions EstructurasDeDatos/myLog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feb 08, 2024 12:50:46 PM LectorPeliculas main
SEVERE: Finalizada la lectura del archivo
Feb 08, 2024 12:50:46 PM LectorPeliculas main
WARNING: Creado el diccionario de peliculas
Feb 08, 2024 12:50:46 PM LectorPeliculas main
INFO: [Ellen Burstyn, Jared Leto, Jennifer Connelly, Marlon Wayans]
Feb 08, 2024 12:50:46 PM LectorPeliculas calculaConjuntoActores
FINEST: Ha tardado: 1
Feb 08, 2024 12:50:46 PM LectorPeliculas main
INFO: Actores únicos: 2003
Feb 08, 2024 12:50:46 PM LectorPeliculas main
FINE: 2003
104 changes: 87 additions & 17 deletions EstructurasDeDatos/src/LectorPeliculas.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,111 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LectorPeliculas {

private static final Logger LOGGER= Logger.getLogger("MyLog");;

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("peliculas.tsv"));

LOGGER.setLevel(Level.ALL);

String line = br.readLine();

List<String> lineas = new ArrayList<>();
Handler ch = new ConsoleHandler();
ch.setLevel(Level.FINE);

while( line != null ) {
lineas.add(line);
line = br.readLine();
}
Handler fh = new FileHandler("myLog.txt");

System.out.println(lineas.size());
fh.setFormatter(new SimpleFormatter());
fh.setLevel(Level.ALL);

LOGGER.addHandler(ch);
LOGGER.addHandler(fh);

List<String> lineas = getLineasDeArchivo("peliculas.tsv");

LOGGER.severe("Finalizada la lectura del archivo");
Map<String, List<String>> peliculas = crearDiccionarioPeliculas(lineas);
LOGGER.warning("Creado el diccionario de peliculas");
LOGGER.info(""+peliculas.get("Requiem for a Dream"));

Set<String> actoresUnicos = calculaConjuntoActores(peliculas);
LOGGER.info("Actores únicos: "+actoresUnicos.size());

Map<String, List<String>> actorPelicula = invierteDiccionarioPeliculas(peliculas);
LOGGER.fine(""+actorPelicula.size());


}

public static Map<String, List<String>> invierteDiccionarioPeliculas(Map<String, List<String>> peliculas) {
Map<String,List<String>> actorPelicula = new HashMap<String, List<String>>();

for(String titulo : peliculas.keySet()) {
for ( String actor : peliculas.get(titulo)) {
if (! actorPelicula.containsKey(actor)) {
actorPelicula.put(actor, new ArrayList<String>());
}
actorPelicula.get(actor).add(titulo);
}
}
return actorPelicula;
}

public static Set<String> calculaConjuntoActores(Map<String, List<String>> peliculas) {
Set<String> actoresUnicos = new HashSet<>();

long t0 = System.currentTimeMillis();
for(String titulo : peliculas.keySet()) {
actoresUnicos.addAll(peliculas.get(titulo));

}
LOGGER.finest("Ha tardado: "+ (System.currentTimeMillis()-t0));
return actoresUnicos;
}

public static Map<String, List<String>> crearDiccionarioPeliculas(List<String> lineas) {
Map<String,List<String>> peliculas = new HashMap<>();


for(String pelicula : lineas) {
String[] columnas = pelicula.split("\t");
if (columnas.length <16) continue;
String titulo = columnas[0];
String[] actores = columnas[16].split(",");
List<String> listaActores = Arrays.asList(actores);
peliculas.put(titulo, listaActores);

}

peliculas.size();

System.out.println(peliculas.get("Requiem for a Dream"));

return peliculas;
}

public static List<String> getLineasDeArchivo(String fileName) throws Exception{

BufferedReader br = new BufferedReader(new FileReader(fileName));

String line = br.readLine();

List<String> lineas = new ArrayList<>();

while( line != null ) {
lineas.add(line);
line = br.readLine();
}


return lineas;
}

public static Integer getMaxPeliculas(Map<String, List<String>> actorPelicula) {
// TODO Auto-generated method stub
return null;
}
}
47 changes: 47 additions & 0 deletions EstructurasDeDatos/src/TestLectorPeliculas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class TestLectorPeliculas {

static List<String> lineas;
static Map<String, List<String>> peliculas, actorPelicula;

@BeforeAll
public static void setUp() {
try {
lineas = LectorPeliculas.getLineasDeArchivo("peliculas.tsv");
peliculas = LectorPeliculas.crearDiccionarioPeliculas(lineas);

actorPelicula = LectorPeliculas.invierteDiccionarioPeliculas(peliculas);
} catch (Exception e) {

}
}

@Test
void testGetLineasDeArchivo() {
try {
List<String> lineas = LectorPeliculas.getLineasDeArchivo("peliculas.tsv");
assertEquals(599, lineas.size());
} catch (Exception e) {
fail("El método lanzó una excepción");
}
}

@Test
void testActorConMasPelis() {


assertEquals(6, LectorPeliculas.getMaxPeliculas(actorPelicula));


}


}

0 comments on commit 015edff

Please sign in to comment.