LoginSignup
0
1

[Python]Mecubなしで固有名詞をテキストから取得するには

Posted at

Amazon Web Services (AWS) には、テキストから固有名詞を抽出するためのサービスであるAmazon Comprehendを使用することができます。また、日本語を含む多数の言語がサポートされています。

以下はPythonとBoto3 (AWS SDK for Python)を使用して、Amazon Comprehendを利用してテキストから固有名詞を抽出する例です:

import boto3
import json

def extract_entities(text):
    comprehend = boto3.client(service_name='comprehend', region_name='your_region_name')
    response = comprehend.detect_entities(Text=text, LanguageCode='ja')
    return response['Entities']

def filter_proper_nouns(entities):
    proper_nouns = [entity for entity in entities if entity['Type'] in ['PERSON', 'LOCATION', 'ORGANIZATION', 'COMMERCIAL_ITEM', 'EVENT', 'DATE', 'QUANTITY', 'TITLE']]
    return proper_nouns

text = "ここに解析したいテキストを入力します。"
entities = extract_entities(text)
proper_nouns = filter_proper_nouns(entities)

for noun in proper_nouns:
    print(noun['Text'])

このスクリプトでは、まずcomprehend.detect_entitiesメソッドを使用してテキストからエンティティを抽出します。その後、filter_proper_nouns関数を使用してエンティティのリストから固有名詞をフィルタリングします。

なお、固有名詞としてフィルタリングするエンティティのタイプは、PERSON(人名)、LOCATION(地名)、ORGANIZATION(組織名)、COMMERCIAL_ITEM(商品名)、EVENT(イベント名)、DATE(日付)、QUANTITY(数量)、TITLE(役職名)となります。

your_region_nameは利用しているAWSのリージョン名を指定します(例えばus-west-2など)。また、LanguageCodeは解析するテキストの言語を指定します(日本語の場合はja)。

以上のコードを使用すれば、日本語のテキストから固有名詞を抽出することができます。

ただし、AWS Comprehendは使用料が発生しますので、その点はご注意ください。

0
1
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
1