Method: Ariel::Learner#learn_rule

Defined in:
lib/ariel/learner.rb

#learn_rule(direction, exhaustive = false) ⇒ Object

Initiates and operates the whole rule induction process. Finds an example to use as its seed example, then finds a rule that matches the maximum number of examples correctly and fails on all overs. All matched examples are then removed and the process is repeated considering all examples that remain. Returns an array of the rules found (in order). learn_rule will take care of reversing the given examples if necessary.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ariel/learner.rb', line 30

def learn_rule(direction, exhaustive=false)
  Log.debug "Searching for a #{direction} rule"
  @examples=@examples.collect {|tokenstream| Rule.prepare_tokenstream(tokenstream, direction)}
  @direction=direction
  @exhaustive=exhaustive
  if exhaustive
    @examples.delete_if {|example| example_is_unsuitable?(example)}
    raise StandardError, "No examples are suitable for exhaustive rule learning" if @examples.empty?
  end
  @current_rule=Rule.new([], direction, exhaustive)
  combined_rules=[]
  while not @examples.empty?
    set_seed unless @examples.include? @current_seed
    rule = find_best_rule() # Find the rule that matches the most examples and fails on the others
    prev_size = @examples.size
    @examples.delete_if {|example| rule.apply_to(example)} #separate and conquer!
    Log.debug "Removing #{prev_size - @examples.size} examples matched by the generated rule, #{@examples.size} remain"
    combined_rules << rule
  end
#      rule = order_rule(rule) #STALKER paper suggests that the generated rules should be ordered. This doesn't make sense, seeing as they are all generated based only on examples not matched by previous rules
  Log.debug "Generated rules: #{combined_rules.inspect}"
  Rule.clear_cache
  return combined_rules
end