xref: /plugin/aichat/Chunk.php (revision 7ebc78955c65af90e7ee0afbd07adc15271113ba)
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    public function getLanguage(): string
142    {
143        return $this->language;
144    }
145
146    /**
147     * @param string $language
148     */
149    public function setLanguage($language): void
150    {
151        $this->language = $language;
152    }
153
154    /**
155     * Initialize the language of the chunk
156     *
157     * When the translation plugin is available it is used to determine the language, otherwise the default language
158     * is used.
159     *
160     * @return string The lanuaage code
161     */
162    protected function determineLanguage()
163    {
164        global $conf;
165        /** @var \helper_plugin_translation $trans */
166        $trans = plugin_load('helper', 'translation');
167        if ($trans) {
168            $lc = $trans->realLC($trans->getLangPart($this->page));
169        } else {
170            $lc = $conf['lang'];
171        }
172        return $lc;
173    }
174
175
176    /**
177     * Create a Chunk from a JSON string
178     *
179     * @param string $json
180     * @return Chunk
181     */
182    public static function fromJSON($json)
183    {
184        $data = json_decode($json, true);
185        return new self(
186            $data['page'],
187            $data['id'],
188            $data['text'],
189            $data['embedding'],
190            $data['language'] ?? '',
191            $data['created']
192        );
193    }
194
195    /** @inheritdoc */
196    public function jsonSerialize()
197    {
198        return [
199            'page' => $this->page,
200            'id' => $this->id,
201            'text' => $this->text,
202            'embedding' => $this->embedding,
203            'language' => $this->language,
204            'created' => $this->created,
205        ];
206    }
207}
208