Azure AI Language是一個雲端式服務,可提供自然語言處理 (NLP) 功能,用於了解和分析文字,包括的項目有:
Reference:https://learn.microsoft.com/zh-tw/azure/ai-services/language-service/overview
當我們輸入一句話給機器人並且想要它知道我們的用意是甚麼就可以透過CLU,CLU有兩個重要的名詞分別是 intent 和 entity
就是判斷一句話的用意是甚麼,舉裡來說:
今天下午我想要跟張三去看鬼滅之刃
這句話的意圖就會是【看電影】
就是句子中重要的資訊是甚麼,以剛剛的句子來說可能會有幾個重要資訊
當然你想要的重要資訊不一定是這些,你有可能想要把時間再細分成"日期"和"時間",也就是把 "今天下午" 拆成 "今天" 和 "下午"
定義好 intent 和 entity 後,需要提供不同類型的句子作為訓練資料,讓模型學習如何辨識和理解不同的意圖和實體。舉例來說,針對剛才提到的「看電影」的意圖,我們可以再準備以下訓練語句:
intent: 看電影
entity:
時間:明天晚上
跟誰去:李四
電影名稱:名偵探柯南
intent: 看電影
entity:
時間:7/5
跟誰去:小華
電影名稱:腦筋急轉彎
intent: 看電影
entity:
時間:七月十五
跟誰去:小明
電影名稱:排球少年
Learned
Learn More →
List
Learn More →
Prebuild
Learn More →
Regex
Learn More →
Reference:https://learn.microsoft.com/en-us/azure/ai-services/luis/concepts/entities
Reference:https://learn.microsoft.com/zh-tw/azure/ai-services/language-service/conversational-language-understanding/overview
https://hackmd.io/@ntuebigdata/setup-linebot-development-environment#建立Python虛擬環境
https://hackmd.io/@ntuebigdata/create-a-line-official-account#1-建立Line-Developers帳號
https://hackmd.io/@ntuebigdata/azure-basic-introduction#註冊教育版的Azure帳號
Learn More →
Learn More →
Learn More →
Learn More →
要注意不是所有地區都有F0以及Conversation Language Understanding可以使用
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
這裡的名稱在後面建立LineBot會用到
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
這裡的名稱也要記住,後面建立LineBot會用到
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
新增一個專案資料夾,並在專案資料夾內新增.env
、requirements.txt
及app.py
檔案,並貼上以下內容
requirements.txt
line-bot-sdk
flask
azure-ai-translation-text
app.py
from flask import Flask, request, abort
from linebot.v3 import (
WebhookHandler
)
from linebot.v3.exceptions import (
InvalidSignatureError
)
from linebot.v3.webhooks import (
MessageEvent,
LocationMessageContent
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
import os
#Azure CLU
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.conversations import ConversationAnalysisClient
app = Flask(__name__)
CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
clu_endpoint = os.getenv("ENDPOINT")
clu_key = os.getenv("API_KEY")
project_name = os.getenv("PROJECT_NAME")
deployment_name = os.getenv("DEPLOYMENT_NAME")
line_handler = WebhookHandler(CHANNEL_SECRET)
configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# parse webhook body
try:
line_handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@line_handler.add(event=MessageEvent, message=LocationMessageContent)
def handle_location_message(event):
address = event.message.address
result = analyze_address(address)
entities = result['prediction']['entities']
messages = []
if len(entities) == 2 and entities[0]['category'] == 'city' and entities[1]['category'] == 'town':
city = result['prediction']['entities'][0]['text']
town = result['prediction']['entities'][1]['text']
messages.append(TextMessage(text=f"你傳送的位址資訊的城市:{city}"))
messages.append(TextMessage(text=f"你傳送的位址資訊的鄉鎮:{town}"))
else:
messages.append(TextMessage(text="無法辨識你傳送的位址資訊"))
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=messages
)
)
def analyze_address(address):
credential = AzureKeyCredential(clu_key)
client = ConversationAnalysisClient(clu_endpoint, credential)
with client:
result = client.analyze_conversation(
task={
"kind": "Conversation",
"analysisInput": {
"conversationItem": {
"participantId": "1",
"id": "1",
"modality": "text",
"language": "zh-hant",
"text": address
},
"isLoggingEnabled": False
},
"parameters": {
"projectName": project_name,
"deploymentName": deployment_name,
"verbose": True
}
}
)
return result['result']
if __name__ == "__main__":
app.run()
.env
CHANNEL_ACCESS_TOKEN = "XXX"
CHANNEL_SECRET = "XXX"
API_KEY = "XXX"
ENDPOINT = "XXX"
PROJECT_NAME = "XXX"
DEPLOYMENT_NAME = "XXX"
貼上之後記得修改幾個地方:
在聊天室點選定位功能,會回傳一個縣市的訊息以及一個鄉鎮的訊息
Reference:https://azure.microsoft.com/zh-tw/pricing/details/cognitive-services/language-service/
國立臺北教育大學 教育大數據微學程
🤖 AI LineBot 練功坊系列課程
從入門到精通,學習如何開發並應用 LINE Bot,讓你輕鬆掌握最前沿的聊天機器人技術。
👨💻 Python 初學小教室
針對零基礎學員設計,循序漸進地教授 Python 基本語法及實作技巧,幫助你快速上手。
📊 統計學小教室
系統講解統計學理論及其應用,適合想要提升數據分析能力的學習者。
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up