Retrieve definition for parenthesized abbreviation, based on letter count

2024/9/16 18:04:23

I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:

String = '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).'

Desired output: family health history (FHH), nurse practitioner (NP)

I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.

 import rea = '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) 
Answer

Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.

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)

This prints:

FHH family health history
NP nurse practitioner
https://en.xdnf.cn/q/72791.html

Related Q&A

Python (Watchdog) - Waiting for file to be created correctly

Im new to Python and Im trying to implement a good "file creation" detection. If I do not put a time.sleep(x) my files are elaborated in a wrong way since they are still being "created&q…

How do I display add model in tabular format in the Django admin?

Im just starting out with Django writing my first app - a chore chart manager for my family. In the tutorial it shows you how to add related objects in a tabular form. I dont care about the related obj…

Python Matplotlib - Impose shape dimensions with Imsave

I plot a great number of pictures with matplotlib in order to make video with it but when i try to make the video i saw the shape of the pictures is not the same in time...It induces some errors. Is th…

Move x-axis tick labels one position to left [duplicate]

This question already has answers here:Aligning rotated xticklabels with their respective xticks(6 answers)Closed last year.I am making a bar chart and I want to move the x-axis tick labels one positio…

PUT dictionary in dictionary in Python requests

I want to send a PUT request with the following data structure:{ body : { version: integer, file_id: string }}Here is the client code:def check_id():id = request.form[id]res = logic.is_id_valid(id)file…

Does python have header files like C/C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

python: Greatest common divisor (gcd) for floats, preferably in numpy

I am looking for an efficient way to determine the greatest common divisor of two floats with python. The routine should have the following layoutgcd(a, b, rtol=1e-05, atol=1e-08) """ Re…

Difference between @property and property()

Is there a difference betweenclass Example(object):def __init__(self, prop):self._prop = propdef get_prop(self):return self._propdef set_prop(self, prop):self._prop = propprop = property(get_prop, set_…

airflow webserver command fails with {filesystemcache.py:224} ERROR - Operation not permitted

I am installing airflow on a Cent OS 7. I have configured airflow db init and checked the status of the nginx server as well its working fine. But when I run the airflow webserver command I am getting …

Django REST Framework - How to return 404 error instead of 403

My API allows access (any request) to certain objects only when a user is authenticated and certain other conditions are satisfied.class SomethingViewSet(viewsets.ModelViewSet):queryset = Something.obj…