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:
并使用2个短语给出所需的输出:
- pizza was good
- 披萨很好吃
- 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?
我如何在我的第二个例子中加入正则表达式模式?
1 个解决方案
#1
11
Firstly, let's take a look at the POS tags that NLTK gives:
首先,我们来看看NLTK给出的POS标签:
>>> 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)
(注意:以上是NLTK v3.1 pos_tag的输出,旧版本可能不同)
What you want to capture is essentially:
您想捕获的内容基本上是:
- NN VBD JJ CC JJ
- NN VBD JJ CC JJ
- NN VBD 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:
让我们回到POS模式:
- NN VBD JJ CC JJ
- NN VBD JJ CC JJ
- NN VBD JJ
- NN VBD JJ
Can be simplified to:
可以简化为:
- NN VBD JJ (CC JJ)
- 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
- 首先,使用pos_tag检查POS模式是什么
- Then generalize patterns and simplify them
- 然后概括模式并简化它们
- Then put them into the
RegexpParser
- 然后将它们放入RegexpParser中
#1
11
Firstly, let's take a look at the POS tags that NLTK gives:
首先,我们来看看NLTK给出的POS标签:
>>> 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)
(注意:以上是NLTK v3.1 pos_tag的输出,旧版本可能不同)
What you want to capture is essentially:
您想捕获的内容基本上是:
- NN VBD JJ CC JJ
- NN VBD JJ CC JJ
- NN VBD 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:
让我们回到POS模式:
- NN VBD JJ CC JJ
- NN VBD JJ CC JJ
- NN VBD JJ
- NN VBD JJ
Can be simplified to:
可以简化为:
- NN VBD JJ (CC JJ)
- 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
- 首先,使用pos_tag检查POS模式是什么
- Then generalize patterns and simplify them
- 然后概括模式并简化它们
- Then put them into the
RegexpParser
- 然后将它们放入RegexpParser中