Class: NattoWrap::MeCabModel

Inherits:
Object
  • Object
show all
Defined in:
lib/natto_wrap/me_cab_model.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word: nil, word_class: nil, word_subclass1: nil, word_subclass2: nil, word_subclass3: nil, conjugation1: nil, conjugation2: nil, prototype: nil, reading: nil, pronunciation: nil) ⇒ MeCabModel

Returns a new instance of MeCabModel.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/natto_wrap/me_cab_model.rb', line 11

def initialize(word: nil, word_class: nil,
               word_subclass1: nil, word_subclass2: nil, word_subclass3: nil,
               conjugation1: nil, conjugation2: nil,
               prototype: nil, reading: nil, pronunciation: nil)
  @word = word
  @word_class = get_object(word_class)
  @word_subclass1 = get_object(word_subclass1)
  @word_subclass2 = get_object(word_subclass2)
  @word_subclass3 = get_object(word_subclass3)
  @conjugation1 = get_object(conjugation1)
  @conjugation2 = get_object(conjugation2)
  @prototype = get_object(prototype) || word
  @reading = get_object(reading) || @prototype
  @pronunciation = get_object(pronunciation) || @reading
end

Instance Attribute Details

#conjugation1Object

Returns the value of attribute conjugation1.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def conjugation1
  @conjugation1
end

#conjugation2Object

Returns the value of attribute conjugation2.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def conjugation2
  @conjugation2
end

#pronunciationObject

Returns the value of attribute pronunciation.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def pronunciation
  @pronunciation
end

#prototypeObject

Returns the value of attribute prototype.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def prototype
  @prototype
end

#readingObject

Returns the value of attribute reading.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def reading
  @reading
end

#wordObject

Returns the value of attribute word.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def word
  @word
end

#word_classObject

Returns the value of attribute word_class.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def word_class
  @word_class
end

#word_subclass1Object

Returns the value of attribute word_subclass1.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def word_subclass1
  @word_subclass1
end

#word_subclass2Object

Returns the value of attribute word_subclass2.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def word_subclass2
  @word_subclass2
end

#word_subclass3Object

Returns the value of attribute word_subclass3.



6
7
8
# File 'lib/natto_wrap/me_cab_model.rb', line 6

def word_subclass3
  @word_subclass3
end

Class Method Details

.convert_model(str) ⇒ Object



97
98
99
100
101
102
# File 'lib/natto_wrap/me_cab_model.rb', line 97

def convert_model(str)
  @natto ||= Natto::MeCab.new
  natto_objects = @natto.parse(str)
  natto_objects.delete_suffix!("EOS\n")
  natto_objects.split("\n").map { |natto_object| create_from_natto_object(natto_object) }
end

.create_as_proper_noun(word, reading = nil, pronunciation = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/natto_wrap/me_cab_model.rb', line 104

def create_as_proper_noun(word, reading = nil, pronunciation = nil)
  new(
    word: word, word_class: '名詞',
    word_subclass1: '固有名詞', word_subclass2: '一般', word_subclass3: nil,
    conjugation1: nil, conjugation2: nil,
    prototype: word,
    reading: reading || word,
    pronunciation: pronunciation || reading || word
  )
end

.extract_nouns(str) ⇒ Object



93
94
95
# File 'lib/natto_wrap/me_cab_model.rb', line 93

def extract_nouns(str)
  convert_model(str).select(&:noun?)
end

.load_from_csv(filename) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/natto_wrap/me_cab_model.rb', line 115

def load_from_csv(filename)
  CSV.open(filename, 'r') do |rows|
    rows.map do |row|
      new(
        word: row[0], word_class: row[4],
        word_subclass1: row[5], word_subclass2: row[6], word_subclass3: row[7],
        conjugation1: row[8], conjugation2: row[9],
        prototype: row[10], reading: row[11], pronunciation: row[12]
      )
    end
  end
end

.to_reading(str, str_threshold = 20) ⇒ Object



87
88
89
90
91
# File 'lib/natto_wrap/me_cab_model.rb', line 87

def to_reading(str, str_threshold = 20)
  # NOTE: 負荷とパフォーマンスが気になるので上限を超えている場合は切り詰める
  mecab_models = convert_model(str.slice(0, str_threshold).tr('ぁ-ん', 'ァ-ン'))
  mecab_models.inject('') { |reading, model| reading + model.reading }
end

Instance Method Details

#adjective?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/natto_wrap/me_cab_model.rb', line 35

def adjective?
  @word_class == '形容詞'
end

#adverb?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/natto_wrap/me_cab_model.rb', line 39

def adverb?
  @word_class == '副詞'
end

#auxiliary_verb?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/natto_wrap/me_cab_model.rb', line 51

def auxiliary_verb?
  @word_class == '助動詞'
end

#conjunction?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/natto_wrap/me_cab_model.rb', line 47

def conjunction?
  @word_class == '接続詞'
end

#determiner?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/natto_wrap/me_cab_model.rb', line 55

def determiner?
  @word_class == '連体詞'
end

#interjection?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/natto_wrap/me_cab_model.rb', line 59

def interjection?
  @word_class == '感動詞'
end

#noun?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/natto_wrap/me_cab_model.rb', line 27

def noun?
  @word_class == '名詞'
end

#postpositional_particle?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/natto_wrap/me_cab_model.rb', line 43

def postpositional_particle?
  @word_class == '助詞'
end

#symbol?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/natto_wrap/me_cab_model.rb', line 63

def symbol?
  @word_class == '記号'
end

#to_csvObject



67
68
69
70
71
72
73
74
# File 'lib/natto_wrap/me_cab_model.rb', line 67

def to_csv
  [
    @word, 0, 0, 0, @word_class,
    get_str(@word_subclass1), get_str(@word_subclass2), get_str(@word_subclass3),
    get_str(@conjugation1), get_str(@conjugation2),
    get_str(@prototype), get_str(@reading), get_str(@pronunciation)
  ]
end

#verb?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/natto_wrap/me_cab_model.rb', line 31

def verb?
  @word_class == '動詞'
end