Azure Text Analytics API 可以分析文本的內容,判斷其內容是屬於何種語言,並且會給予numeric score
範圍是0~1,代表其判斷精準度。
1.首先到此處創建服務,並產生金鑰
https://portal.azure.com/#create/Microsoft.CognitiveServicesTextAnalytics
2.到此處填上金鑰與相關參數https://eastasia.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c7/console
如果出現
401
錯誤時可以檢查服務創建的地區是否與網址的地區相符(例如:eastasia)。
程式範例:
const https = require("https");
const options = {
host: "eastasia.api.cognitive.microsoft.com",
port: 443,
path: `/text/analytics/v2.0/languages`,
method: "POST",
headers: {
"Ocp-Apim-Subscription-Key": "填上金鑰",
"Content-Type": "application/json"
},
}
const req = https.request(options, res => {
let chunk = '';
res.on("data", function (data) {
chunk += data;
});
res.on("end", function (data) {
console.log(chunk);
});
});
req.on("error", e => {
console.error(e);
});
req.write(
JSON.stringify({
"documents": [
{
"id": "1",
"text": "Hi."
},
{
"id": "4",
"text": "英國研究顯示,根據日本2017年雜誌報導,在南美洲的60分鐘等於一個小時。"
},
{
"id": "5",
"text": "???"
}
]
})
);
req.end();
回傳結果:
{"documents":[{"id":"1","detectedLanguages":[{"name":"English","iso6391Name":"en","score":1.0}]},{"id":"4","detectedLanguages":[{"name":"Chinese_Traditional","iso6391Name":"zh_cht","score":0.78571426868438721}]},{"id":"5","detectedLanguages":[{"name":"(Unknown)","iso6391Name":"(Unknown)","score":0.0}]}],"errors":[]}