HomepagePresenter.php 5.85 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

        // lematizacia skolskou sluzbou
Martin Gregor's avatar
Martin Gregor committed
46
//        $this->lematize($sentence);
47
48

        // preklad kazdeho slova z vety
49
        foreach ($this->words as $word)
50
        {
51
            $this->en_words[]     = $this->translate($word);
52
53
        }

54
        $this->genTranslations();
55
56

        $this->template->sentence   = $sentence->sentence;
57
        $this->template->sentences  = $this->sentences;
58
    }
Martin Gregor's avatar
Martin Gregor committed
59

60
61
62
63
64
65
66
67
68
69
    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;
    }
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    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]))
            {
Martin Gregor's avatar
Martin Gregor committed
84
                $s_words[$position] = $this->en_words[$position]->word;
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
                $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);

        $this->sentences = $sentences;
    }

    private function storeTranslation($sentences)
    {
        foreach ($sentences as $sentence)
        {
Martin Gregor's avatar
Martin Gregor committed
113
            $translation_id = $this->db->table('translations')->where(['sentence_id' => $this->sentence_id,'translation' => $sentence])->select('id')->fetch();
114
115
116
117

            if ($translation_id)
            {
                $this->db->table('translations')->update(['view_count = view_count + 1','updated_at' => date('Y-m-d H:i:s',time())]);
Martin Gregor's avatar
Martin Gregor committed
118
119

                $this->storeWords($this->sentences[$sentence],$translation_id);
120
121
122
123
124
            }
            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])]);
Martin Gregor's avatar
Martin Gregor committed
125
                $this->storeWords($this->sentences[$sentence],$this->db->lastInsertId());
126
            }
Martin Gregor's avatar
Martin Gregor committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        }
    }

    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]])->select('id')->fetch();
            if ($word_en_id)
            {
                $exists = $this->db->table('word_translation')->where(['word_en_id' => $word_en_id, 'word_sk_id' => $word_sk_id, 'translation_id' => $translation_id])->count('*');
                if (!$exists)
                {
                    $this->db->table('word_translation')->insert(['word_en_id' => $word_en_id, 'word_sk_id' => $word_sk_id, 'translation_id' => $translation_id, 'word_position' => $position]);
                }
143

Martin Gregor's avatar
Martin Gregor committed
144
145
146
147
148
149
150
            }
            else
            {
                $word_sk = $this->db->table('word_sk')->insert(['word_sk' => $this->words[$position]]);
                $word_en = $this->db->table('word_en')->insert(['word_en' => $word]);
                $this->db->table('word_translation')->insert(['word_en_id' => $word_en->id, 'word_sk_id' => $word_sk->id, 'translation_id' => $translation_id, 'word_position' => $position]);
            }
151
        }
Martin Gregor's avatar
Martin Gregor committed
152
153
154
155
156
157
158
159
160
161
    }

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

Martin Gregor's avatar
Martin Gregor committed
163
        $this->words = explode(' ',$result->body);
164
    }
Martin Gregor's avatar
Martin Gregor committed
165
}