Python實現(xiàn)有趣的親戚關(guān)系計算器
每年的春節(jié),都會有一些自己幾乎沒印象但父母就是很熟的親戚,關(guān)系凌亂到你自己都說不清。
今年趁著春節(jié)在家沒事情干,正好之前知道有中國親戚關(guān)系計算器,想著自己實現(xiàn)一下,特此記錄。
介紹
由于本人能力有限,只完成了基本功能....
需求
計算親戚關(guān)系鏈得出我應(yīng)該怎么稱呼的結(jié)果
數(shù)據(jù)定義
1.定義關(guān)系字符和修飾符
【關(guān)系】f:父,m:母,h:夫,w:妻,s:子,d:女,xb:兄弟,ob:兄,lb:弟,xs:姐妹,os:姐,ls:妹
【修飾符】 &o:年長,&l:年幼,#:隔斷,[a|b]:并列
2.關(guān)系對應(yīng)數(shù)據(jù)集合、關(guān)系過濾數(shù)據(jù)集合(data.json 和 filter.json)
原來參考的作者的關(guān)系過濾數(shù)據(jù)集合json有點問題,改了一下
filter 數(shù)據(jù)集的用途:比如 m,h 是我的媽媽 的丈夫就是爸爸,也就是 f。 filter 的作用是去重和簡化,需要把 exp 用 str 進(jìn)行替換
算法實現(xiàn)
需要解決的情況基本有以下三種:
- 我的爸爸 = 爸爸,
- 我的哥哥的弟弟 = 自己/弟弟/哥哥,
- 我的哥哥的老公 = ?
分析
三種結(jié)果:1.單結(jié)果 2.多結(jié)果 3.錯誤提示 ,那么我們的算法要兼容以上三種情況,下面我們來一步步實現(xiàn)。
算法主要函數(shù)一:transformTitleToKey
該函數(shù)主要負(fù)責(zé)將文字轉(zhuǎn)換成關(guān)系符號
# 將文字轉(zhuǎn)換成關(guān)系符號
def transformTitleToKey(text):
result = text.replace("的", ",").replace("我", "").replace("爸爸", "f").replace("父親", "f").replace("媽媽","m").replace("母親", "m").replace("爺爺","f,f").replace("奶奶", "f,m").replace("外公", "m,f").replace("姥爺", "m,f").replace("外婆", "m,m").replace("姥姥", "m,m").replace("老公","h").replace("丈夫", "h").replace("老婆", "w").replace("妻子", "h").replace("兒子", "s").replace("女兒", "d").replace("兄弟", "xd").replace("哥哥", "ob").replace("弟弟","lb").replace("姐妹","xs").replace("姐姐", "os").replace("妹妹", "ls").strip(",")
return result這里簡化了原參考作者的寫法,更 簡單(不是) 符合計算器設(shè)定

算法主要函數(shù)二:FilteHelper
該函數(shù)主要負(fù)責(zé)去重和簡化
# 去重和簡化
def FilteHelper(text):
result = text
filterName = '/filter.json' # filter.json文件路徑
if not os.path.isfile(filterName):
return "filterName文件不存在"
with open(filterName, "r") as f:
obj = list(ijson.items(f, 'filter'))
for i in range(len(obj[0])):
users = obj[0][i]['exp']
if users == result:
return obj[0][i]['str']
elif re.match(obj[0][i]['exp'], result): # 符合正則
result1 = re.findall(obj[0][i]['exp'], result) # 返回string中所有與pattern匹配的全部字符串,返回形式為數(shù)組
print(result1)
a = 0
result2 = ""
if len(result1)>1:
try:
for i in len(result1):
result = result.replace("$" + str(a + 1), result1[a])
a = a + 1
if result.find("#") != -1:
result_l = result
resultList = list(set(result_l.split("#"))) # # 是隔斷符,所以分割文本
for key in resultList:
result = FilteHelper(key.strip(","))
if (result.find("#") == -1): # 當(dāng)關(guān)系符號不含#時加入最終結(jié)果中
result2 = result2 + result
return result2
else:
return text
except Exception as e:
return text
else:
return str(result1).replace("[\'", "").replace("\']", "")
elif re.match(obj[0][i]['exp'], strInsert(result, 0, ',')): # 符合正則
result1 = re.findall(obj[0][i]['exp'], strInsert(result, 0, ',')) # 返回string中所有與pattern匹配的全部字符串,返回形式為數(shù)組
a = 0
result2 = ""
if len(result1)>1:
try:
for i in len(result1):
result = result.replace("$" + str(a + 1), result1[a])
a = a + 1
if result.find("#") != -1:
result_l = result
resultList = list(set(result_l.split("#"))) # # 是隔斷符,所以分割文本
for key in resultList:
result = FilteHelper(key.strip(","))
if (result.find("#") == -1): # 當(dāng)關(guān)系符號不含#時加入最終結(jié)果中
result2 = result2 + result
return result2
else:
return text
except Exception as e:
return text
else:
return str(result1).replace("[\'", "").replace("\']", "")
return text這里原參考作者解釋的有點亂,我就以我個人見解參考著寫了出來...能跑....有錯歡迎指出
個人測試單結(jié)果,多結(jié)果都能實現(xiàn),建議多結(jié)果實現(xiàn)參考輸出和代碼詳細(xì)理解
算法主要函數(shù)三:dataValueByKeys
該函數(shù)主要負(fù)責(zé)從數(shù)據(jù)源中查找對應(yīng) key 的結(jié)果
# 從數(shù)據(jù)源中查找對應(yīng) key 的結(jié)果
def dataValueByKeys(data_text):
if(isChinese(data_text)): # 判斷是否含有中文,含有的是特殊回復(fù)
return data_text
dataName = '/data.json' # data.json文件路徑
if not os.path.isfile(dataName):
return "data文件不存在"
fo = open(dataName, 'r', encoding='utf-8')
ID_Data = demjson.decode(fo.read())
fo.close()
try:
if ID_Data[data_text]:
cityID = ID_Data[data_text]
text = ""
for key in cityID:
text = text + key + '\\'
return text.strip("\\")
else:
return "未找到"
except Exception as e:
result = ""
resultList = FilteHelper(strInsert(data_text, 0, ',')).split(",")
for key in resultList:
result = result + dataValueByKeys(key)
return result輸出與效果




基本達(dá)到效果
一些細(xì)節(jié)與已知問題
首先,是性別:如果‘我’是女性,那么‘我的父親的兒子’可以為[‘哥哥’,‘弟弟’],而不可以包含‘我’。(上述代碼沒實現(xiàn))
另外,關(guān)于夫妻關(guān)系:在正常情況下,男性稱謂只可以有‘妻子’,女性稱謂只可以有‘丈夫’。(上述代碼已實現(xiàn))
第三,多種可能:‘我的父親的兒子’ 可以是[‘我’,‘哥哥’,‘弟弟’],再若是再往后計算,如‘我的父親的兒子的兒子’ ,需要同時考慮‘我的兒子’,‘哥哥的兒子’,‘弟弟的兒子’這三種可能。(上述代碼已實現(xiàn))
已知問題:某些涉及自己的多重可能還存在莫名BUG
以上就是Python實現(xiàn)有趣的親戚關(guān)系計算器的詳細(xì)內(nèi)容,更多關(guān)于Python親戚關(guān)系計算器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何在windows下安裝Pycham2020軟件(方法步驟詳解)
這篇文章主要介紹了在windows下安裝Pycham2020軟件方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Python3標(biāo)準(zhǔn)庫之functools管理函數(shù)的工具詳解
functools模塊提供的主要工具就是partial類,可以用來“包裝”一個有默認(rèn)參數(shù)的callable對象。這篇文章主要介紹了Python3標(biāo)準(zhǔn)庫functools管理函數(shù)的工具的實例詳解,需要的朋友可以參考下2020-02-02
Python函數(shù)的定義方式與函數(shù)參數(shù)問題實例分析
這篇文章主要介紹了Python函數(shù)的定義方式與函數(shù)參數(shù)問題,結(jié)合實例形式詳細(xì)分析了Python函數(shù)定義、函數(shù)參數(shù)相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下2019-12-12
python list格式數(shù)據(jù)excel導(dǎo)出方法
今天小編就為大家分享一篇python list格式數(shù)據(jù)excel導(dǎo)出方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python cookbook(字符串與文本)針對任意多的分隔符拆分字符串操作示例
這篇文章主要介紹了Python cookbook(字符串與文本)針對任意多的分隔符拆分字符串操作,結(jié)合實例形式分析了Python使用split()及正則表達(dá)式進(jìn)行字符串拆分操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-04-04

