<?php
// use function Google\Cloud\Samples\PubSub\publish_message;
include "../tools/simple_html_dom.php";
include "../database/DatabaseHandler.php";
include 'MiniCurl.php';
class FillConjuction {
public $url;
public $conjuction;
function __construct($verb) {
// $this->connect ();
$this->conjuction["infinitive"] = ["none"=>$verb];
$this->url = "http://api.verbix.com/conjugator/html?language=deu&tableurl=http://tools.verbix.com/webverbix/personal/template.htm&verb=$verb";
}
public function findConjuctions($verb) {
// if($word)
if ($verb) {
echo $contents = file_get_contents ( $url );
exit();
$regex = '/Participio:<\/b> <span class="normal">(.*?)<\/span>/';
preg_match ( $regex, $contents, $treffer, PREG_OFFSET_CAPTURE, 3 );
$regex = '/Participio:<\/b> <span class="irregular">(.*?)<\/span>/';
preg_match ( $regex, $contents, $trefferIre, PREG_OFFSET_CAPTURE, 3 );
if (! empty ( $treffer [1] [0] ) || ! empty ($trefferIre [1] [0])){
$perfect = $treffer [1] [0];
if(empty($perfect))
$perfect= $trefferIre [1] [0];
$perfect = str_replace("ä", "�", $perfect);
$perfect = str_replace("ü", "�", $perfect);
$perfect = str_replace("ö", "�", $perfect);
}
else
$perfect = "-";
$this->updatePerfect($word,$perfect);
}
return $perfect;
}
public function fillConjuctionObject($part){
// $conjuction = [];
$verbForms["present"] = "Presente";
$verbForms["perfect"] = "Perfecto";
$verbForms["past"] = "Imperfecto";
$verbForms["plustPerfect"] = "Pluscuamperfecto";
$verbForms["future"] = "Futuro";
$verbForms["future2"] = "Futuro anterior";
$verbForms["conditional"] = "Condicional";
$verbForms["imperative"] = "du";
$pForms["ich"] = "ich ";
$pForms["du"] = "du ";
$pForms["es/sie/er"] = ";es";
$pForms["wir"] = "wir ";
$pForms["ihr"] = "ihr ";
$pForms["Sie"] = "Sie ";
$saveKey ="";
$savePerson ="";
for($i=0; $i<sizeof($part); $i++){
$isVerb = true;
foreach ($verbForms as $key=>$form){
if(strstr($part[$i], $form )&& $key != "imperative"){
$saveKey=$key;
$isVerb = false;
}
// echo $part[$i].PHP_EOL.(strpos($part[$i], "du")!== false);
if($key == "imperative" && strpos($part[$i], "du") !== false && strpos($part[$i], "ihr") !== false && strpos($part[$i], ";es") == false ){
// echo $part[$i]."jes".PHP_EOL;
$saveKey=$key;
$isVerb = false;
}
}
foreach ($pForms as $pKey=>$person){
if(strstr($part[$i], $person) && $isVerb){
$savePerson = $pKey;
$isVerb = false;
}
}
// echo "current: ".$part[$i].PHP_EOL;
// echo "person: ".$savePerson.PHP_EOL;
if($isVerb && empty($this->conjuction[$saveKey][$savePerson])){
$this->conjuction[$saveKey][$savePerson] = $part[$i];
// echo $saveKey." ".$savePerson." : ".$part[$i].PHP_EOL;
}
}
// print_r($conjuction);
}
public function createConjuctionArray(){
$tables = $this->getRawTables();
if(empty($tables))
return;
foreach ($tables as $table){
if(sizeof($table)>2)
foreach ($table as $part){
// echo "--------part-------".PHP_EOL;
// print_r($part);
if(sizeof($part)>60 || sizeof($part) == 33 ){
$this->fillConjuctionObject($part);
}
// print_r($part);
}
}
// print_r($this->conjuction);
}
public function getRawTables() {
$miniCurl = new MiniCurl($this->url);
$contents = file_get_contents($this->url);
$html = str_get_html($miniCurl->getData());
// $html = file_get_html ( $this->url );
if (empty ( $html ))
return FALSE;
$tables_html = $html->find ( 'table' );
foreach ( $tables_html as $table_html ) {
$table = array ();
foreach ( $table_html->find ( 'tr' ) as $row ) {
$tr = array ();
foreach ( $row->find ( 'td' ) as $col ) {
$td = $col->plaintext;
$tr [] = $td;
}
$table [] = $tr;
}
$tables [] = $table;
}
return $tables;
}
}
class ConjDatabase extends DatabaseHandler{
public $var;
function __construct(){
parent::__construct("conjuct");
}
public function conjucateVerbs(){
$i=0;
// $verbs = $this->getVerbsFromDatabse();
$verbs = [['word' => 'werden']];
foreach ($verbs as $verb){
if(!empty($verb)){
$i++;
$this->addVerbConjuctions($verb['word']);
echo $i."-".$verb['word'].PHP_EOL;
}
}
}
public function getVerbsFromDatabse(){
$sql = "SELECT word FROM `dictionary` WHERE LENGTH(perfect) >1 AND translationrate = 0 GROUP BY word";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die ( $this->connection->error );
while ($wordArrayResult[] = mysqli_fetch_assoc($result));
return $wordArrayResult;
}
public function addVerbConjuctions($verb){
$conjuctor = new FillConjuction($verb);
$conjuctor->createConjuctionArray();
// print_r($conjuctor->conjuction);
// $verb = str_replace("ã¤", "�", $verb);
foreach ($conjuctor->conjuction as $form=>$conjVerbs){
foreach ($conjVerbs as $person=>$cVerb){
$this->insertVerb($form,$person,$cVerb,$verb);
}
}
}
public function insertVerb($form,$person,$cVerb, $infinitive){
$sql = "INSERT INTO `".DatabaseHandler::$databaseName."`.`dic_conjunction` (`form`,`person`,`verb`,`infinitive`)
VALUES ('"
. $form."' ,'"
. $person."' ,'"
. $cVerb."', '"
. $infinitive."'"
.");";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( $this->connection->error );
$sql = "UPDATE `".DatabaseHandler::$databaseName."`.`dictionary` SET translationrate = 1 where word='$infinitive'";
$this->connection->query ( "SET NAMES utf8" );
$result = $this->connection->query ( $sql ) or die( $this->connection->error );
return 1;
}
}
$conjDb = new ConjDatabase();
$conjDb->conjucateVerbs();
?>