class Knp

Constants

KNP_Version

Attributes

asitis[R]
pa_arr[R]
string[R]

Public Class Methods

new(string, id=nil) click to toggle source
# 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

Public Instance Methods

bunsetsu() click to toggle source

dependency relations of Bunsetsu

# 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) click to toggle source

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

# 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