【Dify】 查询改为关键词匹配查询
parent
53964c60c1
commit
8048e83979
@ -0,0 +1,114 @@
|
||||
import difflib
|
||||
from collections import defaultdict, Counter
|
||||
import itertools
|
||||
import re
|
||||
|
||||
class TextIndex:
|
||||
def __init__(self, text_text, index):
|
||||
self.text_text = text_text
|
||||
self.index = index
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"text_text": self.text_text,
|
||||
"index": self.index,
|
||||
}
|
||||
|
||||
def find_all_occurrences(source: str, target: str):
|
||||
return [match.start() for match in re.finditer(re.escape(source), target)]
|
||||
|
||||
def get_text_max_score(search_texts: list[str],search_index: int, pos_map,root_list:list[TextIndex], groups:list[list[TextIndex]]):
|
||||
|
||||
if len(search_texts) == search_index and len(root_list) > 0:
|
||||
groups.append(root_list)
|
||||
return
|
||||
search_text = search_texts[search_index]
|
||||
text_indexs = pos_map[search_text]
|
||||
next_index = search_index + 1
|
||||
if text_indexs:
|
||||
new_root_list = root_list[:]
|
||||
for t_idx,text_index in enumerate(text_indexs):
|
||||
this_root_list = []
|
||||
if t_idx > 0:
|
||||
this_root_list=new_root_list[:]
|
||||
else:
|
||||
this_root_list = root_list
|
||||
this_root_list.append(text_index)
|
||||
get_text_max_score(search_texts=search_texts,search_index=next_index,pos_map=pos_map,root_list=this_root_list,groups=groups)
|
||||
else:
|
||||
get_text_max_score(search_texts=search_texts,search_index=next_index,pos_map=pos_map,root_list=root_list,groups=groups)
|
||||
|
||||
# def get_text_index_score(text_indexs: list[TextIndex],search_texts: list[str]):
|
||||
# # 去掉一个最后面的
|
||||
# # 去掉一个最前面的
|
||||
|
||||
def get_text_index_score(text_indexs: list[TextIndex],search_texts: list[str]):
|
||||
|
||||
deduct_points = 0
|
||||
search_text_count = len("".join(search_texts))
|
||||
text_count = 0
|
||||
for idx,text_index in enumerate(text_indexs):
|
||||
text_count += len(text_index.text_text)
|
||||
if idx < len(text_indexs) - 1:
|
||||
next_text_index = text_indexs[idx + 1]
|
||||
t_score = 0
|
||||
if next_text_index.index > text_index.index:
|
||||
t_score = next_text_index.index - text_index.index - len(text_index.text_text) - 1
|
||||
else:
|
||||
t_score = text_index.index - next_text_index.index - len(next_text_index.text_text)
|
||||
t_score = abs(t_score)
|
||||
deduct_points += t_score
|
||||
if deduct_points > 50:
|
||||
return 0
|
||||
deduct_points += (search_text_count - text_count) * 3
|
||||
return 100 - deduct_points
|
||||
|
||||
def get_full_search_text_max_score(search_texts: list[str], target_text: str) -> (int, list[TextIndex]):
|
||||
import pdb; pdb.set_trace()
|
||||
# 1. 建立 source 中每个字符的索引映射
|
||||
# pos_map = defaultdict(list)
|
||||
text_index_groups:list[list[TextIndex]] = []
|
||||
for search_text in search_texts:
|
||||
idxs = find_all_occurrences(source=search_text, target=target_text)
|
||||
text_indexs = [TextIndex(text_text=search_text,index=idx) for idx in idxs]
|
||||
# pos_map[search_text].extend(text_indexs)
|
||||
text_index_groups.append(text_indexs)
|
||||
|
||||
import pdb; pdb.set_trace()
|
||||
# groups:list[list[TextIndex]] = []
|
||||
max_score = -100000
|
||||
max_index_list:list[TextIndex]
|
||||
for text_index_s in itertools.product(*text_index_groups):
|
||||
text_index_list:list[TextIndex] = list(text_index_s)
|
||||
score_ = get_text_index_score(text_indexs=text_index_list,search_texts=search_texts)
|
||||
if score_ < 50:
|
||||
continue
|
||||
if score_ > max_score:
|
||||
max_score = score_
|
||||
max_index_list = text_index_list
|
||||
|
||||
# get_text_max_score(search_texts=search_texts,search_index=0,pos_map=pos_map,root_list=[], groups=groups)
|
||||
# max_index_list:list[TextIndex] = []
|
||||
# max_score = -100000
|
||||
# import pdb; pdb.set_trace()
|
||||
# for g_list in groups:
|
||||
# score_,milist = get_text_index_score(text_indexs=g_list,search_texts=search_texts)
|
||||
# if score_ > max_score:
|
||||
# max_score = score_
|
||||
# max_index_list = g_list
|
||||
# print("score_",score_)
|
||||
# texts = []
|
||||
# for text_index in g_list:
|
||||
# t_len = len(text_index.text_text)
|
||||
# t_idx = text_index.index
|
||||
# text = target_text[t_idx : t_idx+t_len]
|
||||
# texts.append(text)
|
||||
# print("--------------------------")
|
||||
# print("".join(texts))
|
||||
import pdb; pdb.set_trace()
|
||||
return (max_score,max_index_list)
|
||||
|
||||
if __name__ == "__main__":
|
||||
search_texts=["湖人","阵容"]
|
||||
score, max_index_list =get_full_search_text_max_score(search_texts=search_texts, source="所以,**严格讲,詹姆斯在湖人确实拥有超级巨星(戴维斯),但不像热火三巨头那样多核并立。**更多时候,他还是湖人阵容的绝对核心和领袖。")
|
||||
print(score, len(max_index_list))
|
||||
@ -0,0 +1,264 @@
|
||||
的
|
||||
了
|
||||
和
|
||||
是
|
||||
我
|
||||
也
|
||||
就
|
||||
都
|
||||
而
|
||||
及
|
||||
与
|
||||
着
|
||||
或
|
||||
一个
|
||||
没有
|
||||
我们
|
||||
你们
|
||||
他们
|
||||
她们
|
||||
它们
|
||||
自己
|
||||
这
|
||||
那
|
||||
这些
|
||||
那些
|
||||
它
|
||||
被
|
||||
在
|
||||
对于
|
||||
因为
|
||||
所以
|
||||
如果
|
||||
然后
|
||||
而且
|
||||
并且
|
||||
并
|
||||
但是
|
||||
不过
|
||||
不是
|
||||
而是
|
||||
还有
|
||||
还
|
||||
已
|
||||
已经
|
||||
正在
|
||||
非常
|
||||
很
|
||||
较
|
||||
更
|
||||
最
|
||||
吧
|
||||
啊
|
||||
呀
|
||||
嘛
|
||||
呢
|
||||
么
|
||||
吗
|
||||
哦
|
||||
恩
|
||||
呃
|
||||
咯
|
||||
啊呀
|
||||
啥
|
||||
哈
|
||||
啊哈
|
||||
啦
|
||||
咱
|
||||
什么
|
||||
多少
|
||||
几
|
||||
多
|
||||
你
|
||||
我
|
||||
他
|
||||
她
|
||||
它
|
||||
咱们
|
||||
此
|
||||
其
|
||||
某
|
||||
某个
|
||||
某些
|
||||
每
|
||||
每个
|
||||
各
|
||||
个
|
||||
等
|
||||
等于
|
||||
以及
|
||||
其中
|
||||
从而
|
||||
因此
|
||||
除此之外
|
||||
据此
|
||||
比如
|
||||
例如
|
||||
比如说
|
||||
譬如
|
||||
比如说的
|
||||
说
|
||||
要
|
||||
来
|
||||
去
|
||||
把
|
||||
被
|
||||
给
|
||||
使
|
||||
令
|
||||
让
|
||||
令得
|
||||
所
|
||||
之
|
||||
之所以
|
||||
以
|
||||
以便
|
||||
以免
|
||||
以至
|
||||
以致
|
||||
以内
|
||||
以来
|
||||
之后
|
||||
之前
|
||||
之后
|
||||
期间
|
||||
前后
|
||||
上下
|
||||
以上
|
||||
以下
|
||||
左右
|
||||
当时
|
||||
当年
|
||||
当下
|
||||
眼下
|
||||
马上
|
||||
立刻
|
||||
即将
|
||||
刚刚
|
||||
刚才
|
||||
后来
|
||||
曾经
|
||||
仍然
|
||||
依然
|
||||
一直
|
||||
一直到
|
||||
尚且
|
||||
甚至
|
||||
最终
|
||||
总是
|
||||
总共
|
||||
其实
|
||||
本来
|
||||
原来
|
||||
明显
|
||||
确实
|
||||
大概
|
||||
大约
|
||||
差不多
|
||||
也许
|
||||
可能
|
||||
估计
|
||||
基本
|
||||
尤其
|
||||
尽管
|
||||
虽然
|
||||
然而
|
||||
然而却
|
||||
不过
|
||||
但是
|
||||
还是
|
||||
但是呢
|
||||
毕竟
|
||||
同时
|
||||
并不是
|
||||
并非
|
||||
并无
|
||||
未必
|
||||
尚未
|
||||
不如
|
||||
不然
|
||||
以外
|
||||
之外
|
||||
其中
|
||||
而后
|
||||
而今
|
||||
而后
|
||||
除此
|
||||
除此之外
|
||||
否则
|
||||
万一
|
||||
万万
|
||||
若
|
||||
若是
|
||||
假如
|
||||
假设
|
||||
要是
|
||||
若非
|
||||
非得
|
||||
无非
|
||||
何况
|
||||
况且
|
||||
再说
|
||||
试问
|
||||
只要
|
||||
除非
|
||||
只有
|
||||
宁愿
|
||||
宁可
|
||||
宁肯
|
||||
不如
|
||||
不妨
|
||||
不必
|
||||
务必
|
||||
务须
|
||||
尚需
|
||||
无需
|
||||
都
|
||||
谁
|
||||
啥
|
||||
哪
|
||||
哪儿
|
||||
哪里
|
||||
怎样
|
||||
怎么
|
||||
怎么样
|
||||
何时
|
||||
几时
|
||||
多少
|
||||
几多
|
||||
多么
|
||||
啥也
|
||||
哪怕
|
||||
纵然
|
||||
即使
|
||||
假使
|
||||
便
|
||||
则
|
||||
即
|
||||
乃
|
||||
虽
|
||||
虽说
|
||||
且
|
||||
以致于
|
||||
为止
|
||||
为此
|
||||
为的是
|
||||
不管
|
||||
无论
|
||||
任凭
|
||||
凡是
|
||||
凡
|
||||
既然
|
||||
既
|
||||
既已
|
||||
既然如此
|
||||
说实话
|
||||
说到底
|
||||
也就是说
|
||||
一句话
|
||||
总之
|
||||
总的来说
|
||||
总而言之
|
||||
总的说来
|
||||
换句话说
|
||||
话说
|
||||
Loading…
Reference in New Issue