📅  最后修改于: 2023-12-03 15:24:52.488000             🧑  作者: Mango
本文介绍如何使用Python创建一个简单的AI助手,它可以进行问答、天气查询、翻译、聊天等基本功能,用户可以通过语音或文本输入与之交互。
本AI助手的核心技术包括语音识别、自然语言处理、机器学习、Web API等。
在开始之前,需要搭建Python环境并安装相关的库:
# 语音识别
pip install SpeechRecognition
# 文本转语音
pip install pyttsx3
# 自然语言处理
pip install nltk
# 机器学习
pip install scikit-learn
# Web API
pip install requests
语音识别可以将用户的语音转换为文本,方便程序进行后续处理。
import speech_recognition as sr
# 初始化语音识别器
r = sr.Recognizer()
# 获取麦克风输入
with sr.Microphone() as source:
print("请说话:")
audio = r.listen(source)
# 识别语音
text = r.recognize_google(audio, language='zh-CN')
print("你说的是:", text)
语音助手需要能够以自然的语音与用户进行交互,因此我们需要将文本转换为语音。
import pyttsx3
# 初始化文本转语音器
engine = pyttsx3.init()
# 说出文字
engine.say("你好")
# 等待语音结束
engine.runAndWait()
自然语言处理可以帮助AI助手理解用户语言并做出相应的回答。
import nltk
from nltk.corpus import wordnet
# 初始化nltk
nltk.download('wordnet')
# 获取单词的同义词
synonyms = []
for syn in wordnet.synsets("happy"):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
print(synonyms)
机器学习可以帮助AI助手理解语言中的意图并做出相应的回答。
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
# 数据集
X = ["天气怎么样",
"帮我找个附近的餐厅",
"我想听一首歌",
"讲个笑话吧",
"今天有什么新闻",
"翻译一下apple是什么意思",
"和我聊聊天",
"请问你是谁",
"退出程序"]
y = ["天气查询",
"附近餐厅查询",
"音乐播放",
"讲笑话",
"新闻查询",
"翻译",
"聊天",
"自我介绍",
"退出程序"]
# 特征抽取和降维
clf = Pipeline([
('tfidf', TfidfVectorizer()),
('svd', TruncatedSVD()),
('clf', MultinomialNB()),
])
clf.fit(X, y)
# 预测
text = "天气怎么样"
y_pred = clf.predict([text])
print(classification_report([y_pred], ["天气查询"]))
Web API可以让AI助手访问网络资源,从而提供更加丰富的功能。
import requests
# 天气查询
city = "北京"
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
response = requests.get(url)
data = response.json()
weather = data["data"]["forecast"][0]["type"]
print(weather)
下面是一个完整的AI助手实现代码:
import speech_recognition as sr
import pyttsx3
import nltk
from nltk.corpus import wordnet
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
import requests
# 初始化语音识别器
r = sr.Recognizer()
# 初始化文本转语音器
engine = pyttsx3.init()
# 初始化nltk
nltk.download('wordnet')
# 数据集
X = ["天气怎么样",
"帮我找个附近的餐厅",
"我想听一首歌",
"讲个笑话吧",
"今天有什么新闻",
"翻译一下apple是什么意思",
"和我聊聊天",
"请问你是谁",
"退出程序"]
y = ["天气查询",
"附近餐厅查询",
"音乐播放",
"讲笑话",
"新闻查询",
"翻译",
"聊天",
"自我介绍",
"退出程序"]
# 特征抽取和降维
clf = Pipeline([
('tfidf', TfidfVectorizer()),
('svd', TruncatedSVD(n_components=50)),
('clf', MultinomialNB(alpha=0.01)),
])
clf.fit(X, y)
while True:
# 获取麦克风输入
with sr.Microphone() as source:
print("请说话:")
audio = r.listen(source)
# 识别语音
text = r.recognize_google(audio, language='zh-CN')
print("你说的是:", text)
# 处理命令
if "天气" in text:
city = text.split("天气")[0]
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
response = requests.get(url)
data = response.json()
weather = data["data"]["forecast"][0]["type"]
engine.say(f"{city}的天气是{weather}")
elif "附近的餐厅" in text:
engine.say("还没实现这个功能")
elif "笑话" in text:
engine.say("还没实现这个功能")
elif "新闻" in text:
engine.say("还没实现这个功能")
elif "翻译" in text:
word = text.split("翻译一下")[-1]
url = f"http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i={word}"
response = requests.get(url)
data = response.json()
translated = data["translateResult"][0][0]["tgt"]
engine.say(translated)
elif "自我介绍" in text or "你是谁" in text:
engine.say("我是一台Python写的AI助手,可以帮助你做许多事情")
elif "聊天" in text:
synonyms = []
for syn in wordnet.synsets(text):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
label = clf.predict([text] + synonyms)[0]
if label == "附近餐厅查询":
engine.say("还没实现这个功能")
elif label == "音乐播放":
engine.say("还没实现这个功能")
elif label == "讲笑话":
engine.say("还没实现这个功能")
elif label == "新闻查询":
engine.say("还没实现这个功能")
elif label == "翻译":
engine.say("请说出要翻译的内容")
with sr.Microphone() as source:
audio = r.listen(source)
word = r.recognize_google(audio, language='zh-CN')
url = f"http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i={word}"
response = requests.get(url)
data = response.json()
translated = data["translateResult"][0][0]["tgt"]
engine.say(translated)
elif label == "天气查询":
city = text.split("天气")[0]
url = f"http://wthrcdn.etouch.cn/weather_mini?city={city}"
response = requests.get(url)
data = response.json()
weather = data["data"]["forecast"][0]["type"]
engine.say(f"{city}的天气是{weather}")
elif label == "聊天":
engine.say("还没实现这个功能")
elif label == "自我介绍":
engine.say("我是一台Python写的AI助手,可以帮助你做许多事情")
elif label == "退出程序":
engine.say("好的,下次再见")
break
else:
engine.say("我不太明白,请再说一遍")
# 播放语音
engine.runAndWait()
通过上面的示例代码,我们学习了如何使用Python创建一个简单的AI助手,并且了解到了语音识别、自然语言处理、机器学习等核心技术。希望本文对您有所帮助,谢谢!