【发布时间】:2015-12-06 05:34:38
【问题描述】:
我正在尝试在我的 jsp 文件中运行我的 Mahout_Recommender.java 文件。
这是我的 .jsp 文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Recommendation Results</h1>
</body>
</html>
这是我的 Mahout_Recommender.java 文件: 包mahout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import org.apache.log4j.BasicConfigurator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
/**
* User-Based Recommender for Movies
*
*
*/
public class Movie_Recommender {
//public static String [][] rows = new String[3884][6];
public static void main (String [] args){
int userID = 1; //I put this as a placeholder
BasicConfigurator.configure();
try{
File movies = new File("movies.csv");
String [][] rows = new String[3884][6];
rows = fileRead(movies); //read in movie titles
DataModel model = new FileDataModel(new File("ratings.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
ArrayList recommendations = null;
recommendations = (ArrayList) recommender.recommend(userID, 3);
Object[] recs = recommendations.toArray();
String movieTitles = " ";
int movieID = 0;
for(int i = 0; i < 3; i++){
String s = recs[i].toString();
movieID = Integer.parseInt(s.substring(21, s.indexOf(",")));
movieTitles += "\n "+rows[movieID][1];
}
System.out.println(recs[1]);
System.out.println("Your User-Based Recommendations are: ");
System.out.println(movieTitles);
} catch(Exception e) {
System.out.println(e);
}
}
@SuppressWarnings("resource")
public static String[][] fileRead (File data) throws FileNotFoundException{
Scanner reader = new Scanner(data);
String s = "";
String [][] rows = new String[3884][6];
int i = 0;
while (i<3500){
s = reader.nextLine();
rows[i] = s.split(",");
i++;
}
return rows;
}
}
如何在 .jsp 文件中调用 .java 文件的主要方法?
【问题讨论】: