dqef7931 2018-06-11 14:44
浏览 154
已采纳

通过PHP执行linux命令

I am using the tool for language analysis called Freeling. For language identification I use the analyzer command in the Linux console:

analyzer -f /usr/share/freeling/config/ident.cfg --outlv ident --fidn /usr/share/freeling/common/lang_ident/ident.dat

When I execute this command, its wait for the text entries (sentences) and determine what language they are in. When I write a line of text in Spanish: "la casa es azul" and I press the enter key returns ES which means that it is written in Spanish. If I write "the house is blue" it returns EN, for the English language. To interrupt its execution press <kbd>Ctrl</kbd> + <kbd>C</kbd>.

When I execute this command in the Linux console the first sentence takes some time to respond and the other times responds quickly.

I use this code to execute this php command but it takes many seconds to return the result:

<?php
$cmd = "analyzer -f /usr/share/freeling/config/ident.cfg --outlv ident --fidn /usr/share/freeling/common/lang_ident/ident.dat";

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w"),
);

$process = proc_open($cmd, $descriptorspec, $pipes);

$oracion[0]="we are the word";
$oracion[1]="somos el mundo";

if (is_resource($process)) {

    fwrite($pipes[0], $oracion[0]);
    fwrite($pipes[0], $oracion[1]);
    fclose($pipes[0]);

    while($pdf_content = fgets($pipes[1]))
    {
    echo $pdf_content . "<br>";
    }
    fclose($pipes[1]);
    $return_value = proc_close($process);

}
?>

How can I improve the response time?

展开全部

  • 写回答

1条回答 默认 最新

  • doubang9906 2018-06-13 01:25
    关注

    When you run the program, it needs to load the statistical models for all languages. That's why the first sentence takes longer.

    The "analyzer" program provided with FreeLing is just a demo. The best way to use the library is to write your own program, as shown in FreLing tutorial https://talp-upc.gitbooks.io/freeling-tutorial/content/

    If you want to call FreeLing from PHP, there is no API, so you should probably resort to "server mode", where freeling is loaded once, and then it will accept requests.

    You'll find more information in FreeLing user manual https://talp-upc.gitbooks.io/freeling-4-1-user-manual/content/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部