Skip to content

Commit

Permalink
修复推荐界面查询用户评分过的电影,电影评分显示异常bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fansy1990 committed Aug 22, 2016
1 parent d1269b9 commit 0f78828
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/model/Movie.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package model;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class Movie implements Comparable<Movie>{
//MovieID::Title::Genres
private Integer id;
Expand Down Expand Up @@ -53,4 +57,15 @@ public String toString() {
return this.id+"|"+this.title+"|"+this.genres+"|"+this.rated;
}

public Movie deepCopy(){
try {
Movie copy = (Movie) BeanUtils.cloneBean(this);
return copy;
} catch (IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
9 changes: 8 additions & 1 deletion src/recommend/Recommend.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -39,7 +40,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
List<Movie> rec =null;
if("recommend".equals(flag)){
recNum = request.getParameter("recommendNum");
rec= Utils.predict(Integer.parseInt(uid),Integer.parseInt(recNum));
try {
rec= Utils.predict(Integer.parseInt(uid),Integer.parseInt(recNum));
} catch (NumberFormatException | IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
rec = Utils.check(Integer.parseInt(uid));
}
Expand Down
12 changes: 9 additions & 3 deletions src/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
Expand Down Expand Up @@ -171,8 +173,12 @@ public static String readHDFS(String outputPath) {
* @param uid
* @param recNum
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static List<Movie> predict(int uid,int recNum) {
public static List<Movie> predict(int uid,int recNum) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
if (userFeatures.size() <= 0 || productFeatures.size() <= 0) {
try {
userFeatures = getModelFeatures(userFeaturePath);
Expand All @@ -198,7 +204,7 @@ public static List<Movie> predict(int uid,int recNum) {
double score = 0.0;
BLAS blas = BLAS.getInstance();
for (int candidate : candidates) {
movie = movies.get(candidate);
movie = movies.get(candidate).deepCopy();
pFeature = productFeatures.get(candidate);
if (pFeature == null)
continue;
Expand Down Expand Up @@ -339,7 +345,7 @@ public static void init() throws IOException {
private static Map<Integer, double[]> userFeatures = new HashMap<>();
private static Map<Integer, double[]> productFeatures = new HashMap<>();

public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
init();

int uid = 1;
Expand Down

0 comments on commit 0f78828

Please sign in to comment.