我用 Python 自制成語(yǔ)接龍小游戲,刺激!
作者:小小明
原文鏈接:https://blog.csdn.net/as604049322/article/details/118154687
本文為讀者投稿
在 https://github.com/pwxcoo/chinese-xinhua 項(xiàng)目中可以下載到中華成語(yǔ)的語(yǔ)料庫(kù),該項(xiàng)目收錄包括 14032 條歇后語(yǔ),16142 個(gè)漢字,264434 個(gè)詞語(yǔ),31648 個(gè)成語(yǔ)。
結(jié)構(gòu)如下:
chinese-xinhua/
|
+- data/ <-- 數(shù)據(jù)文件夾
| |
| +- idiom.json <-- 成語(yǔ)
| |
| +- word.json <-- 漢字
| |
| +- xiehouyu.json <-- 歇后語(yǔ)
| |
| +- ci.json <-- 詞語(yǔ)
可以直接從網(wǎng)絡(luò)讀取該github的json文件:
import pandas as pd
chengyu = pd.read_json(
"https://github.com/pwxcoo/chinese-xinhua/blob/master/data/idiom.json?raw=true")
不過(guò)有可能網(wǎng)絡(luò)不佳導(dǎo)致讀取失敗,下載好之后讀取本地文件更佳:
import pandas as pd
import numpy as np
chengyu = pd.read_json("idiom.json")
chengyu.head(2)

該庫(kù)有很多列,word列是我們需要的成語(yǔ),pinyin列已經(jīng)幫我們轉(zhuǎn)換出了對(duì)應(yīng)的拼音。下面我們整理出我們需要的數(shù)據(jù):
t = chengyu.pinyin.str.split()
chengyu["shoupin"] = t.str[0]
chengyu["weipin"] = t.str[-1]
chengyu = chengyu.set_index("word")[["shoupin", "weipin"]]
chengyu

測(cè)試獲取任意一個(gè)成語(yǔ)的接龍結(jié)果集:
word = "阿黨比周"
words = chengyu.index[chengyu.shoupin == chengyu.loc[word, "weipin"]]
words
Index(['舟車勞頓', '舟水之喻', '舟中敵國(guó)', '謅上抑下', '侜張為幻', '周而不比', '周而復(fù)始', '周公吐哺', '周規(guī)折矩',
'周急繼乏', '周郎顧曲', '周情孔思', '周窮恤匱', '周游列國(guó)', '诪張變眩', '诪張為幻', '粥少僧多', '粥粥無(wú)能'],
dtype='object', name='word')
然后隨機(jī)任取一個(gè):
np.random.choice(words)
'诪張為幻'
測(cè)試沒(méi)有問(wèn)題,我們可以寫(xiě)一個(gè)批量接龍程序:
word = input("請(qǐng)輸入一個(gè)成語(yǔ):")
flag = True
if word not in chengyu.index:
print("你輸入的不是一個(gè)成語(yǔ),程序結(jié)束!")
flag = False
while flag:
n = input("接龍的次數(shù)(1-100次的整數(shù),輸入任意字母表示結(jié)束程序)")
if not n.isdigit():
print("程序結(jié)束")
break
n = int(n)
if not (0 < n <= 100):
print("非法數(shù)字,程序結(jié)束")
break
for _ in range(n):
words = chengyu.index[chengyu.shoupin == chengyu.loc[word, "weipin"]]
if words.shape[0] == 0:
print("沒(méi)有找到可以接龍的成語(yǔ),程序結(jié)束")
flag = False
break
word = np.random.choice(words)
print(word)
請(qǐng)輸入一個(gè)成語(yǔ):周郎顧曲
接下來(lái)程序自動(dòng)接龍的次數(shù)(1-100次的整數(shù),其他情況表示結(jié)束)10
曲盡奇妙
妙趣橫生
聲應(yīng)氣求
求人不如求己
掎挈伺詐
詐癡不顛
顛乾倒坤
昆山之玉
玉葉金枝
織當(dāng)訪婢
接下來(lái)程序自動(dòng)接龍的次數(shù)(1-100次的整數(shù),其他情況表示結(jié)束)no
結(jié)束
完整代碼
import pandas as pd
import numpy as np
chengyu = pd.read_json("idiom.json")
t = chengyu.pinyin.str.split()
chengyu["shoupin"] = t.str[0]
chengyu["weipin"] = t.str[-1]
chengyu = chengyu.set_index("word")[["shoupin", "weipin"]]
word = input("請(qǐng)輸入一個(gè)成語(yǔ):")
flag = True
if word not in chengyu.index:
print("你輸入的不是一個(gè)成語(yǔ),程序結(jié)束!")
flag = False
while flag:
n = input("接龍的次數(shù)(1-100次的整數(shù),輸入任意字母表示結(jié)束程序)")
if not n.isdigit():
print("程序結(jié)束")
break
n = int(n)
if not (0 < n <= 100):
print("非法數(shù)字,程序結(jié)束")
break
for _ in range(n):
words = chengyu.index[chengyu.shoupin == chengyu.loc[word, "weipin"]]
if words.shape[0] == 0:
print("沒(méi)有找到可以接龍的成語(yǔ),程序結(jié)束")
flag = False
break
word = np.random.choice(words)
print(word)
我們還可以寫(xiě)一個(gè)與機(jī)器對(duì)戰(zhàn)的成語(yǔ)接龍小游戲:
import pandas as pd
import numpy as np
chengyu = pd.read_json("idiom.json")
t = chengyu.pinyin.str.split()
chengyu["shoupin"] = t.str[0]
chengyu["weipin"] = t.str[-1]
chengyu = chengyu.set_index("word")[["shoupin", "weipin"]]
is_head = input("是否先手(輸入N表示后手,其他表示先手)")
if is_head == "N":
word2 = np.random.choice(chengyu.index)
print(word2)
weipin = chengyu.loc[word2, "weipin"]
else:
weipin = ''
while True:
word = input("請(qǐng)輸入一個(gè)成語(yǔ)(認(rèn)輸或離開(kāi)請(qǐng)按Q):")
if word == "Q":
print("你離開(kāi)了游戲,再見(jiàn)!!!")
break
if word not in chengyu.index:
print("你輸入的不是一個(gè)成語(yǔ),請(qǐng)重新輸入!")
continue
if weipin and chengyu.loc[word, 'shoupin'] != weipin:
print("你輸入的成語(yǔ)并不能與機(jī)器人出的成語(yǔ)接上來(lái),你輸了,游戲結(jié)束!!!")
break
words = chengyu.index[chengyu.shoupin == chengyu.loc[word, "weipin"]]
if words.shape[0] == 0:
print("恭喜你贏了!成語(yǔ)機(jī)器人已經(jīng)被你打敗!!!")
break
word2 = np.random.choice(words)
print(word2)
weipin = chengyu.loc[word2, "weipin"]
是否先手(輸入N表示后手,其他表示先手)
請(qǐng)輸入一個(gè)成語(yǔ)(認(rèn)輸或離開(kāi)請(qǐng)按Q):妙趣橫生
生米煮成熟飯
請(qǐng)輸入一個(gè)成語(yǔ)(認(rèn)輸或離開(kāi)請(qǐng)按Q):飯來(lái)開(kāi)口
口呆目鈍
請(qǐng)輸入一個(gè)成語(yǔ)(認(rèn)輸或離開(kāi)請(qǐng)按Q):遁名匿跡
計(jì)功謀利
由于成語(yǔ)積累量較少,幾局就已經(jīng)快玩不下去,于是我打算再寫(xiě)個(gè)成語(yǔ)查詢器方便開(kāi)掛后再上,而不是瘋狂的百度,代碼如下:
from gooey import Gooey, GooeyParser
import pandas as pd
chengyu = pd.read_json("idiom.json")
t = chengyu.pinyin.str.split()
chengyu["shoupin"] = t.str[0]
chengyu["weipin"] = t.str[-1]
chengyu = chengyu.set_index("word")[["shoupin", "weipin"]]
@Gooey
def main():
parser = GooeyParser(description="成語(yǔ)接龍查詢器 - @小小明")
parser.add_argument('word', help="被查詢的成語(yǔ)")
args = parser.parse_args()
word = args.word
if word not in chengyu.index:
print("你輸入的不是一個(gè)成語(yǔ),請(qǐng)重新輸入!")
else:
words = chengyu.index[chengyu.shoupin == chengyu.loc[word, "weipin"]]
if words.shape[0] > 0:
print("滿足條件的成語(yǔ)有:")
print("、".join(words))
else:
print("抱歉,沒(méi)有找到能夠滿足條件的成語(yǔ)")
print("-----" * 10)
if __name__ == '__main__':
main()
這里我使用了Gooey,需要pip安裝:
pip install Gooey
項(xiàng)目地址:https://github.com/chriskiehl/Gooey
體驗(yàn)一把:

點(diǎn)擊start后:

要重新開(kāi)始查詢只需點(diǎn)擊Edit按鈕即可。Gooey支持的組件還挺多的,可以參考GitHub項(xiàng)目說(shuō)明。雖然Gooey遠(yuǎn)不如專業(yè)的圖形化框架專業(yè),但對(duì)于簡(jiǎn)單的需求也挺簡(jiǎn)單便捷,能快速把命令行程序轉(zhuǎn)換為圖形化程序。
左手Python,右手Java,升職就業(yè)不愁啦!
推薦閱讀:
入門(mén): 最全的零基礎(chǔ)學(xué)Python的問(wèn)題 | 零基礎(chǔ)學(xué)了8個(gè)月的Python | 實(shí)戰(zhàn)項(xiàng)目 |學(xué)Python就是這條捷徑
干貨:爬取豆瓣短評(píng),電影《后來(lái)的我們》 | 38年NBA最佳球員分析 | 從萬(wàn)眾期待到口碑撲街!唐探3令人失望 | 笑看新倚天屠龍記 | 燈謎答題王 |用Python做個(gè)海量小姐姐素描圖 |碟中諜這么火,我用機(jī)器學(xué)習(xí)做個(gè)迷你推薦系統(tǒng)電影
趣味:彈球游戲 | 九宮格 | 漂亮的花 | 兩百行Python《天天酷跑》游戲!
AI: 會(huì)做詩(shī)的機(jī)器人 | 給圖片上色 | 預(yù)測(cè)收入 | 碟中諜這么火,我用機(jī)器學(xué)習(xí)做個(gè)迷你推薦系統(tǒng)電影
小工具: Pdf轉(zhuǎn)Word,輕松搞定表格和水印! | 一鍵把html網(wǎng)頁(yè)保存為pdf!| 再見(jiàn)PDF提取收費(fèi)! | 用90行代碼打造最強(qiáng)PDF轉(zhuǎn)換器,word、PPT、excel、markdown、html一鍵轉(zhuǎn)換 | 制作一款釘釘?shù)蛢r(jià)機(jī)票提示器! |60行代碼做了一個(gè)語(yǔ)音壁紙切換器天天看小姐姐!|
年度爆款文案
2).學(xué)Python真香!我用100行代碼做了個(gè)網(wǎng)站,幫人PS旅行圖片,賺個(gè)雞腿吃
3).首播過(guò)億,火爆全網(wǎng),我分析了《乘風(fēng)破浪的姐姐》,發(fā)現(xiàn)了這些秘密
9).發(fā)現(xiàn)一個(gè)舔狗福利!這個(gè)Python爬蟲(chóng)神器太爽了,自動(dòng)下載妹子圖片
點(diǎn)閱讀原文,領(lǐng)AI全套資料


