辨識實體
命名實體辨識是 Azure Language 提供的一項功能。 它會識別並分類非結構化文字中的實體。 它支持數 種實體類別 ,包括人員、位置、事件、產品、組織等。
有數種方式可以呼叫 具名實體辨識 API。 在這裡,您會使用 azure_ai 擴充功能從 SQL 查詢中的文字辨識實體。
先決條件
您需要適用於 PostgreSQL 的 Azure 資料庫彈性伺服器,並azure_ai啟用並設定擴充功能。 您也需要藉由設定語言資源的密鑰和端點,向 Azure 認知服務 授權 它。
案例
在幾個領域中,實體辨識具有幫助,例如:
- 搜尋和編制索引:使用已識別的實體自動建置知識圖表和標記目錄。
- 程式自動化:自動辨識非結構化文字中的產品和位置,並將其路由傳送至客戶支援要求。
- 市場分析:測量社交媒體、客戶評論、支援票證等最常見的實體和實體叢集,以判斷相關主題並預測趨勢。
在 SQL 中使用具名實體辨識搭配 Azure 認知服務
適用於 PostgreSQL 的 Azure 資料庫彈性伺服器 azure_ai延伸模組 提供使用者定義的函式 (UDF),以直接從 SQL 內部存取 AI 功能。 具名實體辨識 API 是透過 azure_cognitive.recognize_entities 所提供的 azure_ai 函式來存取:
azure_cognitive.recognize_entities(
text text,
language text,
timeout_ms integer DEFAULT 3600000,
throw_on_error boolean DEFAULT true,
disable_service_logs boolean DEFAULT false
)
必要的參數是 text,就是輸入,和 language,即 text 所使用的語言。 例如, en-us 是美式英文,而 fr 是法文。 如需可用語言的完整清單,請參閱 語言支援 。
根據預設,如果實體辨識未在 3,600,000 毫秒 = 1 小時內完成,則會停止實體辨識。 您可以藉由變更 timeout_ms來自訂此延遲。
如果發生錯誤,預設行為是擲回例外狀況,導致交易回復。 您可以將 設定 throw_on_error 為 false 來停用此行為。
如需完整的參數檔,請參閱 Azure 認知服務擴充功能 檔。
例如,叫用此查詢:
SELECT azure_cognitive.recognize_entities('For more information, see Cognitive Services Compliance and Privacy notes.', 'en-us');
得出這個結果:
{"(\"Cognitive Services\",Skill,\"\",0.94)"}
指出實體的名稱為「認知服務」,這被識別為信賴度分數 0.94 的技能。
您可以針對輸入文字使用資料表欄位。
SELECT description, azure_cognitive.recognize_entities(description, 'en-us')
FROM listings LIMIT 1;
這會傳回:
{"(house,Location,\"\",0.77)","(2013.,DateTime,DateRange,1)","(\"rooftop deck\",Location,\"\",0.88)","(\"lounge area\",Location,Structural,0.97)","(tub,Product,\"\",0.52)","
(5,Quantity,Number,0.8)","(bedrooms,Location,\"\",0.92)","(\"gourmet kitchen\",Location,\"\",0.87)","(2-3,Quantity,NumberRange,0.87)","(downtown,Location,Structural,0.8)","(\
"Queen Anne neighborhood\",Location,\"\",0.74)","(house,Location,\"\",0.96)","(barnwood,Product,\"\",0.61)","(steel,Product,\"\",0.73)","(concrete,Product,\"\",0.7)","(living
,Location,Structural,0.53)","(\"gourmet kitchen\",Location,\"\",0.7)","(kitchen,Location,\"\",0.77)","(reading,Skill,\"\",0.54)","(half,Quantity,Number,0.8)","(\"tv room\",Lo
cation,\"\",0.89)","(kitchen,Location,\"\",0.64)","(Fireplace,Product,\"\",0.91)","(sofa,Product,\"\",0.98)","(\"sitting area\",Location,\"\",0.93)","(\"Basement room\",Locat
ion,\"\",0.98)","(kids,PersonType,\"\",0.73)","(room,Location,Structural,0.78)","(patio,Location,Structural,0.75)","(basketball,Product,\"\",0.57)","(bedroom,Location,\"\",0.
8)","(basement,Location,\"\",0.94)","(\"concrete heated floors\",Product,\"\",0.95)","(\"queen sleeper sofa\",Product,\"\",0.86)","(tv,Location,\"\",0.54)","(basement,Locatio
n,\"\",0.92)","(room,Location,Structural,0.9)","(\"a second\",DateTime,Duration,0.85)","(family,PersonType,\"\",0.71)","(kids,PersonType,\"\",0.65)","(\"2nd floor\",Location,
Structural,0.56)","(4,Quantity,Number,0.8)","(bedrooms,Location,\"\",0.66)","(one,Quantity,Number,0.8)","(one,Quantity,Number,0.8)","(bedroom,Location,\"\",0.54)","(\"twin bu
nk beds\",Product,\"\",0.67)"}
總結
具名實體辨識會識別並分類輸入文字中的實體。 Azure 認知服務語言模型會執行繁重的自然語言處理。 適用於 PostgreSQL 的 Azure 資料庫 azure_ai 延伸模組提供 azure_cognitive.recognize_entities API,直接在 SQL 查詢中存取具名實體辨識。