使用PHP的exec函数调用Java类

时间:2015-08-05 16:14:10

标签: java php

我需要将文本拆分成句子,我正在尝试使用Stanford Core NLP。我已经下载了这个库。由于它是一个Java库,我使用PHP的exec命令(请参见下文)来调用它。我的PHP脚本运行良好,我可以将文本解析成句子。目前,脚本需要一个要解析的输入文件。我的问题是我是否可以使用PHP字符串变量而不是输入.txt文件。这对我来说非常方便,因为我将使用mysql db来检索文本/字符串。如果不可能,那么我需要为命令行输入创建相应的文本文件。您提供的任何反馈将非常感谢。

这是我的小PHP脚本

$text = "Maria and Ted Bobola grow sweet corn. But with little corn to harvest because of a plant-withering drought, the Bobolas were forced to buy corn from Georgia to supply their produce and flower shop just outside Dover. And most years, the strawberry picking season runs three to five weeks, but not this year, store manager Dee Chambers said. ``It was so hot and dry that even though we irrigated, we had only two weeks in the season,'' she said. They did not recoup $70,000 they paid for strawberry plants last year in this year's harvest."; 

$parser = "stanford-corenlp-3.5.0.jar";
$class = "edu.stanford.nlp.process.DocumentPreprocessor"; 
$input = "sample.txt"; 
$output = "output.txt";  
if (exec( "java -cp $parser -Xmx2g $class -file $input", $result))
    {
        echo "success";
    }
    else
    {
        echo "failure"; 
    }

// Optional
echo '<pre>' . print_r($result, true); 

我在这里要做的是用$ text(即php变量)替换$ input(即txt文件)。

0 个答案:

没有答案