mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
🐞 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:
@@ -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:
|
||||
# 纯文字
|
||||
|
||||
@@ -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"]:
|
||||
|
||||
Reference in New Issue
Block a user