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;
|
package com.camillepradel.movierecommender.controller;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -34,8 +33,8 @@ public class MainController {
|
|||||||
public ModelAndView showMovies(
|
public ModelAndView showMovies(
|
||||||
@RequestParam(value = "user_id", required = false) Integer userId) {
|
@RequestParam(value = "user_id", required = false) Integer userId) {
|
||||||
|
|
||||||
//List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
List<Movie> movies = Neo4JController.getMoviesNeo4JByUser(userId);
|
||||||
List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
|
//List<Movie> movies = MongoDBController.getMoviesMongoDBByUser(userId);
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("movies");
|
ModelAndView mv = new ModelAndView("movies");
|
||||||
mv.addObject("userId", userId);
|
mv.addObject("userId", userId);
|
||||||
@ -50,13 +49,13 @@ public class MainController {
|
|||||||
System.out.println("GET /movieratings for user " + userId);
|
System.out.println("GET /movieratings for user " + userId);
|
||||||
|
|
||||||
// write query to retrieve all movies from DB
|
// write query to retrieve all movies from DB
|
||||||
List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
|
//List<Movie> allMovies = MongoDBController.getMoviesMongoDBByUser(null);
|
||||||
//List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
|
List<Movie> allMovies =Neo4JController.getMoviesNeo4JByUser(null);
|
||||||
|
|
||||||
// write query to retrieve all ratings from the specified user
|
// 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");
|
ModelAndView mv = new ModelAndView("movieratings");
|
||||||
mv.addObject("userId", userId);
|
mv.addObject("userId", userId);
|
||||||
mv.addObject("allMovies", allMovies);
|
mv.addObject("allMovies", allMovies);
|
||||||
@ -68,13 +67,9 @@ public class MainController {
|
|||||||
@RequestMapping(value = "/movieratings", method = RequestMethod.POST)
|
@RequestMapping(value = "/movieratings", method = RequestMethod.POST)
|
||||||
public String saveOrUpdateRating(@ModelAttribute("rating") Rating rating) {
|
public String saveOrUpdateRating(@ModelAttribute("rating") Rating rating) {
|
||||||
System.out.println("POST /movieratings for user " + rating.getUserId()
|
System.out.println("POST /movieratings for user " + rating.getUserId()
|
||||||
+ ", movie " + rating.getMovie().getId()
|
+ ", movie " + rating.getMovie().getTitle()
|
||||||
+ ", score " + rating.getScore());
|
+ ", score " + rating.getScore());
|
||||||
|
Neo4JController.updateMovieRatingNeo4J(rating);
|
||||||
// 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);
|
|
||||||
return "redirect:/movieratings?user_id=" + rating.getUserId();
|
return "redirect:/movieratings?user_id=" + rating.getUserId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,25 +79,15 @@ public class MainController {
|
|||||||
@RequestParam(value = "processing_mode", required = false, defaultValue = "0") Integer processingMode){
|
@RequestParam(value = "processing_mode", required = false, defaultValue = "0") Integer processingMode){
|
||||||
System.out.println("GET /movieratings for user " + userId);
|
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>();
|
List<Rating> recommendations = new LinkedList<Rating>();
|
||||||
String titlePrefix;
|
|
||||||
if (processingMode.equals(0))
|
if (processingMode.equals(1)){
|
||||||
titlePrefix = "0_";
|
recommendations = Neo4JController.ProcessRecommendationV1(userId);
|
||||||
else if (processingMode.equals(1))
|
}
|
||||||
titlePrefix = "1_";
|
else if (processingMode.equals(2)){
|
||||||
else if (processingMode.equals(2))
|
recommendations = Neo4JController.ProcessRecommendationV2(userId);
|
||||||
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));
|
|
||||||
|
|
||||||
ModelAndView mv = new ModelAndView("recommendations");
|
ModelAndView mv = new ModelAndView("recommendations");
|
||||||
mv.addObject("recommendations", recommendations);
|
mv.addObject("recommendations", recommendations);
|
||||||
|
@ -22,79 +22,176 @@ import org.neo4j.driver.v1.StatementResult;
|
|||||||
*/
|
*/
|
||||||
public class Neo4JController {
|
public class Neo4JController {
|
||||||
|
|
||||||
public static List<Movie>getMoviesNeo4JByUser (Integer userId){
|
public static List<Movie> getMoviesNeo4JByUser(Integer userId) {
|
||||||
List<Movie> movies = new LinkedList<Movie>();
|
List<Movie> movies = new LinkedList<Movie>();
|
||||||
int id=0;
|
int id = 0;
|
||||||
StatementResult result = null;
|
StatementResult result = null;
|
||||||
try {
|
try {
|
||||||
if (userId != null && userId >=0 ){
|
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;" );
|
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) {
|
||||||
result = Neo4jConnector.getInstance().getConnection().run( "MATCH (m:Movie) -[:CATEGORIZED_AS]->(g:Genre) RETURN m.title as movies, collect(g.name) as genre;" );
|
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
} finally {
|
||||||
} catch (ConnectException ex) {
|
Neo4jConnector.getInstance().close();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
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("]", "")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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{
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
} catch (ConnectException ex) {
|
} catch (ConnectException ex) {
|
||||||
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
} finally {
|
||||||
finally{
|
Neo4jConnector.getInstance().close();
|
||||||
Neo4jConnector.getInstance().close();
|
}
|
||||||
}
|
Movie movie = null;
|
||||||
Movie movie = null;
|
Rating rate = 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("]", "")));
|
|
||||||
|
|
||||||
|
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;
|
|
||||||
|
|
||||||
|
}
|
||||||
|
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;
|
package com.camillepradel.movierecommender.controller;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import org.neo4j.driver.v1.*;
|
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
|
Vos recommandations
|
||||||
</h1>
|
</h1>
|
||||||
<ul>
|
<ul>
|
||||||
<c:forEach items="${recommendations}" var="recommendation">
|
<c:forEach items="${recommendations}" var="recommendation">
|
||||||
<li>
|
<li>${recommendation.movie.title} - ${recommendation.score} / 5</li>
|
||||||
${recommendation.getMovie().title}
|
</c:forEach>
|
||||||
</li>
|
|
||||||
</c:forEach>
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
Reference in New Issue
Block a user