Class: Kotoshu::Models::Word
- Inherits:
-
Object
- Object
- Kotoshu::Models::Word
- Defined in:
- lib/kotoshu/core/models/word.rb
Overview
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).
Instance Attribute Summary collapse
-
#flags ⇒ Array<String>
readonly
Morphological flags (e.g., “noun”, “verb”).
-
#morphological_data ⇒ Hash
readonly
Additional morphological data.
-
#text ⇒ String
readonly
The word text.
Class Method Summary collapse
-
.from_dic_line(line) ⇒ Word
Create a word from a Hunspell dictionary line.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare words by text.
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality based on text.
-
#empty? ⇒ Boolean
Check if the word is empty.
-
#has_flag?(flag) ⇒ Boolean
Check if the word has a specific flag.
-
#has_flags? ⇒ Boolean
Check if the word has any flags.
-
#hash ⇒ Integer
Hash based on text.
-
#initialize(text, flags: [], morphological_data: {}) ⇒ Word
constructor
Create a new Word.
-
#length ⇒ Integer
Get the length of the word.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
Convert to string.
-
#valid? ⇒ Boolean
Check if the word is valid (has content).
Constructor Details
#initialize(text, flags: [], morphological_data: {}) ⇒ Word
Create a new Word.
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
#flags ⇒ Array<String> (readonly)
Returns Morphological flags (e.g., “noun”, “verb”).
21 22 23 |
# File 'lib/kotoshu/core/models/word.rb', line 21 def flags @flags end |
#morphological_data ⇒ Hash (readonly)
Returns Additional morphological data.
24 25 26 |
# File 'lib/kotoshu/core/models/word.rb', line 24 def morphological_data @morphological_data end |
#text ⇒ String (readonly)
Returns 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.
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.
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.
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.
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.
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.
59 60 61 |
# File 'lib/kotoshu/core/models/word.rb', line 59 def has_flags? !@flags.empty? end |
#hash ⇒ Integer
Hash based on text.
109 110 111 |
# File 'lib/kotoshu/core/models/word.rb', line 109 def hash @text.hash end |
#length ⇒ Integer
Get the length of the word.
66 67 68 |
# File 'lib/kotoshu/core/models/word.rb', line 66 def length @text.length end |
#to_h ⇒ Hash
Convert to hash.
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_s ⇒ String
Convert to string.
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).
44 45 46 |
# File 'lib/kotoshu/core/models/word.rb', line 44 def valid? !@text.nil? && !@text.empty? end |