class ConlangWordGenerator::SymbolSet

Public Class Methods

new() click to toggle source
# File lib/conlang/symbolset.rb, line 7
def initialize
  @pairs = []
  @weight_accum = 0
end

Public Instance Methods

+(other) click to toggle source

Make Symbolset class appendable

# File lib/conlang/symbolset.rb, line 51
def +(other)
  Append.new(self, other)
end
add_pair(symbol, weight) click to toggle source

Add new symbol with its probability into the set

# File lib/conlang/symbolset.rb, line 13
def add_pair(symbol, weight)
  unless weight > 0 and weight < 100
    raise LangSyntaxError, "Weight of symbol '#{symbol}' must be " +
                           "a value between 1 and 99, inclusive."
  end

  # Insert new pair in the sorted array of pairs
  inserted = false
  @pairs.each_index do |index|
    if @pairs[index][1] > weight
      @pairs.insert(index, [symbol, weight])
      inserted = true
      break
    end
  end

  # Insert at the end, if requiered
  @pairs += [[symbol, weight]] unless inserted
  
  # Sum weights
  @weight_accum += weight
end
sample() click to toggle source

Get a random symbol from the set

# File lib/conlang/symbolset.rb, line 37
def sample
  random = rand(@weight_accum)

  accum = 0
  @pairs.each_index do |index|
    accum += @pairs[index][1]

    if accum > random
      return @pairs[index][0]
    end
  end
end