🐞 fix(text-similarity): 修复除0报错 (#302)

* 🐞 fix(text-similarity): 修复除0报错

没有考虑到bilibili的推送出现动态或者视频简介长度为零的情况,出现文本相似度除0Error

* 🧪 test(bilibili): 添加视频动态内容为空的情况的测试

* 🧪 test(text-similarity): 增加文本相似度函数的测试

* Update test_rss.py

* 📃 docs(text_similarity): 添加文本相似度函数的注释

* 🦄 refactor(text_similarity): 重构文本相似度的比较方法

* 🎈 perf(similar_text): 将比较函数的return改成raise

* 🦄 refactor(text_similarity): 重构文本相似度比较方法

* Update nonebot_bison/platform/bilibili.py

Co-authored-by: felinae98 <731499577@qq.com>

* Update nonebot_bison/platform/rss.py

Co-authored-by: felinae98 <731499577@qq.com>

---------

Co-authored-by: felinae98 <731499577@qq.com>
This commit is contained in:
UKM
2023-08-27 16:28:26 +08:00
committed by GitHub
parent d8dd0cdd39
commit cfa91df5e4
6 changed files with 69 additions and 20 deletions
+11 -11
View File
@@ -125,6 +125,16 @@ class Bilibili(NewMessage):
def get_tags(self, raw_post: RawPost) -> list[Tag]:
return [*(tp["topic_name"] for tp in raw_post["display"]["topic_info"]["topic_details"])]
def _text_process(self, dynamic: str, desc: str, title: str) -> str:
similarity = 1.0 if len(dynamic) == 0 or len(desc) == 0 else text_similarity(dynamic, desc)
if len(dynamic) == 0 and len(desc) == 0:
text = title
elif similarity > 0.8:
text = title + "\n\n" + desc if len(dynamic) < len(desc) else dynamic + "\n=================\n" + title
else:
text = dynamic + "\n=================\n" + title + "\n\n" + desc
return text
def _get_info(self, post_type: Category, card) -> tuple[str, list]:
if post_type == 1:
# 一般动态
@@ -139,17 +149,7 @@ class Bilibili(NewMessage):
dynamic = card.get("dynamic", "")
title = card["title"]
desc = card.get("desc", "")
if text_similarity(desc, dynamic) > 0.8:
# 如果视频简介和动态内容相似,就只保留长的那个
if len(dynamic) > len(desc):
text = f"{dynamic}\n=================\n{title}"
else:
text = f"{title}\n\n{desc}"
else:
# 否则就把两个拼起来
text = f"{dynamic}\n=================\n{title}\n\n{desc}"
text = self._text_process(dynamic, desc, title)
pic = [card["pic"]]
elif post_type == 4:
# 纯文字
+9 -8
View File
@@ -53,18 +53,19 @@ class Rss(NewMessage):
entry["_target_name"] = feed.feed.title
return feed.entries
def _text_process(self, title: str, desc: str) -> str:
similarity = 1.0 if len(title) == 0 or len(desc) == 0 else text_similarity(title, desc)
if similarity > 0.8:
text = title if len(title) > len(desc) else desc
else:
text = title + "\n\n" + desc
return text
async def parse(self, raw_post: RawPost) -> Post:
title = raw_post.get("title", "")
soup = bs(raw_post.description, "html.parser")
desc = soup.text.strip()
if not title or not desc:
text = title or desc
else:
if text_similarity(desc, title) > 0.8:
text = desc if len(desc) > len(title) else title
else:
text = f"{title}\n\n{desc}"
text = self._text_process(title, desc)
pics = [x.attrs["src"] for x in soup("img")]
if raw_post.get("media_content"):
for media in raw_post["media_content"]:
+3
View File
@@ -95,6 +95,9 @@ if plugin_config.bison_filter_log:
def text_similarity(str1, str2) -> float:
"""利用最长公共子序列的算法判断两个字符串是否相似,并返回0到1.0的相似度"""
if len(str1) == 0 or len(str2) == 0:
raise ValueError("The length of string can not be 0")
matcher = difflib.SequenceMatcher(None, str1, str2)
t = sum(temp.size for temp in matcher.get_matching_blocks())
return t / min(len(str1), len(str2))