Class: CueIndex

Inherits:
Object
  • Object
show all
Includes:
SimpleDSL
Defined in:
lib/rbbt/ner/rnorm/cue_index.rb

Defined Under Namespace

Classes: LexiconMissingError

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, &block) ⇒ CueIndex

Returns a new instance of CueIndex.



15
16
17
18
19
20
21
22
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 15

def initialize(file = nil, &block)
  @rules   = []

  file ||= Rbbt.share.rnorm.cue_default.produce if !file && !block

  file = file.find if file.respond_to? :find
  load_config(:define, file, &block)
end

Instance Method Details

#clean(max) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 37

def clean(max)
  @indexes.each{|index|
    remove = []
    index.each{|key,values|
      remove << key if values.length > max
    }
    remove.each{|key|
      index.delete(key)
    }
  }
end

#configObject



24
25
26
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 24

def config
  @config[:define]
end

#cues(word) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 29

def cues(word)
  @rules.collect{|rule|
    c = rule[1].call(word)
    c = [c] unless  c.is_a? Array 
    c
  }
end

#define(name, *args, &block) ⇒ Object



10
11
12
13
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 10

def define(name, *args, &block)
  @rules << [name,block]
  nil
end

#load(file, max_candidates = 50) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 49

def load(file, max_candidates = 50)
  @indexes = Array.new(@rules.size){Hash.new}
  data = TSV === file ? file : TSV.open(file, :type => :flat, :unnamed => true)
  data.each{|code, values|
    values.each{|value|
      cues(value).each_with_index{|cue_list,i|
        cue_list.each{|cue|
          @indexes[i][cue] ||= Set.new
          @indexes[i][cue]  << code unless @indexes[i][cue].include? code
        }
      }
    }
  }
  clean(max_candidates) if max_candidates
  nil
end

#match(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbbt/ner/rnorm/cue_index.rb', line 66

def match(name)
  raise LexiconMissingError, "Load Lexicon before matching" unless @indexes

  cues = cues(name)
  @indexes.each_with_index{|index,i|
    best = []
    cues[i].each{|cue|
      best << index[cue].to_a if index[cue]
    }
    return best.flatten if best.any?
  }

  return []
end