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