class Lingo::Attendee::HalFilter

Public Instance Methods

control(cmd, *) click to toggle source
# File lib/lingo/attendee/hal_filter.rb, line 49
def control(cmd, *)
  case cmd
    when :EOL       then :skip_command
    when *TERMINALS then send_vectors unless @hal.empty?
  end
end
init() click to toggle source
# File lib/lingo/attendee/hal_filter.rb, line 32
def init
  require_lib('hal4r')

  @lex  = get_re('lexicals', '[sy]')
  @skip = get_ary('skip', DEFAULT_SKIP, :upcase)

  @norm = get_key('norm', true)
  @sep  = get_key('sep', '^')
  @min  = get_flo('min', false)
  @dim  = get_int('dim', 2)

  @sort = get_key('sort', false)
  @sort.downcase! if @sort.respond_to?(:downcase!)

  @hal = Hal4R.new([], get_int('window-size', Hal4R::DEFAULT_WINDOW_SIZE))
end
process(obj) click to toggle source
# File lib/lingo/attendee/hal_filter.rb, line 56
def process(obj)
  obj.is_a?(Word) && !@skip.include?(obj.attr) &&
    # TODO: which lexical to select? (currently: first)
    obj.lex_form(@lex) { |form| @hal << Unicode.downcase(form) }
end

Private Instance Methods

each_vector() { |join, v| ... } click to toggle source
# File lib/lingo/attendee/hal_filter.rb, line 84
def each_vector
  @hal.each_distance(@norm, @dim) { |*t, v| v = 1 / v
    yield [t.join(@sep), v] unless v.nan? || (@min && v < @min) }
end
send_vectors() click to toggle source
# File lib/lingo/attendee/hal_filter.rb, line 64
def send_vectors
  vec = []

  fmt = @sort ? @sort == 'sto' ?
    '%s {%.5f}' : '%2$.5f %1$s' : '%s %.5f' unless @sort == 'normal'

  unless @sort
    each_vector { |v| forward(fmt % v) }
  else
    each_vector { |v| vec << v }

    !fmt ? vec.sort!.each { |v, _| forward(v) } :
      vec.sort_by { |v, w| [-w, v] }.each { |v| forward(fmt % v) }

    vec.clear
  end

  @hal.reset
end