帮我写一个训练对话ai的python程序
时间: 2023-03-07 17:51:23 浏览: 94
我可以提供一个简单的Python程序来帮助你训练AI对话:import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()import numpy
import tflearn
import tensorflow
import random
import json
import picklewith open("intents.json") as file:
data = json.load(file)try:
with open("data.pickle", "rb") as f:
words, labels, training, output = pickle.load(f)
except:
words = []
labels = []
docs_x = []
docs_y = [] for intent in data["intents"]:
for pattern in intent["patterns"]:
wrds = nltk.word_tokenize(pattern)
words.extend(wrds)
docs_x.append(wrds)
docs_y.append(intent["tag"]) if intent["tag"] not in labels:
labels.append(intent["tag"]) words = [stemmer.stem(w.lower()) for w in words if w != "?"]
words = sorted(list(set(words))) labels = sorted(labels) training = []
output = [] out_empty = [0 for _ in range(len(labels))] for x, doc in enumerate(docs_x):
bag = [] wrds = [stemmer.stem(w.lower()) for w in doc] for w in words:
if w in wrds:
bag.append(1)
else:
bag.append(0) output_row = out_empty[:]
output_row[labels.index(docs_y[x])] = 1 training.append(bag)
output.append(output_row)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)