Note that there are some explanatory texts on larger screens.

plurals
  1. POHow I can optimize my PHP script to get phonetical readings of Japanese sentences from Yahoo! Japan API?
    primarykey
    data
    text
    <p>I wrote a PHP script which reads Japanese sentences from file, get the phonetical reading of each sentence using Yahoo! Japan API and writes them to output file. But the script is incredibly slow, it has processed only 50,000 sentences in the last 12 hours on the Apache running on my Mac OS X. Is the call to API the main bottleneck? How can I optimize it? Should I use a language other than PHP? Thanks!</p> <p>Here's how the first 4 lines of the input (examples-utf.utf) file look like:</p> <pre><code>A: ムーリエルは20歳になりました。 Muiriel is 20 now.#ID=1282_4707 B: は 二十歳(はたち){20歳} になる[01]{になりました} A: すぐに戻ります。 I will be back soon.#ID=1284_4709 B: 直ぐに{すぐに} 戻る{戻ります} </code></pre> <p>Here's the XML returned by API on the sentence "私は学生です": <a href="http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=YuLAPtSxg64LZ2dsAQnC334w1wGLxuq9cqp0MIGSO3QjZ1tbZCYaRRWkeRKdUCft7qej73DqEg--&amp;grade=1&amp;sentence=%E7%A7%81%E3%81%AF%E5%AD%A6%E7%94%9F%E3%81%A7%E3%81%99" rel="nofollow">http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=YuLAPtSxg64LZ2dsAQnC334w1wGLxuq9cqp0MIGSO3QjZ1tbZCYaRRWkeRKdUCft7qej73DqEg--&amp;grade=1&amp;sentence=%E7%A7%81%E3%81%AF%E5%AD%A6%E7%94%9F%E3%81%A7%E3%81%99</a></p> <p>My script follows:</p> <pre><code>&lt;?php function getReading($wabun) { $res = ""; $applicationID = "YuLAPtSxg64LZ2dsAQnC334w1wGLxuq9cqp0MIGSO3QjZ1tbZCYaRRWkeRKdUCft7qej73DqEg--"; $grade = 1; $url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=".$applicationID."&amp;grade=".$grade."&amp;sentence=".$wabun; $doc = new DOMDocument(); $doc-&gt;load($url); foreach ($doc-&gt;getElementsByTagName('Word') as $node) { $surface = $node-&gt;getElementsByTagName('Surface')-&gt;item(0)-&gt;nodeValue; $furigana = $node-&gt;getElementsByTagName('Furigana')-&gt;item(0)-&gt;nodeValue; $reading = (isset($furigana)) ? $furigana : $surface; $res .= $reading; } return $res; } ?&gt; &lt;?php header('Content-Type: text/html;charset=utf-8'); $myFile = "examples-utf.utf"; $outFile = "examples-output.utf"; $file = fopen($myFile, 'r') or die("can't open read file"); $out = fopen($outFile, 'w') or die("can't open write file"); $i = 1; // line number $start = 3; // beginning of japanese sentence, after "A: " while($line = fgets($file)) { // line starts at "A: " if($i&amp;1) { $pos = strpos($line, "\t"); $japanese = substr($line, $start, $pos - $start); $end = strpos($line, "#ID=", $pos + 1); $english = substr($line, $pos + 1, $end - $pos - 1); $reading = getReading($japanese); fwrite($out, $japanese."\n"); fwrite($out, $english."\n"); fwrite($out, $reading."\n"); } ++$i; } fclose($out); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload