db->table('sentences')->count("*"); $this->sentence_id = (time() % $count) + 1; $current_sentence_id = $this->sentence_id; $sentenceIdSession = $this->getSession('sentenceIdSession'); // inicializacia session if (!is_array($sentenceIdSession->ids) || count($sentenceIdSession->ids) == $count) { $sentenceIdSession->ids = []; } // vyhladanie nezobrazenej vety while (in_array($current_sentence_id,$sentenceIdSession->ids)) { $current_sentence_id = mt_rand(1, $count); } // ulozenie id vybranej vety a ziskanie vety z db $sentenceIdSession->ids[] = $current_sentence_id; // else // { // $current_sentence_id = $this->sentence_id = $_POST['sentence_id']; // } $sentence = $this->db->table('sentences')->get($current_sentence_id); // rozdelenie vety na slova a inicializacia pola duplicit $this->words = explode(' ', $sentence->sentence); // lematizacia skolskou sluzbou $this->lematize($sentence->sentence); // preklad kazdeho slova z vety foreach ($this->words_lema as $word) { $this->en_words[] = $this->translate($word); } $this->genTranslations(); $this->template->sentence = $sentence->sentence; } } protected function createComponentTranslationForm() { $form = new Nette\Application\UI\Form(); foreach ($this->sentences as $id => $sentence) { $label = \Nette\Utils\Html::el()->setHtml('
'.$sentence[0].'
'); $form->addRadioList('sentence'.$id,$label,['correct' => 'Správne','incorrect' => 'Nesprávne']); foreach ($sentence[1] as $position => $word) { $form->addText($id.'word'.$position,'Preklad '.$word,40,100); } $form->addText('note'.$id,'Poznámka',60,500); } $form->addHidden('sentence_id',$this->sentence_id); $form->addSubmit('send','Ďalej'); $form->onSuccess[] = $this->translationFormSucceeded; return $form; } public function translationFormSucceeded(Nette\Application\UI\Form $form) { $values = $_POST;//$form->getValues(TRUE); $sentences = []; foreach ($values as $id => $value) { if (strstr($id,'sentence') && $id != 'sentence_id') { $s_id = intval(str_replace('sentence','',$id)); $sentences[$s_id]['type'] = $value; if ($value) $this->correctTranslation($s_id,$value); if ($_POST['note'.$s_id]) $sentences[$s_id]['note'] = $_POST['note'.$s_id]; } else if (strstr($id,'word')) { $tmp = explode('word',$id); if ($value) $sentences[intval($tmp[0])]['words'][$tmp[1]] = $value; } } foreach ($sentences as $id => $values) { if ($values['type'] == 'incorrect' && (isset($values['words']) || isset($values['note']))) { $tmp = $this->db->table('translations')->get($id); $tmp = explode(' ',$tmp->translation); foreach ($values['words'] as $position => $word) { $tmp[$position] = $word; } $correction = implode(' ',$tmp); $this->storeCorrection($id,$correction,$values['words'],$values['note']); } } $this->redirect('default'); } private function correctTranslation($id, $value) { if ($value == 'correct') { $correct_count = $this->db->table('translations')->where(['id' => $id])->select('correct_count')->fetch()->correct_count; $this->db->table('translations')->where(['id' => $id])->update(['correct_count' => $correct_count + 1]); } else { $incorrect_count = $this->db->table('translations')->where(['id' => $id])->select('incorrect_count')->fetch()->incorrect_count; $this->db->table('translations')->where(['id' => $id])->update(['incorrect_count' => $incorrect_count + 1]); } } private function storeCorrection($trans_id,$correction,$words,$comment) { $correction_id = $this->db->table('corrections')->select('id')->where(['translation_id' => $trans_id,'correction' => $correction])->fetch(); if ($correction_id) { $correction_id = $correction_id->id; $use_count = $this->db->table('corrections')->select('use_count')->where(["id" => $correction_id])->fetch()->use_count; $this->table('corrections')->where(['id' => $correction_id])->update(['use_count' => $use_count + 1,'updated_at' => date('Y-m-d H:i:s',time())]); } else { $this->db->table('corrections')->insert(['use_count' => 1, 'translation_id' => $trans_id, 'correction' => $correction,'correction_count' => count($words)]); $correction_id = $this->db->lastInsertId(); foreach ($words as $position => $word) { $word_en_id = $this->db->table('word_en')->select('id')->where(['word_en' => $word])->fetch(); if (!$word_en_id) { $this->db->table('word_en')->insert(['word_en' => $word]); $word_en_id = $this->db->lastInsertId(); } else { $word_en_id = $word_en_id->word_en_id; } $word_sk_id = $this->db->table('word_translation')->select('word_sk_id') ->where(['translation_id' => $trans_id,'word_position' => $position])->fetch(); if ($word_sk_id->word_sk_id) $this->db->table('word_correction')->insert(['correction_id' => $correction_id,'word_sk_id' => $word_sk_id->word_sk_id, 'word_en_id' => $word_en_id]); } } if ($comment) { $this->db->table('comments')->insert(['correction_id' => $correction_id,'comment' => $comment]); } } private function translate($word) { $word_en = $this->db->query('SELECT dict_en.word as word, dict_en.rank3 as rank, dict_sk_en.type as type'. ' FROM dict_sk_en LEFT JOIN dict_sk ON dict_sk_en.sk_id = dict_sk.id JOIN dict_en ON dict_en.id = dict_sk_en.en_id'. ' WHERE dict_sk.word = ? ORDER BY original_pos',$word)->fetch(); if ($word_en) return $word_en; 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[$position] = $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) { $sentences = array_rand($this->sentences, $this->limit); } $this->storeTranslation($sentences); } private function storeTranslation($sentences) { $tmp_sentences = []; foreach ($sentences as $sentence) { $translation_id = $this->db->table('translations')->where(['sentence_id' => $this->sentence_id,'translation' => $sentence])->select('id')->fetch(); if ($translation_id) { $translation_id = $translation_id->id; $view_count = $this->db->table('translations')->select('view_count')->where(['id' => $translation_id])->fetch()->view_count; $this->db->table('translations')->where(['id' => $translation_id])->update(['view_count' => $view_count + 1,'updated_at' => date('Y-m-d H:i:s',time())]); $this->storeWords($this->sentences[$sentence],$translation_id); } else { $this->db->table('translations')->insert(['sentence_id' => $this->sentence_id,'translation' => $sentence, 'lemas' => implode(' ',$this->words_lema), 'correct_count' => 0, 'incorrect_count' => 0, 'view_count' => 1, 'translated_word_count' => count($this->sentences[$sentence])]); $translation_id = $this->db->lastInsertId(); $this->storeWords($this->sentences[$sentence],$translation_id); } $tmp_sentences[$translation_id] = [$sentence,$this->sentences[$sentence]]; } $this->sentences = $tmp_sentences; } private function storeWords($words, $translation_id) { foreach ($words as $position => $word) { $word_en_id = $this->db->table('word_en')->where(['word_en' => $word])->select('id')->fetch(); $word_sk_id = $this->db->table('word_sk')->where(['word_sk' => $this->words[$position],'word_lema' => $this->words_lema[$position]])->select('id')->fetch(); if (!$word_en_id) $word_en_id = $this->db->table('word_en')->insert(['word_en' => $word]); if (!$word_sk_id) $word_sk_id = $this->db->table('word_sk')->insert(['word_sk' => $this->words[$position], 'word_lema' => $this->words_lema[$position]]); $exists = $this->db->table('word_translation')->where(['word_en_id' => $word_en_id->id, 'word_sk_id' => $word_sk_id->id, 'translation_id' => $translation_id])->count('*'); if (!$exists) { $this->db->table('word_translation')->insert(['word_en_id' => $word_en_id->id, 'word_sk_id' => $word_sk_id->id, 'translation_id' => $translation_id, 'word_position' => $position]); } } } private function lematize($sentence) { $curl = new Curl(); $curl->headers = [ 'Content-Type' => 'text/plain' ]; $result = $curl->post('http://text.fiit.stuba.sk:8080/lematizer/services/lemmatizer/lemmatize/fast',$sentence); $this->words_lema = explode(' ',$result->body); } }