Merge branch 'master' of https://github.com/DonRenando/MovieRecommender
This commit is contained in:
commit
cad44a4abb
@ -1,6 +1,5 @@
|
||||
package com.camillepradel.movierecommender.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@ -34,8 +33,8 @@ public class MainController {
|
||||
public ModelAndView showMovies(
|
||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||
|
||||
//List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
||||
List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
|
||||
List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
||||
//List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
|
||||
|
||||
ModelAndView mv = new ModelAndView("movies");
|
||||
mv.addObject("userId", userId);
|
||||
@ -50,13 +49,13 @@ public class MainController {
|
||||
System.out.println("GET /movieratings for user " + userId);
|
||||
|
||||
// write query to retrieve all movies from DB
|
||||
List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
|
||||
//List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
|
||||
//List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
|
||||
List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
|
||||
|
||||
// write query to retrieve all ratings from the specified user
|
||||
List<Rating> ratings = MongoDBController.getRatingMongoDBByUser(userId);
|
||||
//List<Rating> ratings = MongoDBController.getRatingMongoDBByUser(userId);
|
||||
|
||||
//List<Rating> ratings=Neo4JController.GetMoviesRatingNeo4JByUser(userId);
|
||||
List<Rating> ratings=Neo4JController.GetMoviesRatingNeo4JByUser(userId);
|
||||
ModelAndView mv = new ModelAndView("movieratings");
|
||||
mv.addObject("userId", userId);
|
||||
mv.addObject("allMovies", allMovies);
|
||||
@ -68,13 +67,9 @@ public class MainController {
|
||||
@RequestMapping(value = "/movieratings", method = RequestMethod.POST)
|
||||
public String saveOrUpdateRating(@ModelAttribute("rating") Rating rating) {
|
||||
System.out.println("POST /movieratings for user " + rating.getUserId()
|
||||
+ ", movie " + rating.getMovie().getId()
|
||||
+ ", movie " + rating.getMovie().getTitle()
|
||||
+ ", score " + rating.getScore());
|
||||
|
||||
// TODO: add query which
|
||||
// - add rating between specified user and movie if it doesn't exist
|
||||
// - update it if it does exist
|
||||
MongoDBController.updateMovieRating(rating);
|
||||
Neo4JController.updateMovieRatingNeo4J(rating);
|
||||
return "redirect:/movieratings?user_id=" + rating.getUserId();
|
||||
}
|
||||
|
||||
@ -84,26 +79,16 @@ public class MainController {
|
||||
@RequestParam(value = "processing_mode", required = false, defaultValue = "0") Integer processingMode){
|
||||
System.out.println("GET /movieratings for user " + userId);
|
||||
|
||||
// TODO: process recommendations for specified user exploiting other users ratings
|
||||
// use different methods depending on processingMode parameter
|
||||
Genre genre0 = new Genre(0, "genre0");
|
||||
Genre genre1 = new Genre(1, "genre1");
|
||||
Genre genre2 = new Genre(2, "genre2");
|
||||
List<Rating> recommendations = new LinkedList<Rating>();
|
||||
String titlePrefix;
|
||||
if (processingMode.equals(0))
|
||||
titlePrefix = "0_";
|
||||
else if (processingMode.equals(1))
|
||||
titlePrefix = "1_";
|
||||
else if (processingMode.equals(2))
|
||||
titlePrefix = "2_";
|
||||
else
|
||||
titlePrefix = "default_";
|
||||
recommendations.add(new Rating(new Movie(0, titlePrefix + "Titre 0", Arrays.asList(new Genre[] {genre0, genre1})), userId, 5));
|
||||
recommendations.add(new Rating(new Movie(1, titlePrefix + "Titre 1", Arrays.asList(new Genre[] {genre0, genre2})), userId, 5));
|
||||
recommendations.add(new Rating(new Movie(2, titlePrefix + "Titre 2", Arrays.asList(new Genre[] {genre1})), userId, 4));
|
||||
recommendations.add(new Rating(new Movie(3, titlePrefix + "Titre 3", Arrays.asList(new Genre[] {genre0, genre1, genre2})), userId, 3));
|
||||
|
||||
if (processingMode.equals(1)){
|
||||
recommendations = Neo4JController.ProcessRecommendationV1(userId);
|
||||
}
|
||||
else if (processingMode.equals(2)){
|
||||
recommendations = Neo4JController.ProcessRecommendationV2(userId);
|
||||
|
||||
}
|
||||
|
||||
ModelAndView mv = new ModelAndView("recommendations");
|
||||
mv.addObject("recommendations", recommendations);
|
||||
|
||||
|
@ -21,80 +21,177 @@ import org.neo4j.driver.v1.StatementResult;
|
||||
* @author renando
|
||||
*/
|
||||
public class Neo4JController {
|
||||
|
||||
public static List<Movie>getMoviesNeo4JByUser (Integer userId){
|
||||
List<Movie> movies = new LinkedList<Movie>();
|
||||
int id=0;
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >=0 ){
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (me:User { id: "+userId+" })-[:RATED]->(movie:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN movie.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
else{
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (m:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN m.title as movies, collect(g.name) as genre;" );
|
||||
}
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally{
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for(String g : listGenre){
|
||||
if(g != null)
|
||||
listTypeGenre.add(new Genre(id, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
|
||||
}
|
||||
movies.add(new Movie(id, record.get("movies").asString(), (listTypeGenre.isEmpty())?null:listTypeGenre ) );
|
||||
id+=1;
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
|
||||
public static List<Rating>GetMoviesRatingNeo4JByUser(Integer userId){
|
||||
List<Rating> moviesRating = new LinkedList<Rating>();
|
||||
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >=0 ){
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (u:User{id:"+userId+"})-[r:RATED]->(m:Movie)-[:CATEGORIZED_AS]->(g:Genre) RETURN m.id AS id, m.title AS title, collect(g.name) AS genre, r.note AS note ORDER BY id" );
|
||||
public static List<Movie> getMoviesNeo4JByUser(Integer userId) {
|
||||
List<Movie> movies = new LinkedList<Movie>();
|
||||
int id = 0;
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >= 0) {
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run("MATCH (me:User { id: " + userId + " })-[:RATED]->(movie:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN movie.title as movies, collect(g.name) as genre;");
|
||||
} else {
|
||||
result = Neo4jConnector.getInstance().getConnection().run("MATCH (m:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN m.title as movies, collect(g.name) as genre;");
|
||||
}
|
||||
else{
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
while (result.hasNext()) {
|
||||
Record record = result.next();
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for (String g : listGenre) {
|
||||
if (g != null) {
|
||||
listTypeGenre.add(new Genre(id, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
}
|
||||
|
||||
}
|
||||
movies.add(new Movie(id, record.get("movies").asString(), (listTypeGenre.isEmpty()) ? null : listTypeGenre));
|
||||
id += 1;
|
||||
}
|
||||
return movies;
|
||||
}
|
||||
|
||||
public static List<Rating> GetMoviesRatingNeo4JByUser(Integer userId) {
|
||||
List<Rating> moviesRating = new LinkedList<Rating>();
|
||||
|
||||
StatementResult result = null;
|
||||
try {
|
||||
if (userId != null && userId >= 0) {
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run("MATCH (u:User{id:" + userId + "})-[r:RATED]->(m:Movie)-[:CATEGORIZED_AS]->(g:Genre) RETURN m.id AS id, m.title AS title, collect(g.name) AS genre, r.note AS note ORDER BY id");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
finally{
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
Movie movie = null;
|
||||
Rating rate = null;
|
||||
|
||||
while ( result.hasNext() )
|
||||
{
|
||||
Record record = result.next();
|
||||
System.out.println(record.get("p").toString());
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for(String g : listGenre){
|
||||
if(g != null)
|
||||
listTypeGenre.add(new Genre(0, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
|
||||
}
|
||||
movie = new Movie(record.get("id").asInt(), record.get("title").asString(),listTypeGenre );
|
||||
rate = new Rating(movie, userId, record.get("note").asInt());
|
||||
moviesRating.add(rate);
|
||||
}
|
||||
return moviesRating;
|
||||
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
|
||||
Movie movie = null;
|
||||
Rating rate = null;
|
||||
|
||||
while (result.hasNext()) {
|
||||
Record record = result.next();
|
||||
System.out.println(record.get("p").toString());
|
||||
String[] listGenre = record.get("genre").toString().split(",");
|
||||
List<Genre> listTypeGenre = new LinkedList<Genre>();
|
||||
for (String g : listGenre) {
|
||||
if (g != null) {
|
||||
listTypeGenre.add(new Genre(0, g.replace("\"", "").replace("[", "").replace("]", "")));
|
||||
}
|
||||
|
||||
}
|
||||
movie = new Movie(record.get("id").asInt(), record.get("title").asString(), listTypeGenre);
|
||||
rate = new Rating(movie, userId, record.get("note").asInt());
|
||||
moviesRating.add(rate);
|
||||
}
|
||||
return moviesRating;
|
||||
|
||||
}
|
||||
|
||||
public static void updateMovieRatingNeo4J(Rating note) {
|
||||
|
||||
Integer user_id = note.getUserId();
|
||||
try {
|
||||
if (user_id != null && user_id >= 0) {
|
||||
|
||||
Neo4jConnector.getInstance().getConnection().run("MATCH (u:User{id:" + user_id + "})-[r:RATED]->(m:Movie)\n"
|
||||
+ "where m.id =" + note.getMovie().getId() + "\n"
|
||||
+ "delete r");
|
||||
}
|
||||
|
||||
} catch (ConnectException ex) {
|
||||
System.out.println(ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
|
||||
try {
|
||||
Neo4jConnector.getInstance().getConnection().run("MATCH (u:User{id:" + user_id + "}),(m:Movie)\n"
|
||||
+ "where m.id = " + note.getMovie().getId() + "\n"
|
||||
+ "CREATE (u)-[r:RATED{note:" + note.getScore() + ", timestamp: " + System.currentTimeMillis() + "}]->(m) \n"
|
||||
+ "RETURN *");
|
||||
|
||||
} catch (ConnectException ex) {
|
||||
System.out.println(ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Rating> ProcessRecommendationV1(Integer userId) {
|
||||
|
||||
List<Rating> ratings = new LinkedList<Rating>();
|
||||
StatementResult result = null;
|
||||
|
||||
try {
|
||||
result = Neo4jConnector.getInstance().getConnection().run("MATCH (target_user:User {id : " + userId + "})-[:RATED]->(m:Movie)<-[:RATED]-(other_user:User)\n"
|
||||
+ "WITH other_user, count(distinct m.title) AS num_common_movies, target_user\n"
|
||||
+ "ORDER BY num_common_movies DESC\n"
|
||||
+ "LIMIT 1\n"
|
||||
+ "MATCH (other_user)-[rat_other_user:RATED]->(m2:Movie)\n"
|
||||
+ "WHERE NOT ((target_user)-[:RATED]->(m2))\n"
|
||||
+ "RETURN m2.id AS mov_id, m2.title AS rec_movie_title, rat_other_user.note AS rating, other_user.id AS watched_by\n"
|
||||
+ "ORDER BY rat_other_user.note DESC");
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(Neo4JController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
Record record = null;
|
||||
int idMovie = 0;
|
||||
String titre = "";
|
||||
int note = 0;
|
||||
while (result.hasNext()) {
|
||||
record = result.next();
|
||||
idMovie = record.get("mov_id").asInt();
|
||||
titre = record.get("rec_movie_title").asString();
|
||||
note = record.get("rating").asInt();
|
||||
System.out.println(titre);
|
||||
ratings.add(new Rating(new Movie(idMovie, titre,null), userId, note));
|
||||
}
|
||||
return ratings;
|
||||
|
||||
}
|
||||
|
||||
public static List<Rating> ProcessRecommendationV2(Integer userId) {
|
||||
|
||||
List<Rating> ratings = new LinkedList<Rating>();
|
||||
StatementResult result = null;
|
||||
|
||||
try {
|
||||
|
||||
result = Neo4jConnector.getInstance().getConnection().run("MATCH (target_user:User {id : " + userId + "})-[:RATED]->(m:Movie)<-[:RATED]-(other_user:User)\n"
|
||||
+ "WITH other_user, count(distinct m.title) AS num_common_movies, target_user\n"
|
||||
+ "ORDER BY num_common_movies DESC\n"
|
||||
+ "LIMIT 5\n"
|
||||
+ "MATCH (other_user)-[rat_other_user:RATED]->(m2:Movie)\n"
|
||||
+ "WHERE NOT ((target_user)-[:RATED]->(m2))\n"
|
||||
+ "RETURN m2.id AS mov_id, m2.title AS rec_movie_title, rat_other_user.note AS rating, other_user.id AS watched_by\n"
|
||||
+ "ORDER BY rat_other_user.note DESC");
|
||||
|
||||
} catch (ConnectException ex) {
|
||||
Logger.getLogger(Neo4JController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} finally {
|
||||
Neo4jConnector.getInstance().close();
|
||||
}
|
||||
Record record = null;
|
||||
int idMovie = 0;
|
||||
String titre = "";
|
||||
int note = 0;
|
||||
while (result.hasNext()) {
|
||||
record = result.next();
|
||||
idMovie = record.get("mov_id").asInt();
|
||||
titre = record.get("rec_movie_title").asString();
|
||||
note = record.get("rating").asInt();
|
||||
|
||||
ratings.add(new Rating(new Movie(idMovie, titre,null), userId, note));
|
||||
}
|
||||
return ratings;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,6 @@
|
||||
package com.camillepradel.movierecommender.controller;
|
||||
import java.net.ConnectException;
|
||||
import org.neo4j.driver.v1.*;
|
||||
import org.neo4j.driver.v1.exceptions.Neo4jException;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.camillepradel.movierecommender.testscript;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class TestGetRecommendations {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String urlStart = "http://localhost:8080/MovieRecommender/recommendations?user_id=";
|
||||
int nbIterations = 100;
|
||||
int userId= 0;
|
||||
long startTime = System.nanoTime();
|
||||
|
||||
for (int i= 0; i < nbIterations; i++) {
|
||||
|
||||
URL u;
|
||||
InputStream is = null;
|
||||
DataInputStream dis;
|
||||
|
||||
try
|
||||
{
|
||||
u = new URL(urlStart + userId);
|
||||
is = u.openStream();
|
||||
dis = new DataInputStream(new BufferedInputStream(is));
|
||||
while ((dis.readLine()) != null)
|
||||
{
|
||||
}
|
||||
System.out.println(i + "/" + nbIterations);
|
||||
}
|
||||
catch (MalformedURLException mue)
|
||||
{
|
||||
System.err.println("Ouch - a MalformedURLException happened.");
|
||||
mue.printStackTrace();
|
||||
System.exit(2);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
System.err.println("Oops- an IOException happened.");
|
||||
ioe.printStackTrace();
|
||||
System.exit(3);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long endTime = System.nanoTime();
|
||||
System.out.println("Time to get " + nbIterations + " times recommendation page: " + (endTime - startTime) + "ns");
|
||||
|
||||
}
|
||||
}
|
@ -12,11 +12,9 @@
|
||||
Vos recommandations
|
||||
</h1>
|
||||
<ul>
|
||||
<c:forEach items="${recommendations}" var="recommendation">
|
||||
<li>
|
||||
${recommendation.getMovie().title}
|
||||
</li>
|
||||
</c:forEach>
|
||||
<c:forEach items="${recommendations}" var="recommendation">
|
||||
<li>${recommendation.movie.title} - ${recommendation.score} / 5</li>
|
||||
</c:forEach>
|
||||
</ul>
|
||||
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user