2012-01-25 49 views
1

比方說,我有一個帶標籤的語料庫(如棕色語料庫),我想提取僅用'/ nn'標記的單詞。例如:在Python中使用nltk提取大塊

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

這是標記的語料庫'brown'的一部分。我想要做的就是提取單詞,如估計(因爲它被標記爲/ nn)並將它們添加到列表中。但是我發現的大部分例子都是關於標註一個語料庫的。看到這些例子我真的很困惑。 任何人都可以通過提供一個關於從標籤語料庫中提取單詞的例子或教程來幫助我。

在此先感謝。

回答

3

參見: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 ''/'' ./.""" 
+0

感謝,但如果我嘗試與[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

+0

這很奇怪我得到: '[('jury','NN'),('number','NN'),('interest','NN')]'。當你在我的文章中複製發送的'string'時,你省略了省略號,即'...'。如果你不把它們拉出來,我會得到你描述的錯誤。 – sgallen

相關問題