REST API と SynapseML で Fabric で事前構築済みの Text Analytics を使用する (プレビュー)
Text Analytics は、自然言語処理 (NLP) 機能を使用してテキスト マイニングとテキスト分析を実行できる、Azure AI サービス です。
このチュートリアルでは、RESTful API で Fabric でテキスト分析を使用して、次のことを行う方法について説明します。
- 文またはドキュメント レベルでセンチメント ラベルを検出します。
- 特定のテキスト入力の言語を識別します。
- テキストからキー フェーズを抽出します。
- テキスト内のさまざまなエンティティを識別し、それらを定義済みのクラスまたは型に分類します。
前提 条件
# Get workload endpoints and access token
from synapse.ml.mlflow import get_mlflow_env_config
import json
mlflow_env_configs = get_mlflow_env_config()
access_token = access_token = mlflow_env_configs.driver_aad_token
prebuilt_AI_base_host = mlflow_env_configs.workload_endpoint + "cognitive/textanalytics/"
print("Workload endpoint for AI service: \n" + prebuilt_AI_base_host)
service_url = prebuilt_AI_base_host + "language/:analyze-text?api-version=2022-05-01"
# Make a RESful request to AI service
post_headers = {
"Content-Type" : "application/json",
"Authorization" : "Bearer {}".format(access_token)
}
def printresponse(response):
print(f"HTTP {response.status_code}")
if response.status_code == 200:
try:
result = response.json()
print(json.dumps(result, indent=2, ensure_ascii=False))
except:
print(f"pasre error {response.content}")
else:
print(response.headers)
print(f"error message: {response.content}")
感情分析
感情分析機能は、文とドキュメント レベルでセンチメント ラベル ("負"、"ニュートラル"、"肯定的"など) と信頼度スコアを検出する方法を提供します。 また、この機能は、ドキュメントごとに 0 から 1 の信頼度スコアを返し、その中の文は肯定的、中立的、否定的なセンチメントを返します。 有効な言語の一覧については、感情分析とオピニオン マイニング言語のサポート を参照してください。
import requests
from pprint import pprint
import uuid
post_body = {
"kind": "SentimentAnalysis",
"parameters": {
"modelVersion": "latest",
"opinionMining": "True"
},
"analysisInput":{
"documents":[
{
"id":"1",
"language":"en",
"text": "The food and service were unacceptable. The concierge was nice, however."
}
]
}
}
post_headers["x-ms-workload-resource-moniker"] = str(uuid.uuid1())
response = requests.post(service_url, json=post_body, headers=post_headers)
# Output all information of the request process
printresponse(response)
アウトプット
HTTP 200
{
"kind": "SentimentAnalysisResults",
"results": {
"documents": [
{
"id": "1",
"sentiment": "mixed",
"confidenceScores": {
"positive": 0.43,
"neutral": 0.04,
"negative": 0.53
},
"sentences": [
{
"sentiment": "negative",
"confidenceScores": {
"positive": 0.0,
"neutral": 0.01,
"negative": 0.99
},
"offset": 0,
"length": 40,
"text": "The food and service were unacceptable. ",
"targets": [
{
"sentiment": "negative",
"confidenceScores": {
"positive": 0.01,
"negative": 0.99
},
"offset": 4,
"length": 4,
"text": "food",
"relations": [
{
"relationType": "assessment",
"ref": "#/documents/0/sentences/0/assessments/0"
}
]
},
{
"sentiment": "negative",
"confidenceScores": {
"positive": 0.01,
"negative": 0.99
},
"offset": 13,
"length": 7,
"text": "service",
"relations": [
{
"relationType": "assessment",
"ref": "#/documents/0/sentences/0/assessments/0"
}
]
}
],
"assessments": [
{
"sentiment": "negative",
"confidenceScores": {
"positive": 0.01,
"negative": 0.99
},
"offset": 26,
"length": 12,
"text": "unacceptable",
"isNegated": false
}
]
},
{
"sentiment": "positive",
"confidenceScores": {
"positive": 0.86,
"neutral": 0.08,
"negative": 0.07
},
"offset": 40,
"length": 32,
"text": "The concierge was nice, however.",
"targets": [
{
"sentiment": "positive",
"confidenceScores": {
"positive": 1.0,
"negative": 0.0
},
"offset": 44,
"length": 9,
"text": "concierge",
"relations": [
{
"relationType": "assessment",
"ref": "#/documents/0/sentences/1/assessments/0"
}
]
}
],
"assessments": [
{
"sentiment": "positive",
"confidenceScores": {
"positive": 1.0,
"negative": 0.0
},
"offset": 58,
"length": 4,
"text": "nice",
"isNegated": false
}
]
}
],
"warnings": []
}
],
"errors": [],
"modelVersion": "2022-11-01"
}
}
言語検出機能
Language Detector は、各ドキュメントのテキスト入力を評価し、分析の強度を示すスコアを持つ言語識別子を返します。 この機能は、言語が不明な任意のテキストを収集するコンテンツ ストアに役立ちます。 対応言語の一覧については、言語検出でサポートされている言語に関するページを参照してください。
post_body = {
"kind": "LanguageDetection",
"parameters": {
"modelVersion": "latest"
},
"analysisInput":{
"documents":[
{
"id":"1",
"text": "This is a document written in English."
}
]
}
}
post_headers["x-ms-workload-resource-moniker"] = str(uuid.uuid1())
response = requests.post(service_url, json=post_body, headers=post_headers)
# Output all information of the request process
printresponse(response)
アウトプット
HTTP 200
{
"kind": "LanguageDetectionResults",
"results": {
"documents": [
{
"id": "1",
"detectedLanguage": {
"name": "English",
"iso6391Name": "en",
"confidenceScore": 0.99
},
"warnings": []
}
],
"errors": [],
"modelVersion": "2022-10-01"
}
}
キー フレーズエクストラクター
キー フレーズ抽出は、非構造化テキストを評価し、キー フレーズの一覧を返します。 この機能は、ドキュメントのコレクション内の主要なポイントをすばやく識別する必要がある場合に便利です。 有効な言語の一覧については、キー フレーズ抽出 の サポートされている言語を参照してください。
post_body = {
"kind": "KeyPhraseExtraction",
"parameters": {
"modelVersion": "latest"
},
"analysisInput":{
"documents":[
{
"id":"1",
"language":"en",
"text": "Dr. Smith has a very modern medical office, and she has great staff."
}
]
}
}
post_headers["x-ms-workload-resource-moniker"] = str(uuid.uuid1())
response = requests.post(service_url, json=post_body, headers=post_headers)
# Output all information of the request process
printresponse(response)
アウトプット
HTTP 200
{
"kind": "KeyPhraseExtractionResults",
"results": {
"documents": [
{
"id": "1",
"keyPhrases": [
"modern medical office",
"Dr. Smith",
"great staff"
],
"warnings": []
}
],
"errors": [],
"modelVersion": "2022-10-01"
}
}
名前付きエンティティ認識 (NER)
名前付きエンティティ認識 (NER) は、テキスト内のさまざまなエンティティを識別し、ユーザー、場所、イベント、製品、組織などの定義済みのクラスまたは型に分類する機能です。 有効な言語の一覧については、NER 言語サポートの を参照してください。
post_body = {
"kind": "EntityRecognition",
"parameters": {
"modelVersion": "latest"
},
"analysisInput":{
"documents":[
{
"id":"1",
"language": "en",
"text": "I had a wonderful trip to Seattle last week."
}
]
}
}
post_headers["x-ms-workload-resource-moniker"] = str(uuid.uuid1())
response = requests.post(service_url, json=post_body, headers=post_headers)
# Output all information of the request process
printresponse(response)
アウトプット
HTTP 200
{
"kind": "EntityRecognitionResults",
"results": {
"documents": [
{
"id": "1",
"entities": [
{
"text": "trip",
"category": "Event",
"offset": 18,
"length": 4,
"confidenceScore": 0.74
},
{
"text": "Seattle",
"category": "Location",
"subcategory": "GPE",
"offset": 26,
"length": 7,
"confidenceScore": 1.0
},
{
"text": "last week",
"category": "DateTime",
"subcategory": "DateRange",
"offset": 34,
"length": 9,
"confidenceScore": 0.8
}
],
"warnings": []
}
],
"errors": [],
"modelVersion": "2021-06-01"
}
}