Commit 1a830e6a authored by Martin Gregor's avatar Martin Gregor
Browse files

Pridanie ukladania prekladov a viet

parent d5a93fac
......@@ -8,16 +8,18 @@ use NlpTools\Stemmers\PorterStemmer;
*/
class HomepagePresenter extends BasePresenter
{
protected $sentence_id = null;
private $limit = 10;
private $exchange_count = 2;
private $sentences = [];
private $words = [];
private $en_words = [];
public function renderDefault()
{
$count = $this->db->table('sentences')->count("*");
$next_sentence_id = (time() % $count) + 1;
$current_sentence_id = $next_sentence_id;
$en_words = [];
$sentences = [];
$this->sentence_id = (time() % $count) + 1;
$current_sentence_id = $this->sentence_id;
$sentenceIdSession = $this->getSession('sentenceIdSession');
......@@ -38,8 +40,7 @@ class HomepagePresenter extends BasePresenter
$sentence = $this->db->table('sentences')->get($current_sentence_id);
// rozdelenie vety na slova a inicializacia pola duplicit
$words = explode(' ', $sentence->sentence);
$duplicity = array_fill(0,count($words),0);
$this->words = explode(' ', $sentence->sentence);
// lematizacia skolskou sluzbou
// $curl = new Curl();
......@@ -52,50 +53,15 @@ class HomepagePresenter extends BasePresenter
// $words = explode(' ',$result->body);
// preklad kazdeho slova z vety
foreach ($words as $word)
foreach ($this->words as $word)
{
$en_words[] = $this->translate($word);
}
// nahodny vyber slov z vety na preklad a vytvorenie vety
foreach ($words as $word)
{
$tmp_words = $words;
$counter = 0;
do
{
$position = mt_rand(0,count($words)-1);
$counter++;
} while(($en_words[$position] == null || $duplicity[$position]) && $counter < 30);
if ($counter == 30)
break;
$duplicity[$position] = 1;
$tmp_words[$position] = $en_words[$position]->word;
$sentences[] = implode(' ',$tmp_words);
for ($i = 1; $i < $this->exchange_count; $i++)
{
$next_position = mt_rand(0,count($tmp_words) - $position - 1);
if ($en_words[$position+$next_position] != null)
{
$tmp_words[$position+$next_position] = $en_words[$position+$next_position]->word;
$tmp_sentece = implode(' ',$tmp_words);
if (!in_array($tmp_sentece,$sentences))
{
$sentences[] = $tmp_sentece;
}
}
}
if (count($sentences) == $this->limit)
break;
$this->en_words[] = $this->translate($word);
}
$this->genTranslations();
$this->template->sentence = $sentence->sentence;
$this->template->sentences = $sentences;
$this->template->sentences = $this->sentences;
}
private function translate($word)
......@@ -108,4 +74,66 @@ class HomepagePresenter extends BasePresenter
else
return null;
}
private function generateTransRekurzive($words,$position,$level,$s_words = [])
{
if ($position == count($words))
return;
$this->generateTransRekurzive($words,$position + 1,$level,$s_words);
if ($this->en_words[$position] != null)
{
$words[$position] = $this->en_words[$position]->word;
$sentence = implode(' ',$words);
if (!isset($this->sentences[$sentence]))
{
$s_words[] = $this->en_words[$position]->word;
$this->sentences[$sentence] = $s_words;
}
}
if ($level < $this->exchange_count)
{
$this->generateTransRekurzive($words, $position + 1,$level + 1,$s_words);
}
}
private function genTranslations()
{
$sentences = [];
$this->generateTransRekurzive($this->words,0,1);
if (count($this->sentences) >= $this->limit)
{
// shuffle($this->sentences);
// $this->sentences = array_slice($this->sentences,0,$this->limit);
$sentences = array_rand($this->sentences, $this->limit);
}
$this->storeTranslation($sentences);
$this->sentences = $sentences;
}
private function storeTranslation($sentences)
{
foreach ($sentences as $sentence)
{
$translation_id = $this->db->table('translation')->where(['sentence_id' => $this->sentence_id,'translation' => $sentence])->select('id')->fetch();
if ($translation_id)
{
$this->db->table('translations')->update(['view_count = view_count + 1','updated_at' => date('Y-m-d H:i:s',time())]);
$this->updateWords($this->sentences[$sentence],$translation_id);
}
else
{
$this->db->table('translations')->insert(['sentence_id' => $this->sentence_id,'translation' => $sentence,
'correct_count' => 0, 'incorrect_count' => 0, 'view_count' => 1, 'translated_word_count' => count($this->sentences[$sentence])]);
$this->storeWords($this->sentences[$sentence],$translation_id);
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment