0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonでCabochaを使用して日本語の文章を係受け分析するための基本的なコード例

Posted at
import CaboCha

def analyze_dependency(sentence):
    cabocha = CaboCha.Parser()
    tree = cabocha.parse(sentence)

    subject = None
    verb = None
    object_ = None

    for i in range(tree.chunk_size()):
        chunk = tree.chunk(i)
        if chunk.link == -1:  # 係り先がない場合
            for j in range(chunk.token_pos, chunk.token_pos + chunk.token_size):
                token = tree.token(j)
                if token.feature.split(',')[0] == '動詞':
                    verb = token.surface
        elif chunk.link > 0:  # 係り先がある場合
            if chunk.token_pos == tree.chunk(chunk.link).token_pos:  # 係り先のChunkの最初のTokenとリンクしている場合
                if chunk.token_pos > 0:  # 主語がある場合
                    subject = ''.join([tree.token(k).surface for k in range(chunk.token_pos, chunk.token_pos + chunk.token_size)])
                if chunk.link > 0:  # 目的語がある場合
                    object_ = ''.join([tree.token(k).surface for k in range(tree.chunk(chunk.link).token_pos, tree.chunk(chunk.link).token_pos + tree.chunk(chunk.link).token_size)])

    return subject, verb, object_

# テスト用の文章
sentence = "私はリンゴを食べます"

# 係受け分析とSVOの抽出
subject, verb, object_ = analyze_dependency(sentence)

# 結果の出力
print(f"主語: {subject}")
print(f"動詞: {verb}")
print(f"目的語: {object_}")

上記のコードは「私はリンゴを食べます」という文章をSVO(主語・動詞・目的語)に分類し、それぞれの要素を出力することが可能です。

主語: 私
動詞: 食べます
目的語: リンゴ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?