我决定在这个答案中添加一些关于我实际上是如何解决问题的细节。请注意,我稍后进行了重大修改并使代码更加面向对象。为了便于理解,这只是一个简单的解释。
这是一个WebClientRecommender
基本上是一个小包装器的类TranslatingClientRecommender
。
package suggester.client.recommender;
import java.io.File;
import java.io.Reader;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author Nilesh Chakraborty
*/
public class WebClientRecommender extends AbstractClientRecommender {
public WebClientRecommender(MyrrixClientConfiguration myrrixClientConfiguration) {
super(myrrixClientConfiguration);
}
@Override
public List<TranslatedRecommendedItem> recommend(String idListFile, String recommendTo, String recommendType, int howMany) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommend(recommendTo, howMany, false, new String[]{recommendType});
return recommendations;
}
@Override
public List<TranslatedRecommendedItem> recommendAnonymous(String idListFile, String recommendType, int howMany, String[] list) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
float[] values = new float[list.length];
Arrays.fill(values, 30);
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommendToAnonymous(list, values, howMany, new String[]{recommendType}, "testID");
return recommendations;
}
public List<TranslatedRecommendedItem> recommendAnonymous(URI idListFile, String recommendType, int howMany, String[] list) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
float[] values = new float[list.length];
Arrays.fill(values, 30);
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommendToAnonymous(list, values, howMany, new String[]{recommendType}, "testID");
return recommendations;
}
@Override
public void ingest(Reader csvReader) throws TasteException {
clientRecommender.ingest(csvReader);
}
}
这里是AbstractClientRecommender
:
package suggester.client.recommender;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.myrrix.client.ClientRecommender;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import net.myrrix.client.translating.TranslatingClientRecommender;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author nilesh
*/
abstract class AbstractClientRecommender {
protected TranslatingClientRecommender clientRecommender;
public AbstractClientRecommender(MyrrixClientConfiguration myrrixClientConfiguration) {
try {
clientRecommender = new TranslatingClientRecommender(new ClientRecommender(myrrixClientConfiguration));
} catch (IOException ex) {
Logger.getLogger(CLIClientRecommender.class.getName()).log(Level.SEVERE, null, ex);
}
}
public abstract List<TranslatedRecommendedItem> recommend(String idListFile, String recommendTo, String recommendType, int howMany) throws TasteException;
public abstract List<TranslatedRecommendedItem> recommendAnonymous(String idListFile, String recommendType, int howMany, String[] list) throws TasteException;
public void ingest(String csvFile) throws TasteException {
}
public void ingest(Reader csvReader) throws TasteException {
}
}
现在,我WebClientRecommender
在自定义 Servlet 中使用该类。这是SuggesterServlet
:
package suggester.client.servlets;
import com.google.common.base.Splitter;
import suggester.client.recommender.WebClientRecommender;
import java.io.IOException;
import java.net.URL;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.web.servlets.AbstractMyrrixServlet;
/**
*
* @author nilesh
*/
public abstract class AbstractSuggesterServlet extends AbstractMyrrixServlet {
static final Splitter SLASH = Splitter.on('/').omitEmptyStrings();
private WebClientRecommender recommender;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
MyrrixClientConfiguration config = new MyrrixClientConfiguration();
config.setHost(request.getServerName());
config.setPort(request.getServerPort());
recommender = new WebClientRecommender(config);
}
protected final WebClientRecommender getClientRecommender() {
return recommender;
}
}
现在,我们在这里使用SuggesterServlet
:
package suggester.client.servlets;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.List;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author nilesh
*/
public class SuggesterServlet extends AbstractSuggesterServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
super.doGet(request, response);
String pathInfo = request.getPathInfo();
String[] pathComponents = Iterables.toArray(SLASH.split(pathInfo), String.class);
if (pathComponents.length == 0) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
List<TranslatedRecommendedItem> recommended = getClientRecommender().recommendAnonymous(getPropFilePath("proplist").toURI(), request.getParameter("type"), getHowMany(request), pathComponents);
output(request, response, recommended);
} catch (URISyntaxException use) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, use.toString());
} catch (NamingException ne) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ne.toString());
} catch (TasteException te) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, te.toString());
}
}
protected final void output(HttpServletRequest request,
ServletResponse response,
List<TranslatedRecommendedItem> items) throws IOException {
PrintWriter writer = response.getWriter();
// Always print JSON
writer.write('[');
boolean first = true;
for (TranslatedRecommendedItem item : items) {
if (first) {
first = false;
} else {
writer.write(',');
}
writer.write("[\"");
writer.write(item.getItemID());
writer.write("\",");
writer.write(Float.toString(item.getValue()));
writer.write(']');
}
writer.write(']');
}
}
读取道具文件并将字符串 ID 添加到TranslatingClientRecommender
(连接到 Myrrix REST Servlet),TranslatingClientRecommender
在自定义 Servlet(HAS-A 关系)中使用它 - 这基本上就是我的做法。我只需要修改 Myrrix 的 web.xml 以将我的自定义 servlet 添加到列表中,并且所有内容都可以在 Tomcat 上的单个应用程序中运行。