groupword 支持自定义背景图(图片白色区域为不显示部分)

This commit is contained in:
xtaodada 2022-03-10 22:48:54 +08:00
parent 4f74c53edc
commit 17f2221623
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 42 additions and 57 deletions

View File

@ -1,83 +1,56 @@
from asyncio import sleep
from wordcloud import WordCloud from wordcloud import WordCloud
from io import BytesIO from io import BytesIO
from os.path import exists from os.path import exists
from os import makedirs from os import makedirs, sep, remove
from sys import executable from PIL import Image
from collections import defaultdict from collections import defaultdict
from requests import get from requests import get
from pagermaid import version from pagermaid import version
from pagermaid.utils import execute, alias_command from pagermaid.utils import execute, alias_command, pip_install
from pagermaid.listener import listener from pagermaid.listener import listener
imported = True pip_install("jieba")
imported_ = True
import jieba
punctuation = {33: ' ', 34: ' ', 35: ' ', 36: ' ', 37: ' ', 38: ' ', 39: ' ', 40: ' ', 41: ' ', 42: ' ', 43: ' ', punctuation = {33: ' ', 34: ' ', 35: ' ', 36: ' ', 37: ' ', 38: ' ', 39: ' ', 40: ' ', 41: ' ', 42: ' ', 43: ' ',
44: ' ', 45: ' ', 46: ' ', 47: ' ', 58: ' ', 59: ' ', 60: ' ', 61: ' ', 62: ' ', 63: ' ', 64: ' ', 44: ' ', 45: ' ', 46: ' ', 47: ' ', 58: ' ', 59: ' ', 60: ' ', 61: ' ', 62: ' ', 63: ' ', 64: ' ',
91: ' ', 92: ' ', 93: ' ', 94: ' ', 95: ' ', 96: ' ', 123: ' ', 124: ' ', 125: ' ', 126: ' ', 91: ' ', 92: ' ', 93: ' ', 94: ' ', 95: ' ', 96: ' ', 123: ' ', 124: ' ', 125: ' ', 126: ' ',
65311: ' ', 65292: ' ', 65281: ' ', 12304: ' ', 12305: ' ', 65288: ' ', 65289: ' ', 12289: ' ', 65311: ' ', 65292: ' ', 65281: ' ', 12304: ' ', 12305: ' ', 65288: ' ', 65289: ' ', 12289: ' ',
12290: ' ', 65306: ' ', 65307: ' ', 8217: ' ', 8216: ' ', 8230: ' ', 65509: ' ', 183: ' '} 12290: ' ', 65306: ' ', 65307: ' ', 8217: ' ', 8216: ' ', 8230: ' ', 65509: ' ', 183: ' '}
try:
import jieba
except ImportError:
imported = False
try:
import paddle
except ImportError:
imported_ = False
@listener(is_plugin=True, outgoing=True, command=alias_command("groupword"), @listener(is_plugin=True, outgoing=True, command=alias_command("groupword"),
description="拉取最新 300 条消息生成词云。", description="拉取最新 500 条消息生成词云,回复图片可自定义背景图。(图片白色区域为不显示部分)",
parameters="[任意内容启用AI分词]") parameters="[任意内容启用AI分词]")
async def group_word(context): async def group_word(context):
if not exists(f"plugins{sep}groupword"):
makedirs(f"plugins{sep}groupword")
# 自定义背景图
reply = await context.get_reply_message()
if reply and reply.photo:
if exists(f"plugins{sep}groupword{sep}circle.jpg"):
remove(f"plugins{sep}groupword{sep}circle.jpg")
await context.client.download_media(reply, file=f"plugins{sep}groupword{sep}circle.jpg")
return await context.edit("自定义背景图设置完成。")
imported_1 = False imported_1 = False
if len(context.parameter) >= 1: if len(context.parameter) >= 1:
pip_install("paddlepaddle-tiny", alias="paddle")
imported_1 = True imported_1 = True
if not imported:
try:
await context.edit("支持库 `jieba` 未安装...\n正在尝试自动安装...")
await execute(f'{executable} -m pip install jieba')
await sleep(10)
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(f"自动安装失败..请尝试手动安装 `{executable} -m pip install jieba` 随后,请重启 PagerMaid-Modify 。")
return
except:
return
if not imported_ and imported_1:
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: try:
await context.edit('正在生成中。。。') await context.edit('正在生成中。。。')
except: except:
return return
if not exists("plugins/groupword"): if not exists(f"plugins{sep}groupword{sep}wqy-microhei.ttc"):
makedirs("plugins/groupword")
if not exists("plugins/groupword/wqy-microhei.ttc"):
await context.edit('正在拉取中文字体文件。。。(等待时间请评估你的服务器)') await context.edit('正在拉取中文字体文件。。。(等待时间请评估你的服务器)')
r = get('https://cdn.jsdelivr.net/gh/anthonyfok/fonts-wqy-microhei/wqy-microhei.ttc') r = get('https://cdn.jsdelivr.net/gh/anthonyfok/fonts-wqy-microhei/wqy-microhei.ttc')
with open("plugins/groupword/wqy-microhei.ttc", "wb") as code: with open(f"plugins{sep}groupword{sep}wqy-microhei.ttc", "wb") as code:
code.write(r.content) code.write(r.content)
words = defaultdict(int) words = defaultdict(int)
count = 0 count = 0
try: try:
if imported_ and imported_1: if imported_1:
try: try:
jieba.enable_paddle() jieba.enable_paddle()
except: except:
@ -87,7 +60,7 @@ async def group_word(context):
continue continue
if msg.text and not msg.text.startswith('/') and not msg.text.startswith('-') and not '//' in msg.text: if msg.text and not msg.text.startswith('/') and not msg.text.startswith('-') and not '//' in msg.text:
try: try:
if imported_ and imported_1: if imported_1:
for word in jieba.cut(msg.text.translate(punctuation), use_paddle=True): for word in jieba.cut(msg.text.translate(punctuation), use_paddle=True):
word = word.lower() word = word.lower()
words[word] += 1 words[word] += 1
@ -106,9 +79,21 @@ async def group_word(context):
except: except:
return return
try: try:
image = WordCloud(font_path="plugins/groupword/wqy-microhei.ttc", width=800, if not exists(f"plugins{sep}groupword{sep}circle.jpg"):
image = WordCloud(font_path=f"plugins{sep}groupword{sep}wqy-microhei.ttc", width=800,
height=400).generate_from_frequencies( height=400).generate_from_frequencies(
words).to_image() words).to_image()
else:
import numpy as np
background = Image.open(f"plugins{sep}groupword{sep}circle.jpg")
graph = np.array(background) # noqa
image = WordCloud(font_path=f"plugins{sep}groupword{sep}wqy-microhei.ttc", width=800,
height=800,
mask=graph,
scale=5,
background_color='white').generate_from_frequencies(
words).to_image()
stream = BytesIO() stream = BytesIO()
image.save(stream, 'PNG') image.save(stream, 'PNG')
except: except:

View File

@ -492,13 +492,13 @@
}, },
{ {
"name": "groupword", "name": "groupword",
"version": "1.061", "version": "1.1",
"section": "chat", "section": "chat",
"maintainer": "xtaodada", "maintainer": "xtaodada, matsuri_nya",
"size": "3.32 kb", "size": "4.5 kb",
"supported": true, "supported": true,
"des-short": "将聊天内容生成为词云", "des-short": "将聊天内容生成为词云",
"des": "拉取最新 300 条消息生成一张词云图片。\n\n命令-groupword" "des": "拉取最新 500 条消息生成词云,回复图片可自定义背景图。(图片白色区域为不显示部分)\n\n命令-groupword"
}, },
{ {
"name": "kongge", "name": "kongge",