本帖最后由 lightninng 于 2021-11-20 04:54 编辑
按个人理解的意思写了出来,感觉不如意,但是凑合用吧。
注:代码里面
dictionary={key.strip():[item.strip(".") for item in re.split("[①②]",value.strip()) if item] for key,value in [line.split("#") for line in string.splitlines()]}
这一句中的"[①②]"这个字符串要把多个释义的所有出现过的数字标号都放进去,你给的里面只出现了①②,所以我只加了①②
你需要的输出在output字典里面,大概这样{'A&A:Yn': ['A&A:Yn#B&B:Yn', 'B&B:Yn#F&F:Yn'],
'C&C:Yn': ['C&C:Yn#D&D:Yn-YIN T0BCI NEREIDUL'],
'G&G:Yn': ['G&G:Yn#H&H:Yn'],
'd&d': []}
未登录词在missing_word列表里面,大概这样只能帮你到这里了~~
代码如下:import time
import re
def get_dictionary(string):
#函数用于从字典文本中获取词和对应的释义
if "&" not in string or "#" not in string:
return {}
dictionary={key.strip():[item.strip(".") for item in re.split("[①②]",value.strip()) if item] for key,value in [line.split("#") for line in string.splitlines()]}
return dictionary
def get_interpretation(word,dictionary,result):
#函数用于在dictionary中查询word的释义,结果添加到result列表中
interpre_list = dictionary.get(word,[])
if not interpre_list:
return []
for item in interpre_list:
if "&" in item:
result.append(f"{word}#{item}")
get_interpretation(item,dictionary,result)
if __name__ == "__main__":
#textA为A.txt内容,textB同理
textA="""A&A:Yn
C&C:Yn
G&G:Yn
d&d"""
textB = """AAA#BBB.
CCC#DDDD.
A&A:Yn#B&B:Yn.
C&C:Yn#D&D:Yn-YIN T0BCI NEREIDUL.
B&B:Yn#F&F:Yn.
G&G:Yn#①H&H:Yn②DDDDDDD."""
#从字符串中获kye为词和value其对应的所有释义的列表的字典
dictionary=get_dictionary(textB)
#控制台打印获取到的字典内容
print("字典内容为:")
for k,v in dictionary.items():
print(f"{k}:{v}")
#获取释义
output={}#存放查询结果:key为查询的词,value为查询到的所有释义(包含等价词义)
for word in textA.splitlines():
if "&" in word:
output[word]=[]
get_interpretation(word,dictionary,output[word])
#控制台打印输出结果
missing_word =[] #存放未登录词
print("查询结果为:")
for key,value in output.items():
if not value:
missing_word.append(key) #满足需求1
else:
print(f"{key}释义:{str(value)}")
#控制台打印输出未登录词
print(f"未登录词条:\n{missing_word}")
运行后控制台结果:字典内容为:
AAA:['BBB']
CCC:['DDDD']
A&A:Yn:['B&B:Yn']
C&C:Yn:['D&D:Yn-YIN T0BCI NEREIDUL']
B&B:Yn:['F&F:Yn']
G&G:Yn:['H&H:Yn', 'DDDDDDD']
查询结果为:
A&A:Yn释义:['A&A:Yn#B&B:Yn', 'B&B:Yn#F&F:Yn']
C&C:Yn释义:['C&C:Yn#D&D:Yn-YIN T0BCI NEREIDUL']
G&G:Yn释义:['G&G:Yn#H&H:Yn']
未登录词条:
['d&d']
|