Class: Sbn::StringVariable

Inherits:
Variable show all
Defined in:
lib/sbn/string_variable.rb

Constant Summary collapse

DEFAULT_NGRAM_SIZES =
[3, 5, 10]

Constants inherited from Variable

Variable::NEGLIGIBLE_PROBABILITY

Instance Attribute Summary

Attributes inherited from Variable

#children, #name, #parents, #probability_table, #states

Instance Method Summary collapse

Methods inherited from Variable

#==, #===, #add_child, #add_parent, #can_be_evaluated?, #eql?, #evaluate_marginal, #evidence_name, #get_observed_state, #get_random_state, #get_random_state_with_markov_blanket, #set_probabilities, #set_probabilities_from_sample_points!, #set_probability, #set_states, #to_s

Constructor Details

#initialize(net, name = '') ⇒ StringVariable

Returns a new instance of StringVariable.



50
51
52
53
54
55
56
# File 'lib/sbn/string_variable.rb', line 50

def initialize(net, name = '')
  @net = net
  @covariables = {}
  @covariable_children = []
  @covariable_parents = []
  super(net, name, [], [])
end

Instance Method Details

#add_child_no_recurse(variable) ⇒ Object

This node never has any parents or children. It just sets the parents or children of its covariables.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sbn/string_variable.rb', line 123

def add_child_no_recurse(variable) # :nodoc:
  return if variable == self or @covariable_children.include?(variable)
  if variable.is_a?(StringVariable)
    @covariable_children.concat variable.covariables
    @covariables.each {|ng, covar| variable.covariables.each {|varcovar| covar.add_child(varcovar) } }
  else
    @covariable_children << variable
    @covariables.each {|ng, covar| covar.add_child(variable) }
  end
  variable.generate_probability_table
end

#add_covariable(covariable) ⇒ Object

This method is used when reconstituting saved networks



115
116
117
118
119
# File 'lib/sbn/string_variable.rb', line 115

def add_covariable(covariable) # :nodoc:
  @covariable_children.each {|child| covariable.add_child(child) }
  @covariable_parents.each {|parent| covariable.add_parent(parent) }
  @covariables[covariable.text_to_match] = covariable
end

#add_parent_no_recurse(variable) ⇒ Object

:nodoc:



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sbn/string_variable.rb', line 135

def add_parent_no_recurse(variable) # :nodoc:
  return if variable == self or @covariable_parents.include?(variable)
  if variable.is_a?(StringVariable)
    @covariable_parents.concat variable.covariables
    @covariables.each {|ng, covar| variable.covariables.each {|varcovar| covar.add_parent(varcovar) } }
  else
    @covariable_parents << variable
    @covariables.each {|ng, covar| covar.add_parent(variable) }
  end
  generate_probability_table
end

#add_sample_point(evidence) ⇒ Object

create co-variables when new n-grams are encountered



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sbn/string_variable.rb', line 59

def add_sample_point(evidence) # :nodoc:
  val = evidence[@name].downcase.strip
  len = val.length
  ngrams = []
  
  # Make ngrams as small as 3 characters in length up to
  # the length of the string.  We may need to whittle this
  # down significantly to avoid severe computational burdens.
  DEFAULT_NGRAM_SIZES.each {|n| ngrams.concat val.ngrams(n) }
  ngrams.uniq!
  ngrams.each do |ng|
    unless @covariables.has_key?(ng)
      # these probabilities are temporary and will get erased after learning
      newcovar = StringCovariable.new(@net, @name, ng, [0.5, 0.5])
      count = 0
      @covariable_parents.each {|p| newcovar.add_parent(p) }
      @covariable_children.each {|p| newcovar.add_child(p) }
      @covariables[ng] = newcovar
    end
    @covariables[ng].add_sample_point(evidence)
  end
end

#covariablesObject

returns an array of the variable’s string covariables in alphabetical order



83
84
85
86
87
# File 'lib/sbn/string_variable.rb', line 83

def covariables # :nodoc:
  returnval = []
  @covariables.keys.sort.each {|key| returnval << @covariables[key] }
  returnval
end

#generate_probability_tableObject

:nodoc:



147
148
149
# File 'lib/sbn/string_variable.rb', line 147

def generate_probability_table # :nodoc:
  @covariables.each {|ng, covar| covar.generate_probability_table }
end

#is_complete_evidence?(evidence) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/sbn/string_variable.rb', line 151

def is_complete_evidence?(evidence) # :nodoc:
  parent_names = @covariable_parents.map {|p| p.name.to_s }
  super(evidence) {|varnames| varnames.concat(parent_names) }
end

#set_in_evidence?(evidence) ⇒ Boolean

This node never influences the probabilities. Its sole responsibility is to manage the co-variables, so it should always appear to be set in the evidence so that it won’t waste time in the inference process.

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/sbn/string_variable.rb', line 109

def set_in_evidence?(evidence) # :nodoc:
  true
  # raise "String variables should never be used in inference--only their covariables"
end

#to_xmlbif_definition(xml) ⇒ Object

:nodoc:



101
102
103
# File 'lib/sbn/string_variable.rb', line 101

def to_xmlbif_definition(xml) # :nodoc:
  # string variables do not have any direct probabilities--only their covariables
end

#to_xmlbif_variable(xml) ⇒ Object

:nodoc:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sbn/string_variable.rb', line 89

def to_xmlbif_variable(xml) # :nodoc:
  super(xml) do |x|
    covars = @covariables.keys.sort
    parents = @covariable_parents.map {|p| p.name }
    x.property("Covariables = #{covars.join(',')}") unless covars.empty?

    # A string variable's parents cannot be specified in the "given"
    # section below, because only its covariables actually have them.
    x.property("Parents = #{parents.join(',')}") unless parents.empty?
  end
end

#transform_evidence_value(val) ⇒ Object

:nodoc:



156
157
158
# File 'lib/sbn/string_variable.rb', line 156

def transform_evidence_value(val) # :nodoc:
  val.to_s.downcase
end