dongqian9567 2015-05-21 04: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 06: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 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
  • ¥15 python梯形积分与GPS测得位移使用卡尔曼滤波融合问题
  • ¥15 匈牙利算法分割求损失问题
  • ¥30 ABAQUS 实体单元如何在分析步中分步多次施加绕某轴的运动?
  • ¥15 docker部署redis集群,springboot启动报错DefaultClusterTopologyRefresh,而且第一个节点的IP也变了
  • ¥15 关于延迟常微分方程DDE的分岔图
  • ¥15 putimage函数输出错误
  • ¥15 进行刷新动态页面和点击进入一个动态,会包含在一个网络五元组会话内吗
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部