/** Counts number of semantic relations filtered by language code and type of semantic relation. * @return int */ public static function countRelations($lang_code, $relation_type_name) { $link_db = Piwidict::getDatabaseConnection(); $lang_id = TLang::getIDByLangCode($lang_code); $relation_type_id = TRelationType::getIDByName($relation_type_name); $query = "SELECT meaning_id from relation, lang_pos, meaning where lang_pos.id=meaning.lang_pos_id and meaning.id=relation.meaning_id " . "and relation_type_id=" . (int) $relation_type_id . " and lang_pos.lang_id=" . (int) $lang_id; $result = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); return $link_db->query_count($result); }
/** Gets TRelation object by property $property_name with value $property_value. * @return TRelation or NULL in case of error */ public static function getRelation($property_name, $property_value, $meaning_obj = NULL) { global $LINK_DB; $query = "SELECT * FROM relation WHERE `{$property_name}`='{$property_value}' order by id"; $result = $LINK_DB->query_e($query, "Query failed in " . __METHOD__ . " in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); if ($LINK_DB->query_count($result) == 0) { return NULL; } $relation_arr = array(); while ($row = $result->fetch_object()) { /* if ($meaning_obj == NULL) $meaning_obj = TMeaning::getByID($row->meaning_id); */ $relation_arr[] = new TRelation($row->id, $meaning_obj, TWikiText::getByID($row->wiki_text_id), TRelationType::getByID($row->relation_type_id), $row->meaning_summary); } return $relation_arr; }
use piwidict\Piwidict; //use piwidict\sql\{TLang, TPage, TPOS, TRelationType}; //use piwidict\widget\WForm; require '../config_examples.php'; require '../config_password.php'; include LIB_DIR . "header.php"; // $pw = new Piwidict(); Piwidict::setDatabaseConnection($config['hostname'], $config['user_login'], $config['user_password'], $config['dbname']); $link_db = Piwidict::getDatabaseConnection(); $wikt_lang = "ru"; // Russian language is the main language in ruwikt (Russian Wiktionary) Piwidict::setWiktLang($wikt_lang); $pos_name = "adjective"; $lang_id = TLang::getIDByLangCode("ru"); $pos_id = TPOS::getIDByName($pos_name); $syn_id = TRelationType::getIDByName("synonyms"); $fh1 = fopen('synset_synonyms_only_' . $pos_name . '.txt', 'w'); $fh2 = fopen('synset_all_relations_' . $pos_name . '.txt', 'w'); $query = "SELECT page_title as first_word, meaning.id as meaning_id\n FROM lang_pos, meaning, page \n WHERE lang_pos.id = meaning.lang_pos_id \n AND page.id = lang_pos.page_id\n AND page_title NOT LIKE '% %'\n AND lang_id = {$lang_id}\n AND pos_id={$pos_id}\n ORDER BY page_title"; $result_meaning = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); while ($row = $result_meaning->fetch_object()) { $query = "SELECT wiki_text.text as relation_word, relation_type_id\n FROM wiki_text, relation \n WHERE relation.wiki_text_id=wiki_text.id \n AND wiki_text.text NOT LIKE '% %'\n AND relation.meaning_id = " . $row->meaning_id . " ORDER BY wiki_text.text"; $result_relation = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); $num = $link_db->query_count($result_relation); if ($num > 1) { $synonyms = array(); fwrite($fh2, $row->first_word); while ($row_relation = $result_relation->fetch_object()) { fwrite($fh2, ' ' . $row_relation->relation_word); if ($row_relation->relation_type_id == $syn_id) { $synonyms[] = $row_relation->relation_word;
require '../../../vendor/autoload.php'; use piwidict\Piwidict; use piwidict\sql\{TLang, TPage, TPOS, TRelationType}; use piwidict\widget\WForm; require '../config_examples.php'; require '../config_password.php'; // $pw = new Piwidict(); Piwidict::setDatabaseConnection($config['hostname'], $config['user_login'], $config['user_password'], $config['dbname']); $link_db = Piwidict::getDatabaseConnection(); $wikt_lang = "ru"; // Russian language is the main language in ruwikt (Russian Wiktionary) Piwidict::setWiktLang($wikt_lang); $lang_id = TLang::getIDByLangCode("ru"); $pos_ids = array(TPOS::getIDbyName('noun') => "сущ", TPOS::getIDbyName('adjective') => "прил", TPOS::getIDbyName('verb') => "глаг", TPOS::getIDbyName('adverb') => "нареч"); $relation_type_id = (int) TRelationType::getIDByName("synonyms"); $fh = fopen('ru.wiktionary.with.synonyms.json', 'w'); $query = "SELECT page_title, lang_pos.id as id, pos_id FROM lang_pos,page WHERE lang_pos.page_id = page.id AND lang_id={$lang_id} order by page_title"; print "<p>{$query}"; $result = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); while ($row = $result->fetch_object()) { $def_arr = $synonyms = array(); $is_exists_syn = 0; $query = "SELECT text, meaning.id as meaning_id FROM meaning, wiki_text WHERE lang_pos_id=" . (int) $row->id . " and meaning.wiki_text_id=wiki_text.id order by meaning_n"; $result_meaning = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); if ($link_db->query_count($result_meaning)) { while ($row_meaning = $result_meaning->fetch_object()) { $def_arr[] = PWString::escapeQuotes($row_meaning->text); $query = "SELECT text FROM relation, wiki_text WHERE relation.wiki_text_id=wiki_text.id AND relation.meaning_id=" . (int) $row_meaning->meaning_id . " AND relation_type_id=" . (int) $relation_type_id; //print "<p>$query"; $result_relation = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>");
?> <form action="<?php echo $php_self; ?> " method="GET"> <p>Language: <?php echo TLang::getDropDownList($lang_id, "lang_id", ''); ?> </p> <p>Part of speech: <?php echo TPOS::getDropDownList($pos_id, "pos_id", ''); ?> </p> <p>Relation type: <?php echo TRelationType::getDropDownList($relation_type_id, "relation_type_id", ''); ?> </p> <p>Word: <input type="text" name="page_title" value="<?php echo $page_title; ?> "></p> <p><input type="submit" name="view_list" value="search"></p> </form> <?php if (isset($view_list) && $view_list) { $query_lang_pos = "SELECT lang_pos.id as id, page_title FROM lang_pos, page WHERE lang_pos.page_id=page.id"; if ($lang_id) { $query_lang_pos .= " and lang_id=" . (int) $lang_id; } if ($pos_id) {
// 2. get meaning.id by lang_pos_id $query_meaning = "SELECT id FROM meaning WHERE lang_pos_id=" . $lang_pos_id; $result_meaning = mysqli_query($LINK_DB, $query_meaning) or die("Query failed (line 58) in list_hypo.php: " . mysqli_error() . ". Query: " . $query_meaning); while ($row_m = mysqli_fetch_array($result_meaning)) { $meaning_id = $row_m['id']; // 3. get relation by meaning_id $query_relation = "SELECT wiki_text_id, relation_type_id FROM relation WHERE meaning_id=" . $meaning_id; $result_relation = mysqli_query($LINK_DB, $query_relation) or die("Query failed (line 64) in list_hypo.php: " . mysqli_error() . ". Query: " . $query_relation); while ($row_rel = mysqli_fetch_array($result_relation)) { $relation_type_id = $row_rel['relation_type_id']; $wiki_text_id = $row_rel['wiki_text_id']; // 4. filter by relation type if ($relation_type_id != $relation_type_id_hyponyms && $relation_type_id != $relation_type_id_hypernyms) { continue; } $relation_type_name = TRelationType::getNameByID($relation_type_all, $relation_type_id); // 5. get relation word by $wiki_text_id $query_rwt = "SELECT text FROM wiki_text WHERE id=" . $wiki_text_id; $result_rwt = mysqli_query($LINK_DB, $query_rwt) or die("Query failed (line 76) in list_hypo.php: " . mysqli_error() . ". Query: " . $query_rwt); if ($row_rwt = mysqli_fetch_array($result_rwt)) { $relation_wiki_text = $row_rwt['text']; print "" . $lang_pos->pos->name . ";" . $lang_pos->page->page_title . ";" . $relation_type_name . ";" . $relation_wiki_text . "<BR>"; $counter++; } } // eo relation } // eo meaning // if($counter > 100) // break; }
<p><input type="submit" name="view_list" value="search"></p> </form> <?php if (isset($view_list) && $view_list) { $query_lang_pos = "SELECT lang_pos.id as id, page_title, relation_type_id, wiki_text.text as wiki_text FROM lang_pos, page, relation, meaning, wiki_text " . "WHERE lang_pos.page_id=page.id AND relation.meaning_id=meaning.id AND meaning.lang_pos_id=lang_pos.id AND relation.wiki_text_id=wiki_text.id AND wiki_text.text is not null"; if ($relation_type_id) { $query_lang_pos .= " and relation_type_id=" . (int) $relation_type_id; } if ($lang_id) { $query_lang_pos .= " and lang_id=" . (int) $lang_id; } if ($pos_id) { $query_lang_pos .= " and pos_id=" . (int) $pos_id; } if ($page_title) { $query_lang_pos .= " and page_title like '%{$page_title}%'"; } $query_lang_pos .= " order by page_title, id"; //print $query_lang_pos; $result_lang_pos = $link_db->query_e($query_lang_pos, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); $numAll = $link_db->query_count($result_lang_pos); print "{$numAll} semantic relations (with these parameters) are found"; $result_lang_pos = $link_db->query_e($query_lang_pos . " LIMIT {$start_rec},{$limit}", "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); print "<table border=1>\n"; $counter = $start_rec; while ($row = $result_lang_pos->fetch_object()) { print "<tr><td>" . ++$counter . ".</td><td>" . TPage::getURL($row->page_title) . "</td><td>" . TRelationType::getNameByID($row->relation_type_id) . "</td><td>" . $row->wiki_text . "</td></tr>\n"; } print "</table><br />\n" . WForm::goNextStep($numAll, $limit, $php_self . "?lang_id={$lang_id}&pos_id={$pos_id}&relation_type_id={$relation_type_id}&page_title={$page_title}&view_list=1", 2, "Go to", $step_s); } include LIB_DIR . "footer.php";
use piwidict\Piwidict; //use piwidict\sql\{TLang, TPage, TPOS, TRelationType}; //use piwidict\widget\WForm; require '../config_examples.php'; require '../config_password.php'; include LIB_DIR . "header.php"; // $pw = new Piwidict(); Piwidict::setDatabaseConnection($config['hostname'], $config['user_login'], $config['user_password'], $config['dbname']); $link_db = Piwidict::getDatabaseConnection(); $wikt_lang = "ru"; // Russian language is the main language in ruwikt (Russian Wiktionary) Piwidict::setWiktLang($wikt_lang); $php_self = "antonym_synsets.php"; $lang_name = "ru"; $lang_id = TLang::getIDByLangCode($lang_name); $ant_id = TRelationType::getIDByName("antonyms"); $out_file_name = SITE_ROOT . preg_replace("/^\\/src(\\/.+)\\.php\$/", "data\$1", $php_self); $pos_name = "noun"; //$pos_name = "verb"; //$pos_name = "adjective"; //$pos_name = "adverb"; $pos_id = TPOS::getIDByName($pos_name); //$fh = gzopen($out_file_name.'.txt.gz','wb9'); $fh = gzopen($out_file_name . '_' . $lang_name . '_' . $pos_name . '.txt.gz', 'wb9'); gzwrite($fh, '## Database version: ' . NAME_DB . "\n\n"); $query = "SELECT page_title as first_word, meaning.id as meaning_id\n FROM lang_pos, meaning, page \n WHERE lang_pos.id = meaning.lang_pos_id \n AND page.id = lang_pos.page_id\n AND page_title NOT LIKE '% %'\n AND lang_id = {$lang_id} " . " AND pos_id={$pos_id} " . "ORDER BY page_title"; $result_meaning = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); while ($row = $result_meaning->fetch_object()) { $query = "SELECT wiki_text.text as relation_word\n FROM wiki_text, relation\n WHERE relation.wiki_text_id=wiki_text.id \n AND wiki_text.text NOT LIKE '% %'\n AND relation_type_id = {$ant_id}\n AND relation.meaning_id = " . $row->meaning_id . " ORDER BY wiki_text.text"; $result_relation = $link_db->query_e($query, "Query failed in file <b>" . __FILE__ . "</b>, string <b>" . __LINE__ . "</b>"); $num = $link_db->query_count($result_relation);
/** Gets TRelationType object by ID * @return TRelationType or NULL in case of error */ public static function getByID($_id) { $relation_arr = TRelationType::getRelationType("id", $_id); return $relation_arr[0]; }