構文とは、句と文の構造について研究したものです。構文と形態論は連携して文法的な関係を示しますが、言語が異なればその工程も異なります。たとえば、直接目的語の役割を示す場合、ロシア語では接尾辞(「книгу」の「у」)を使用する一方、英語では単語の順序を使用し、動詞の後に直接目的語を続けます(read the book)。
使い方
Natural Language API に直接送信されたテキスト文字列に対して構文解析を行う例を次に示します。
構文解析を行う関数の定義
入力した文章全体に対する感情と1文ずつの感情を取得して、それぞれ表示するプログラムを作成します。
from google.cloud import language_v1
from google.cloud.language_v1 import enums
from google.cloud.language_v1 import types
def sample_analyze_sentiment(text_content):
"""
Analyzing Sentiment in a String
Args:
text_content The text content to analyze
"""
client = language_v1.LanguageServiceClient()
type_ = language_v1.types.Document.Type.PLAIN_TEXT
document = types.Document(
content=text_content,
type=enums.Document.Type.PLAIN_TEXT)
response = client.analyze_sentiment(document=document)
# Get overall sentiment of the input document
print(u"Document sentiment score: {}".format(response.document_sentiment.score))
print(
u"Document sentiment magnitude: {}".format(
response.document_sentiment.magnitude
)
)
# Get sentiment for all sentences in the document
for sentence in response.sentences:
print(u"Sentence text: {}".format(sentence.text.content))
print(u"Sentence sentiment score: {}".format(sentence.sentiment.score))
print(u"Sentence sentiment magnitude: {}".format(sentence.sentiment.magnitude))
# the automatically-detected language.
print(u"Language of the text: {}".format(response.language))
Token text: この
Location of this token in overall document: -1
Lemma: この
Head token index: 1
Token text: 人
Location of this token in overall document: -1
Lemma: 人
Head token index: 13
Token text: は
Location of this token in overall document: -1
Lemma: は
Head token index: 1
Token text: 、
Location of this token in overall document: -1
Lemma: 、
Head token index: 13
Token text: この
Location of this token in overall document: -1
Lemma: この
Head token index: 5
Token text: 世の中
Location of this token in overall document: -1
Lemma: 世の中
Head token index: 13
Token text: で
Location of this token in overall document: -1
Lemma: で
Head token index: 5
Token text: 、
Location of this token in overall document: -1
Lemma: 、
Head token index: 13
Token text: いちばん
Location of this token in overall document: -1
Lemma: いちばん
Head token index: 9
Token text: しあわせ
Location of this token in overall document: -1
Lemma: しあわせ
Head token index: 11
Token text: な
Location of this token in overall document: -1
Lemma: だ
Head token index: 9
Token text: 人
Location of this token in overall document: -1
Lemma: 人
Head token index: 13
Token text: に
Location of this token in overall document: -1
Lemma: に
Head token index: 11
Token text: ちがいあり
Location of this token in overall document: -1
Lemma: ちがいあり
Head token index: 13
Token text: ませ
Location of this token in overall document: -1
Lemma: ませ
Head token index: 13
Token text: ん
Location of this token in overall document: -1
Lemma: ん
Head token index: 13
Token text: 。
Location of this token in overall document: -1
Lemma: 。
Head token index: 13
Language of the text: ja