Skip to content

Instantly share code, notes, and snippets.

@siahr
Created February 3, 2012 11:04

Revisions

  1. siahr revised this gist Feb 6, 2012. 1 changed file with 86 additions and 12 deletions.
    98 changes: 86 additions & 12 deletions WN4P.php
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,118 @@
    <?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);
    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->getWords($word);
    $senses = $this->getSensesByWordId($words[0]['wordid']);
    foreach($senses as $sens) {
    $wordIdArray = $this->getSensesBySynset($sens['synset'], $lang);
    foreach($wordIdArray as $wordid) {
    $synonym = $this->getWord($wordid['wordid']);
    $synonyms[] = $synonym[0]['lemma'];
    $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;
    }

    private function getWords($lemma) {
    /**
    * 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));
    }

    private function getWord($wordid) {
    /**
    * 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));
    }

    private function getSensesByWordId($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));
    }

    private function getSensesBySynset($synset, $lang) {
    /**
    * 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));
    @@ -53,6 +121,12 @@ private function getSensesBySynset($synset, $lang) {
    }
    }

    /**
    * 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) {
  2. siahr revised this gist Feb 3, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WN4P.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ class WN4P {

    public function __construct($dict) {
    try {
    $this->db = new SQLite3($dict);
    $this->db = new SQLite3($dict, SQLITE3_OPEN_READONLY);
    } catch (Exception $e) {
    throw new Exception("Unable to connect to the SQLite3 database: ".$dict);
    }
  3. siahr revised this gist Feb 3, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions WN4P.php
    Original file line number Diff line number Diff line change
    @@ -33,23 +33,23 @@ public function getSynonyms($word, $lang=null) {
    }

    private function getWords($lemma) {
    return $this->getRows("select * from word where lemma = :lemma", array(':lemma'=>$lemma));
    return $this->getRows("select wordid from word where lemma = :lemma", array(':lemma'=>$lemma));
    }

    private function getWord($wordid) {
    return $this->getRows("select * from word where wordid = :wordid", array(':wordid'=>$wordid));
    return $this->getRows("select lemma from word where wordid = :wordid", array(':wordid'=>$wordid));
    }

    private function getSensesByWordId($wordid) {
    return $this->getRows("select * from sense where wordid = :wordid", array(':wordid'=>$wordid));
    return $this->getRows("select synset from sense where wordid = :wordid", array(':wordid'=>$wordid));
    }

    private function getSensesBySynset($synset, $lang) {
    if (!empty($lang)) {
    return $this->getRows("select * from sense where synset = :synset and lang = :lang",
    return $this->getRows("select wordid from sense where synset = :synset and lang = :lang",
    array(':synset'=>$synset, ':lang'=>$lang));
    } else {
    return $this->getRows("select * from sense where synset = :synset", array(':synset'=>$synset));
    return $this->getRows("select wordid from sense where synset = :synset", array(':synset'=>$synset));
    }
    }

  4. siahr created this gist Feb 3, 2012.
    69 changes: 69 additions & 0 deletions WN4P.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?php
    class WN4P {
    private $db;

    public function __construct($dict) {
    try {
    $this->db = new SQLite3($dict);
    } catch (Exception $e) {
    throw new Exception("Unable to connect to the SQLite3 database: ".$dict);
    }
    }

    public function close() {
    $this->db->close();
    }

    public function __destruct() {
    $this->close();
    }

    public function getSynonyms($word, $lang=null) {
    $synonyms = array();
    $words = $this->getWords($word);
    $senses = $this->getSensesByWordId($words[0]['wordid']);
    foreach($senses as $sens) {
    $wordIdArray = $this->getSensesBySynset($sens['synset'], $lang);
    foreach($wordIdArray as $wordid) {
    $synonym = $this->getWord($wordid['wordid']);
    $synonyms[] = $synonym[0]['lemma'];
    }
    }
    return $synonyms;
    }

    private function getWords($lemma) {
    return $this->getRows("select * from word where lemma = :lemma", array(':lemma'=>$lemma));
    }

    private function getWord($wordid) {
    return $this->getRows("select * from word where wordid = :wordid", array(':wordid'=>$wordid));
    }

    private function getSensesByWordId($wordid) {
    return $this->getRows("select * from sense where wordid = :wordid", array(':wordid'=>$wordid));
    }

    private function getSensesBySynset($synset, $lang) {
    if (!empty($lang)) {
    return $this->getRows("select * from sense where synset = :synset and lang = :lang",
    array(':synset'=>$synset, ':lang'=>$lang));
    } else {
    return $this->getRows("select * from sense where synset = :synset", array(':synset'=>$synset));
    }
    }

    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;
    }
    }
    ?>