Skip to content

Commit

Permalink
lib/lingo/database/source*: Use default constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Jun 23, 2016
1 parent 4f6a8ca commit 1d5ada9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/lingo.rb
Expand Up @@ -331,9 +331,9 @@ def warn(*msg)
require_relative 'lingo/filter'
require_relative 'lingo/array_utils'
require_relative 'lingo/text_utils'
require_relative 'lingo/language'
require_relative 'lingo/progress'
require_relative 'lingo/database'
require_relative 'lingo/language'
require_relative 'lingo/attendee'
require_relative 'lingo/version'

Expand Down
10 changes: 7 additions & 3 deletions lib/lingo/database/source.rb
Expand Up @@ -51,13 +51,17 @@ class Source

LEXICAL_SEPARATOR = '#'.freeze

DEFAULT_SEPARATOR = nil

DEFAULT_DEF_WC = nil

def self.get(name, *args)
Lingo.get_const(name, self).new(*args)
end

attr_reader :pos

def initialize(id, lingo, def_wc_default = nil)
def initialize(id, lingo)
@config = lingo.database_config(id)

source_file = Lingo.find(:dict, name = @config['name'], relax: true)
Expand All @@ -72,9 +76,9 @@ def initialize(id, lingo, def_wc_default = nil)

raise SourceFileNotFoundError.new(name, id) unless @src.exist?

@def = @config.fetch('def-wc', def_wc_default)
@sep = @config.fetch('separator', self.class::DEFAULT_SEPARATOR)
@def = @config.fetch('def-wc', self.class::DEFAULT_DEF_WC)
@def = @def.downcase if @def
@sep = @config['separator']

@wrd = "(?:#{Language::Char::ANY})+"
@pat = /^#{@wrd}$/
Expand Down
6 changes: 4 additions & 2 deletions lib/lingo/database/source/key_value.rb
Expand Up @@ -41,9 +41,11 @@ class KeyValue < self

DEFAULT_SEPARATOR = '*'.freeze

DEFAULT_DEF_WC = Language::LA_UNKNOWN

def initialize(id, lingo)
super(id, lingo, Language::LA_UNKNOWN)
@pat = /^(#{@wrd})#{Regexp.escape(@sep ||= DEFAULT_SEPARATOR)}(#{@wrd})$/
super
@pat = /^(#{@wrd})#{Regexp.escape(@sep)}(#{@wrd})$/
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/lingo/database/source/multi_key.rb
Expand Up @@ -44,7 +44,7 @@ class MultiKey < self

def initialize(id, lingo)
super
@pat = /^#{@wrd}(?:#{Regexp.escape(@sep ||= DEFAULT_SEPARATOR)}#{@wrd})*$/
@pat = /^#{@wrd}(?:#{Regexp.escape(@sep)}#{@wrd})*$/
end

def set(db, key, val)
Expand Down
2 changes: 1 addition & 1 deletion lib/lingo/database/source/multi_value.rb
Expand Up @@ -42,7 +42,7 @@ class MultiValue < self

def initialize(id, lingo)
super
@pat = /^#{@wrd}(?:#{Regexp.escape(@sep ||= DEFAULT_SEPARATOR)}#{@wrd})*$/
@pat = /^#{@wrd}(?:#{Regexp.escape(@sep)}#{@wrd})*$/
end

def set(db, key, val)
Expand Down
4 changes: 3 additions & 1 deletion lib/lingo/database/source/single_word.rb
Expand Up @@ -38,8 +38,10 @@ class Source

class SingleWord < self

DEFAULT_DEF_WC = Language::LA_NOUN

def initialize(id, lingo)
super(id, lingo, Language::LA_NOUN)
super
@pat = /^(#{@wrd})$/
@mul = @config.fetch('def-mul-wc', @def).downcase
end
Expand Down
20 changes: 14 additions & 6 deletions lib/lingo/database/source/word_class.rb
Expand Up @@ -44,14 +44,21 @@ class WordClass < self

GENDER_SEPARATOR = '.'.freeze

VALUE_SEPARATOR = '|'.freeze

WC_SEPARATOR = '#'.freeze

SCAN_RE = /(\S.*?)\s*#{WC_SEPARATOR}(\S+)/o

def initialize(id, lingo)
super

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

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

@pat = /^(#{@wrd})#{sep}((?:#{@wrd}#{wc})+)$/
end
Expand All @@ -61,10 +68,11 @@ def initialize(id, lingo)
def convert_line(line, key, val)
values = []

val.strip.scan(/(\S.*?)\s*#(\S+)/) { |k, v|
v, f = v.split('.')
val.strip.scan(SCAN_RE) { |k, v|
v, f = v.split(GENDER_SEPARATOR)
f = f ? f.split(VALUE_SEPARATOR) : [nil]

combinations(v.split('|'), f ? f.split('|') : [nil]) { |w, g|
combinations(v.split(VALUE_SEPARATOR), f) { |w, g|
values << lexical(k, w, g)
}
}
Expand Down

0 comments on commit 1d5ada9

Please sign in to comment.