Skip to content

Commit

Permalink
Merge pull request #158 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
本地问答库支持自定义变量的形式来开发动态语句;本地问答库-文本 改为相似度匹配,共用一个相似度值
  • Loading branch information
Ikaros-521 authored Jul 23, 2023
2 parents 6c8e0e1 + 53ec8d4 commit 0af5341
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ cmd输入命令即可:`doctoc /path/to/file`
- 修复音频播放部分,音频变速语速低于1时变速合成的音频不正常的bug
- 改变弹幕处理逻辑,先由process_data预处理函数进行指定时间内的数据丢弃后,再进行数据处理,可以有效降低高并发造成的一系列问题
- dy_old改为接入第三方大佬提供的免费不可商用ws监听
- 本地问答库支持自定义变量的形式来开发动态语句
- 本地问答库-文本 修改完全匹配为相似度匹配,暂时和音频匹配共用一个相似度值


</details>

Expand Down
4 changes: 3 additions & 1 deletion data/本地问答库.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
在?
在哟
奇数行是问题行,下一行就是这个问题对应的答案
我就是上一行问题的回答
我就是上一行问题的回答
是录播吗
现在是{cur_time},不是录播哦
2 changes: 1 addition & 1 deletion utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def audio_speed_change(self, audio_path, speed_factor=1.0, pitch_factor=1.0):
Returns:
str: 变速后的音频路径
"""
logging.info(f"audio_path={audio_path}, speed_factor={speed_factor}, pitch_factor={pitch_factor}")
logging.debug(f"audio_path={audio_path}, speed_factor={speed_factor}, pitch_factor={pitch_factor}")

# 使用 pydub 打开音频文件
audio = AudioSegment.from_file(audio_path)
Expand Down
7 changes: 7 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def get_bj_time(self, type=0):
tgt_time = current_milliseconds % 100 # 用于生成音频文件名

return str(tgt_time)
elif type == 5:
now = time.localtime() # 获取当前时间

hour = now.tm_hour # 获取当前小时
minute = now.tm_min # 获取当前分钟

return str(hour) + "点" + str(minute)

# 删除多余单词
def remove_extra_words(self, text="", max_len=30, max_char_len=50):
Expand Down
41 changes: 29 additions & 12 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,25 +171,32 @@ def __init__(self, config_path):
def get_room_id(self):
return self.room_id

def find_answer(self, question, qa_file_path):
def find_answer(self, question, qa_file_path, similarity=1):
"""从本地问答库中搜索问题的答案
Args:
question (_type_): 问题文本
qa_file_path (_type_): 问答库的路径
question (str): 问题文本
qa_file_path (str): 问答库的路径
similarity (float): 相似度
Returns:
_type_: 答案文本 或 None
str: 答案文本 或 None
"""

with open(qa_file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

for i in range(0, len(lines), 2):
if question.strip() == lines[i].strip():
if i + 1 < len(lines):
return lines[i + 1].strip()
else:
return None
q_list = [lines[i].strip() for i in range(0, len(lines), 2)]
q_to_answer_index = {q: i + 1 for i, q in enumerate(q_list)}

q = self.common.find_best_match(question, q_list, similarity)
# print(f"q={q}")

if q is not None:
answer_index = q_to_answer_index.get(q)
# print(f"answer_index={answer_index}")
if answer_index is not None and answer_index < len(lines):
return lines[answer_index * 2 - 1].strip()

return None

Expand Down Expand Up @@ -230,9 +237,18 @@ def commit_handle(self, data):
# 1、匹配本地问答库 触发后不执行后面的其他功能
if self.local_qa["text"]["enable"] == True:
# 输出当前用户发送的弹幕消息
logging.info(f"[{user_name}]: {content}")
tmp = self.find_answer(content, self.local_qa["text"]["file_path"])
tmp = self.find_answer(content, self.local_qa["text"]["file_path"], self.local_qa["similarity"])

if tmp != None:
logging.info(f"触发本地问答库-文本 [{user_name}]: {content}")
# 将问答库中设定的参数替换为指定内容,开发者可以自定义替换内容
if "{cur_time}" in tmp:
tmp = tmp.format(cur_time=self.common.get_bj_time(5))
else:
tmp = tmp

logging.info(f"本地问答库-文本回答为: {tmp}")

resp_content = tmp
# 将 AI 回复记录到日志文件中
with open(self.commit_file_path, "r+", encoding="utf-8") as f:
Expand Down Expand Up @@ -271,6 +287,7 @@ def commit_handle(self, data):

# 2、匹配本地问答音频库 触发后不执行后面的其他功能
if self.local_qa["audio"]["enable"] == True:
logging.info(f"触发本地问答库-语音 [{user_name}]: {content}")
# 输出当前用户发送的弹幕消息
# logging.info(f"[{user_name}]: {content}")
# 获取本地问答音频库文件夹内所有的音频文件名
Expand Down

0 comments on commit 0af5341

Please sign in to comment.