dongqian9567 2015-05-21 12:54
浏览 70
已采纳

在PHP代码中运行Java程序[重复]

This question already has an answer here:

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?

</div>
  • 写回答

2条回答 默认 最新

  • dongxiangxie8181 2015-05-21 14:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果