Class: Langue::Japanese::Structurer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/langue/japanese/structurer.rb

Constant Summary collapse

WORD_CLASSES =
%w(
  period
  verb
  adjective
  adjective_noun
  pronoun
  noun
).map do |word_name|
  require "langue/japanese/words/#{word_name}"
  Langue::Japanese.const_get(word_name.camelize)
end

Instance Method Summary collapse

Methods included from Logging

#null_logger

Constructor Details

#initialize(options = {}) ⇒ Structurer

Returns a new instance of Structurer.



25
26
27
# File 'lib/langue/japanese/structurer.rb', line 25

def initialize(options = {})
  @logger = options[:logger] || null_logger
end

Instance Method Details

#structure(morphemes) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/langue/japanese/structurer.rb', line 29

def structure(morphemes)
  sentences = []
  words = []
  arrived = false
  index = 0
  length = morphemes.length

  while index < length
    word_class = nil
    size = 0

    WORD_CLASSES.each do |wc|
      s = wc.take(morphemes, index)

      if s > 0
        word_class = wc
        size = s
        break
      end
    end

    if word_class.nil?
      word_class = Word
      size = 1
    end

    word = word_class.new(morphemes[index, size])

    if arrived && !word.instance_of?(Period)
      sentences << Sentence.new(words)
      words.clear
      arrived = false
    elsif word.instance_of?(Period)
      arrived = true
    end

    words << word
    index += size
  end

  sentences << Sentence.new(words) unless words.empty?
  Text.new(sentences)
end