-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChunk.php
204 lines (182 loc) · 3.88 KB
/
Chunk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace dokuwiki\plugin\aichat;
class Chunk implements \JsonSerializable, \Stringable
{
/** @var int */
protected $created;
/** @var string */
protected $language;
/**
* @param string $page
* @param int $id
* @param string $text
* @param float[] $embedding
* @param int $created
* @param int $score
*/
public function __construct(
protected $page,
protected $id,
protected $text,
protected $embedding,
$lang = '',
$created = '',
protected $score = 0
) {
$this->language = $lang ?: $this->determineLanguage();
$this->created = $created ?: time();
}
public function __toString(): string
{
$string = $this->page . '#' . $this->id;
if ($this->score) {
$string .= sprintf(' (%.2f)', $this->score);
}
return $string;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getPage()
{
return $this->page;
}
/**
* @param string $page
*/
public function setPage($page): void
{
$this->page = $page;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @param string $text
*/
public function setText($text): void
{
$this->text = $text;
}
/**
* @return float[]
*/
public function getEmbedding()
{
return $this->embedding;
}
/**
* @param float[] $embedding
*/
public function setEmbedding($embedding): void
{
$this->embedding = $embedding;
}
/**
* @return int
*/
public function getCreated()
{
return $this->created;
}
/**
* @param int $created
*/
public function setCreated($created): void
{
$this->created = $created;
}
/**
* @return int
*/
public function getScore()
{
return $this->score;
}
/**
* @param int
*/
public function setScore($score): void
{
$this->score = $score;
}
public function getLanguage(): string
{
return $this->language;
}
/**
* @param string $language
*/
public function setLanguage($language): void
{
$this->language = $language;
}
/**
* Initialize the language of the chunk
*
* When the translation plugin is available it is used to determine the language, otherwise the default language
* is used.
*
* @return string The lanuaage code
*/
protected function determineLanguage()
{
global $conf;
/** @var \helper_plugin_translation $trans */
$trans = plugin_load('helper', 'translation');
if ($trans) {
$lc = $trans->realLC($trans->getLangPart($this->page));
} else {
$lc = $conf['lang'];
}
return $lc;
}
/**
* Create a Chunk from a JSON string
*
* @param string $json
* @return Chunk
*/
public static function fromJSON($json)
{
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
return new self(
$data['page'],
$data['id'],
$data['text'],
$data['embedding'],
$data['language'] ?? '',
$data['created']
);
}
/** @inheritdoc */
public function jsonSerialize()
{
return [
'page' => $this->page,
'id' => $this->id,
'text' => $this->text,
'embedding' => $this->embedding,
'language' => $this->language,
'created' => $this->created,
];
}
}