简体   繁体   English

如何在nltk中使用Regexp Tagger?

[英]How do I use Regexp Tagger in nltk?

If I try this code : 如果我尝试这段代码:

import nltk
pattern = [(r'(March)$','MAR')]
tagger=nltk.RegexpTagger(pattern)
print tagger.tag('He was born in March 1991')

I get an output likr this: 我得到一个输出类似于:

[('H', None), ('e', None), (' ', None), ('w', None), ('a', None), ('s', None), (' ', None), >('b', None), ('o', None), ('r', None), ('n', None), (' ', None), ('i', None), ('n', None), (' ', None), ('M', None), ('a', None), ('r', None), ('c', None), ('h', None), (' ', None), ('1', None), ('9', None), ('9', None), ('1', None)] [('H',无),('e',无),('',无),('w',无),('a',无),('s',无),(' ',无),>('b',无),('o',无),('r',无),('n',无),('',无),('我',无),('n',无),('',无),('M',无),('a',无),('r',无),('c',无), ('h',无),('',无),('1',无),('9',无),('9',无),('1',无)]

In fact I would like this tagger to recognise 'March' word with 'MAR' tag. 事实上,我希望这个标记器能够识别带有'MAR'标签的'March'字样。

Here try this: 试试这个:

import nltk
pattern = [(r'(March)$','MAR')]
tagger = nltk.RegexpTagger(pattern)
print tagger.tag(nltk.word_tokenize('He was born in March 1991'))

You have to tokenize the words. 你必须对单词进行标记。

This is the output I get: 这是我得到的输出:

[('He', None), ('was', None), ('born', None), ('in', None), ('March', 'MAR'), ('1991', None)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM