1

I am trying to make a simple recommender system, and I found that with mahout it is pretty easy to make one. I have the following code (I am running it on eclipse and everything works great:

package com.predictionmarketing.RecommenderApp;

import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
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;


/**
 * Java's application, user based recommender system
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        // Modelo
        DataModel model = null;

        // Inicializar similaridad
        UserSimilarity similarity = null;

        // Leer .cv  userID, itemID, value
        try {
            model = new FileDataModel(new File("data/dataset.csv"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // Encontrar matriz de similaridad
        try {
            similarity = new PearsonCorrelationSimilarity(model);
        } catch (TasteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
        UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
        java.util.List<RecommendedItem> recommendations = null;
        try {
            recommendations = recommender.recommend(2, 3);
        } catch (TasteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Mostrar Recomendaciones
        for (RecommendedItem recommendation : recommendations) {
          System.out.println(recommendation.getItemID());
        }
    }

}

However, I need to run this code online because I am making the application on PHP and that is where my problem arises. Is there a way to run this code on PHP, so I can use the "recommendation" variable?

3
  • Just so I make sure I understand the question correctly. Are you just trying to figure out a way to call Java code from PHP? Commented May 21, 2015 at 13:08
  • yes, in principle that is what I want to do. Commented May 21, 2015 at 13:11
  • do you have any other questions or does my answer work for your question? Commented May 22, 2015 at 11:56

2 Answers 2

0

You can run this java code (compiled first) from php code with shell_exec.

But is a better solution build a REST service (or another) to do it language agnostic.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried doing that, but it does not work. It works well with commands like "echo hello world". But I tried to run a compiled java program and it did not work, it did not print anything, I dont know why. I even tested with shell_exec('printf "asd" ') but that does not work neither.
shell_exec is for run shell commands. If the hello world is running, the compiled java program must run. Have you tried to run the java program directly from shell first ?.
Yes. I tried, actually it has the output I wanted. Perhaps it is because I am on localhost and I missed a step? (something like isntalling java on localhost or IDK). Sorry ig I am a noob :(.
Maybe, check your enviroment config ;)
solved, it was a thing with relative paths.
|
0

There is no simple solution for this. To make it work and communicate with PHP you have to create some interface for it. For example create java servlet, and put it on Servlet container (Java web server). This is simplest I see now.

Other solution you could consider also REST or SOAP service, to exchange data between this Java code and your PHP application. This also will need JavaEE container.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.