Class: Kotoshu::Algorithms::Lookup::AffixForm
- Inherits:
-
Object
- Object
- Kotoshu::Algorithms::Lookup::AffixForm
- Defined in:
- lib/kotoshu/algorithms/lookup.rb
Overview
AffixForm is a hypothesis of how some word might be split into stem, suffixes and prefixes.
It always has full text and stem, and may have up to two suffixes and up to two prefixes.
The following is always true (considering absent affixes as empty):
prefix + prefix2 + stem + suffix2 + suffix = text
prefix2/suffix2 are “secondary”, so if the word has only one suffix, it is stored in suffix and suffix2 is nil.
Instance Attribute Summary collapse
-
#in_dictionary ⇒ Hash?
readonly
Dictionary entry for stem.
-
#prefix ⇒ Hash?
readonly
Prefix affix data.
-
#prefix2 ⇒ Hash?
readonly
Secondary prefix affix data.
-
#stem ⇒ String
readonly
Stem (word without affixes).
-
#suffix ⇒ Hash?
readonly
Suffix affix data.
-
#suffix2 ⇒ Hash?
readonly
Secondary suffix affix data.
-
#text ⇒ String
readonly
Full word text.
Instance Method Summary collapse
-
#all_affixes ⇒ Array<Hash>
Get all affixes (excluding nils).
-
#flags ⇒ Set<String>
Get all flags from stem and affixes.
-
#has_affixes? ⇒ Boolean
Check if this form has any affixes.
-
#initialize(text, stem, prefix: nil, suffix: nil, prefix2: nil, suffix2: nil, in_dictionary: nil) ⇒ AffixForm
constructor
A new instance of AffixForm.
-
#is_base? ⇒ Boolean
Check if this is a base form (no affixes).
-
#replace(**changes) ⇒ AffixForm
Create a copy with changes.
-
#to_s ⇒ String
(also: #inspect)
String representation.
Constructor Details
#initialize(text, stem, prefix: nil, suffix: nil, prefix2: nil, suffix2: nil, in_dictionary: nil) ⇒ AffixForm
Returns a new instance of AffixForm.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 64 def initialize(text, stem, prefix: nil, suffix: nil, prefix2: nil, suffix2: nil, in_dictionary: nil) @text = text @stem = stem @prefix = prefix @suffix = suffix @prefix2 = prefix2 @suffix2 = suffix2 @in_dictionary = in_dictionary end |
Instance Attribute Details
#in_dictionary ⇒ Hash? (readonly)
Returns Dictionary entry for stem.
62 63 64 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 62 def in_dictionary @in_dictionary end |
#prefix ⇒ Hash? (readonly)
Returns Prefix affix data.
50 51 52 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 50 def prefix @prefix end |
#prefix2 ⇒ Hash? (readonly)
Returns Secondary prefix affix data.
56 57 58 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 56 def prefix2 @prefix2 end |
#stem ⇒ String (readonly)
Returns Stem (word without affixes).
47 48 49 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 47 def stem @stem end |
#suffix ⇒ Hash? (readonly)
Returns Suffix affix data.
53 54 55 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 53 def suffix @suffix end |
#suffix2 ⇒ Hash? (readonly)
Returns Secondary suffix affix data.
59 60 61 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 59 def suffix2 @suffix2 end |
#text ⇒ String (readonly)
Returns Full word text.
44 45 46 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 44 def text @text end |
Instance Method Details
#all_affixes ⇒ Array<Hash>
Get all affixes (excluding nils).
120 121 122 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 120 def all_affixes [@prefix2, @prefix, @suffix, @suffix2].compact end |
#flags ⇒ Set<String>
Get all flags from stem and affixes.
110 111 112 113 114 115 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 110 def flags flags = @in_dictionary ? Set.new(@in_dictionary[:flags] || []) : Set.new flags.merge(@prefix[:flags] || []) if @prefix flags.merge(@suffix[:flags] || []) if @suffix flags end |
#has_affixes? ⇒ Boolean
Check if this form has any affixes.
96 97 98 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 96 def has_affixes? !@suffix.nil? || !@prefix.nil? end |
#is_base? ⇒ Boolean
Check if this is a base form (no affixes).
103 104 105 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 103 def is_base? !has_affixes? end |
#replace(**changes) ⇒ AffixForm
Create a copy with changes.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 81 def replace(**changes) self.class.new( changes.fetch(:text, @text), changes.fetch(:stem, @stem), prefix: changes.fetch(:prefix, @prefix), suffix: changes.fetch(:suffix, @suffix), prefix2: changes.fetch(:prefix2, @prefix2), suffix2: changes.fetch(:suffix2, @suffix2), in_dictionary: changes.fetch(:in_dictionary, @in_dictionary) ) end |
#to_s ⇒ String Also known as: inspect
String representation.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kotoshu/algorithms/lookup.rb', line 127 def to_s return @text if is_base? parts = [] parts << @prefix.inspect if @prefix parts << @prefix2.inspect if @prefix2 parts << @stem parts << @suffix2.inspect if @suffix2 parts << @suffix.inspect if @suffix "AffixForm(#{@text} = #{parts.join(' + ')})" end |