Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

python - How to use nltk regex pattern to extract a specific phrase chunk?

I have written the following regex to tag certain phrases pattern

pattern = """
        P2: {<JJ>+ <RB>? <JJ>* <NN>+ <VB>* <JJ>*}
        P1: {<JJ>? <NN>+ <CC>? <NN>* <VB>? <RB>* <JJ>+}
        P3: {<NP1><IN><NP2>}
        P4: {<NP2><IN><NP1>}

    """

This pattern would correctly tag a phrase such as:

a = 'The pizza was good but pasta was bad'

and give the desired output with 2 phrases:

  1. pizza was good
  2. pasta was bad

However, if my sentence is something like:

a = 'The pizza was awesome and brilliant'

matches only the phrase:

'pizza was awesome' 

instead of the desired:

'pizza was awesome and brilliant'

How do I incorporate the regex pattern for my second example as well?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, let's take a look at the POS tags that NLTK gives:

>>> from nltk import pos_tag
>>> sent = 'The pizza was awesome and brilliant'.split()
>>> pos_tag(sent)
[('The', 'DT'), ('pizza', 'NN'), ('was', 'VBD'), ('awesome', 'JJ'), ('and', 'CC'), ('brilliant', 'JJ')]
>>> sent = 'The pizza was good but pasta was bad'.split()
>>> pos_tag(sent)
[('The', 'DT'), ('pizza', 'NN'), ('was', 'VBD'), ('good', 'JJ'), ('but', 'CC'), ('pasta', 'NN'), ('was', 'VBD'), ('bad', 'JJ')]

(Note: The above are the outputs from NLTK v3.1 pos_tag, older version might differ)

What you want to capture is essentially:

  • NN VBD JJ CC JJ
  • NN VBD JJ

So let's catch them with these patterns:

>>> from nltk import RegexpParser
>>> sent1 = ['The', 'pizza', 'was', 'awesome', 'and', 'brilliant']
>>> sent2 = ['The', 'pizza', 'was', 'good', 'but', 'pasta', 'was', 'bad']
>>> patterns = """
... P: {<NN><VBD><JJ><CC><JJ>}
... {<NN><VBD><JJ>}
... """
>>> PChunker = RegexpParser(patterns)
>>> PChunker.parse(pos_tag(sent1))
Tree('S', [('The', 'DT'), Tree('P', [('pizza', 'NN'), ('was', 'VBD'), ('awesome', 'JJ'), ('and', 'CC'), ('brilliant', 'JJ')])])
>>> PChunker.parse(pos_tag(sent2))
Tree('S', [('The', 'DT'), Tree('P', [('pizza', 'NN'), ('was', 'VBD'), ('good', 'JJ')]), ('but', 'CC'), Tree('P', [('pasta', 'NN'), ('was', 'VBD'), ('bad', 'JJ')])])

So that's "cheating" by hardcoding!!!

Let's go back to the POS patterns:

  • NN VBD JJ CC JJ
  • NN VBD JJ

Can be simplified to:

  • NN VBD JJ (CC JJ)

So you can use the optional operators in the regex, e.g.:

>>> patterns = """
... P: {<NN><VBD><JJ>(<CC><JJ>)?}
... """
>>> PChunker = RegexpParser(patterns)
>>> PChunker.parse(pos_tag(sent1))
Tree('S', [('The', 'DT'), Tree('P', [('pizza', 'NN'), ('was', 'VBD'), ('awesome', 'JJ'), ('and', 'CC'), ('brilliant', 'JJ')])])
>>> PChunker.parse(pos_tag(sent2))
Tree('S', [('The', 'DT'), Tree('P', [('pizza', 'NN'), ('was', 'VBD'), ('good', 'JJ')]), ('but', 'CC'), Tree('P', [('pasta', 'NN'), ('was', 'VBD'), ('bad', 'JJ')])])

Most probably you're using the old tagger, that's why your patterns are different but I guess you see how you could capture the phrases you need using the example above.

The steps are:

  • First, check what is the POS patterns using the pos_tag
  • Then generalize patterns and simplify them
  • Then put them into the RegexpParser

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...