快来!建立你的第一个Python聊天机器人项目
为了训练模型,把每个输入模式转换成数字。首先,对模式中的每个单词进行引理,并创建一个长度与单词总数相同的零列表。只将值1设置为那些在模式中包含单词的索引。同样,将1设置为模式所属的类输入,来创建输出。 # create the training data training= [] # create empty array for the output output_empty = [0] * len(classes) # training set, bag of words for everysentence fordoc in documents: # initializing bag of words bag= [] # list of tokenized words for thepattern word_patterns = doc[0] # lemmatize each word - create baseword, in attempt to represent related words word_patterns = [lemmatizer.lemmatize(word.lower()) for word in word_patterns] # create the bag of words array with1, if word is found in current pattern forword in words: bag.append(1) if word inword_patterns else bag.append(0) # output is a '0' for each tag and '1'for current tag (for each pattern) output_row = list(output_empty) output_row[classes.index(doc[1])] = 1 training.append([bag, output_row]) # shuffle the features and make numpyarray random.shuffle(training) training= np.array(training) # create training and testing lists. X- patterns, Y - intents train_x= list(training[:,0]) train_y= list(training[:,1]) print("Training data is created") 第四步:训练模型 (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |