Class: Daidai::Word

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daidai/word.rb

Overview

A conjugated word — the full paradigm for one dictionary-form input.

Forms are reached by name, with optional negative:/polite: modifiers:

word.past                      #=> Form  (plain affirmative)
word.past(polite: true)        #=> Form
word.non_past(negative: true)  #=> Form

…or through fluent views that read like grammar (and chain):

word.polite.past
word.negative.non_past
word.polite.negative.te

‘word[:past, polite: true]` does the same dynamically, and a Word is Enumerable over all of its forms.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word:, pos:, kind:, forms:) ⇒ Word

Returns a new instance of Word.



68
69
70
71
72
73
74
# File 'lib/daidai/word.rb', line 68

def initialize(word:, pos:, kind:, forms:)
  @word  = word
  @pos   = pos
  @kind  = kind
  @forms = forms
  @index = forms.group_by { |f| [ f.name, f.negative, f.polite ] }
end

Instance Attribute Details

#formsObject (readonly)

Returns the value of attribute forms.



66
67
68
# File 'lib/daidai/word.rb', line 66

def forms
  @forms
end

#kindObject (readonly)

Returns the value of attribute kind.



66
67
68
# File 'lib/daidai/word.rb', line 66

def kind
  @kind
end

#posObject (readonly)

Returns the value of attribute pos.



66
67
68
# File 'lib/daidai/word.rb', line 66

def pos
  @pos
end

#wordObject (readonly)

Returns the value of attribute word.



66
67
68
# File 'lib/daidai/word.rb', line 66

def word
  @word
end

Instance Method Details

#[](name, negative: false, polite: false) ⇒ Object Also known as: form

The primary Form for ‘name` in the given polarity/formality, or nil.



77
78
79
# File 'lib/daidai/word.rb', line 77

def [](name, negative: false, polite: false)
  @index[[ name, negative, polite ]]&.min_by(&:onum)
end

#affirmativeObject



99
# File 'lib/daidai/word.rb', line 99

def affirmative = View.new(self, negative: false, polite: false)

#conjugationsObject

The form names present for this word, in table order.



90
91
92
93
# File 'lib/daidai/word.rb', line 90

def conjugations
  present = @forms.map(&:name).uniq
  FORMS.keys.select { |name| present.include?(name) }
end

#eachObject



87
# File 'lib/daidai/word.rb', line 87

def each(&) = @forms.each(&)

#inspectObject



110
# File 'lib/daidai/word.rb', line 110

def inspect = "#<Daidai::Word #{word} (#{pos}, #{kind}): #{@forms.size} forms>"

#negativeObject



98
# File 'lib/daidai/word.rb', line 98

def negative    = View.new(self, negative: true, polite: false)

#plainObject



97
# File 'lib/daidai/word.rb', line 97

def plain       = View.new(self, negative: false, polite: false)

#politeObject

Fluent views — a lens with polarity/formality pre-applied.



96
# File 'lib/daidai/word.rb', line 96

def polite      = View.new(self, negative: false, polite: true)

#variants(name, negative: false, polite: false) ⇒ Object

Every accepted variant (all onums) for a form, primary first.



83
84
85
# File 'lib/daidai/word.rb', line 83

def variants(name, negative: false, polite: false)
  (@index[[ name, negative, polite ]] || []).sort_by(&:onum)
end