python - nltk 中的退避标记器

标签 python regex nltk pos-tagger

我是 python 编码的新手。我想使用 UnigramTagger 和退避(在我的例子中是 RegexpTagger),我一直在努力弄清楚下面的错误是什么。感谢对此的任何帮助。

>>> train_sents = (['@Sakshi', 'Hi', 'I', 'am', 'meeting', 'my', 'friend', 'today'])    
>>> from tag_util import patterns  
>>> from nltk.tag import RegexpTagger  
>>> re_tagger = RegexpTagger(patterns)  
>>> from nltk.tag import UnigramTagger  
>>> from tag_util import backoff_tagger  
>>> tagger = backoff_tagger(train_sents, UnigramTagger, backoff=re_tagger)

Traceback (most recent call last):  
 File "<pyshell#6>", line 1, in <module>  
    tagger = backoff_tagger(train_sents, UnigramTagger, backoff=re_tagger)  
  File "tag_util.py", line 12, in backoff_tagger  
     for cls in tagger_classes:  
TypeError: 'YAMLObjectMetaclass' object is not iterable

这是我在 tag_util 中用于模式和 backoff_tagger 的代码

import re  
patterns = [  
    (r'^@\w+', 'NNP'),  
    (r'^\d+$', 'CD'),  
    (r'.*ing$', 'VBG'), # gerunds, i.e. wondering  
    (r'.*ment$', 'NN'),  
    (r'.*ful$', 'JJ'), # i.e. wonderful  
    (r'.*', 'NN')  
]  

def backoff_tagger(train_sents, tagger_classes, backoff=None):
    for cls in tagger_classes:
        backoff = cls(train_sents, backoff=backoff)
    return backoff

最佳答案

您只需更改几处即可使其正常工作。

您遇到的错误是因为您无法迭代 UnigramTagger 类。我不确定您是否还有其他想法,只是删除 for 循环。此外,您需要向 UnigramTagger 传递一个 list tagged sentences 表示为 list of (word, tag) 元组 - 不仅仅是单词列表。否则,它不知道如何训练。部分内容可能如下所示:

[[('@Sakshi', 'NN'), ('Hi', 'NN'),...],...[('Another', 'NN'), ('sentence', 'NN')]]

注意这里每个句子本身就是一个列表。此外,您可以为此使用来自 NTLK 的标记语料库(我推荐)。

编辑:

在阅读您的帖子后,在我看来,您既对某些函数的输入/输出感到困惑,又缺乏对 NLP 意义上的训练的理解。我认为你会从 reading the NLTK book, starting at the beginning 中受益匪浅.

我很高兴向您展示如何解决此问题,但我认为如果不进行更多研究,您将无法完全理解底层机制。

tag_util.py(基于您的代码)

from nltk.tag import RegexpTagger, UnigramTagger
from nltk.corpus import brown

patterns = [
    (r'^@\w+', 'NNP'),
    (r'^\d+$', 'CD'),
    (r'.*ing$', 'VBG'),
    (r'.*ment$', 'NN'),
    (r'.*ful$', 'JJ'),
    (r'.*', 'NN')
]
re_tagger = RegexpTagger(patterns)
tagger = UnigramTagger(brown.tagged_sents(), backoff=re_tagger) # train tagger

在 Python 解释器中

>>> import tag_util
>>> tag_util.brown.tagged_sents()[:2]
[[('The', 'AT'), ('Fulton', 'NP-TL'), ('County', 'NN-TL'), ('Grand', 'JJ-TL'), ('Jury', 'NN-TL'), ('said', 'VBD'), ('Friday', 'NR'), ('an', 'AT'), ('investigation', 'NN'), ('of', 'IN'), ("Atlanta's", 'NP$'), ('recent', 'JJ'), ('primary', 'NN'), ('election', 'NN'), ('produced', 'VBD'), ('``', '``'), ('no', 'AT'), ('evidence', 'NN'), ("''", "''"), ('that', 'CS'), ('any', 'DTI'), ('irregularities', 'NNS'), ('took', 'VBD'), ('place', 'NN'), ('.', '.')], [('The', 'AT'), ('jury', 'NN'), ('further', 'RBR'), ('said', 'VBD'), ('in', 'IN'), ('term-end', 'NN'), ('presentments', 'NNS'), ('that', 'CS'), ('the', 'AT'), ('City', 'NN-TL'), ('Executive', 'JJ-TL'), ('Committee', 'NN-TL'), (',', ','), ('which', 'WDT'), ('had', 'HVD'), ('over-all', 'JJ'), ('charge', 'NN'), ('of', 'IN'), ('the', 'AT'), ('election', 'NN'), (',', ','), ('``', '``'), ('deserves', 'VBZ'), ('the', 'AT'), ('praise', 'NN'), ('and', 'CC'), ('thanks', 'NNS'), ('of', 'IN'), ('the', 'AT'), ('City', 'NN-TL'), ('of', 'IN-TL'), ('Atlanta', 'NP-TL'), ("''", "''"), ('for', 'IN'), ('the', 'AT'), ('manner', 'NN'), ('in', 'IN'), ('which', 'WDT'), ('the', 'AT'), ('election', 'NN'), ('was', 'BEDZ'), ('conducted', 'VBN'), ('.', '.')]]

注意这里的输出。我从带标签的句子的布朗语料库中得到前两个句子。您需要将这种数据作为输入传递给标注器(如 UnigramTagger)以对其进行训练。现在让我们使用我们在 tag_util.py 中训练的标注器。

回到 Python 解释器

>>> tag_util.tagger.tag(['I', 'just', 'drank', 'some', 'coffee', '.'])
[('I', 'PPSS'), ('just', 'RB'), ('drank', 'VBD'), ('some', 'DTI'), ('coffee', 'NN'), ('.', '.')]

到此为止,使用您的方法对句子中的单词进行 POS 标记。

关于python - nltk 中的退避标记器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15584387/

相关文章:

python - django rest 框架中补丁方法的验证

javascript - String.prototype.replace() 删除破折号和下划线

c# - 有条件地忽略最后一个词

python - 比较同义词 NLTK

regex - 如何对 NLP 解析树进行逆向工程以得到原始句子?

python - 绘制具有周期性边界且没有线段的 nice 曲线

python - 使用 Pandas 计算 30 分钟时间段内列中的字符串值

ruby - 在 Ruby gsub 中使用正则表达式全局变量

nltk - NLTK 的 BLEU 分数和 SacreBLEU 有什么区别?

Python for循环查询