0

I am using Python and would like to match the last Noun before "needing".

text = "Charles and Kim are needing a good hot dog"

I must make that using re.findall and nltk

I tried the follow but with that show all the information before and I only need the last noun

post = re.findall(r'.*needing', text)[0]

I hope to get

Kim

1 Answers1

1

Just use POS tagging from nltk.

You'll need to download a few nltk resources, then just tag and find what you want. This code would do it:

import nltk

# You'll need to run these two resource downloads the first time you do this.
# So uncomment the following two lines

# nltk.download('punkt')
# nltk.download('averaged_perceptron_tagger')


text = "Charles and Kim are needing a good hot dog"
tokens = nltk.word_tokenize(text)
tags = nltk.pos_tag(tokens)

# You are interested in splitting the sentence here
sentence_split = tokens.index("needing")

# Find the words where tag meets your criteria (must be a noun / proper noun)
nouns_before_split = [word for (word, tag) in tags[:sentence_split] if   tag.startswith('NN')]

# Show the result. The last such noun
print(nouns_before_split[-1])
Bill Huneke
  • 746
  • 4
  • 12