python实现成语接龙,调戏QQ小冰

群里面玩的成语接龙很嗨,想到了之前的一个顶俩的项目,自己搞一个陷害QQ小冰的程序吧。

扩展包

pypinyin
扩展包地址传送门

成语词典数据来源

https://github.com/mozillazg/python-pinyin
嫌GitHub慢的也可去
http://ggalaxy.top/file/idiom.json自取

具体代码

最新更新,之前的不支持多音字,现在的可以不同音调的成语接龙了

import json
import pypinyin
import unicodedata

def judge(a,b):
	if(unicodedata.normalize('NFKD', pypinyin.lazy_pinyin(a)[0]).encode('ascii','ignore') == unicodedata.normalize('NFKD', pypinyin.lazy_pinyin(b)[0]).encode('ascii','ignore')):
		return True
	else:
		return False
file = open("H:/2020/idiom/idiom.json","rb")
file_json = json.load(file)
l = len(file_json)
while(1):
	s = input("目前要开始的字符:(输入exit退出):")
	if(s == ""):
		continue
	if(s == "exit"):
		break
	pinyin1 = pypinyin.pinyin(s)[-1][0]
	print(pinyin1)
	#转为拼音
	e = input("想要结尾的字符,不要求的输入nothing:")
	if e == "" or e =="nothing":
		for i in range(l):
			ll = len(file_json[i]["word"])
			if(judge(file_json[i]["pinyin"].split(" ")[0] , pinyin1)):
				print("-----------------------------------------------------------------------------------------------------")
				print(file_json[i]["word"],end = "")
				print(" ||拼音:",end = "")
				print(file_json[i]["pinyin"],end = "")
				print(" ||释意:",end = "")
				print(file_json[i]["derivation"])
	else:
		for i in range(l):
			ll = len(file_json[i]["word"])
			if(judge(file_json[i]["pinyin"].split(" ")[0] , pinyin1) and judge(file_json[i]["word"][ll-1] , e)):
				print("----------------------------------------------------------------------------------------")
				print(file_json[i]["word"],end = "")
				print(" ||拼音:",end = "")
				print(file_json[i]["pinyin"],end = "")
				print(" ||释意:",end = "")
				print(file_json[i]["derivation"])
print(file_json[9356]["derivation"])



# for i in range(l):#给结尾的成语
# 	ll = len(file_json[i]["word"])
# 	if(file_json[i]["word"] == "侯门似海"):
# 		print(file_json[i])
# 		print(i)


调戏效果

python实现成语接龙,调戏QQ小冰_第1张图片

你可能感兴趣的:(python)