在我们的日常聊天中,情景擦?鲱重要的。我们将应用 TensorFlow 构建一个聊天机械人框架,并且添加一些高低文处理机制来使得机械人加倍智能。

你是否想过一个问题,为什么那么多的聊天机械人会缺乏会话情景功能?
接下来,我们将创建一个聊天机械人的框架,并且以一个岛屿简便摩托车租赁店为例子,建立一个对话模型。这个小企业的聊天机械人须要处理一些关于租赁时光,租赁选项等的简单问题。我们也欲望这个机械人可以处理一些高低文的信息,比如萌芽同一天的租赁信息。如不雅可以解决这个问题,那么我们将节约很多的时光。
关于构建聊天机械人,我们经由过程以下三部进行:
- 我们会应用 TensorFlow 来编写对话意图模型。
- 接下啦,我们将构建一个处理对话的聊天机械人框架。
- 最后,我们将介绍若何将高低文信息归并到我们的响应式处理器中。
起首,我们对没有高低文信息的 "today" 的回应是不合的。我们的分类产生了 2 个合适的意图,但 "opentoday" 被选中了。所以这个随机性就比较大年夜,高低文信息很重要!
在模型中,我们将应用 tflearn 框架,这是一个 TensorFlow 的高层 API,并且我们将应用 IPython 作为开辟对象。
1. 我们会应用 TensorFlow 来编写对话意图模型。
完全的 notebook 文档,可以点击这里。
对于一个聊天机械人框架,我们须要定义一个会话意图的构造。最简单便利的方法是应用一个 JSON 格局的文件,如下所示:

每个会话意图包含:
- 标签(独一的名称)
- 模式(我们的神经收集文本分类器须要分类的句子)
- 回应(一个将被用作回应的句子)
稍后,我们也会添加一些根本的高低文元素。
起首,我们来导入一些我们须要的包:
# things we need for NLPimport nltkfrom nltk.stem.lancaster import LancasterStemmerstemmer = LancasterStemmer()# things we need for Tensorflowimport numpy as npimport tflearnimport tensorflow as tfimport random
如不雅你还不懂得 TensorFlow,那么可以进修一下这个教程或者这个教程。
# import our chat-bot intents fileimport jsonwith open('intents.json') as json_data: intents = json.load(json_data)代码中的 JSON 文件可以这里下载,接下来我们可以开端组织代码的文件,数据和分类器。
words = []classes = []documents = []ignore_words = ['?']# loop through each sentence in our intents patternsfor intent in intents['intents']: for pattern in intent['patterns']: # tokenize each word in the sentence w = nltk.word_tokenize(pattern) # add to our words list words.extend(w) # add to documents in our corpus documents.append((w, intent['tag'])) # add to our classes list if intent['tag'] not in classes: classes.append(intent['tag'])# stem and lower each word and remove duplicateswords = [stemmer.stem(w.lower()) for w in words if w not in ignore_words]words = sorted(list(set(words)))# remove duplicatesclasses = sorted(list(set(classes)))print (len(documents), "documents")print (len(classes), "classes", classes)print (len(words), "unique stemmed words", words)
我们创建了一个文件列表(每个句子),每个句子都是由一些词干构成,并且每个文档都属于一个特定的类别。
27 documents9 classes ['goodbye', 'greeting', 'hours', 'mopeds', 'opentoday', 'payments', 'rental', 'thanks', 'today']44 unique stemmed words ["'d", 'a', 'ar', 'bye', 'can', 'card', 'cash', 'credit', 'day', 'do', 'doe', 'good', 'goodby', 'hav', 'hello', 'help', 'hi', 'hour', 'how', 'i', 'is', 'kind', 'lat', 'lik', 'mastercard', 'mop', 'of', 'on', 'op', 'rent', 'see', 'tak', 'thank', 'that', 'ther', 'thi', 'to', 'today', 'we', 'what', 'when', 'which', 'work', 'you']
比如,词干 tak 将和 take,taking,takers 等匹配。在实际过程中,我们可以删除一些无用的条目,但在这里已经足够了。
不幸的是,这种数据构造不克不及在 TensorFlow 中应用,我们须要进一步将这个数据进行转换:大年夜单词转换到数字的┞放量。
# create our training datatraining = []output = []# create an empty array for our outputoutput_empty = [0] * len(classes)# training set, bag of words for each sentencefor doc in documents: # initialize our bag of words bag = [] # list of tokenized words for the pattern pattern_words = doc[0] # stem each word pattern_words = [stemmer.stem(word.lower()) for word in pattern_words] # create our bag of words array for w in words: bag.append(1) if w in pattern_words else bag.append(0) # output is a '0' for each tag and '1' for current tag output_row = list(output_empty) output_row[classes.index(doc[1])] = 1 training.append([bag, output_row])# shuffle our features and turn into np.arrayrandom.shuffle(training)training = np.array(training)# create train and test liststrain_x = list(training[:,0])train_y = list(training[:,1])
推荐阅读
维基解密最新宣布的CIA黑客对象中包含了CherryBlossom项目,该项目凸显了路由器的安然问题,包含缺乏固件签名方面的验证等。“在企业级产品方面,大年夜型路由器制造商已经供给签名固>>>详细阅读
本文标题:利用 TensorFlow 实现上下文的 Chat-bots
地址:http://www.17bianji.com/lsqh/35941.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示