使用nltk自定义标记

Spl*_*iFF 27 python nltk

我正在尝试创建一个类似英语的小语言来指定任务.基本思想是将一个陈述分成这些动词应该适用的动词和名词短语.我正在使用nltk,但没有得到我希望的结果,例如:

>>> nltk.pos_tag(nltk.word_tokenize("select the files and copy to harddrive'"))
[('select', 'NN'), ('the', 'DT'), ('files', 'NNS'), ('and', 'CC'), ('copy', 'VB'), ('to', 'TO'), ("harddrive'", 'NNP')]
>>> nltk.pos_tag(nltk.word_tokenize("move the files to harddrive'"))
[('move', 'NN'), ('the', 'DT'), ('files', 'NNS'), ('to', 'TO'), ("harddrive'", 'NNP')]
>>> nltk.pos_tag(nltk.word_tokenize("copy the files to harddrive'"))
[('copy', 'NN'), ('the', 'DT'), ('files', 'NNS'), ('to', 'TO'), ("harddrive'", 'NNP')]
Run Code Online (Sandbox Code Playgroud)

在每种情况下,它都没有意识到第一个单词(选择,移动和复制)是作为动词.我知道我可以创建自定义标注器和语法来解决这一点,但在同一时间,我不愿去重新发明轮子,当很多东西是我的联赛.我特别希望能够处理非英语语言的解决方案.

所以无论如何,我的问题之一是:这种语法有更好的标记吗?有没有办法可以比现有的标记更加频繁地使用动词形式?有没有办法训练标记器?有更好的方法吗?

Jac*_*cob 29

一种解决方案是创建一个备份到NLTK标记器的手动UnigramTagger.像这样的东西:

>>> import nltk.tag, nltk.data
>>> default_tagger = nltk.data.load(nltk.tag._POS_TAGGER)
>>> model = {'select': 'VB'}
>>> tagger = nltk.tag.UnigramTagger(model=model, backoff=default_tagger)
Run Code Online (Sandbox Code Playgroud)

然后你得到

>>> tagger.tag(['select', 'the', 'files'])
[('select', 'VB'), ('the', 'DT'), ('files', 'NNS')]
Run Code Online (Sandbox Code Playgroud)

只要您有适当的默认标记符,这种方法就可以用于非英语语言.您可以通过训练自己的标记者train_tagger.pyNLTK,教练和适当的语料库.


Cer*_*rin 22

雅各布的回答很明显.但是,要扩展它,你可能会发现你需要的不仅仅是unigrams.

例如,考虑三个句子:

select the files
use the select function on the sockets
the select was good
Run Code Online (Sandbox Code Playgroud)

这里,单词"select"分别用作动词,形容词和名词.unigram标记器将无法对此进行建模.即使是一个二元组标签也无法处理它,因为其中两个案例共享相同的前一个词(即"the").你需要一个trigram标记来正确处理这种情况.

import nltk.tag, nltk.data
from nltk import word_tokenize
default_tagger = nltk.data.load(nltk.tag._POS_TAGGER)

def evaluate(tagger, sentences):
    good,total = 0,0.
    for sentence,func in sentences:
        tags = tagger.tag(nltk.word_tokenize(sentence))
        print tags
        good += func(tags)
        total += 1
    print 'Accuracy:',good/total

sentences = [
    ('select the files', lambda tags: ('select', 'VB') in tags),
    ('use the select function on the sockets', lambda tags: ('select', 'JJ') in tags and ('use', 'VB') in tags),
    ('the select was good', lambda tags: ('select', 'NN') in tags),
]

train_sents = [
    [('select', 'VB'), ('the', 'DT'), ('files', 'NNS')],
    [('use', 'VB'), ('the', 'DT'), ('select', 'JJ'), ('function', 'NN'), ('on', 'IN'), ('the', 'DT'), ('sockets', 'NNS')],
    [('the', 'DT'), ('select', 'NN'), ('files', 'NNS')],
]

tagger = nltk.TrigramTagger(train_sents, backoff=default_tagger)
evaluate(tagger, sentences)
#model = tagger._context_to_tag
Run Code Online (Sandbox Code Playgroud)

注意,您可以使用NLTK的NgramTagger使用任意大量的n-gram来训练标记器,但通常在三元组之后不会获得太多的性能提升.


小智 6

见雅各布的回答.

在以后的版本(至少nltk 3.2)nltk.tag._POS_TAGGER不存在.默认标记通常下载到nltk_data/taggers /目录中,例如:

>>> import nltk
>>> nltk.download('maxent_treebank_pos_tagger') 
Run Code Online (Sandbox Code Playgroud)

用法如下.

>>> import nltk.tag, nltk.data
>>> tagger_path = '/path/to/nltk_data/taggers/maxent_treebank_pos_tagger/english.pickle'
>>> default_tagger = nltk.data.load(tagger_path)
>>> model = {'select': 'VB'}
>>> tagger = nltk.tag.UnigramTagger(model=model, backoff=default_tagger)
Run Code Online (Sandbox Code Playgroud)

另请参见:如何使用Python中的NLTK POS标记器进行POS标记.