Class: Kotoshu::Readers::LatinScriptConditionChecker

Inherits:
ConditionChecker show all
Defined in:
lib/kotoshu/readers/condition_checker.rb

Overview

Condition checker for Latin-script dictionaries.

Hunspell conditions are regex-like patterns. Spylls compiles them verbatim (with - escaped so it doesn't become a range inside an unintended context) into a regex anchored at the start (for prefixes) or end (for suffixes), and uses re.search to test.

Examples (suffix unless noted):

  • '.' matches any stem (regex: /.$/ finds end-of-string)
  • 'y' matches stems ending with 'y' (regex: /y$/)
  • '[^y]' matches stems NOT ending with 'y' (regex: /[^y]$/)
  • '[aeiou]y' matches stems ending with vowel + 'y' (regex: /[aeiou]y$/)
  • '.[^aeiou]y' matches stems whose last 3 chars are any-non-vowel-y
  • 'wr.' (prefix) matches stems starting with 'wr' + any char

This is NOT suitable for RTL scripts or CJK languages.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(condition:, type:, anchor:) ⇒ LatinScriptConditionChecker

Returns a new instance of LatinScriptConditionChecker.



87
88
89
90
91
92
# File 'lib/kotoshu/readers/condition_checker.rb', line 87

def initialize(condition:, type:, anchor:)
  @condition = condition
  @type = type
  @anchor = anchor
  @pattern = compile_pattern
end

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



75
76
77
# File 'lib/kotoshu/readers/condition_checker.rb', line 75

def anchor
  @anchor
end

#conditionObject (readonly)

Returns the value of attribute condition.



75
76
77
# File 'lib/kotoshu/readers/condition_checker.rb', line 75

def condition
  @condition
end

#patternObject (readonly)

Returns the value of attribute pattern.



75
76
77
# File 'lib/kotoshu/readers/condition_checker.rb', line 75

def pattern
  @pattern
end

#typeObject (readonly)

Returns the value of attribute type.



75
76
77
# File 'lib/kotoshu/readers/condition_checker.rb', line 75

def type
  @type
end

Class Method Details

.compile(condition, type: :suffix) ⇒ LatinScriptConditionChecker

Compile a condition string.

Parameters:

  • condition (String)

    The condition string (e.g., '[^y]', '[abc]', 'y', '.')

  • type (Symbol) (defaults to: :suffix)

    :suffix (end-anchor) or :prefix (start-anchor)

Returns:



82
83
84
85
# File 'lib/kotoshu/readers/condition_checker.rb', line 82

def self.compile(condition, type: :suffix)
  anchor = type == :prefix ? :start : :ending
  new(condition: condition, type: type, anchor: anchor)
end

Instance Method Details

#matches?(stem) ⇒ Boolean

Check if the stem matches the condition.

Parameters:

  • stem (String)

    The stem to check

Returns:

  • (Boolean)

    True if the stem matches



98
99
100
101
102
# File 'lib/kotoshu/readers/condition_checker.rb', line 98

def matches?(stem)
  return true if @condition.nil? || @condition.empty? || @condition == '.'

  @pattern.match?(stem)
end