ramenjunitiメモ

自分なりに調べたものをまとめたり、文章を書く練習したり

Cabochaで依存構文木を作る

juliaでCaboChaを呼んで係り受け解析を行い、依存構文木を作ってみました

taku910.github.io

コードはこんな感じです。

using PyCall

# cabocha
# 辞書のパス
const dic = ENV["DIC"]
const cabocha = pyimport("cabocha.analyzer")
const cabocha_analyzer = cabocha.CaboChaAnalyzer("-d $dic")

struct Node
    id::Int64
    parent::Int64
    children::Array{Int64,1}
    body::String
end

sentence = "一郎は二郎が書いた絵を三郎に贈った"

chunks = cabocha_analyzer.parse(sentence)

tree = []
for chunk in chunks
    id::Int64 = chunk.id + 1

    # 親のノードid
    parent::Int64 = chunk.next_link_id + 1

    # 子のノードid
    children::Array{Int,1} = []
    for children_id in chunk.prev_link_ids
        push!(children, children_id + 1)
    end

    # 文字そのもの
    body::String = chunk.surface

    push!(tree, Node(id, parent, children, body))
end

これにより以下のような依存構文木を生成できます

f:id:ramenjuniti:20200819135550p:plain
「一郎は二郎が書いた絵を三郎に贈った」の依存構文木