Class: CrossLanguageSpotter::Context
- Inherits:
-
Object
- Object
- CrossLanguageSpotter::Context
- Defined in:
- lib/crosslanguagespotter/context.rb
Instance Attribute Summary collapse
-
#sequence_of_values ⇒ Object
readonly
Returns the value of attribute sequence_of_values.
Instance Method Summary collapse
- #clone ⇒ Object
- #count ⇒ Object
- #declarators_per_value(value) ⇒ Object
- #has_value?(v) ⇒ Boolean
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #intersection(values) ⇒ Object
- #intersection!(values) ⇒ Object
- #merge(other) ⇒ Object
- #register(value, declarator) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
25 26 27 28 29 |
# File 'lib/crosslanguagespotter/context.rb', line 25 def initialize @map = Hash.new {|h,k| h[k]=[]} @sequence_of_values = [] @register_sequence = [] end |
Instance Attribute Details
#sequence_of_values ⇒ Object (readonly)
Returns the value of attribute sequence_of_values.
23 24 25 |
# File 'lib/crosslanguagespotter/context.rb', line 23 def sequence_of_values @sequence_of_values end |
Instance Method Details
#clone ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/crosslanguagespotter/context.rb', line 65 def clone new_instance = Context.new @register_sequence.each do |r| new_instance.register(r[:value],r[:declarator]) end new_instance end |
#count ⇒ Object
35 36 37 |
# File 'lib/crosslanguagespotter/context.rb', line 35 def count values.count end |
#declarators_per_value(value) ⇒ Object
43 44 45 |
# File 'lib/crosslanguagespotter/context.rb', line 43 def declarators_per_value(value) @map[value] end |
#has_value?(v) ⇒ Boolean
39 40 41 |
# File 'lib/crosslanguagespotter/context.rb', line 39 def has_value?(v) values.include?(v) end |
#intersection(values) ⇒ Object
73 74 75 76 77 |
# File 'lib/crosslanguagespotter/context.rb', line 73 def intersection(values) new_instance = self.clone new_instance.intersection!(values) new_instance end |
#intersection!(values) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/crosslanguagespotter/context.rb', line 79 def intersection!(values) @map.keys.each do |k| if values.is_a? Array @map[k] = [] unless values.include?(k) elsif values.is_a? Context if values.has_value?(k) @map[k].concat(values.declarators_per_value(k)) else @map[k] = [] end else raise "error" end end self end |
#merge(other) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/crosslanguagespotter/context.rb', line 57 def merge(other) other.values.each do |v| other.declarators_per_value(v).each do |d| register(v,d) end end end |
#register(value, declarator) ⇒ Object
51 52 53 54 55 |
# File 'lib/crosslanguagespotter/context.rb', line 51 def register(value,declarator) @sequence_of_values << value @map[value] << declarator unless @map[value].include?(declarator) @register_sequence << {value:value, declarator:value} end |
#to_a ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/crosslanguagespotter/context.rb', line 100 def to_a a = [] values.sort.each do |v| a << {value:v,declarators:declarators_per_value(v)} end a end |
#to_s ⇒ Object
108 109 110 |
# File 'lib/crosslanguagespotter/context.rb', line 108 def to_s to_a.to_s end |
#values ⇒ Object
31 32 33 |
# File 'lib/crosslanguagespotter/context.rb', line 31 def values @map.keys.select {|k| @map[k].count>0} end |