Class: Kotoshu::Readers::CompoundRule

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/readers/aff_data.rb

Overview

Compound rule pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ CompoundRule

Create a new compound rule.

Parameters:

  • text (String)

    The rule text (e.g., “A*B?CD”)



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/kotoshu/readers/aff_data.rb', line 207

def initialize(text)
  @text = text
  # Parse flags from rule text
  if text.include?('(')
    @flags = text.scan(/\((.+?)\)/).flatten.to_set
    parts = text.scan(/\([^*?]+?\)[*?]?/)
  else
    @flags = text.gsub(/[*?]/, '').chars.to_set
    # Handle ) as a flag character (used in sv dictionaries)
    parts = text.gsub(/(?<=[^*?])\)/, '\\)').gsub(/([^*?])/, '\1')
                  .scan(/[^*?][*?]?/)
  end

  @re = Regexp.new(parts.join)
end

Instance Attribute Details

#flagsSet<String>

Flags in this rule

Returns:

  • (Set<String>)

    the current value of flags



201
202
203
# File 'lib/kotoshu/readers/aff_data.rb', line 201

def flags
  @flags
end

#reRegexp

Compiled regex for full matching

Returns:

  • (Regexp)

    the current value of re



201
202
203
# File 'lib/kotoshu/readers/aff_data.rb', line 201

def re
  @re
end

#textString

The rule text

Returns:

  • (String)

    the current value of text



201
202
203
# File 'lib/kotoshu/readers/aff_data.rb', line 201

def text
  @text
end

Instance Method Details

#fullmatch(flag_sets) ⇒ Boolean

Check if flag sets fully match this rule.

Parameters:

  • flag_sets (Array<Set<String>>)

    Array of flag sets

Returns:

  • (Boolean)

    True if matches



227
228
229
230
231
232
233
# File 'lib/kotoshu/readers/aff_data.rb', line 227

def fullmatch(flag_sets)
  relevant_flags = flag_sets.map { |f| @flags.intersection(f).to_a }
  # Try all combinations of relevant flags
  relevant_flags[0].product(*relevant_flags[1..]).any? do |fc|
    @re.match?(fc.join)
  end
end