Created
February 3, 2012 11:04
-
-
Save siahr/1729681 to your computer and use it in GitHub Desktop.
日本語WordNetの辞書(SQLite3)から同義語を取り出す。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WN4P: To access the Japanese WordNet thesaurus dictionary. | |
* | |
* @package Sugina | |
* @copyright Copyright 2012, Toshio HIRAI <[email protected]> | |
* @license http://opensource.org/licenses/BSD-3-Clause The BSD License | |
* | |
*/ | |
/* | |
* 辞書の再配布に関しては、以下に示すリンク先の記載内容を遵守すること。 | |
* About the redistribution of the dictionary, | |
* Comply with the restrictions that are described in the following links. | |
* | |
* http://nlpwww.nict.go.jp/wn-ja/ | |
* http://nlpwww.nict.go.jp/wn-ja/jpn/downloads.html | |
* http://nlpwww.nict.go.jp/wn-ja/license.txt | |
*/ | |
class WN4P { | |
/** Connection to SQLite3 databse.*/ | |
private $db; | |
/** | |
* Constructor. | |
* | |
* @param Pass to `wnjpn.db`(A thesaurus dictionary) | |
* @throws Exception: throws when connection failed. | |
*/ | |
public function __construct($dict) { | |
try { | |
$this->db = new SQLite3($dict, SQLITE3_OPEN_READONLY); | |
} catch (Exception $e) { | |
throw new Exception("Unable to connect to the SQLite3 database: ".$dict, 0, $e); | |
} | |
} | |
/** | |
* Close Databse. | |
* | |
*/ | |
public function close() { | |
$this->db->close(); | |
} | |
/** | |
* Destructor. | |
* | |
*/ | |
public function __destruct() { | |
$this->close(); | |
} | |
/** | |
* Returns the list of synonym of the specified word. | |
* | |
* @param string $word The word. | |
* @param string $lang The language of the word to capture. ('jpn'|'eng'|null:default(both)) | |
* @return array The array of synonyms. | |
*/ | |
public function getSynonyms($word, $lang=null) { | |
$synonyms = array(); | |
$words = $this->getWord($word); | |
foreach($words as $w) { | |
$senses = $this->getSynset($w['wordid']); | |
foreach($senses as $sense) { | |
$wordIds = $this->getWordId($sense['synset'], $lang); | |
foreach($wordIds as $wordId) { | |
$synonym = $this->getLemma($wordId['wordid']); | |
$synonyms[] = $synonym[0]['lemma']; | |
} | |
} | |
} | |
return $synonyms; | |
} | |
/** | |
* Returns the list of WordId. | |
* | |
* @param string $lemma The lemma (an entry word). | |
* @return array The array of word id. | |
*/ | |
private function getWord($lemma) { | |
return $this->getRows("select wordid from word where lemma = :lemma", array(':lemma'=>$lemma)); | |
} | |
/** | |
* Returns the lemma. | |
* | |
* @param string $wordid The word id. | |
* @return array The array of lemma. | |
*/ | |
private function getLemma($wordid) { | |
return $this->getRows("select lemma from word where wordid = :wordid", array(':wordid'=>$wordid)); | |
} | |
/** | |
* Returns the synset. | |
* | |
* @param string $wordid The word id. | |
* @return array The array of synset (A set of one or more synonyms). | |
*/ | |
private function getSynset($wordid) { | |
return $this->getRows("select synset from sense where wordid = :wordid", array(':wordid'=>$wordid)); | |
} | |
/** | |
* Returns the list of wordid. | |
* | |
* @param string $synset The synset. | |
* @param string $lang The Language. | |
* @return array The array of wordid. | |
*/ | |
private function getWordId($synset, $lang) { | |
if (!empty($lang)) { | |
return $this->getRows("select wordid from sense where synset = :synset and lang = :lang", | |
array(':synset'=>$synset, ':lang'=>$lang)); | |
} else { | |
return $this->getRows("select wordid from sense where synset = :synset", array(':synset'=>$synset)); | |
} | |
} | |
/** | |
* Executes SQL, and returns the result. | |
* | |
* @param string $sql Skeleton of the SQL. | |
* @param string $params The variable of binding params and values. | |
*/ | |
private function getRows($sql, $params) { | |
$stmt = $this->db->prepare($sql); | |
foreach($params as $param=>$value) { | |
$stmt->bindValue($param, $value); | |
} | |
$result = $stmt->execute(); | |
$rows = array(); | |
while($res = $result->fetchArray(SQLITE3_ASSOC)){ | |
$rows[] = $res; | |
} | |
return $rows; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment