Class: Knp

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

Constant Summary collapse

KNP_Version =
"4.11"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, id = nil) ⇒ Knp

Returns a new instance of Knp.



104
105
106
107
108
109
# File 'lib/juman_knp.rb', line 104

def initialize(string, id=nil)
  @id = id
  @string = string
  @pa_arr = pa(string)
  @asitis = @pa_arr[2]
end

Instance Attribute Details

#asitisObject (readonly)

Returns the value of attribute asitis.



102
103
104
# File 'lib/juman_knp.rb', line 102

def asitis
  @asitis
end

#pa_arrObject (readonly)

Returns the value of attribute pa_arr.



102
103
104
# File 'lib/juman_knp.rb', line 102

def pa_arr
  @pa_arr
end

#stringObject (readonly)

Returns the value of attribute string.



102
103
104
# File 'lib/juman_knp.rb', line 102

def string
  @string
end

Instance Method Details

#bunsetsuObject

dependency relations of Bunsetsu



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/juman_knp.rb', line 156

def bunsetsu
  # bunsetsu_hs
  # key:Information of ma of Bunsetsu(文節) [Array]
  # val:[Info of Bunsetsu, Where its dependence]
  bunsetsu_hs = Hash.new
  pa_tmp = @pa_arr[0]
  pa_tmp.each do |e|
    key = e.shift.split(/\s/)
    key.shift
    bunsetsu_hs[e]= key
  end
  return bunsetsu_hs
end

#pa(string, opttion = nil) ⇒ Object

Parsing(pa) with KNP Parameter > String for ma Return > Array of console output



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/juman_knp.rb', line 114

def pa(string, opttion = nil)

  asitis = []
  paarr = [[]]
  kihonku = [[]]
 
  # Juman's input is only Shift-JIS(for Windos)
  string.encode!("Windows-31J", "UTF-8", :invalid => :replace, :undef => :replace, :replace => '')
   
  # using open3, execute JUMAN|KNP
  begin
  out, err, status = Open3.capture3("juman | knp -simple ", :stdin_data => string)
  i = -1
  j = -1
  out.each_line do |line|
    line.chomp!.encode!("UTF-16BE", "Windows-31J", :invalid => :replace, :undef => :replace, :replace => '').encode!("UTF-8")
    asitis.push(line) unless line == "EOS"

    # making the array of Bunsetsu(文節)
    if line.split(/\s/)[0] == "*"
      i += 1
      paarr[i] = []
    end
    paarr[i].push(line) unless /^\+.+/ =~ line || line == "EOS"
 
    # making the array of Kihonku(基本句)
    if line.split(/\s/)[0] == "+"
      j += 1
      kihonku[j] = []
    end
    kihonku[j].push(line) unless /^\*.+/ =~ line || line == "EOS"
  end
  return paarr, kihonku, asitis

  rescue
    print("[エラー]:JUMANへPathを通してください。\n")
    exit!
  end

end