【发布时间】:2019-10-20 03:39:52
【问题描述】:
我有一个程序可以抓取缩写词(即查找括号中的单词),然后根据缩写词中的字符数,返回那么多单词并对其进行定义。到目前为止,它适用于以大写字母开头的前置词或大多数前置词以大写字母开头的定义。对于后者,它会跳过像“in”这样的小写字母并转到下一个字母。但是,我的问题是对应单词的数量都是小写的。
电流输出:
All Awesome Dudes (AAD)
临床试验方法、测量和疼痛评估倡议 (IMMPACT)
试验(影响)。一些患者更喜欢常规护理(UC)
期望的输出:
All Awesome Dudes (AAD)
临床试验方法、测量和疼痛评估倡议 (IMMPACT)
常规护理 (UC)
import re
s = """Too many people, but not All Awesome Dudes (AAD) only care about the
Initiative on Methods, Measurement, and Pain Assessment in Clinical
Trials (IMMPACT). Some patient perfer the usual care (UC) approach of
doing nothing"""
allabbre = []
for match in re.finditer(r"\((.*?)\)", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()
count=0
for k,i in enumerate(words[::-1]):
if i[0].isupper():count+=1
if count==size:break
words=words[-k-1:]
definition = " ".join(words)
abbr_keywords = definition + " " + "(" + abbr + ")"
pattern='[A-Z]'
if re.search(pattern, abbr):
if abbr_keywords not in allabbre:
allabbre.append(abbr_keywords)
print(abbr_keywords)
【问题讨论】:
标签: python regex text text-parsing