Class: Kotoshu::Models::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/core/models/word.rb

Overview

Note:

This class is immutable and frozen on initialization.

Word model representing a dictionary word with metadata.

This is a value object that represents a word in the dictionary along with its morphological information (flags and data).

Examples:

Creating a word

word = Models::Word.new("hello", flags: ["noun"], morphological_data: { root: "hell" })
word.text        # => "hello"
word.valid?      # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, flags: [], morphological_data: {}) ⇒ Word

Create a new Word.

Parameters:

  • text (String)

    The word text

  • flags (Array<String>) (defaults to: [])

    Morphological flags (optional)

  • morphological_data (Hash) (defaults to: {})

    Additional morphological data (optional)

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/kotoshu/core/models/word.rb', line 31

def initialize(text, flags: [], morphological_data: {})
  raise ArgumentError, "Text cannot be empty" if text.nil? || text.empty?

  @text = text.dup.freeze
  @flags = flags.dup.freeze
  @morphological_data = morphological_data.dup.freeze

  freeze
end

Instance Attribute Details

#flagsArray<String> (readonly)

Returns Morphological flags (e.g., “noun”, “verb”).

Returns:

  • (Array<String>)

    Morphological flags (e.g., “noun”, “verb”)



21
22
23
# File 'lib/kotoshu/core/models/word.rb', line 21

def flags
  @flags
end

#morphological_dataHash (readonly)

Returns Additional morphological data.

Returns:

  • (Hash)

    Additional morphological data



24
25
26
# File 'lib/kotoshu/core/models/word.rb', line 24

def morphological_data
  @morphological_data
end

#textString (readonly)

Returns The word text.

Returns:

  • (String)

    The word text



18
19
20
# File 'lib/kotoshu/core/models/word.rb', line 18

def text
  @text
end

Class Method Details

.from_dic_line(line) ⇒ Word

Create a word from a Hunspell dictionary line.

Examples:

Word.from_dic_line("hello/N")  # => Word with text "hello" and flag "N"
Word.from_dic_line("hello")    # => Word with text "hello" and no flags

Parameters:

  • line (String)

    Dictionary line (e.g., “hello/flag” or “hello”)

Returns:

  • (Word)

    New word instance



131
132
133
134
135
136
137
138
139
# File 'lib/kotoshu/core/models/word.rb', line 131

def self.from_dic_line(line)
  return nil if line.nil? || line.empty?

  parts = line.split("/", 2)
  text = parts[0]
  flags = parts[1] ? parts[1].split("") : []

  new(text, flags: flags)
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare words by text.

Parameters:

  • other (Word)

    The other word

Returns:

  • (Integer)

    Comparison result



117
118
119
120
121
# File 'lib/kotoshu/core/models/word.rb', line 117

def <=>(other)
  return nil unless other.is_a?(Word)

  @text <=> other.text
end

#==(other) ⇒ Boolean Also known as: eql?

Check equality based on text.

Parameters:

  • other (Word, String)

    The other object

Returns:

  • (Boolean)

    True if equal



99
100
101
102
103
# File 'lib/kotoshu/core/models/word.rb', line 99

def ==(other)
  return false unless other.is_a?(Word)

  @text == other.text
end

#empty?Boolean

Check if the word is empty.

Returns:

  • (Boolean)

    True if the word is empty



73
74
75
# File 'lib/kotoshu/core/models/word.rb', line 73

def empty?
  @text.empty?
end

#has_flag?(flag) ⇒ Boolean

Check if the word has a specific flag.

Parameters:

  • flag (String)

    The flag to check

Returns:

  • (Boolean)

    True if the word has the flag



52
53
54
# File 'lib/kotoshu/core/models/word.rb', line 52

def has_flag?(flag)
  @flags.include?(flag)
end

#has_flags?Boolean

Check if the word has any flags.

Returns:

  • (Boolean)

    True if the word has flags



59
60
61
# File 'lib/kotoshu/core/models/word.rb', line 59

def has_flags?
  !@flags.empty?
end

#hashInteger

Hash based on text.

Returns:

  • (Integer)

    Hash code



109
110
111
# File 'lib/kotoshu/core/models/word.rb', line 109

def hash
  @text.hash
end

#lengthInteger

Get the length of the word.

Returns:

  • (Integer)

    Word length



66
67
68
# File 'lib/kotoshu/core/models/word.rb', line 66

def length
  @text.length
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)

    Hash representation



87
88
89
90
91
92
93
# File 'lib/kotoshu/core/models/word.rb', line 87

def to_h
  {
    text: @text,
    flags: @flags,
    morphological_data: @morphological_data
  }
end

#to_sString

Convert to string.

Returns:

  • (String)

    The word text



80
81
82
# File 'lib/kotoshu/core/models/word.rb', line 80

def to_s
  @text
end

#valid?Boolean

Check if the word is valid (has content).

Returns:

  • (Boolean)

    True if the word is valid



44
45
46
# File 'lib/kotoshu/core/models/word.rb', line 44

def valid?
  !@text.nil? && !@text.empty?
end