From 520beb02641ef771fa9bfbfd0e92f8587cd609d9 Mon Sep 17 00:00:00 2001 From: wongxy Date: Sat, 21 Aug 2021 11:32:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9D=E7=BB=9D=E5=AD=90=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/bot_juejuezi.py | 188 ++++++++++++++++++++++++++++++++++++++++ plugins/readme.md | 10 ++- 2 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 plugins/bot_juejuezi.py diff --git a/plugins/bot_juejuezi.py b/plugins/bot_juejuezi.py new file mode 100644 index 0000000..469f1df --- /dev/null +++ b/plugins/bot_juejuezi.py @@ -0,0 +1,188 @@ +"""绝绝子生成器 +1. 绝绝子更新: 更新词库 +2. 绝绝子+{内容}: 内容请使用空格间隔动词与名词,例如:喝奶茶 => 喝 奶茶 +""" +import random +from typing import List + +from botoy import GroupMsg, S +from botoy.contrib import download, get_cache_dir +from botoy import decorators as deco +from pydantic import BaseModel + +data_path = get_cache_dir('juejuezi') / 'juejuezi.json' + + +def download_material(): + download( + 'https://cdn.jsdelivr.net/gh/kingcos/JueJueZiGenerator@main/materials.json', + data_path, + ) + + +if not data_path.exists(): + download_material() + + +class Emotion(BaseModel): + emoji: List[str] + xiaohongshu: List[str] + weibo: List[str] + + +class Material(BaseModel): + emotions: Emotion # 表情 + symbols: List[str] + auxiliaryWords: List[str] + dividers: List[str] # 断句符 + fashion: List[str] # 潮流 + attribute: List[str] # 定语 + + beginning: List[str] # 开头 + ending: List[str] # 结尾 + who: List[str] # 主语 + someone: List[str] # 和/跟谁 + + todosth: List[str] # 干什么 + another: List[str] # 扯另一个淡 + collections: List[str] # 一些固定搭配 + default: List[str] # 默认 something + + +material = Material.parse_file(data_path) + + +class Random: + @staticmethod + def word(words: List[str], nullable=False, divier='') -> str: + word = random.choice( + [*words, *([''] * (nullable and int(len(words) / 3) or 0))] + ) + if word: + return word + divier + return '' + + @staticmethod + def word_not_contain(words: List[str], already: str) -> str: + word = Random.word(words) + + word_set = set(word.replace(' ', '')) + already_set = set(already.replace(' ', '')) + + if len(word_set & already_set) == 0: + return word + + return Random.word_not_contain(words, already) + + @staticmethod + def words(words_: List[str], count: int) -> List[str]: + if len(words_) >= count: + words_ = words_.copy() + random.shuffle(words_) + return words_[:count] + + @staticmethod + def repeat(word: str, times: int = -1) -> str: + if times > 0: + return word * times + + num = random.randint(1, 3) + if num == 2: + return '' + return word * num + + +def generate_beginning(divider: str): + beginning = ( + Random.word(material.beginning) + .replace('who', Random.word(material.who)) + .replace('someone', Random.word(material.someone)) + ) + emotion = Random.word(material.emotions.emoji, True) + return beginning + emotion + divider + + +def generate_dosth(something: str, divider: str): + todosth = Random.word(material.todosth).replace(' ', '').replace('dosth', something) + emotion = Random.repeat(Random.word(material.emotions.emoji)) + return todosth + emotion + divider + + +def praise_sth(something: str, praised_words: List[str], has_also=False) -> str: + praise_word = Random.word(praised_words) + + verb, noun = something.split(' ')[:2] + + intro = Random.word(['这家的', '这家店的', '这个', '这件', '这杯']) + also = has_also and '也' or '' + + praise_word.replace('dosth', verb) + + return intro + noun + also + praise_word + + +def generate(something: str) -> str: + divider = Random.word(material.dividers) + + fashion_words = Random.words(material.fashion, len(material.fashion)) + + first = generate_beginning(divider) + second = fashion_words[0] + divider + third = generate_dosth(something, divider) + forth = fashion_words[1] + divider + fifth = Random.repeat(Random.word(material.auxiliaryWords), 3) + divider + sixth = praise_sth(something, material.attribute) + Random.repeat( + Random.word(material.symbols), 3 + ) + seventh = praise_sth( + Random.word_not_contain(material.another, something), material.attribute, True + ) + Random.repeat(Random.word(material.symbols), 3) + eighth = fashion_words[2] + divider + ninth = ( + Random.word(material.collections, True, divider) + fashion_words[3] + divider + ) + tenth = Random.repeat(Random.word(material.auxiliaryWords), 3) + divider + last = Random.word(material.ending) + Random.word(material.emotions.emoji) + + return ( + first + + second + + third + + forth + + fifth + + sixth + + seventh + + eighth + + ninth + + tenth + + last + ) + + +# bot +@deco.ignore_botself +@deco.startswith('绝绝子') +def receive_group_msg(ctx: GroupMsg): + do = ctx.Content[3:].strip() + if not do: + return + + if do == '更新': + try: + download_material() + except Exception: + S.bind(ctx).text('下载出错') + else: + S.bind(ctx).text('好了,没事不要老更新') + else: + try: + sentence = generate(do) + except: + pass + else: + S.bind(ctx).text(sentence) + + +if __name__ == "__main__": + for _ in range(3): + print(generate('想 躺平')) diff --git a/plugins/readme.md b/plugins/readme.md index 9bdfbf8..066ff90 100644 --- a/plugins/readme.md +++ b/plugins/readme.md @@ -35,8 +35,9 @@ - [TaoShow(买家秀)](#taoshow买家秀) - [kiss_gif(亲亲表情包)](#kiss_gif亲亲表情包) - [rip_avatar(撕开头像)](#rip_avatar撕开头像) +- [juejuezi(绝绝子生成器)](#juejuezi绝绝子生成器) - + @@ -303,3 +304,10 @@ botoy.json 1. AT 一人发送撕 2. 发送图片加撕 + +# juejuezi(绝绝子生成器) + +修改自:[kingcos/JueJueZiGenerator](https://github.com/kingcos/JueJueZiGenerator) + +1. 绝绝子更新: 更新词库 +2. 绝绝子+{内容}: 内容请使用空格间隔动词与名词,例如:喝奶茶 => 喝 奶茶