Class: NudgeType::StringRewritingGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/interpreter/types/codeType.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StringRewritingGenerator

Returns a new instance of StringRewritingGenerator.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/interpreter/types/codeType.rb', line 34

def initialize(options = {})
  @incoming_options = options
  @target_size_in_points = options[:target_size_in_points] || 20
  @probabilities = options[:probabilities] || {b:1, r:1, v:1, i:1}
  raise(ArgumentError, "probabilities Hash doesn't have necessary keys") unless
    @probabilities.keys.sort == [:b,:i,:r,:v]
  @probabilities.values.each {|v| raise(ArgumentError, "probabilities must be positive") if v < 0}
  raise(ArgumentError, "probability values must be positive") unless
    @probabilities.values.inject(:+) > 0
  
  
  @reference_names = options[:reference_names] || []
  @type_names = options[:type_names] ||
    NudgeType::all_types.collect {|t| t.to_nudgecode.to_s}
  @instruction_names = options[:instruction_names] ||
    Instruction.all_instructions.collect {|i| i.to_nudgecode.to_s}
end

Instance Attribute Details

#incoming_optionsObject

Returns the value of attribute incoming_options.



26
27
28
# File 'lib/interpreter/types/codeType.rb', line 26

def incoming_options
  @incoming_options
end

#instruction_namesObject

Returns the value of attribute instruction_names.



31
32
33
# File 'lib/interpreter/types/codeType.rb', line 31

def instruction_names
  @instruction_names
end

#probabilitiesObject

Returns the value of attribute probabilities.



28
29
30
# File 'lib/interpreter/types/codeType.rb', line 28

def probabilities
  @probabilities
end

#reference_namesObject

Returns the value of attribute reference_names.



29
30
31
# File 'lib/interpreter/types/codeType.rb', line 29

def reference_names
  @reference_names
end

#target_size_in_pointsObject

Returns the value of attribute target_size_in_points.



27
28
29
# File 'lib/interpreter/types/codeType.rb', line 27

def target_size_in_points
  @target_size_in_points
end

#type_namesObject

Returns the value of attribute type_names.



30
31
32
# File 'lib/interpreter/types/codeType.rb', line 30

def type_names
  @type_names
end

Instance Method Details

#backboneObject



69
70
71
# File 'lib/interpreter/types/codeType.rb', line 69

def backbone
  return "*" * @target_size_in_points
end

#choose_weighted(normalized_hash) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/interpreter/types/codeType.rb', line 53

def choose_weighted(normalized_hash)
  total = @probabilities.values.inject(:+)
  rand_sample = rand(total)
  @probabilities.each do |key,value|
    return key if rand_sample < value
    rand_sample -= value
  end
end

#closed_framework(without_braces = self.open_framework) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/interpreter/types/codeType.rb', line 80

def closed_framework(without_braces = self.open_framework)
  braceless = /[b]([^{])/
  if without_braces[0] == 'b'
    without_braces << "}"
    without_braces.insert(1,'{')
  end
  
  with_some_braces = without_braces
  while with_some_braces.match(braceless)
    where = with_some_braces.index(braceless)
    with_some_braces.insert(where+1, '{')
    close_position = [where+rand(10)+1,with_some_braces.length].min
    while with_some_braces[close_position] == '{'
      close_position += 1
    end
    with_some_braces.insert(close_position,'} ')
  end
  return with_some_braces
end

#filled_framework(recipe = self.closed_framework) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/interpreter/types/codeType.rb', line 101

def filled_framework(recipe = self.closed_framework)
  top, bottom = '',''
  recipe.chars do |c|
    case
    when c == 'b'
      top << 'block '
    when c == 'i'
      top << "do #{instruction_names.sample} "
    when c == 'r'
      ref_name = @reference_names.sample || next_name
      top << "ref #{ref_name} "
    when c == 'v'
      begin
        type_name = @type_names.sample || 'unknown'
        type_class = "#{type_name}_type".camelize.constantize
        reduced_size = rand(@target_size_in_points/4)
        reduced_option = {target_size_in_points:reduced_size}
        sampled_value = type_class.any_value(@incoming_options.merge(reduced_option)).to_s
      rescue NameError
        sampled_value = ""
      end
      top << "value «#{type_name}» "
      bottom << "\n«#{type_name}» #{sampled_value}"
    else
      top << c
    end
  end
  return {:code_part => top.strip, :footnote_part => bottom.strip}
end

#generateObject



63
64
65
66
# File 'lib/interpreter/types/codeType.rb', line 63

def generate
  top_and_bottom = self.filled_framework
  return "#{top_and_bottom[:code_part].strip} \n#{top_and_bottom[:footnote_part].strip}".strip
end

#next_nameObject



132
133
134
# File 'lib/interpreter/types/codeType.rb', line 132

def next_name
  @next_name = (@next_name || "aaa000").succ
end

#open_framework(stars = self.backbone) ⇒ Object



73
74
75
76
77
# File 'lib/interpreter/types/codeType.rb', line 73

def open_framework(stars = self.backbone)
  stars[0]= 'b' if stars.length > 1 # because any long framework bust be in a block
  asteriskless = stars.gsub(/\*/) {|match| choose_weighted(@probabilities).to_s}
  return asteriskless
end