xref: /plugin/aichat/Chunk.php (revision e33a1d7adcbf36c57f516e2f829ec8ad59cdb47b)
1<?php
2
3namespace dokuwiki\plugin\aichat;
4
5class Chunk implements \JsonSerializable
6{
7    /** @var string */
8    protected $page;
9    /** @var int */
10    protected $id;
11    /** @var string */
12    protected $text;
13    /** @var float[] */
14    protected $embedding;
15    /** @var int */
16    protected $created;
17    /** @var int */
18    protected $score;
19    /** @var string */
20    protected $language;
21
22    /**
23     * @param string $page
24     * @param int $id
25     * @param string $text
26     * @param float[] $embedding
27     * @param int $created
28     */
29    public function __construct($page, $id, $text, $embedding, $lang = '', $created = '', $score = 0)
30    {
31        $this->page = $page;
32        $this->id = $id;
33        $this->text = $text;
34        $this->embedding = $embedding;
35        $this->language = $lang ?: $this->determineLanguage();
36        $this->created = $created ?: time();
37        $this->score = $score;
38    }
39
40    public function __toString()
41    {
42        return $this->page . '#' . $this->id;
43    }
44
45    /**
46     * @return int
47     */
48    public function getId()
49    {
50        return $this->id;
51    }
52
53    /**
54     * @param int $id
55     */
56    public function setId($id): void
57    {
58        $this->id = $id;
59    }
60
61    /**
62     * @return string
63     */
64    public function getPage()
65    {
66        return $this->page;
67    }
68
69    /**
70     * @param string $page
71     */
72    public function setPage($page): void
73    {
74        $this->page = $page;
75    }
76
77    /**
78     * @return string
79     */
80    public function getText()
81    {
82        return $this->text;
83    }
84
85    /**
86     * @param string $text
87     */
88    public function setText($text): void
89    {
90        $this->text = $text;
91    }
92
93    /**
94     * @return float[]
95     */
96    public function getEmbedding()
97    {
98        return $this->embedding;
99    }
100
101    /**
102     * @param float[] $embedding
103     */
104    public function setEmbedding($embedding): void
105    {
106        $this->embedding = $embedding;
107    }
108
109    /**
110     * @return int
111     */
112    public function getCreated()
113    {
114        return $this->created;
115    }
116
117    /**
118     * @param int $created
119     */
120    public function setCreated($created): void
121    {
122        $this->created = $created;
123    }
124
125    /**
126     * @return int
127     */
128    public function getScore()
129    {
130        return $this->score;
131    }
132
133    /**
134     * @param int
135     */
136    public function setScore($score): void
137    {
138        $this->score = $score;
139    }
140
141    /**
142     * @return string
143     */
144    public function getLanguage(): string
145    {
146        return $this->language;
147    }
148
149    /**
150     * @param string $language
151     */
152    public function setLanguage($language): void
153    {
154        $this->language = $language;
155    }
156
157    /**
158     * Initialize the language of the chunk
159     *
160     * When the translation plugin is available it is used to determine the language, otherwise the default language
161     * is used.
162     *
163     * @return string The lanuaage code
164     */
165    protected function determineLanguage()
166    {
167        global $conf;
168        /** @var \helper_plugin_translation $trans */
169        $trans = plugin_load('helper', 'translation');
170        if ($trans) {
171            $lc = $trans->realLC($trans->getLangPart($this->page));
172        } else {
173            $lc = $conf['lang'];
174        }
175        return $lc;
176    }
177
178
179    /**
180     * Create a Chunk from a JSON string
181     *
182     * @param string $json
183     * @return Chunk
184     */
185    static public function fromJSON($json)
186    {
187        $data = json_decode($json, true);
188        return new self(
189            $data['page'],
190            $data['id'],
191            $data['text'],
192            $data['embedding'],
193            $data['language'] ?? '',
194            $data['created']
195        );
196    }
197
198    /** @inheritdoc */
199    public function jsonSerialize()
200    {
201        return [
202            'page' => $this->page,
203            'id' => $this->id,
204            'text' => $this->text,
205            'embedding' => $this->embedding,
206            'language' => $this->language,
207            'created' => $this->created,
208        ];
209    }
210}
211