Class: Ariel::CandidateRefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/ariel/candidate_refiner.rb

Overview

Given an array of candidate Rules, and an array of LabeledStreams, allows heuristics to be applied to select the ideal Rule. All refine_* instance methods will remove candidates from the internal candidates array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(candidates, examples) ⇒ CandidateRefiner

Returns a new instance of CandidateRefiner.



9
10
11
12
# File 'lib/ariel/candidate_refiner.rb', line 9

def initialize(candidates, examples)
  @candidates=candidates.dup #Just in case we directly modify the array. Shouldn't happen.
  @examples=examples
end

Instance Attribute Details

#candidatesObject

Returns the value of attribute candidates.



8
9
10
# File 'lib/ariel/candidate_refiner.rb', line 8

def candidates
  @candidates
end

Instance Method Details

#random_from_remainingObject

Returns a random candidate. Meant for making the final choice in case previous selections have still left multiple candidates.



64
65
66
67
# File 'lib/ariel/candidate_refiner.rb', line 64

def random_from_remaining
  Log.debug "Selecting random from last #{candidates.size} candidate rules"
  @candidates.sort_by {rand}.first
end

#refine_by_fewer_wildcardsObject



31
32
33
34
35
# File 'lib/ariel/candidate_refiner.rb', line 31

def refine_by_fewer_wildcards
  Log.debug "Refining to the rules with the fewest wildcards"
  @candidates = highest_scoring_by {|rule| -rule.wildcard_count} #hack or not?
  return @candidates
end

#refine_by_label_proximityObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ariel/candidate_refiner.rb', line 37

def refine_by_label_proximity
  Log.debug "Selecting rules that match the examples closest to the label"
  @candidates = highest_scoring_by do |rule|
    rule_score=0
    matched_examples=0
    @examples.each do |example|
      match_index = rule.closest_match(example)
      if match_index.nil?
        next
      else
        rule_score+= (example.label_index - match_index).abs
        matched_examples+=1
      end
    end
    rule_score = rule_score.to_f/matched_examples unless matched_examples==0 #mean distance from label_index
    -rule_score #So highest scoring = closest to label index.
  end
  return @candidates
end

#refine_by_longer_end_landmarksObject



57
58
59
60
# File 'lib/ariel/candidate_refiner.rb', line 57

def refine_by_longer_end_landmarks
  Log.debug "Selecting rules that have longer end landmarks"
  @candidates = highest_scoring_by {|rule| rule.landmarks.last.size unless rule.landmarks.last.nil?}
end

#refine_by_match_type(*match_types) ⇒ Object

Selects the Rule candidates that have the most matches of a given type against the given examples. e.g. select_best_by_match_type(:early, :perfect) will select the rules that have the most matches that are early or perfect.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ariel/candidate_refiner.rb', line 18

def refine_by_match_type(*match_types)
  Log.debug "Refining by match types #{match_types.inspect}"
  return @candidates if @candidates.size==1
  @candidates = highest_scoring_by do |rule|
    rule_score=0
    @examples.each do |example|
      rule_score+=1 if rule.matches(example, *match_types)
    end
    rule_score #why doesn't return rule_score raise an error?
  end
  return @candidates
end