通过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 Ctrl + C.

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?

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/