-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNERTrainingInstance.php
57 lines (43 loc) · 1.52 KB
/
NERTrainingInstance.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
<?php
namespace Mitie;
class NERTrainingInstance
{
public $pointer;
private $ffi;
public function __construct($tokens)
{
$this->ffi = FFI::instance();
$tokensPointer = Utils::arrayToPointer($tokens);
$this->pointer = $this->ffi->mitie_create_ner_training_instance($tokensPointer);
if (is_null($this->pointer)) {
throw new Exception('Unable to create training instance. Probably ran out of RAM.');
}
}
public function __destruct()
{
FFI::mitie_free($this->pointer);
}
public function addEntity($start, $end, $label)
{
Utils::checkRange($start, $end, $this->numTokens());
if ($this->overlapsAnyEntity($start, $end)) {
throw new \InvalidArgumentException('Range overlaps existing entity');
}
if ($this->ffi->mitie_add_ner_training_entity($this->pointer, $start, $end - $start + 1, $label) != 0) {
throw new Exception('Unable to add entity to training instance. Probably ran out of RAM.');
}
}
public function numEntities()
{
return $this->ffi->mitie_ner_training_instance_num_entities($this->pointer);
}
public function numTokens()
{
return $this->ffi->mitie_ner_training_instance_num_tokens($this->pointer);
}
public function overlapsAnyEntity($start, $end)
{
Utils::checkRange($start, $end, $this->numTokens());
return $this->ffi->mitie_overlaps_any_entity($this->pointer, $start, $end - $start + 1) == 1;
}
}