Class: Mitie::NERTrainingInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/mitie/ner_training_instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ NERTrainingInstance

Returns a new instance of NERTrainingInstance.

Raises:



5
6
7
8
9
10
11
# File 'lib/mitie/ner_training_instance.rb', line 5

def initialize(tokens)
  tokens_pointer = Utils.array_to_pointer(tokens)

  @pointer = FFI.mitie_create_ner_training_instance(tokens_pointer)
  raise Error, "Unable to create training instance. Probably ran out of RAM." if @pointer.null?
  @pointer.free = FFI["mitie_free"]
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



3
4
5
# File 'lib/mitie/ner_training_instance.rb', line 3

def pointer
  @pointer
end

Instance Method Details

#add_entity(range, label) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mitie/ner_training_instance.rb', line 13

def add_entity(range, label)
  Utils.check_range(range, num_tokens)

  raise ArgumentError, "Range overlaps existing entity" if overlaps_any_entity?(range)

  unless FFI.mitie_add_ner_training_entity(@pointer, range.begin, range.size, +label).zero?
    raise Error, "Unable to add entity to training instance. Probably ran out of RAM."
  end

  nil
end

#num_entitiesObject



25
26
27
# File 'lib/mitie/ner_training_instance.rb', line 25

def num_entities
  FFI.mitie_ner_training_instance_num_entities(@pointer)
end

#num_tokensObject



29
30
31
# File 'lib/mitie/ner_training_instance.rb', line 29

def num_tokens
  FFI.mitie_ner_training_instance_num_tokens(@pointer)
end

#overlaps_any_entity?(range) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/mitie/ner_training_instance.rb', line 33

def overlaps_any_entity?(range)
  Utils.check_range(range, num_tokens)

  FFI.mitie_overlaps_any_entity(@pointer, range.begin, range.size) == 1
end