作家
登录

利用 TensorFlow 实现上下文的 Chat-bots

作者: 来源: 2017-06-28 15:02:14 阅读 我要评论

如不雅另一个意图想要与高低文进行接洽关系,那么可以如许做:

传递给 response() 的每个句子都邑被分类。我们分类器应用 model.predict() 函数来进行类别猜测,这个办法异常快。模型返回的概率值和我们定义的意图是一直的,用来生成潜在的响应列表。

接下来,我们须要导入方才应用 TensorFlow(tflearn 框架)练习好的模型。请留意,你第一步照样须要去定义 TensorFlow 模型构造,正如我们在第一部分中做的那样。

鉴于高低文在所有的对话场景中的重要性,那么竽暌怪该若何参加这个特点?

如不雅一个或多个分类结不雅高于阈值,那么我们会选掏出一个与意图匹配的标签,然后处理。我们将我们的分类列表作为一个客栈,并大年夜这个客栈中寻找一个合适的匹配,直到找到一个最好的或者直到客栈变空。

我们来举一个例子,模型会返回最有可能的标签和其概率。

用户界面(客户端)平日是无状况的,例如:HTTP 或 SMS。

请留意,“is your shop open today?” 不是这个意图中的任何模式:“pattern : ["Are you open today?", "When do you open today?", "What are your hours today?"]”。然则,“open” 和 “today” 术语对我们的模式是异常有效的(他们在选择意图时,有决定性的感化)。

这部分,完全的代码在这里。

我们如今大年夜用户的输入数据中产生一个结不雅:

response('is your shop open today?')Our hours are 9am-9pm every day

再来一些例子:

response('do you take cash?')We accept VISA, Mastercard and AMEXresponse('what kind of mopeds do you rent?')We rent Yamaha, Piaggio and Vespa mopedsresponse('Goodbye, see you later')Bye! Come back again soon.

我们想处理的是一个关于租赁摩托车的问题,并询问一些有关房钱的事。对于用户问题的懂得应当是异常轻易的,语境异常清楚。如不雅用户询问 “today”,那么高低文的租赁信息就是进入时光框架,那么最好你还能指定是哪一个自行车,如许交换起来就不会浪费时光。

为了实现这一点,我们须要在框架中再参加一个概念 “state” 。这须要一个数据构造来保护这个新的概念和本来的意图。

接下来,我们给出根本语境的答复过程:

# create a data structure to hold user contextcontext = {}ERROR_THRESHOLD = 0.25def classify(sentence):    # generate probabilities from the model    results = model.predict([bow(sentence, words)])[0]    # filter out predictions below a threshold    results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]    # sort by strength of probability    results.sort(key=lambda x: x[1], reverse=True)    return_list = []    for r in results:        return_list.append((classes[r[0]], r[1]))    # return tuple of intent and probability    return return_listdef response(sentence, userID='123', show_details=False):    results = classify(sentence)    # if we have a classification then find the matching intent tag    if results:        # loop as long as there are matches to process        while results:            for i in intents['intents']:                # find a tag matching the first result                if i['tag'] == results[0][0]:                    # set context for this intent if necessary                    if 'context_set' in i:                        if show_details: print ('context:', i['context_set'])                        context[userID] = i['context_set']                    # check if this intent is contextual and applies to this user's conversation                    if not 'context_filter' in i or \                        (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):                        if show_details: print ('tag:', i['tag'])                        # a random response from the intent                        return print(random.choice(i['responses']))            results.pop(0)

我们的高低文状况是一个字典,它将包含每个用户的状况。我将为每个用户应用一个独一的标识(例如,cell#)。这许可我们的框架和状况机同事保护多个用户的状况。

# create a data structure to hold user contextcontext = {}

我们在意图处理流程中,添加了高低文信息,具体如下:

                if i['tag'] == results[0][0]:                    # set context for this intent if necessary                    if 'context_set' in i:                        if show_details: print ('context:', i['context_set'])                        context[userID] = i['context_set']                    # check if this intent is contextual and applies to this user's conversation                    if not 'context_filter' in i or \                        (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]):                        if show_details: print ('tag:', i['tag'])                        # a random response from the intent                        return print(random.choice(i['responses']))	
				
			

  推荐阅读

  美CIA CherryBlossom项目暴露路由器安全问题

维基解密最新宣布的CIA黑客对象中包含了CherryBlossom项目,该项目凸显了路由器的安然问题,包含缺乏固件签名方面的验证等。“在企业级产品方面,大年夜型路由器制造商已经供给签名固>>>详细阅读


本文标题:利用 TensorFlow 实现上下文的 Chat-bots

地址:http://www.17bianji.com/lsqh/35941.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)