import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.getcwd(), "key.json")
from google.cloud import language
# Instantiates a client
client = language.Client()
# The text to analyze
text = u'日本人は「縁の下の力持ち」という言葉が大好きだが、日本ほど「縁の下の力持ち」的な職業の人たちの待遇が悪い先進国はない。'
document = client.document_from_text(text)
# call All APIs
annotate_response = document.annotate_text(
include_sentiment=True,
include_syntax=True,
include_entities=True)
sentiment = annotate_response.sentiment
(sentiment.score, sentiment.magnitude)
(-0.9, 0.9)
for entity in annotate_response.entities:
print('=' * 20)
print(' name: {0}'.format(entity.name))
print(' type: {0}'.format(entity.entity_type))
print(' metadata: {0}'.format(entity.metadata))
print(' salience: {0}'.format(entity.salience))
==================== name: 縁 type: OTHER metadata: {} salience: 0.17860587 ==================== name: 日本 type: LOCATION metadata: {'wikipedia_url': 'https://en.wikipedia.org/wiki/Japan', 'mid': '/m/03_3d'} salience: 0.1564701 ==================== name: 力持ち type: OTHER metadata: {} salience: 0.1523732 ==================== name: 下 type: OTHER metadata: {} salience: 0.08780072 ==================== name: 人 type: PERSON metadata: {} salience: 0.081347436 ==================== name: 人たち type: PERSON metadata: {} salience: 0.061262988 ==================== name: 待遇 type: OTHER metadata: {} salience: 0.061262988 ==================== name: 職業 type: OTHER metadata: {} salience: 0.058626268 ==================== name: 先進国 type: OTHER metadata: {} salience: 0.056576863 ==================== name: 下 type: OTHER metadata: {} salience: 0.053513076 ==================== name: 言葉 type: OTHER metadata: {} salience: 0.052160498
token0 = annotate_response.tokens[0]
(token0.edge_index, token0.edge_label,
token0.lemma, token0.text_begin, token0.text_content)
(14, 'ADVPHMOD', '日本', 0, '日本')
for token in annotate_response.tokens:
print((token.edge_index, token.edge_label, token.lemma, token.text_begin, token.text_content))
(14, 'ADVPHMOD', '日本', 0, '日本') (0, 'SUFF', '人', 2, '人') (0, 'PRT', 'は', 3, 'は') (8, 'P', '「', 4, '「') (6, 'NN', '縁', 5, '縁') (4, 'PRT', 'の', 6, 'の') (8, 'NN', '下', 7, '下') (6, 'PRT', 'の', 8, 'の') (14, 'NSUBJ', '力持ち', 9, '力持ち') (8, 'P', '」', 12, '」') (8, 'PRT', 'という', 13, 'という') (8, 'APPOS', '言葉', 16, '言葉') (8, 'PRT', 'が', 18, 'が') (14, 'PREF', '大', 19, '大') (39, 'ADVCL', '好きだ', 20, '好き') (14, 'AUX', 'だ', 22, 'だ') (14, 'PRT', 'が', 23, 'が') (39, 'P', '、', 24, '、') (39, 'NSUBJ', '日本', 25, '日本') (18, 'PRT', 'ほど', 27, 'ほど') (25, 'P', '「', 29, '「') (23, 'NN', '縁', 30, '縁') (21, 'PRT', 'の', 31, 'の') (25, 'NN', '下', 32, '下') (23, 'PRT', 'の', 33, 'の') (31, 'AMOD', '力持ち', 34, '力持ち') (25, 'P', '」', 37, '」') (25, 'SUFF', '的', 38, '的') (25, 'AUX', 'だ', 39, 'な') (31, 'NN', '職業', 40, '職業') (29, 'PRT', 'の', 42, 'の') (34, 'NN', '人', 43, '人') (31, 'SUFF', 'たち', 44, 'たち') (31, 'PRT', 'の', 46, 'の') (36, 'NSUBJ', '待遇', 47, '待遇') (34, 'PRT', 'が', 49, 'が') (37, 'RCMOD', '悪い', 50, '悪い') (39, 'NN', '先進', 52, '先進') (37, 'SUFF', '国', 54, '国') (39, 'ROOT', 'はない', 55, 'はない') (39, 'P', '。', 58, '。')