Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to find exact match and 5 vocab before and after it in python

I have below dataframe in python,

Text = provide written informed consent healthy male or female age between 31 to 59 years fluent in german language

it needs looking fore age and add 5 vocab before and after that word.
target value = age
my desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

result = healthy male or female age between 31 to 59 years

my code:

 Text = "provide written informed consent healthy male or female age between 31 to 59 years fluent in german language"
 r1 = re.search(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,3} age (?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,3}", text)
 r1.group()

my result is

 age 16 years old

my data has some words like manage or agent that should be ignore.

thanks

>Solution :

One way to do so, without using regex, might be to split the text into words and retrieve the position of age in the word list.

Text = "provide written informed consent healthy male or female age between 31 to 59 years fluent in german language"
Text = Text.split()

result = Text[Text.index("age") - 4:Text.index("age") + 5]
print(result)  # ['healthy', 'male', 'or', 'female', 'age', 'between', '31', 'to', '59']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading