Zane Carter Zane Carter - 4 months ago 16
Python Question

NLTK: How can I extract information based on sentence maps?

I know you can use noun extraction to get nouns out of sentences but how can I use sentence overlays/maps to take out phrases?

For example:


Sentence Overlay:

"First, @action; Second, Foobar"


Input:

"First, Dance and Code; Second, Foobar"


I want to return:

action = "Dance and Code"




  • Normal Noun Extractions wont work because it wont always be nouns

  • The way sentences are phrased differs so it cant be words[x] ... because the positioning of the words changes


Answer

You can slightly rewrite your string templates to turn them into regexps, and see which one (or which ones) match.

>>> template = "First, (?P<action>.*); Second, Foobar"
>>> mo = re.search(template, "First, Dance and Code; Second, Foobar")
>>> if mo:
        print(mo.group("action"))
Dance and Code