在基于标记器的NLTK分块器中对新句子进行分块



我正在尝试在NLTK中实现一个基于标记器的分块器。我把代码写成,

>>> import nltk,pprint,re,nltk.tree,string,codecs
>>> import nltk.corpus, nltk.tag
>>> import datetime
>>> from nltk.corpus import conll2000
>>> train_sents = conll2000.chunked_sents('train.txt', chunk_types=['NP'])
>>> test_sents = conll2000.chunked_sents('test.txt', chunk_types=['NP'])
>>> def conll_tag_chunks(chunk_sents):
    tag_sents = [nltk.chunk.tree2conlltags(tree) for tree in chunk_sents]
        return [[(t, c) for (w, t, c) in chunk_tags] for chunk_tags in tag_sents]
>>> tr_chunk = conll_tag_chunks(train_sents)
>>> te_chunk = conll_tag_chunks(test_sents)
>>> u_chunker = nltk.UnigramTagger(tr_chunk)
>>> accuracy=u_chunker.evaluate(te_chunk)
>>> print accuracy
0.832112628491

事情总体上看起来不错,但我无法给出新的申请句子进行分类。这可能是一件非常愚蠢的事情,但无法解决。

如果有人可以建议

训练数据始终是(<input>, <desired output>)对的列表。然后,您训练的标记器采用相同的<input>。因此,请查看您的训练输入:

>>> print(tr_chunk[0][:5], "...")
[('NN', 'B-NP'), ('IN', 'O'), ('DT', 'B-NP'), ('NN', 'I-NP'), ('VBZ', 'O')] ...

您已经训练了一个分块器,该分块器仅根据其 POS 标签(丢弃原始单词)查找命名实体,因此要使用它,您还必须传递 POS 标签列表。

>>> words = "I sure love New York .".split()
>>> tags = [ t for (w, t) in nltk.pos_tag(words) ]
>>> u_chunker.tag(tags)

如果它没有任何好处,请训练一个更好的块块机。

最新更新