2012-12-14 42 views
0

我找到了一種名爲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> 
+1

你正試圖通過你的JavaScript加載到你的PHP腳本頁面上的內容? – Sherif

+0

不,JavaScript僅用於將搜索引擎放入此頁面。 – Arcius

+0

我明白,但這個內容,你想交給你的PHP腳本......我*假設*它來自你正在使用的搜索引擎API,並且它將內容填充到你的頁面的div與id *內容**?否則我不明白你的問題。 – Sherif

回答

1

所以你基本上與您的JavaScript API,你要交給PHP內容填充一個div。做到這一點的最簡單方法是通過調用您的PHP腳本的ajax來調用JavaScript,就像您的API執行填充頁面的方式一樣。除此之外,您將把內容交給PHP,然後找回結果,然後您可以將其放入頁面的任何位置,比如說您想要的另一個div

<div id="content">Loading...</div> 

您可以通過使用帶有javascript的文檔的getElementById屬性來訪問此內容。

<script> 
$.ajax({ 
    type: "POST", 
    data: {"text": document.getElementById('content').innerHTML }, 
    url: "http://www.example.com/your-php-script.php", // put the URL to your PHP script here 
}).done(function (data) { 
    document.getElementById('myOutPutDiv').innerHTML = data; 
}); 
</script> 

那麼你的HTML將有輸出相應的div

<div id="myOutPutDiv">Output goes here...</div> 

然後在你的PHP腳本您將收到$ _ POST/$ _ REQUEST超全局...

數據

只要確保將PHP從JavaScript中分離出來,因爲它們將獨立執行。

+0

但是,每次執行搜索權時,「內容」都會有所不同?它不是一個URL或文本,所以無論如何我可以把這些'內容'在getElementById()? – Arcius

+0

是的,每次你打電話給你的API時,你都要調用你的PHP腳本來獲取在該div中填充的新內容,並將它傳遞給你的PHP腳本。你必須弄清楚如何在你的API上設置一個回調函數,以便它更新div,然後將更新後的div發送給PHP,如上所示。 – Sherif

相關問題