根据字母数检索带括号的缩写的定义

ten*_*tio 6 python regex text text-parsing abbreviation

我需要根据括号中包含的字母数来检索首字母缩写词的定义。对于我正在处理的数据,括号中的字母数与要检索的单词数相对应。我知道这不是获取缩写的可靠方法,但就我而言,确实如此。例如:

String ='尽管家庭健康史(FHH)通常被认为是常见的慢性疾病的重要危险因素,但护士(NP)很少考虑到它。

期望的产出:家庭健康史(FHH),执业护士(NP)

我知道如何从字符串中提取括号,但是在那之后我被卡住了。任何帮助表示赞赏。

 import re

 a = 'Although family health history (FHH) is commonly accepted as an 
 important risk factor for common, chronic diseases, it is rarely considered 
 by a nurse practitioner (NP).'

 x2 = re.findall('(\(.*?\))', a)

 for x in x2:
    length = len(x)
    print(x, length) 
Run Code Online (Sandbox Code Playgroud)

Kea*_*nge 5

使用正则表达式匹配查找匹配开始的位置。然后使用python字符串索引来获取直到比赛开始的子字符串。按单词拆分子字符串,并获取最后n个单词。其中n是缩写的长度。

import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


for match in re.finditer(r"\((.*?)\)", s):
    start_index = match.start()
    abbr = match.group(1)
    size = len(abbr)
    words = s[:start_index].split()[-size:]
    definition = " ".join(words)

    print(abbr, definition)
Run Code Online (Sandbox Code Playgroud)

打印:

FHH family health history
NP nurse practitioner
Run Code Online (Sandbox Code Playgroud)