【问题标题】:detecting POS tag pattern along with specified words检测 POS 标签模式以及指定的单词
【发布时间】:2016-01-08 08:59:02
【问题描述】:

我需要在某些特定单词之前/之后识别某些词性标签,例如以下标记句子:

[('This', 'DT'), ('feature', 'NN'), ('would', 'MD'), ('be', 'VB'), ('nice', 'JJ'), ('to', 'TO'), ('have', 'VB')]

可以抽象为“would be”+形容词的形式

同理:

[('I', 'PRP'), ('am', 'VBP'), ('able', 'JJ'), ('to', 'TO'), ('delete', 'VB'), ('the', 'DT'), ('group', 'NN'), ('functionality', 'NN')]

是“能够”+动词的形式

我怎样才能在句子中检查这些类型的模式。我正在使用 NLTK。

【问题讨论】:

  • “检查”是什么意思?
  • 我的意思是如何检测到句子中存在“能够”+动词形式的模式。或者,例如,句子中存在“would be”+比较形容词之类的内容。
  • 那么你想打印True,如果它存在的话?
  • 是的,我已经看到了仅匹配 POS 的示例,但在我的情况下,如果有意义的话,我需要匹配单词和 POS 标签...
  • 另请注意,'JJ' 不是比较形容词——它只是一个形容词。

标签: python nlp nltk pos-tagger


【解决方案1】:

假设您想逐字检查“would”,然后是“be”,然后是一些形容词,您可以这样做:

def would_be(tagged):
    return any(['would', 'be', 'JJ'] == [tagged[i][0], tagged[i+1][0], tagged[i+2][1]] for i in xrange(len(tagged) - 2))

输入是一个带有 POS 标记的句子(元组列表,根据 NLTK)。

它检查列表中是否存在任何三个元素,使得“would”在“be”旁边,“be”在标记为形容词('JJ')的单词旁边。只要匹配了这个“模式”,它就会返回True

你可以对第二种句子做一些非常相似的事情:

def am_able_to(tagged):
    return any(['am', 'able', 'to', 'VB'] == [tagged[i][0], tagged[i+1][0], tagged[i+2][0], tagged[i+3][1]] for i in xrange(len(tagged) - 3))

这是程序的驱动程序:

s1 = [('This', 'DT'), ('feature', 'NN'), ('would', 'MD'), ('be', 'VB'), ('nice', 'JJ'), ('to', 'TO'), ('have', 'VB')]
s2 = [('I', 'PRP'), ('am', 'VBP'), ('able', 'JJ'), ('to', 'TO'), ('delete', 'VB'), ('the', 'DT'), ('group', 'NN'), ('functionality', 'NN')]

def would_be(tagged):
   return any(['would', 'be', 'JJ'] == [tagged[i][0], tagged[i+1][0], tagged[i+2][1]] for i in xrange(len(tagged) - 2))

def am_able_to(tagged):
    return any(['am', 'able', 'to', 'VB'] == [tagged[i][0], tagged[i+1][0], tagged[i+2][0], tagged[i+3][1]] for i in xrange(len(tagged) - 3))

sent1 = ' '.join(s[0] for s in s1)
sent2 = ' '.join(s[0] for s in s2)

print("Is '{1}' of type 'would be' + adj? {0}".format(would_be(s1), sent1))
print("Is '{1}' of type 'am able to' + verb? {0}".format(am_able_to(s1), sent1))

print("Is '{1}' of type 'would be' + adj? {0}".format(would_be(s2), sent2))
print("Is '{1}' of type 'am able to' + verb? {0}".format(am_able_to(s2), sent2))

这正确输出:

Is 'This feature would be nice to have' of type 'would be' + adj? True
Is 'This feature would be nice to have' of type 'am able to' + verb? False
Is 'I am able to delete the group functionality' of type 'would be' + adj? False
Is 'I am able to delete the group functionality' of type 'am able to' + verb? True

如果您想概括这一点,您可以更改是检查文字词还是它们的 POS 标签。

【讨论】:

  • 我要做一些通用的事情,比如 am_able_to(s1),我得到一个列表索引超出范围错误。除此之外,它有效。谢谢!
  • 修正了功能。
  • 感谢埃里普。我在另一句话“我能够删除组功能”上测试了 will_be,但仍然得到列表索引超出范围错误。
  • @newdev14 那句话的词性标签列表是什么?我这台机器上没有 nltk。
  • [('I', 'PRP'), ('am', 'VBP'), ('able', 'JJ'), ('to', 'TO'), ( 'delete', 'VB'), ('the', 'DT'), ('group', 'NN'), ('functionality', 'NN')]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多