简体   繁体   中英

exracting chunks in python using nltk

let's say i have a tagged corpus (like brown corpus) and i want to extract the words which are only tagged with '/nn'. For example :

            Daniel/np termed/vbd ``/`` extremely/rb conservative/jj ''/'' his/pp$    estimate/nn.....

this is a part of tagged corpus 'brown'. what i want to do is to extract the words, like- estimate (as it is tagged with /nn) and add them to a list. But most of the example i have found it usually about tagging a corpus. I'm really getting confused seeing these example. Can any one please help me by providing an example or tutorial about extracting words from a tagged corpus.

Thanks in advance.

See: 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'), ... ('.', '.')]

If you just want those tagged with NN , you could do:

>>> [nltk.tag.str2tuple(t) for t in sent.split() if t.split('/')[1] == 'NN']
[('jury', 'NN'), ('number', 'NN'), ('interest', 'NN')]

Edit:

Here is sent as a string minus the ellipses.

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 ''/'' ./."""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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