我需要在 NLTK 的正则表达式解析器中创建一个非条件作为我的语法的一部分。我想将那些具有结构的单词分块,'Coffee & Tea'
但如果<IN>
在序列之前有一个类型的单词,它不应该分块。例如'in London and Paris'
不应该被解析器分块。
我的代码如下:
grammar = r'''NP: {(^<IN>)<NNP>+<CC><NN.*>+}'''
我尝试了上述语法来解决问题,但它不起作用,有人可以告诉我我做错了什么。
例子:
def parse_sentence(sentence):
pos_sentence = nltk.pos_tag(nltk.word_tokenize(sentence))
grammar = r'''NP: {<NNP>+<CC><NN.*>+}'''
parser = nltk.RegexpParser(grammar)
result = parser.parse(pos_sentence)
print result
sentence1 = 'Who is the front man of the band that wrote Coffee & TV?'
parse_sentence(sentence1)
sentence2 = 'Who of those resting in Westminster Abbey wrote a book set in London and Paris?'
parse_sentence(sentence2)
Result for sentence 1 is:
(S
Who/WP
is/VBZ
the/DT
front/JJ
man/NN
of/IN
the/DT
band/NN
that/WDT
wrote/VBD
(NP Coffee/NNP &/CC TV/NN)
?/.)
Result for sentence2 is:
(S
Who/WP
of/IN
those/DT
resting/VBG
in/IN
Westminster/NNP
Abbey/NNP
wrote/VBD
a/DT
book/NN
set/VBN
in/IN
(NP London/NNP and/CC Paris/NNP)
?/.)
从句子 1 和句子 2 中都可以看出,短语Coffee & Tea
和London and Paris
被分块为一个组,尽管我不想分块London and Paris
。一种方法是忽略那些前面带有<IN>
POS 标签的模式。
简而言之,我需要知道如何在正则表达式解析器的语法中为 POS 标签添加 NOT(否定)条件。使用 '^' 后跟标签定义的标准语法似乎不起作用