From 0f78828f797be5d1af4048288d7400d510d03c9b Mon Sep 17 00:00:00 2001 From: fansy Date: Mon, 22 Aug 2016 18:32:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A8=E8=8D=90=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E6=9F=A5=E8=AF=A2=E7=94=A8=E6=88=B7=E8=AF=84=E5=88=86?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E7=94=B5=E5=BD=B1=EF=BC=8C=E7=94=B5=E5=BD=B1?= =?UTF-8?q?=E8=AF=84=E5=88=86=E6=98=BE=E7=A4=BA=E5=BC=82=E5=B8=B8bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/Movie.java | 15 +++++++++++++++ src/recommend/Recommend.java | 9 ++++++++- src/util/Utils.java | 12 +++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/model/Movie.java b/src/model/Movie.java index 36442d4..1e6dbb8 100644 --- a/src/model/Movie.java +++ b/src/model/Movie.java @@ -1,5 +1,9 @@ package model; +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.beanutils.BeanUtils; + public class Movie implements Comparable{ //MovieID::Title::Genres private Integer id; @@ -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; + } } diff --git a/src/recommend/Recommend.java b/src/recommend/Recommend.java index 41de02d..9827516 100644 --- a/src/recommend/Recommend.java +++ b/src/recommend/Recommend.java @@ -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; @@ -39,7 +40,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t List 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)); } diff --git a/src/util/Utils.java b/src/util/Utils.java index ea10835..4c0d8d0 100644 --- a/src/util/Utils.java +++ b/src/util/Utils.java @@ -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; @@ -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 predict(int uid,int recNum) { + public static List predict(int uid,int recNum) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { if (userFeatures.size() <= 0 || productFeatures.size() <= 0) { try { userFeatures = getModelFeatures(userFeaturePath); @@ -198,7 +204,7 @@ public static List 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; @@ -339,7 +345,7 @@ public static void init() throws IOException { private static Map userFeatures = new HashMap<>(); private static Map 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;