阿里筆試 智慧型對話簡化版之query指令槽位識別

2021-08-27 13:48:43 字數 2553 閱讀 6519

頭天貼主參加了阿里的筆試,第一道程式設計題就夠做了。不是時間短,是腦子不夠用。好了,不廢話了,上乾貨。

現下網際網路ai戰爭以智慧型音箱為切入口,敲開市場大門,搶奪市場使用者。智慧型音箱需要語音互動,這就涉及到query指令的語義理解。例如:"我要看章子怡的一代宗師",這裡邊需要識別出來:動作「看」,「章子怡」,「一代宗師」。一般都會建立乙個知識庫:名詞標註各種標籤,這裡邊可能會有:章子怡是演員,一代宗師是電影。另外有的名詞可能會有包含關係,例如,「周杰」,「周杰倫」,這個採用從左到右最長字串優選匹配原則。

輸入格式:

第一行是簡化版知識庫:《標籤1>_《名詞1>|《名詞2>|《名詞3>;《標籤2>_《名詞2>|《名詞4>|《名詞5>;.....

第二行是query指令。

輸出格式:

**** 《名詞1>/《標籤1> **** 《名詞2>/《標籤1>,《標籤2> ****

範例:

輸入:

singer_周杰|周杰倫|劉德華|王力巨集;song_冰雨|北京歡迎你|七里香;actor_周杰倫|孫儷

輸出:

由於有從左到右優先的匹配的選擇,所以考慮把輸入的知識庫轉換成名詞對映多個標籤:,並且按key=《名詞k>倒序;之後按排序後的key依次遍歷匹配query,匹配成功,則替換query中名詞為帶編號的特殊標識(為了不讓後邊短的子字串覆蓋長的父字串),並且記錄這個名詞,遍歷完之後,得到乙個有序的名詞列表,最後遍歷有序的名詞列表,替換query中的特殊標識得到最終的輸出結果。

#!/usr/bin/env python

# -*-encoding=utf8-*-

import re

def match_process():

row1 = raw_input()

datas = {}

entity_str_list = row1.split(";")

for entity_str in entity_str_list:

entity_name, entity_values = entity_str.split("_")

entity_value_list = entity_values.split("|")

for entity_value in entity_value_list:

if datas.has_key(entity_value):

else:

datas[entity_value] = [entity_name]

entity_list = sorted(datas.keys(), reverse=true)

row2 = raw_input()

words = row2

result =

tmp_words = words

temp_entity = ""

count = 1

for entity_value in entity_list:

if entity_value in tmp_words:

if temp_entity == "":

temp_entity = entity_value

else:

tmp_words = tmp_words.replace(temp_entity, "|&{}&|".format(count))

count += 1

temp_entity = entity_value

else:

if temp_entity != "":

tmp_words = tmp_words.replace(temp_entity, "|&{}&|".format(count))

count += 1

temp_entity = ""

if temp_entity != "":

tmp_words = tmp_words.replace(temp_entity, "|&{}&|".format(count))

final_list = sorted(result, reverse=true)

for index in xrange(len(final_list)):

st = ",".join(datas[final_list[index]])

new_str = " " + final_list[index] + "/" + st + " "

tmp_words = tmp_words.replace("|&{}&|".format(index + 1), new_str)

print " ".join(tmp_words.split())

if __name__ == '__main__':

match_process()

#singer_周杰|周杰倫|劉德華|王力巨集;song_冰雨|北京歡迎你|七里香;actor_周杰倫|孫儷

執行截圖:

180118 有趣的人工智慧對話小程式

print hello world 輸入 print what is your name?ask for their name 詢問名字 myname input 該你來回答名字了 print it is good to meet you,myname 根據你的名字來給你打個招呼 print the...

智慧型對話架構師速成秘訣在這裡

如今,我們生活中的訴求正在被各種機械人快捷 完美的處理和滿足,是因為越來越多的產品搭載著智慧型對話技術,把ai帶進人們的生活。但是智慧型對話產品或專案的建設,對於廣大企業或開發者來說存在著一定的難度。在智慧型對話技術應用的過程中,企業通常面臨著 我該怎麼入手?怎麼才能實現乙個完整的語音對話系統?怎麼...

評估智慧型對話機械人的7大資料指標

如果你正負責一款智慧型對話機械人產品,不管是軟體還是硬體,不管叫 小a 還是叫 小b 總要遇到乙個對於產品本身好壞的衡量指標的難題。如果你是leader更要弄清楚到底怎麼給做這款產品的下屬制定okr及考核指標。智慧型對話機械人,或者有些公司會將其命名為 智慧型助理 智慧型小助手 多數是以 工具 閒聊...