Is there a way to find specific sequence of words in a text corpus?

Viewed 53

Keywors to find with sequence

Using Python I'm trying to find age(Keyword 4) of a patient when he/she is diagnosed with Asthma(Keyword 2).

Text sentence may contain other words in between the required sequence of these keywords.

eg. text sentence:"Hi Steph, welcome and sorry to hear that you're not doing too well at the moment! You're not alone! I was a mild controlled exercise-induced asthmatic aged 13, then went through a similar situation to yours aged 19. Randomly started having issues, went through Ventolin like no tomorrow"

expected output: 13

1 Answers

Without more example sentences it's hard to give example code, but what I would do in this case is first filter your input to get examples containing your trigger terms (like "mild controlled exercised-induced asthmatic"). This can be done using spaCy's EntityRuler.

Having your candidate sentences, you can then look for numbers. You can use the dependency parser to look for phrases like "aged 19" or you can look for numbers that are in particular phrases (like "when I was 12"). Dependency parses use the overall structure of a sentence, so the number of words in between things isn't important.

You can see example dependency parses here, though note that's using spaCy 2 on the backend.

You may also want to read over the docs for the recently added Dependency Matcher, which will help you find specific dependency structures.

Related