【发布时间】:2020-04-13 11:25:45
【问题描述】:
我有一个首字母缩略词列表,我想做的是在文本中找到它们的定义,然后将它们放入字典中。我已经编写了一个代码,但是是硬编码的并且不会产生想要的结果。我希望我的最终结果是这样的。
{'NBA': ' National Basketball Association', 'NCAA': 'National Collegiate Athletic Association'}
代码:
dict = {}
full_form = ' '
s = " NBA comes from the words National Basketball Association is a men's professional basketball league in North America, composed of 30 teams. On the other hand NCAA stands for The National Collegiate Athletic Association"
acro = ['NBA', 'NCAA']
for char in range(len(acro)):
for n,word in enumerate (list_str):
if acro[char][0] == word[0] and word not in acro:
full_form += word + ' '
print(full_form)
if acro[char][1] == list_str[n+1][0] and word not in acro:
print(list_str[n+1])
full_form += list_str[n+1] + ' '
if acro[char][2] == list_str[n+2][0] and word not in acro:
full_form += list_str[n+2] + ''
d[acro[char]] = full_form
print(d)
out: {'NBA': ' National Basketball Association', 'NCAA': ' National Basketball AssociationNorth National National North National Collegiate Athletic'}
任何有关如何在 pythonic wat 中实现预期结果的帮助将不胜感激。
【问题讨论】:
-
你可以在那里应用正则表达式。
-
你想让你的代码理解任意文本的定义吗?如果是这样那就是一个 ML\DS 主题,查找 Named Entity Recognition,但这并不容易。
-
^ 是的,我愿意。你认为只有 NLP 才能做到?
标签: python python-3.x string list dictionary