Class: Mitie::NER

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, pointer: nil) ⇒ NER

Returns a new instance of NER.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mitie/ner.rb', line 5

def initialize(path = nil, pointer: nil)
  if path
    # better error message
    raise ArgumentError, "File does not exist" unless File.exist?(path)
    @pointer = FFI.mitie_load_named_entity_extractor(+path)
    @pointer.free = FFI["mitie_free"]
  elsif pointer
    @pointer = pointer
  else
    raise ArgumentError, "Must pass either a path or a pointer"
  end
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



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

def pointer
  @pointer
end

Instance Method Details

#doc(text) ⇒ Object



24
25
26
# File 'lib/mitie/ner.rb', line 24

def doc(text)
  Document.new(self, text)
end

#entities(text) ⇒ Object



28
29
30
# File 'lib/mitie/ner.rb', line 28

def entities(text)
  doc(text).entities
end

#save_to_disk(filename) ⇒ Object



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

def save_to_disk(filename)
  if FFI.mitie_save_named_entity_extractor(+filename, pointer) != 0
    raise Error, "Unable to save model"
  end
  nil
end

#tagsObject



18
19
20
21
22
# File 'lib/mitie/ner.rb', line 18

def tags
  FFI.mitie_get_num_possible_ner_tags(pointer).times.map do |i|
    FFI.mitie_get_named_entity_tagstr(pointer, i).to_s
  end
end

#tokens(text) ⇒ Object



39
40
41
# File 'lib/mitie/ner.rb', line 39

def tokens(text)
  doc(text).tokens
end

#tokens_with_offset(text) ⇒ Object



43
44
45
# File 'lib/mitie/ner.rb', line 43

def tokens_with_offset(text)
  doc(text).tokens_with_offset
end