HomepagePresenter.php 4.67 KB
Newer Older
Martin Gregor's avatar
Martin Gregor committed
1
2
<?php

3
4
5
use NlpTools\Stemmers\PorterStemmer;
//use Curl;

Martin Gregor's avatar
Martin Gregor committed
6
7
8
9
10
/**
 * Homepage presenter.
 */
class HomepagePresenter extends BasePresenter
{
11
    protected $sentence_id  = null;
12
13
    private $limit          = 10;
    private $exchange_count = 2;
14
15
16
    private $sentences      = [];
    private $words          = [];
    private $en_words       = [];
17
18
19

    public function renderDefault()
    {
20
        $count                  = $this->db->table('sentences')->count("*");
21
22
        $this->sentence_id      = (time() % $count) + 1;
        $current_sentence_id    = $this->sentence_id;
23
24
25
26
27
28
29
30

        $sentenceIdSession = $this->getSession('sentenceIdSession');

        // inicializacia session
        if (!is_array($sentenceIdSession->ids) || count($sentenceIdSession->ids) == $count)
        {
            $sentenceIdSession->ids = [];
        }
31

32
33
        // vyhladanie nezobrazenej vety
        while (in_array($current_sentence_id,$sentenceIdSession->ids))
34
35
36
37
        {
            $current_sentence_id = mt_rand(1, $count);
        }

38
39
40
41
42
        // ulozenie id vybranej vety a ziskanie vety z db
        $sentenceIdSession->ids[]   = $current_sentence_id;
        $sentence                   = $this->db->table('sentences')->get($current_sentence_id);

        // rozdelenie vety na slova a inicializacia pola duplicit
43
        $this->words      = explode(' ', $sentence->sentence);
44
45
46
47
48
49
50
51
52
53
54
55

        // lematizacia skolskou sluzbou
        //        $curl = new Curl();
        //
        //        $curl->headers = [
        //            'Content-Type' => 'text/plain'
        //        ];
        //        $result = $curl->post('http://text.fiit.stuba.sk:8080/lematizer/services/lemmatizer/lemmatize/fast','test');
        //
        //        $words = explode(' ',$result->body);

        // preklad kazdeho slova z vety
56
        foreach ($this->words as $word)
57
        {
58
            $this->en_words[]     = $this->translate($word);
59
60
        }

61
        $this->genTranslations();
62
63

        $this->template->sentence   = $sentence->sentence;
64
        $this->template->sentences  = $this->sentences;
65
    }
Martin Gregor's avatar
Martin Gregor committed
66

67
68
69
70
71
72
73
74
75
76
    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;
    }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

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

        }

    }
Martin Gregor's avatar
Martin Gregor committed
139
}