Class: Kotoshu::Algorithms::Lookup::AffixForm

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_dictionaryHash? (readonly)

Returns Dictionary entry for stem.

Returns:

  • (Hash, nil)

    Dictionary entry for stem



62
63
64
# File 'lib/kotoshu/algorithms/lookup.rb', line 62

def in_dictionary
  @in_dictionary
end

#prefixHash? (readonly)

Returns Prefix affix data.

Returns:

  • (Hash, nil)

    Prefix affix data



50
51
52
# File 'lib/kotoshu/algorithms/lookup.rb', line 50

def prefix
  @prefix
end

#prefix2Hash? (readonly)

Returns Secondary prefix affix data.

Returns:

  • (Hash, nil)

    Secondary prefix affix data



56
57
58
# File 'lib/kotoshu/algorithms/lookup.rb', line 56

def prefix2
  @prefix2
end

#stemString (readonly)

Returns Stem (word without affixes).

Returns:

  • (String)

    Stem (word without affixes)



47
48
49
# File 'lib/kotoshu/algorithms/lookup.rb', line 47

def stem
  @stem
end

#suffixHash? (readonly)

Returns Suffix affix data.

Returns:

  • (Hash, nil)

    Suffix affix data



53
54
55
# File 'lib/kotoshu/algorithms/lookup.rb', line 53

def suffix
  @suffix
end

#suffix2Hash? (readonly)

Returns Secondary suffix affix data.

Returns:

  • (Hash, nil)

    Secondary suffix affix data



59
60
61
# File 'lib/kotoshu/algorithms/lookup.rb', line 59

def suffix2
  @suffix2
end

#textString (readonly)

Returns Full word text.

Returns:

  • (String)

    Full word text



44
45
46
# File 'lib/kotoshu/algorithms/lookup.rb', line 44

def text
  @text
end

Instance Method Details

#all_affixesArray<Hash>

Get all affixes (excluding nils).

Returns:

  • (Array<Hash>)

    List of affix data



120
121
122
# File 'lib/kotoshu/algorithms/lookup.rb', line 120

def all_affixes
  [@prefix2, @prefix, @suffix, @suffix2].compact
end

#flagsSet<String>

Get all flags from stem and affixes.

Returns:

  • (Set<String>)

    Combined flags



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.

Returns:

  • (Boolean)


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).

Returns:

  • (Boolean)


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.

Parameters:

  • changes (Hash)

    Changes to apply

Returns:

  • (AffixForm)

    New affix form with changes applied



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_sString Also known as: inspect

String representation.

Returns:

  • (String)


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