class Lingo::Database::Source::WordClass

Constants

DEFAULT_SEPARATOR
GENDER_SEPARATOR
SCAN_RE
VALUE_SEPARATOR
WC_SEPARATOR

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/lingo/database/source/word_class.rb, line 53
def initialize(*)
  super

  gen = Regexp.escape(GENDER_SEPARATOR)
  val = Regexp.escape(VALUE_SEPARATOR)
  sep = Regexp.escape(@sep)

  w, a = "\\w%1$s(?:#{val}\\w%1$s)*", '[+]?'
  wc   = "#{WC_SEPARATOR}#{w % a}(?:#{gen}#{w % ''})?"

  @pat = /^(#{@wrd})#{sep}((?:#{@wrd}#{wc})+)$/
end

Public Instance Methods

dump_line(key, val, key_sep = nil, val_sep = nil, compact = true, *) click to toggle source
# File lib/lingo/database/source/word_class.rb, line 81
def dump_line(key, val, key_sep = nil, val_sep = nil, compact = true, *)
  "#{key}#{key_sep || @sep}#{dump_values(val, compact).join(val_sep || ' ')}"
end
dump_values(val, compact = true) click to toggle source
# File lib/lingo/database/source/word_class.rb, line 85
def dump_values(val, compact = true)
  join = lambda { |v|
    v.compact!; v.uniq!; v.sort!; v.join(VALUE_SEPARATOR) }

  if compact
    values = Hash.new { |h, k| h[k] = [[], []] }; val.each { |lex|
      a, g = values[lex.form]; a << lex.attr; g << lex.gender }
  else
    values = val.map { |lex| [lex.form, [[lex.attr], [lex.gender]]] }
  end

  values.sort.map { |form, (attrs, genders)|
    res = "#{form} #{WC_SEPARATOR}#{join[attrs]}"
    genders.any? ? "#{res}#{GENDER_SEPARATOR}#{join[genders]}" : res
  }
end
parse_line(line, key, val) click to toggle source
# File lib/lingo/database/source/word_class.rb, line 66
def parse_line(line, key, val)
  values = []

  val.strip.scan(SCAN_RE) { |k, v|
    v, f = v.split(GENDER_SEPARATOR)
    f = f ? f.split(VALUE_SEPARATOR) : [nil]

    combinations(v.split(VALUE_SEPARATOR), f) { |w, g|
      values << lexical(k, w, g)
    }
  }

  [key.strip, values]
end