从我给你的代码后面接条形图代码
时间: 2023-12-20 11:25:57 浏览: 55
好的,以下是在你给出的代码后面添加条形图代码的完整代码:
```python
import openai
import re
import jieba
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.feature_extraction.text import TfidfVectorizer
# 配置OpenAI API
openai.api_key = "YOUR_API_KEY"
# 聊天模型
model_engine = "text-davinci-002"
# 对话循环
while True:
user_input = input("You: ")
if user_input.lower() == 'bye':
print("Chatbot: Goodbye!")
break
else:
# 发送请求
response = openai.Completion.create(
engine=model_engine,
prompt=user_input,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# 提取回复
raw_text = response.choices[0].text
reply_text = raw_text.strip()
# 去除标点符号等无关内容
reply_text = re.sub('[^\w\s]', '', reply_text)
# 分词
words = jieba.cut(reply_text)
# 统计词频
word_counts = {}
for word in words:
if len(word) > 1:
word_counts[word] = word_counts.get(word, 0) + 1
# 转换成DataFrame
df = pd.DataFrame.from_dict(word_counts, orient='index', columns=['count'])
# 计算TF-IDF值
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform([reply_text])
weights = np.asarray(tfidf_matrix.mean(axis=0)).ravel().tolist()
weights_df = pd.DataFrame({'term': tfidf.get_feature_names(), 'weight': weights})
weights_df = weights_df.sort_values('weight', ascending=False)
# 绘制条形图
top_n = 10 # 取前10个高频词
top_words = df.sort_values('count', ascending=False).head(top_n)
top_words.plot(kind='bar', x=top_words.index, y='count', color='blue', legend=False)
plt.xlabel('Words')
plt.ylabel('Frequency')
plt.title('Top {} Words'.format(top_n))
plt.show()
# 输出回复
print("Chatbot:", reply_text)
```
这段代码将使用OpenAI进行对话,并在每次回复后绘制回复中出现的高频词汇的条形图,您可以通过修改`top_n`的值来控制绘制的高频词汇数量。
阅读全文