groupword 支持智能分词,修复其他插件报错。 (#170)

This commit is contained in:
Xtao_dada 2021-06-21 14:49:23 +08:00 committed by GitHub
parent b8a4cb6571
commit ca45d6d587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 24 deletions

View File

@ -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 以查看帮助。")

13
bc.py
View File

@ -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()

View File

@ -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')

View File

@ -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",

3
pl.py
View File

@ -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:

View File

@ -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: