2

我正在标记文本以搜索名词和形容词:

text = u"""Developed at the Vaccine and Gene Therapy Institute at the Oregon Health and Science University (OHSU), the vaccine proved successful in about fifty percent of the subjects tested and could lead to a human vaccine preventing the onset of HIV/AIDS and even cure patients currently on anti-retroviral drugs."""
nltk.pos_tag(nltk.word_tokenize(text))

这导致:

[('Developed', 'NNP'), ('at', 'IN'), ('the', 'DT'), ('Vaccine', 'NNP'), ('and', 'CC') , ('Gene', 'NNP'), ('Therapy', 'NNP'), ('Institute', 'NNP'), ('at', 'IN'), ('the', 'DT') , ('Oregon', 'NNP'), ('Health', 'NNP'), ('and', 'CC'), ('Science', 'NNP'), ('University', 'NNP') , ('(', 'NNP') , ('OHSU', 'NNP'), (')', 'NNP'), (',', ','), ('the', 'DT'), ('vaccine', 'NN'), ('proved', 'VBD'), ('successful', 'JJ') , ('in', 'IN'), ('about', 'IN'), ('fifty', 'JJ'), ('percent', 'NN'), ('of', 'IN') , ('the', 'DT'), ('subjects', 'NNS'), ('tested', 'VBD'), ('and', 'CC'), ('could', 'MD') , ('lead', 'VB'), ('to', 'TO'), ('a', 'DT'), ('human', 'NN'), ('vaccine', 'NN') , ('预防', 'VBG'), ('the', 'DT'), ('onset', 'NN'), ('of', 'IN'), ('HIV/AIDS', 'NNS' '), ('和','CC'), ('even', 'RB'), ('治愈', 'NN'), ('患者', 'NNS'), ('当前', 'RB'), ('on', 'IN'), ('anti-retroviral', 'JJ'), ('drugs', 'NNS'), ('.', '.')]

标记句子时是否有正确检测括号的内置方法?

4

1 回答 1

2

如果您知道要作为括号的标记值返回什么,则可以使用 RegexpTagger 来匹配括号并回退到所有其他的首选标记器。

import nltk
from nltk.data import load
_POS_TAGGER = 'taggers/maxent_treebank_pos_tagger/english.pickle'
tagger = load(_POS_TAGGER)  # same tagger as using nltk.pos_tag

regexp_tagger = nltk.tag.RegexpTagger([(r'\(|\)', '--')], backoff = tagger)

regexp_tagger.tag(nltk.word_tokenize(text))

结果:

[(u'Developed', 'NNP'), (u'at', 'IN'), (u'the', 'DT'), (u'Vaccine', 'NNP'), (u'and' , 'CC'), (u'Gene', 'NNP'), (u'Therapy', 'NNP'), (u'Institute', 'NNP'), (u'at', 'IN'), (u'the', 'DT'), (u'Oregon', 'NNP'), (u'Health', 'NNP'), (u'and', 'CC'), (u'Science', 'NNP'), (u'University', 'NNP'), (u'(', '--'), (u'OHSU', 'NNP'), (u')', '--') , (u',', ','), (u'the', 'DT'), (u'vaccine', 'NN'), (u'proved', 'VBD'), (u'successful' , 'JJ'), (u'in', 'IN'), (u'about', 'IN'), (u'fifty', 'JJ'), (u'percent', 'NN'), (u'of', 'IN'), (u'the', 'DT'), (u 'subjects', 'NNS'), (u'tested', 'VBD'), (u'and', 'CC'), (u'could', 'MD'), (u'lead', 'VB '), (u'to', 'TO'), (u'a', 'DT'), (u'human', 'NN'), (u'vaccine', 'NN'), (u'预防', 'VBG'), (u'the', 'DT'), (u'onset', 'NN'), (u'of', 'IN'), (u'HIV/AIDS', ' NNS'), (u'and', 'CC'), (u'even', 'RB'), (u'cure', 'NN'), (u'患者', 'NNS'), (u '当前', 'RB'), (u'on', 'IN'), (u'抗逆转录病毒', 'JJ'), (u'drugs', 'NNS'), (u'.', '.')]

于 2013-09-15T18:19:35.427 回答