Class: Kabosu::MorphemeList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kabosu/morpheme_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_or_morphemes, internal_cost: nil) ⇒ MorphemeList

Returns a new instance of MorphemeList.



7
8
9
10
11
# File 'lib/kabosu/morpheme_list.rb', line 7

def initialize(source_or_morphemes, internal_cost: nil)
  @source = source_or_morphemes if lazy_source?(source_or_morphemes)
  @morphemes = @source ? Array.new(@source.size) : source_or_morphemes
  @internal_cost = internal_cost || (@source&.internal_cost)
end

Instance Attribute Details

#internal_costObject

Returns the value of attribute internal_cost.



5
6
7
# File 'lib/kabosu/morpheme_list.rb', line 5

def internal_cost
  @internal_cost
end

Instance Method Details

#[](index) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/kabosu/morpheme_list.rb', line 27

def [](index)
  return to_a[index] if index.is_a?(Range)

  idx = normalize_index(index)
  return nil if idx.nil?

  fetch(idx)
end

#dictionary_formsObject



74
75
76
# File 'lib/kabosu/morpheme_list.rb', line 74

def dictionary_forms
  map(&:dictionary_form)
end

#each(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kabosu/morpheme_list.rb', line 13

def each(&block)
  return enum_for(:each) unless block

  if @source
    i = 0
    while i < size
      block.call(fetch(i))
      i += 1
    end
  else
    @morphemes.each(&block)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/kabosu/morpheme_list.rb', line 60

def empty?
  @morphemes.empty?
end

#first(n = nil) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
# File 'lib/kabosu/morpheme_list.rb', line 36

def first(n = nil)
  return self[0] unless n

  n = Integer(n)
  raise ArgumentError, "negative array size" if n.negative?

  limit = [n, size].min
  (0...limit).map { fetch(_1) }
end

#inspectObject



118
119
120
121
122
# File 'lib/kabosu/morpheme_list.rb', line 118

def inspect
  base = "#<Kabosu::MorphemeList (#{size} morphemes)"
  base += " cost=#{@internal_cost}" if @internal_cost
  base + ": #{surfaces.join(" | ")}>"
end

#last(n = nil) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
# File 'lib/kabosu/morpheme_list.rb', line 46

def last(n = nil)
  return self[-1] unless n

  n = Integer(n)
  raise ArgumentError, "negative array size" if n.negative?

  start = [size - n, 0].max
  (start...size).map { fetch(_1) }
end

#normalized_formsObject



78
79
80
# File 'lib/kabosu/morpheme_list.rb', line 78

def normalized_forms
  map(&:normalized_form)
end

#readingsObject



70
71
72
# File 'lib/kabosu/morpheme_list.rb', line 70

def readings
  map(&:reading_form)
end

#reject_pos(matcher_or_pattern) ⇒ Object

Inverse of select_pos. Returns a new MorphemeList excluding matching morphemes.



107
108
109
110
# File 'lib/kabosu/morpheme_list.rb', line 107

def reject_pos(matcher_or_pattern)
  matcher = coerce_to_matcher(matcher_or_pattern)
  self.class.new(matcher.reject(to_a))
end

#select_pos(matcher_or_pattern) ⇒ Object

Filter morphemes by POS. Accepts a PosMatcher or an array pattern. Returns a new MorphemeList with only matching morphemes.

list.select_pos(Kabosu::PosMatcher.nouns)
list.select_pos(["名詞", "固有名詞"])


101
102
103
104
# File 'lib/kabosu/morpheme_list.rb', line 101

def select_pos(matcher_or_pattern)
  matcher = coerce_to_matcher(matcher_or_pattern)
  self.class.new(matcher.filter(to_a))
end

#sizeObject



56
57
58
# File 'lib/kabosu/morpheme_list.rb', line 56

def size
  @morphemes.size
end

#surfacesObject



64
65
66
67
68
# File 'lib/kabosu/morpheme_list.rb', line 64

def surfaces
  return @source.surfaces if @source&.respond_to?(:surfaces)

  map(&:surface)
end

#synonym_group_idsObject



86
87
88
# File 'lib/kabosu/morpheme_list.rb', line 86

def synonym_group_ids
  map(&:synonym_group_ids)
end

#to_aObject



112
113
114
115
116
# File 'lib/kabosu/morpheme_list.rb', line 112

def to_a
  return @morphemes.dup unless @source

  (0...size).map { fetch(_1) }
end

#to_textObject

Joins all surfaces back into the original text (no spaces, for Japanese text).



91
92
93
# File 'lib/kabosu/morpheme_list.rb', line 91

def to_text
  surfaces.join
end

#total_costsObject



82
83
84
# File 'lib/kabosu/morpheme_list.rb', line 82

def total_costs
  map(&:total_cost)
end