Created
October 4, 2018 05:13
-
-
Save RedL0tus/fa4deee371551bcf17d57b31f52bc47a to your computer and use it in GitHub Desktop.
Not-Chicory!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"against": [ | |
"\u4e0d\u662f" | |
], | |
"blacklist": { | |
"chars": [ | |
"\u200b" | |
], | |
"ids": [] | |
}, | |
"chicories": [ | |
"\u5927\u4f6c" | |
], | |
"first_person": [ | |
"\u6211" | |
], | |
"names": [ | |
"\u54b8\u9c7c" | |
], | |
"non_sense": [ | |
"\u662f" | |
], | |
"replacements": { | |
"pa": "_(:\u0437\u300d\u2220)_" | |
}, | |
"self": 123456789, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- encoding: utf-8 -*- | |
import re | |
import json | |
import jieba | |
from pyrogram import Client, Filters | |
app = Client("Not-Chicory") | |
with open('config.json') as f: | |
config = json.load(f) | |
def save_config(): | |
print(" | - Saving config") | |
try: | |
with open('config.json', 'w') as f: | |
json.dump(config, f, sort_keys=True, indent=4) | |
print(" | - Config saved") | |
except Exception: | |
print(" | - Failed to save config") | |
def add_to_obj(client, message, obj): | |
print(" | - Received command to add something to an object.") | |
try: | |
name = str(message.text.split(maxsplit=1)[1]) | |
print(" | - Adding %s" % name) | |
if name in obj: | |
raise ValueError | |
obj.append(name) | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
"把 %s 添加到了列表里" % name | |
) | |
save_config() | |
print(" | - Success") | |
except Exception: | |
print(" | - Failed") | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
"发生了什么不得了的事情" | |
) | |
def add_value_pair(client, message, obj): | |
try: | |
print(" | - Received command to add a value pair.") | |
splitted = message.text.split(maxsplit=2) | |
key = splitted[1] | |
value = splitted[2] | |
print(" | - %s : %s" % (key, value)) | |
obj[key] = value | |
save_config() | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
"现在 %s 对应 %s" % (key, value) | |
) | |
print(" | - Success") | |
except Exception: | |
print(" | - Failed") | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
"发生了什么不得了的事情" | |
) | |
def list_obj(client, message, obj): | |
print(" | - Received command to list .") | |
try: | |
names = "" | |
for name in obj: | |
names += "%s, " % name | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
names[:-2] | |
) | |
print(" | - Success") | |
except Exception: | |
print(" | - Failed") | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
"发生了什么不得了的事情" | |
) | |
def replace(client, message): | |
try: | |
orig_text = message.text | |
text = orig_text | |
for key in config['replacements'].keys(): | |
text = text.replace("'%s" % key, config['replacements'][key]) | |
if text != orig_text: | |
print(" | - Substitute to " + text) | |
client.edit_message_text( | |
message.chat.id, message.message_id, | |
text | |
) | |
return 0 | |
print(" | - Not changed") | |
except Exception: | |
pass | |
def not_chicory(client, message): | |
def strip(text): | |
try: | |
filter = re.compile(u'[^\u4E00-\u9FA5a-zA-Z0-9]') | |
text = filter.sub(r'', text) | |
text = text.strip().lower() | |
for character in config['blacklist']['chars']: | |
text = text.replace(character, '') | |
return text | |
except Exception: | |
return text | |
def chicory(splitted, result, count, threshold): | |
try: | |
word = next(splitted, None) | |
if word is not None: | |
if word in config['non_sense']: | |
return chicory(splitted, result, count, threshold) | |
elif word in config['chicories']: | |
result += word | |
return chicory(splitted, result, count, threshold) | |
elif word in config['against']: | |
return result | |
else: | |
count += 1 | |
if count < threshold: | |
return chicory(splitted, result, count, threshold) | |
else: | |
return result | |
else: | |
return result | |
except Exception: | |
return result | |
result = '' | |
try: | |
stripped = strip(message.text) | |
print(" | - Stripped: %s" % stripped) | |
splitted = jieba.cut(stripped) | |
word = next(splitted, None) | |
while word is not None: | |
if word in config['names']: | |
result += chicory(splitted, result, 0, 20) | |
word = next(splitted, None) | |
print(" | - Chicory: %s" % result) | |
again = False | |
if result == '': | |
if (message.reply_to_message is not None): | |
if (message.reply_to_message.from_user.id == config['self'])\ | |
and (message.from_user.id != config['self']): | |
again = True | |
for name in config['first_person']: | |
if name in stripped: | |
again = False | |
break | |
print(" | - Try again? %s" % again) | |
if again: | |
splitted = jieba.cut(stripped) | |
result += chicory(splitted, result, 0, 20) | |
if result != '': | |
print(" | - I'm not a %s" % result) | |
client.send_message( | |
message.chat.id, "不是%s" % result, | |
reply_to_message_id=message.message_id | |
) | |
except Exception: | |
pass | |
@app.on_message(Filters.text) | |
def router(client, message): | |
print( | |
">>> " + | |
str( | |
message.chat.title or message.chat.first_name or | |
message.chat.last_name or message.chat.username | |
) + | |
" (" + str(message.chat.id) + "): " + | |
message.text | |
) | |
try: | |
if message.from_user.id == int(config['self']): | |
if message.text.startswith("'add_name"): | |
add_to_obj(client, message, config['names']) | |
elif message.text.startswith("'list_names"): | |
list_obj(client, message, config['names']) | |
elif message.text.startswith("'add_chicory"): | |
add_to_obj(client, message, config['chicories']) | |
elif message.text.startswith("'list_chicories"): | |
list_obj(client, message, config['chicories']) | |
elif message.text.startswith("'add_banned_char"): | |
add_to_obj(client, message, config['blacklist']['chars']) | |
elif message.text.startswith("'list_banned_chars"): | |
list_obj(client, message, config['blacklist']['chars']) | |
elif message.text.startswith("'add_replacement"): | |
add_value_pair(client, message, config['replacements']) | |
elif message.text.startswith("'add_non_sense"): | |
add_to_obj(client, message, config['non_sense']) | |
elif message.text.startswith("'add_against"): | |
add_to_obj(client, message, config['against']) | |
elif message.text.startswith("'add_first_person"): | |
add_to_obj(client, message, config['first_person']) | |
elif "'" in message.text: | |
replace(client, message) | |
else: | |
not_chicory(client, message) | |
except Exception: | |
not_chicory(client, message) | |
if __name__ == '__main__': | |
print("Aaaaあaa啊aあああ啊あああ!!!!!!!") | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment