我找到了一種名爲mecab的軟件,它基本上讀取一個字符串,並將這些單詞分類爲名詞,動詞等。我正在嘗試使用Google搜索API把搜索功能放在我的頁面上,所以每當我查找一個位置,例如牛津街,將顯示一些結果,並且mecab將單獨地採取這些結果並且完成它的工作。如何從網頁中提取字符串並將其放入PHP變量中
我堅持的是我不知道如何將這些結果餵給mecab。
這裏是代碼:
<html>
<head></head>
<body>
<script type="text/javascript" src="http://www.google.com/jsapi?key=AIzaSyBX85AAhYSkh66lk8i2VBSqVJSY_462zGM"></script>
<script type="text/javascript">
function OnLoad()
{
// Create Search Control
var searchControl = new google.search.SearchControl();
// Add searcher to Search Control
searchControl.addSearcher(new google.search.WebSearch());
searchControl.draw(document.getElementById('content'));
// Execute search
searchControl.execute('新宿');
}
// Load Google Search API
google.load('search', '1');
google.setOnLoadCallback(OnLoad);
</script>
<div id="content">Loading...</div>
<?php
define('Mecab_Encoding', 'SJIS');
define('Mecab_ResultEncoding', 'UTF-8');
define('MeCab_Path', 'mecab.exe');
function morph_analysis($text) {
$text = mb_convert_encoding($text, Mecab_Encoding, Mecab_ResultEncoding);
$descriptorspec = array (
0 => array ("pipe", "r"), // stdin
1 => array ("pipe", "w") // stdout
);
$process = proc_open(MeCab_Path, $descriptorspec, $pipes);
if (is_resource($process)) {
// Feed string to macab
fwrite($pipes[0], $text);
fclose($pipes[0]);
// Read the string
while (!feof($pipes[1])) {
$result .= fread($pipes[1], 4096);
}
fclose($pipes[1]);
proc_close($process);
$result = mb_convert_encoding($result, Mecab_ResultEncoding, Mecab_Encoding);
$lines = explode("\r\n", $result);
$res = array();
foreach($lines as $line) {
if(in_array(trim($line), array('EOS', ''))) {continue;}
$s = explode("\t", $line);
$word = $s[0];
$words = explode(',', $s[1]);
if ($words[0] == "名詞"){
$res[] = array(
'word' => $word,
'class' => $words[0],
'detail1' => $words[1],
'detail2' => $words[2],
'detail3' => $words[3],
'conjugation1' => $words[4],
'conjugation2' => $words[5]
);
}
}
return $res;
} else {
return false;
}
}
$text ="今日はいい天気です。";
$result = morph_analysis($text);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
</body>
</html>
你正試圖通過你的JavaScript加載到你的PHP腳本頁面上的內容? – Sherif
不,JavaScript僅用於將搜索引擎放入此頁面。 – Arcius
我明白,但這個內容,你想交給你的PHP腳本......我*假設*它來自你正在使用的搜索引擎API,並且它將內容填充到你的頁面的div與id *內容**?否則我不明白你的問題。 – Sherif