參見:http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html
>>> sent = '''
... The/AT grand/JJ jury/NN commented/VBD on/IN a/AT number/NN of/IN
... other/AP topics/NNS ,/, AMONG/IN them/PPO the/AT Atlanta/NP and/CC
... Fulton/NP-tl County/NN-tl purchasing/VBG departments/NNS which/WDT it/PPS
... said/VBD ``/`` ARE/BER well/QL operated/VBN and/CC follow/VB generally/RB
... accepted/VBN practices/NNS which/WDT inure/VB to/IN the/AT best/JJT
... interest/NN of/IN both/ABX governments/NNS ''/'' ./.
... '''
>>> [nltk.tag.str2tuple(t) for t in sent.split()]
[('The', 'AT'), ('grand', 'JJ'), ('jury', 'NN'), ('commented', 'VBD'),
('on', 'IN'), ('a', 'AT'), ('number', 'NN'), ... ('.', '.')]
如果你只是想那些標有NN
,你可以這樣做:
>>> [nltk.tag.str2tuple(t) for t in sent.split() if t.split('/')[1] == 'NN']
[('jury', 'NN'), ('number', 'NN'), ('interest', 'NN')]
編輯:
這裏是sent
作爲字符串減去橢圓。
sent = """The/AT grand/JJ jury/NN commented/VBD on/IN a/AT number/NN of/IN other/AP topics/NNS ,/, AMONG/IN them/PPO the/AT Atlanta/NP and/CC Fulton/NP-tl County/NN-tl purchasing/VBG departments/NNS which/WDT it/PPS said/VBD ``/`` ARE/BER well/QL operated/VBN and/CC follow/VB generally/RB accepted/VBN practices/NNS which/WDT inure/VB to/IN the/AT best/JJT interest/NN of/IN both/ABX governments/NNS ''/'' ./."""
感謝,但如果我嘗試與[nltk.tag.str2tuple(T)對於t在sent.split()如果t.split( '/')[1] == 'NN']它給錯誤[nltk.tag.str2tuple(t)for t in sent.split()if t.split('/')[1] =='NN'] IndexError:列表索引超出範圍 – user1052462
這很奇怪我得到: '[('jury','NN'),('number','NN'),('interest','NN')]'。當你在我的文章中複製發送的'string'時,你省略了省略號,即'...'。如果你不把它們拉出來,我會得到你描述的錯誤。 – sgallen