Commit 234a6ad2 authored by 于飞's avatar 于飞

在编辑,添加,删除的时候,更新关键词

parent a59352e2
...@@ -49,6 +49,32 @@ class DFAFilter(): ...@@ -49,6 +49,32 @@ class DFAFilter():
if i == len(chars) - 1: if i == len(chars) - 1:
level[self.delimit] = 0 level[self.delimit] = 0
#从字典中删除一个关键词
def remove(self, keyword):
"""Remove a keyword from the DFA filter"""
if not isinstance(keyword, str):
keyword = keyword.decode('utf-8')
keyword = keyword.lower()
chars = keyword.strip()
if not chars:
return
def _remove_recursively(level, chars, index):
"""Helper function to recursively remove the keyword."""
if index == len(chars):
if self.delimit in level:
# Remove the terminal node (end of the keyword)
del level[self.delimit]
return len(level) == 0
char = chars[index]
if char in level and _remove_recursively(level[char], chars, index + 1):
# If the sub-level is empty, remove this character
del level[char]
return len(level) == 0
return False
_remove_recursively(self.keyword_chains, chars, 0)
#从文本里加载 敏感词 #从文本里加载 敏感词
def parse(self, path): def parse(self, path):
with open(path, encoding='UTF-8') as f: with open(path, encoding='UTF-8') as f:
......
...@@ -170,15 +170,22 @@ async def get_spacy_keywords(dialogue: ConversationVo = Body(), auth: Auth = Dep ...@@ -170,15 +170,22 @@ async def get_spacy_keywords(dialogue: ConversationVo = Body(), auth: Auth = Dep
return SuccessResponse(result) #返回type=3 return SuccessResponse(result) #返回type=3
#没有敏感词的时候,查找是否有相关图片 或者 视频 #没有敏感词的时候,查找是否有相关图片 或者 视频
#words = get_key_words(dialogue.user_input)
words = get_key_words_nlp(dialogue.user_input) #100%匹配算法 | 只取匹配到的第一个 words = get_key_words_nlp(dialogue.user_input) #100%匹配算法 | 只取匹配到的第一个
if len(words) > 0: if len(words) > 0:
print(f"----匹配到的关键词--->:{words[0]}") print(f"---算法1-匹配到的关键词--->:{words[0]}")
result = await get_media_datas(dialogue.conv_uid, words[0], auth.db) result = await get_media_datas(dialogue.conv_uid, words[0], auth.db)
return SuccessResponse(result) return SuccessResponse(result)
else: else:
print(f"-----没有找到需要查询的内容:---->") print(f"---算法2-begin--->")
return ErrorResponse("没有找到需要查询的内容") #上面的算法没找到,换一种算法继续找
words2 = get_key_words(dialogue.user_input)
if len(words2) > 0:
print(f"---算法2-匹配到的关键词--->:{words[0]}")
result = await get_media_datas(dialogue.conv_uid, words2[0], auth.db)
return SuccessResponse(result)
else:
print(f"-----没有找到需要查询的内容:---->")
return ErrorResponse("没有找到需要查询的内容")
@router.get("/load_parse_from_db", summary="加载敏感词和资源关键词") @router.get("/load_parse_from_db", summary="加载敏感词和资源关键词")
......
...@@ -9,6 +9,7 @@ from . import schemas, crud ...@@ -9,6 +9,7 @@ from . import schemas, crud
from .params.media_list import MediaListParams, GroupListParams, MediaEditParams, QuestionListParams, \ from .params.media_list import MediaListParams, GroupListParams, MediaEditParams, QuestionListParams, \
QuestionEditParams, CorrelationListParams QuestionEditParams, CorrelationListParams
from ...core.dependencies import IdList from ...core.dependencies import IdList
from dbgpt.app.apps.utils.filter import mydfafiter, mydfafiter_picture, mydfafiter_question, mydfafiter_video
router = APIRouter() router = APIRouter()
...@@ -67,7 +68,11 @@ async def media_edit(params: MediaEditParams = Depends(), auth: Auth = Depends(F ...@@ -67,7 +68,11 @@ async def media_edit(params: MediaEditParams = Depends(), auth: Auth = Depends(F
@router.post("/question/add", summary="新建问答对") @router.post("/question/add", summary="新建问答对")
async def question_add(data: schemas.Question, auth: Auth = Depends(FullAdminAuth())): async def question_add(data: schemas.Question, auth: Auth = Depends(FullAdminAuth())):
return SuccessResponse(await crud.QuestionDal(auth.db).create_data(data=data)) ret = await crud.QuestionDal(auth.db).create_data(data=data)
if len(ret) > 0:
print(f"-------------------------add->:{ret}")
mydfafiter_question.add(data.key_word) # 向关键词字典中添加一个新的问答对
return SuccessResponse(ret)
@router.get("/question/list", summary="问答对列表") @router.get("/question/list", summary="问答对列表")
...@@ -78,12 +83,28 @@ async def question_list(params: QuestionListParams = Depends(), auth: Auth = Dep ...@@ -78,12 +83,28 @@ async def question_list(params: QuestionListParams = Depends(), auth: Auth = Dep
@router.post("/question/del", summary="删除问答对") @router.post("/question/del", summary="删除问答对")
async def question_add(ids: IdList = Depends(), auth: Auth = Depends(FullAdminAuth())): async def question_add(ids: IdList = Depends(), auth: Auth = Depends(FullAdminAuth())):
return SuccessResponse(await crud.QuestionDal(auth.db).delete_datas(ids.ids, v_soft=True)) ret = await crud.QuestionDal(auth.db).delete_datas(ids.ids, v_soft=True)
#print(f"-------------------------delete->:{ret}")
question_dic = {'page': 1, 'limit': 0, 'v_order': None, 'v_order_field': None, 'id': ids.ids[0]}
question_datas, count = await crud.QuestionDal(auth.db).get_datas(**question_dic, v_return_count=True)
if len(question_datas) > 0:
print(f"------要删除问答对------>:{question_datas[0]}")
mydfafiter_question.remove(question_datas[0].get('key_word')) # 将关键词字典中删除一个新的问答对
return SuccessResponse(ret)
@router.post("/question/edit", summary="编辑问答对") @router.post("/question/edit", summary="编辑问答对")
async def question_edit(params: QuestionEditParams = Depends(), auth: Auth = Depends(FullAdminAuth())): async def question_edit(params: QuestionEditParams = Depends(), auth: Auth = Depends(FullAdminAuth())):
return SuccessResponse(await crud.QuestionDal(auth.db).update_question_datas(params)) if len(params.key_word) > 0:
question_dic = {'page': 1, 'limit': 0, 'v_order': None, 'v_order_field': None, 'id': params.ids[0]}
question_datas, count = await crud.QuestionDal(auth.db).get_datas(**question_dic, v_return_count=True)
if len(question_datas) > 0:
print(f"------编辑时要删除旧的问答对------>:{question_datas[0]}")
mydfafiter_question.remove(question_datas[0].get('key_word'))
print(f"------编辑时要添加新的问答对------>:{params.key_word}")
mydfafiter_question.add(params.key_word)
ret = await crud.QuestionDal(auth.db).update_question_datas(params)
return SuccessResponse(ret)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment