Skip to content

Instantly share code, notes, and snippets.

@jmlon
Created November 11, 2021 13:11
Show Gist options
  • Save jmlon/c9d6f274061ed2e407ad2d083a81835c to your computer and use it in GitHub Desktop.
Save jmlon/c9d6f274061ed2e407ad2d083a81835c to your computer and use it in GitHub Desktop.
package edu.upb.estalg.busqueda.movies;
import java.util.ArrayList;
import edu.princeton.cs.algs4.Bag;
import edu.princeton.cs.algs4.RedBlackBST;
import edu.princeton.cs.algs4.SeparateChainingHashST;
import edu.princeton.cs.algs4.StdOut;
public class GeneroPredominante {
public static RedBlackBST<String, Bag<MoviesImdb>> moviesPerActor(ArrayList<MoviesImdb> lista) {
RedBlackBST<String, Bag<MoviesImdb>> index = new RedBlackBST<>();
for(MoviesImdb movie: lista) {
if (movie.getActors()!=null && movie.getActors().length()>0) {
for(String actor: movie.getActors().split(",\\s*")) {
if (!index.contains(actor)) index.put(actor, new Bag<MoviesImdb>());
index.get(actor).add(movie);;
}
}
}
return index;
}
public static SeparateChainingHashST<String, String> mainGenrePerActor(RedBlackBST<String, Bag<MoviesImdb>> index) {
SeparateChainingHashST<String, String> maingenreXactor = new SeparateChainingHashST<>();
for(String actor: index.keys()) {
SeparateChainingHashST<String, Integer> moviesXgenre = new SeparateChainingHashST<>();
for(MoviesImdb movie: index.get(actor)) {
String[] genres = movie.getGenre().replace("\"", "").split(",\\s*");
for(String g: genres) {
if (!moviesXgenre.contains(g)) moviesXgenre.put(g,0);
moviesXgenre.put(g, moviesXgenre.get(g)+1);
}
}
// StdOut.print(actor+" : ");
int maxGenreCount = 0;
for(String g: moviesXgenre.keys()) {
// StdOut.print(g+"="+moviesXgenre.get(g)+", ");
if (moviesXgenre.get(g)>maxGenreCount) {
maxGenreCount = moviesXgenre.get(g);
maingenreXactor.put(actor, g);
}
}
}
return maingenreXactor;
}
public static void main(String[] args) {
String ruta = "./Datasets/IMDb movies.csv";
ArrayList<MoviesImdb> lista = MoviesImdb.readFile(ruta);
RedBlackBST<String, Bag<MoviesImdb>> index = moviesPerActor(lista);
SeparateChainingHashST<String, String> gXa = mainGenrePerActor(index);
for(String actor: gXa.keys()) {
StdOut.println(actor+" : "+gXa.get(actor));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment