From ca45d6d587ce1b24f71055b14138fb76e9387dfc Mon Sep 17 00:00:00 2001 From: Xtao_dada Date: Mon, 21 Jun 2021 14:49:23 +0800 Subject: [PATCH] =?UTF-8?q?groupword=20=E6=94=AF=E6=8C=81=E6=99=BA?= =?UTF-8?q?=E8=83=BD=E5=88=86=E8=AF=8D=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E6=8F=92=E4=BB=B6=E6=8A=A5=E9=94=99=E3=80=82=20(#170)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autorm.py | 4 ++-- bc.py | 13 +++++++------ groupword.py | 48 +++++++++++++++++++++++++++++++++++++++--------- list.json | 10 +++++----- pl.py | 3 ++- sendat.py | 2 +- 6 files changed, 56 insertions(+), 24 deletions(-) diff --git a/autorm.py b/autorm.py index 4840020..0c883b8 100644 --- a/autorm.py +++ b/autorm.py @@ -10,7 +10,7 @@ try: except ImportError: imported = False -import asyncio, time, traceback +import asyncio, time, traceback, sys from pagermaid.listener import listener from pagermaid.utils import alias_command @@ -104,7 +104,7 @@ async def autorm(context): args = args.strip() if not imported: - await edit(context, "Please install dateparser first: python3 -m pip install dateparser") + await edit(context, f"Please install dateparser first: {sys.executable} -m pip install dateparser") return if len(args) == 0: await edit(context, "参数不能为空。使用 -help autorm 以查看帮助。") diff --git a/bc.py b/bc.py index 0c7b2b9..d1c639c 100644 --- a/bc.py +++ b/bc.py @@ -8,6 +8,7 @@ # from asyncio import sleep +from sys import executable import urllib.request from telethon.tl.custom.message import Message from pagermaid.listener import listener @@ -50,17 +51,17 @@ async def coin(context: Message) -> None: """ coin change """ if not imported: await context.edit("支持库 `python-binance` `xmltodict` 未安装...\n正在尝试自动安装...") - await execute('python3 -m pip install python-binance') - await execute('python3 -m pip install xmltodict') + await execute(f'{executable} -m pip install python-binance') + await execute(f'{executable} -m pip install xmltodict') await sleep(10) - result = await execute('python3 show python-binance') - result1 = await execute('python3 show xmltodict') + result = await execute(f'{executable} -m pip show python-binance') + result1 = await execute(f'{executable} -m pip show xmltodict') if len(result) > 0 and len(result1) > 0: await context.edit('支持库 `python-binance` `xmltodict` 安装成功...\n正在尝试自动重启...') await context.client.disconnect() else: - await context.edit("自动安装失败..请尝试手动安装 `python3 -m pip install python-binance`\n\n`python3 -m pip install " - "xmltodict`\n随后,请重启 PagerMaid") + await context.edit(f"自动安装失败..请尝试手动安装 `{executable} -m pip install python-binance`\n\n`" + f"{executable} -m pip install xmltodict`\n随后,请重启 PagerMaid") return init() action = context.arguments.split() diff --git a/groupword.py b/groupword.py index 8037f5f..532746f 100644 --- a/groupword.py +++ b/groupword.py @@ -3,12 +3,14 @@ from wordcloud import WordCloud from io import BytesIO from os.path import exists from os import makedirs +from sys import executable from collections import defaultdict from requests import get from pagermaid.utils import execute, alias_command from pagermaid.listener import listener imported = True +imported_ = True punctuation = {33: ' ', 34: ' ', 35: ' ', 36: ' ', 37: ' ', 38: ' ', 39: ' ', 40: ' ', 41: ' ', 42: ' ', 43: ' ', 44: ' ', 45: ' ', 46: ' ', 47: ' ', 58: ' ', 59: ' ', 60: ' ', 61: ' ', 62: ' ', 63: ' ', 64: ' ', 91: ' ', 92: ' ', 93: ' ', 94: ' ', 95: ' ', 96: ' ', 123: ' ', 124: ' ', 125: ' ', 126: ' ', @@ -18,6 +20,10 @@ try: import jieba except ImportError: imported = False +try: + import paddle +except ImportError: + imported_ = False @listener(is_plugin=True, outgoing=True, command=alias_command("groupword"), @@ -25,18 +31,34 @@ except ImportError: async def group_word(context): if not imported: try: - await context.edit("支持库 `jieba` 未安装...\n正在尝试自动安装...") - await execute('python3 -m pip install jieba') + await context.edit("支持库 `jieba` `paddlepaddle-tiny` 未安装...\n正在尝试自动安装...") + await execute(f'{executable} -m pip install jieba') + await execute(f'{executable} -m pip install paddlepaddle-tiny') await sleep(10) - result = await execute('python3 show jieba') + result = await execute(f'{executable} -m pip show jieba') if len(result) > 0: await context.edit('支持库 `jieba` 安装成功...\n正在尝试自动重启...') await context.client.disconnect() else: - await context.edit("自动安装失败..请尝试手动安装 `python3 -m pip install jieba` 随后,请重启 PagerMaid-Modify 。") + await context.edit(f"自动安装失败..请尝试手动安装 `{executable} -m pip install jieba` 随后,请重启 PagerMaid-Modify 。") return except: return + if not imported_: + try: + await context.edit("支持库 `paddlepaddle-tiny` 未安装...\n正在尝试自动安装...") + await execute(f'{executable} -m pip install paddlepaddle-tiny') + await sleep(10) + result = await execute(f'{executable} -m pip show paddlepaddle-tiny') + if len(result) > 0 and not 'WARNING' in result: + await context.edit('支持库 `paddlepaddle-tiny` 安装成功...\n正在尝试自动重启...') + await context.client.disconnect() + else: + await context.edit(f"自动安装失败,可能是系统不支持..\nAI 分词不可用,切换到基础分词。\n" + f"您可以尝试手动安装 `{executable} -m pip install paddlepaddle-tiny` 。") + await sleep(4) + except: + return try: await context.edit('正在生成中。。。') except: @@ -51,14 +73,21 @@ async def group_word(context): words = defaultdict(int) count = 0 try: + if imported_: + jieba.enable_paddle() async for msg in context.client.iter_messages(context.chat, limit=500): if msg.id == context.id: continue - if msg.text: + if msg.text and not msg.text.startswith('/') and not msg.text.startswith('-') and not '//' in msg.text: try: - for word in jieba.cut(msg.text.translate(punctuation)): - word = word.lower() - words[word] += 1 + if imported_: + for word in jieba.cut(msg.text.translate(punctuation), use_paddle=True): + word = word.lower() + words[word] += 1 + else: + for word in jieba.cut(msg.text.translate(punctuation)): + word = word.lower() + words[word] += 1 count += 1 except: pass @@ -70,7 +99,8 @@ async def group_word(context): except: return try: - image = WordCloud(font_path="plugins/groupword/wqy-microhei.ttc", width=800, height=400).generate_from_frequencies( + image = WordCloud(font_path="plugins/groupword/wqy-microhei.ttc", width=800, + height=400).generate_from_frequencies( words).to_image() stream = BytesIO() image.save(stream, 'PNG') diff --git a/list.json b/list.json index 9a9eff3..ab5686b 100644 --- a/list.json +++ b/list.json @@ -32,7 +32,7 @@ }, { "name": "autorm", - "version": "1.01", + "version": "1.02", "section": "chat", "maintainer": "fruitymelon", "size": "6.7 kb", @@ -42,7 +42,7 @@ }, { "name": "sendat", - "version": "1.31", + "version": "1.32", "section": "chat", "maintainer": "fruitymelon", "size": "15.0 kb", @@ -442,7 +442,7 @@ }, { "name": "bc", - "version": "1.22", + "version": "1.23", "section": "chat", "maintainer": "Pentacene", "size": "8.0 kb", @@ -472,7 +472,7 @@ }, { "name": "pl", - "version": "1.01", + "version": "1.02", "section": "chat", "maintainer": "lowking", "size": "4.0 kb", @@ -502,7 +502,7 @@ }, { "name": "groupword", - "version": "1.03", + "version": "1.04", "section": "chat", "maintainer": "xtaodada", "size": "3.32 kb", diff --git a/pl.py b/pl.py index 069b93f..64a2161 100644 --- a/pl.py +++ b/pl.py @@ -12,6 +12,7 @@ except ImportError: from asyncio import sleep from requests import get +from sys import executable from pagermaid.listener import listener from pagermaid.utils import alias_command from urllib import parse @@ -22,7 +23,7 @@ from urllib import parse parameters="<食物名>") async def pl(context): if not imported: - await context.edit("请先安装依赖:\n`python3 -m pip install bs4`\n随后,请重启 pagermaid。") + await context.edit(f"请先安装依赖:\n`{executable} -m pip install bs4`\n随后,请重启 pagermaid。") return action = context.arguments.split() if len(action) == 1: diff --git a/sendat.py b/sendat.py index 7c740cb..0d17eec 100644 --- a/sendat.py +++ b/sendat.py @@ -87,7 +87,7 @@ async def sendat(context): chat = await context.get_chat() args = " ".join(context.parameter).split("|") if not imported: - await context.edit("请先安装依赖:`python3 -m pip install dateparser`\n随后,请重启 pagermaid。") + await context.edit(f"请先安装依赖:`{sys.executable} -m pip install dateparser`\n随后,请重启 pagermaid。") return await context.edit(f"debug: tz data: {time.timezone} {time.tzname} {sign}{offset}") if len(args) != 2: