我正在尝试一个简短的电影推荐人,在这里引用一个简短的视频 - https://www.youtube.com/watch?v=63k560Livmg。我正在尝试diff而不是控制台,我想在浏览器上显示建议,所以我使用servlet。但问题是当我在表单中输入值(用户ID)时,会显示空白屏幕。请帮助我做错了。来自此处的数据集 - https://mahout.apache.org/users/recommender/userbased-5-minutes.html。请参阅附加的web.xml和层次结构截图。
的index.jsp
<html>
<body>
<h2>Hello World!</h2>
<form action="demo" method="post">
loginID:<input type="text" name="name"/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
demo.java- servlet,用于收集用户值和显示结果。
package com.issac;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
/**
* Servlet implementation class demo
*/
public class demo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public demo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @throws IOException
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
String n = request.getParameter("name");
int k = Integer.parseInt(n);
PrintWriter out = response.getWriter();
List<RecommendedItem> recommendations = new ArrayList<RecommendedItem> ();
try {
recommendations = App.getRecommend(k);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (RecommendedItem recommendation : recommendations) {
out.println(recommendation);
}
}}
app.java - 用于生成推荐的逻辑
package com.issac;
import java.io.File;
import java.util.List;
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.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
public class App
{
public static List<RecommendedItem> getRecommend(int k) throws Exception
{
DataModel model = new FileDataModel(new File("data/dataset.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(k, 3);
return recommendations;
}
}
eclipse控制台的堆栈跟踪 -
INFO: Server startup in 15288 ms
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
java.io.FileNotFoundException: data\dataset.csv
at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init> (FileDataModel.java:182)
at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init>(FileDataModel.java:169)
at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<init>(FileDataModel.java:149)
at com.issac.App.getRecommend(App.java:24)
at com.issac.demo.doPost(demo.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt erChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
在JEE应用程序中,应通过getResourceAsStream
方法读取文件:
try(InputStream input=getClass().getClassLoader().getResourceAsStream("data/dataset.csv")){
...
}
data
文件夹应立即放在src/main/resources
内。
在你的情况下,还存在一个并发症:由于缺少来自InputStream的DataModel的构造函数,我们需要首先将inputStream的内容保存到(临时)文件中:
try(InputStream input=getClass().getClassLoader().getResourceAsStream("data/dataset.csv")){
// Copy the contents of the inputStream to a File, to fix the constructor FileDataModel(file):
File tmp=File.createTempFile("model-", "");
tmp.deleteOnExit();
InputStream input=new ByteArrayInputStream("enero".getBytes("ISO-8859-1"));
Files.copy(input, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
DataModel model = new FileDataModel(new File("data/dataset.csv"));
...
}
答案 1 :(得分:0)
所以这就是我的成功之道。获取FileNotFound异常后,我尝试了这个 - &gt;
ClassLoader classLoader = App.class.getClassLoader();
DataModel model = new FileDataModel(new File(classLoader.getResource("data/dataset.csv").getFile()));
而不是
DataModel model = new FileDataModel(new File("data/dataset.csv"));
我没有完全了解它为什么在互联网上找到它的工作,与类FileDataModel的参数有关。欢迎任何能够解释的人。