2

我有 a6347324、b7432408、f54789922(非数字)和数字项目 ID 等用户 ID。我希望能够在 Myrrix 中使用 REST API 来插入这样的用户-项目对。但/pref/a6347324/1234返回错误。可以预期,考虑到https://github.com/myrrix/myrrix-recommender/blob/master/web-common/src/net/myrrix/web/servlets/PreferenceServlet.javaparseLong的第 74 行

我使用的是 0.11 版。我不想使用“标签”功能,因为它不会像等效的数字 ID 那样返回字符串标签,而且我认为它不适用于becauseAPI(因为不返回标签!)。会很快解决这个问题吗?是否有我可以快速使用或实施的直接解决方法?

4

2 回答 2

2

根据设计,REST API 本身根据数字 ID 工作。客户端可能会对其他一些 ID(无论是否为数字)进行散列处理以生成数字 ID。您想TranslatingClientRecommender改用它,它可以帮助管理翻译回原始 ID。

额外的好处是潜在的一些安全性;您实际上不需要将真实的用户和项目 ID 发送到服务,如果它要托管在 Amazon EC2 等第三方云上,这可能很重要。

于 2013-04-14T18:17:40.273 回答
1

我决定在这个答案中添加一些关于我实际上是如何解决问题的细节。请注意,我稍后进行了重大修改并使代码更加面向对象。为了便于理解,这只是一个简单的解释。

这是一个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 上的单个应用程序中运行。

于 2014-01-16T15:40:23.070 回答