Add Chengyu
This commit is contained in:
30903
chengyu.csv
Normal file
30903
chengyu.csv
Normal file
File diff suppressed because it is too large
Load Diff
76
func_chengyu.py
Normal file
76
func_chengyu.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import random
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class Chengyu(object):
|
||||
def __init__(self) -> None:
|
||||
self.df = pd.read_csv("chengyu.csv", delimiter="\t")
|
||||
self.cys, self.zis, self.yins = self._build_data()
|
||||
|
||||
def _build_data(self):
|
||||
df = self.df.copy()
|
||||
df["shouzi"] = df["chengyu"].apply(lambda x: x[0])
|
||||
df["mozi"] = df["chengyu"].apply(lambda x: x[-1])
|
||||
|
||||
df["shouyin"] = df["pingyin"].apply(lambda x: x.split(" ")[0])
|
||||
df["moyin"] = df["pingyin"].apply(lambda x: x.split(" ")[-1])
|
||||
|
||||
cys = dict(zip(df["chengyu"], df["moyin"]))
|
||||
zis = df.groupby("shouzi").agg({"chengyu": set})["chengyu"].to_dict()
|
||||
yins = df.groupby("shouyin").agg({"chengyu": set})["chengyu"].to_dict()
|
||||
|
||||
return cys, zis, yins
|
||||
|
||||
def isChengyu(self, cy):
|
||||
return self.cys.get(cy, None) is not None
|
||||
|
||||
def getNext(self, cy, tongyin=True) -> str:
|
||||
"""获取下一个成语
|
||||
cy: 当前成语
|
||||
tongyin: 是否允许同音字
|
||||
"""
|
||||
zi = cy[-1]
|
||||
ansers = list(self.zis.get(zi, {}))
|
||||
try:
|
||||
ansers.remove(cy) # 移除当前成语
|
||||
except Exception as e:
|
||||
pass # Just ignore...
|
||||
|
||||
if ansers:
|
||||
return random.choice(ansers)
|
||||
|
||||
# 如果找不到同字,允许同音
|
||||
if tongyin:
|
||||
yin = self.cys.get(cy)
|
||||
ansers = list(self.yins.get(yin, {}))
|
||||
|
||||
try:
|
||||
ansers.remove(cy) # 移除当前成语
|
||||
except Exception as e:
|
||||
pass # Just ignore...
|
||||
|
||||
if ansers:
|
||||
return random.choice(ansers)
|
||||
|
||||
return None
|
||||
|
||||
def getMeaning(self, cy):
|
||||
ress = self.df[self.df["chengyu"] == cy].to_dict(orient="records")
|
||||
if ress:
|
||||
res = ress[0]
|
||||
rsp = res["chengyu"] + "\n" + res["pingyin"] + "\n" + res["jieshi"]
|
||||
if res["chuchu"] and res["chuchu"] != "无":
|
||||
rsp += "\n出处:" + res["chuchu"]
|
||||
if res["lizi"] and res["lizi"] != "无":
|
||||
rsp += "\n例子:" + res["lizi"]
|
||||
return rsp
|
||||
return None
|
||||
|
||||
|
||||
cy = Chengyu()
|
||||
|
||||
if __name__ == "__main__":
|
||||
answer = cy.getNext("便宜行事")
|
||||
print(answer)
|
||||
21
main.py
21
main.py
@@ -3,6 +3,7 @@
|
||||
|
||||
import re
|
||||
|
||||
from func_chengyu import cy
|
||||
import robot.sdk.wcferry as WxSDK
|
||||
from robot.base_robot import BaseRobot
|
||||
from robot.configuration import Config
|
||||
@@ -16,6 +17,23 @@ class Robot(BaseRobot):
|
||||
super().__init__(sdk)
|
||||
self.config = config
|
||||
|
||||
def toChengyu(self, msg):
|
||||
texts = re.findall(r"^([#|?|?])(.*)$", msg.content)
|
||||
# [('#', '天天向上')]
|
||||
if texts:
|
||||
flag = texts[0][0]
|
||||
text = texts[0][1]
|
||||
if flag == "#": # 接龙
|
||||
if cy.isChengyu(text):
|
||||
rsp = cy.getNext(text)
|
||||
if rsp:
|
||||
self.sendTextMsg(msg.roomId, rsp)
|
||||
elif flag in ["?", "?"]: # 查词
|
||||
if cy.isChengyu(text):
|
||||
rsp = cy.getMeaning(text)
|
||||
if rsp:
|
||||
self.sendTextMsg(msg.roomId, rsp)
|
||||
|
||||
def processMsg(self, msg) -> None:
|
||||
"""当接收到消息的时候,会调用本方法。如果不实现本方法,则打印原始消息。
|
||||
"""
|
||||
@@ -31,6 +49,9 @@ class Robot(BaseRobot):
|
||||
rsp = "你@我干嘛?"
|
||||
self.sendTextMsg(msg.roomId, rsp, msg.wxId)
|
||||
|
||||
else:
|
||||
self.toChengyu(msg)
|
||||
|
||||
# 非群聊信息
|
||||
elif msg.type == 37: # 好友请求
|
||||
self.autoAcceptFriendRequest(msg)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
PyYAML
|
||||
pandas
|
||||
schedule
|
||||
|
||||
Reference in New Issue
Block a user