Amazon Comprehend 構文解析をしてみた

Pocket

はじめに

Amazon Comprehend を利用して英文の文章を構文解析を試してみました。

Amazon Comprehend とは

AWSの公式の説明では以下の通りです。

Amazon Comprehend は、機械学習を使用して、テキストからインサイトや関係性を発見するための自然言語処理 (NLP) サービスです。

参照:https://aws.amazon.com/jp/comprehend/

入力されたテキストから構文分析による単語抽出(品詞判定)や、感情分析、エンティティ認識、キーフレーズ抽出等が行えるサービスです。
Comprehend は日本語を含め全12言語をサポートしています。ただし、PII エンティティの検出等、一部機能は英語のみの対応となっています。
対応リージョンについては東京も含まれています。(大阪リージョンはまだ未対応)
(23年3月現在)

本ブログでは詳細なサービス説明は割愛しますので、詳細が知りたい方は以下公式のサイトをご参照ください。

https://aws.amazon.com/jp/comprehend/

構文分析について

Comprehendで実施できる構文分析は入力された文章を単語単位で分解し、分解された単語に対して品詞を判定する機能となります。
また、単語の位置情報(文書内の出現箇所)も返してくれるため、単語間の組み合わせに関する情報も得られます。

尚、残念ながら現在は構文分析の日本語対応はされていません。

今回の検証内容

今回も適当な文章をChatGPTを利用して生成してもらい、その文章をInputとして構文分析をする形しています。
複数文章の解析を行うため、25個の文章を作成し、解析対象としています。(※)
※ 後述しますが1回の解析で最大25個のドキュメントという上限があるので上限値としています。

The cat sat on the mat.
The quick brown fox jumped over the lazy dog.
Mary had a little lamb, its fleece was white as snow.
John is studying for his final exams.
The sun sets in the west.
The red car drove down the winding road.
She was a kind and loving person.
The children played happily in the park.
He cooked dinner for his family last night.
The teacher gave the students a difficult assignment.
She ran the marathon in under three hours.
The airplane flew over the mountains and into the clouds.
The flowers in the garden were in full bloom.
They watched a scary movie late at night.
The waves crashed against the shore.
He played the guitar for hours on end.
The city skyline was visible from the rooftop.
She wore a beautiful dress to the party.
The restaurant served delicious food and drinks.
The dog barked loudly at the mailman.
The rain fell gently on the roof of the house.
He rode his bike to work every day.
The book she was reading was very interesting.
The baby smiled and cooed at its mother.
They climbed to the top of the mountain and took in the breathtaking view.

利用方法

実行方法

Comprehendでの構文解析は入力に平文で解析する文章を指定して実行する形となります。
利用するAPIはBatchDetectSyntaxAPIとなり、TextListで入力文章を指定、LanguageCodeで入力言語の指定をします。
実行は同期実行となります。

具体的には以下の様な形となります。

import boto3

my_session = boto3.Session(profile_name='ProfileName')
ComprehendClient = my_session.client('comprehend', region_name="ap-northeast-1")

response = ComprehendClient.batch_detect_syntax(
    TextList=[
        "The cat sat on the mat.",
        "The quick brown fox jumped over the lazy dog.",
        (略)
    ],
    LanguageCode='en'
    ) 

print(response)

レスポンスについて

構文解析のレスポンスについては、以下の様に返却されます。

{'ResultList': [
        {'Index': 0, 'SyntaxTokens': [
                {'TokenId': 1, 'Text': 'The', 'BeginOffset': 0, 'EndOffset': 3, 'PartOfSpeech': {'Tag': 'DET', 'Score': 0.9999803304672241
                    }
                },
                {'TokenId': 2, 'Text': 'cat', 'BeginOffset': 4, 'EndOffset': 7, 'PartOfSpeech': {'Tag': 'NOUN', 'Score': 0.9994094371795654
                    }
                },
                {'TokenId': 3, 'Text': 'sat', 'BeginOffset': 8, 'EndOffset': 11, 'PartOfSpeech': {'Tag': 'VERB', 'Score': 0.9993413090705872
                    }
                },
                {'TokenId': 4, 'Text': 'on', 'BeginOffset': 12, 'EndOffset': 14, 'PartOfSpeech': {'Tag': 'ADP', 'Score': 0.9986516833305359
                    }
                },
                {'TokenId': 5, 'Text': 'the', 'BeginOffset': 15, 'EndOffset': 18, 'PartOfSpeech': {'Tag': 'DET', 'Score': 0.9999942183494568
                    }
                },
                {'TokenId': 6, 'Text': 'mat', 'BeginOffset': 19, 'EndOffset': 22, 'PartOfSpeech': {'Tag': 'NOUN', 'Score': 0.9999004602432251
                    }
                },
                {'TokenId': 7, 'Text': '.', 'BeginOffset': 22, 'EndOffset': 23, 'PartOfSpeech': {'Tag': 'PUNCT', 'Score': 0.9999987483024597
                    }
                }
            ]
        },
(略)
}

入力された入力毎にIndexが発行され、Indexが同一の単語は同一入力内に出現した単語として判定できます。

ただし、 “The cat sat on the mat.The quick brown fox jumped over the lazy dog.” の様に同一入力に2つの文章が存在する場合でも、「The cat ~」の文章も、「The quick ~」の文章も同一のIndexとなる様です。

また、同様に “The cat sat on ““the mat.” と一つの文章を分割して入力として渡した場合は、「The cat ~」と 「the mat.」は別なIndexとなります。

Indexはあくまでも入力値に対して発行されるidであるため、文書内の単語間の繋がりを検出したい場合は入力字に文章毎に分割して入力する必要がありそうです。

構文解析結果は、Index内に単語の出現位置の早い順からTokenIdが発行され、結果が出力されます。
解析した単語はTextに記載され、品詞はTagに記載されます。

BeginOffsetEndOffsetにより単語の出現位置も分かる為、本値を利用することで単語間の繋がりについても判定する事が出来そうです。

実行時間について

今回の検証では実行から数秒程度で結果が返ってきました。

まとめ

今回Comprehendの利用方法をまとめてみました。
機械学習の知識が不要で簡単に利用できるサービスになっておりましたので、今後構文解析の要件がある場合には積極的に利用したいと思います。

Pocket

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です