HomepagePresenter.php 3.61 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
12
13
14
15
    private $limit          = 10;
    private $exchange_count = 2;

    public function renderDefault()
    {
16
17
18
        $count                  = $this->db->table('sentences')->count("*");
        $next_sentence_id       = (time() % $count) + 1;
        $current_sentence_id    = $next_sentence_id;
19
20
21
22
23
24
25
26
27
28
        $en_words   = [];
        $sentences  = [];

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

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

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

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        // 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
        $words      = explode(' ', $sentence->sentence);
        $duplicity  = array_fill(0,count($words),0);

        // 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
        foreach ($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->template->sentence   = $sentence->sentence;
        $this->template->sentences  = $sentences;
    }
Martin Gregor's avatar
Martin Gregor committed
100

101
102
103
104
105
106
107
108
109
110
    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;
    }
Martin Gregor's avatar
Martin Gregor committed
111
}