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