2016-11-04 34 views
0

NLTK具有對棕色語料庫的接口和POS標籤和它可以這樣進行訪問:如何簡單地提取布朗語料庫NLTK中的單詞和標籤?

>>> from nltk.corpus import brown 
>>> brown.tagged_sents() 
[[(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR'), (u'an', u'AT'), (u'investigation', u'NN'), (u'of', u'IN'), (u"Atlanta's", u'NP$'), (u'recent', u'JJ'), (u'primary', u'NN'), (u'election', u'NN'), (u'produced', u'VBD'), (u'``', u'``'), (u'no', u'AT'), (u'evidence', u'NN'), (u"''", u"''"), (u'that', u'CS'), (u'any', u'DTI'), (u'irregularities', u'NNS'), (u'took', u'VBD'), (u'place', u'NN'), (u'.', u'.')], [(u'The', u'AT'), (u'jury', u'NN'), (u'further', u'RBR'), (u'said', u'VBD'), (u'in', u'IN'), (u'term-end', u'NN'), (u'presentments', u'NNS'), (u'that', u'CS'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'Executive', u'JJ-TL'), (u'Committee', u'NN-TL'), (u',', u','), (u'which', u'WDT'), (u'had', u'HVD'), (u'over-all', u'JJ'), (u'charge', u'NN'), (u'of', u'IN'), (u'the', u'AT'), (u'election', u'NN'), (u',', u','), (u'``', u'``'), (u'deserves', u'VBZ'), (u'the', u'AT'), (u'praise', u'NN'), (u'and', u'CC'), (u'thanks', u'NNS'), (u'of', u'IN'), (u'the', u'AT'), (u'City', u'NN-TL'), (u'of', u'IN-TL'), (u'Atlanta', u'NP-TL'), (u"''", u"''"), (u'for', u'IN'), (u'the', u'AT'), (u'manner', u'NN'), (u'in', u'IN'), (u'which', u'WDT'), (u'the', u'AT'), (u'election', u'NN'), (u'was', u'BEDZ'), (u'conducted', u'VBN'), (u'.', u'.')], ...] 

brown.tagged_sents()是列表,列表中的每個元素是一個句子和句子的列表第一個元素是單詞的元組,第二個元素是POS標籤。

我們的目標是處理brown語料庫,以便我得到一個像這樣的文件,其中每行是製表符分隔的句子,其中第一列包含由空格分隔的句子的單詞,第二列包含相應的標記由空格隔開:

The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced `` no evidence '' that any irregularities took place . AT NP-TL NN-TL JJ-TL NN-TL VBD NR AT NN IN NP$ JJ NN NN VBD `` AT NN '' CS DTI NNS VBD NN . 
The jury further said in term-end presentments that the City Executive Committee , which had over-all charge of the election , `` deserves the praise and thanks of the City of Atlanta '' for the manner in which the election was conducted . AT NN RBR VBD IN NN NNS CS AT NN-TL JJ-TL NN-TL , WDT HVD JJ NN IN AT NN , `` VBZ AT NN CC NNS IN AT NN-TL IN-TL NP-TL '' IN AT NN IN WDT AT NN BEDZ VBN . 
The September-October term jury had been charged by Fulton Superior Court Judge Durwood Pye to investigate reports of possible `` irregularities '' in the hard-fought primary which was won by Mayor-nominate Ivan Allen Jr. . AT NP NN NN HVD BEN VBN IN NP-TL JJ-TL NN-TL NN-TL NP NP TO VB NNS IN JJ `` NNS '' IN AT JJ NN WDT BEDZ VBN IN NN-TL NP NP NP . 

我已經試過這樣:

from nltk.corpus import brown 
tagged_sents = brown.tagged_sents() 
fout = open('brown.txt', 'w') 
fout.write('\n'.join([' '.join(sent)+'\t'+' '.join(tags) 
         for sent, tags in 
         [zip(*tagged_sent) for tagged_sent in tagged_sents]])) 

和它的作品,但必須有一個更好的方式來Munge時間語料庫。

回答

0
data = [[(u'The', u'AT'), (u'Fulton', u'NP-TL'), (u'County', u'NN-TL'), (u'Grand', u'JJ-TL'), (u'Jury', u'NN-TL'), (u'said', u'VBD'), (u'Friday', u'NR')]] 

# takes the data in and throws it in a loop 
def data_printer(data): 
    # adds each element to this string 
    string = '' 
    for dat in data: 
     for da in dat: 
      string += ' ' + da[0] 
    print string 
    return string 

data_printer(data) 

有一個更好的方法來通過有序對做到這一點。這是一種無進口的簡約方式。

+0

您錯過了標籤; P在問題中向右滾動所需的輸出。 – alvas

+0

另外,它不應該打印,但寫入罰款=) – alvas

+0

好..我只是得到了它的一個樣本。 :) –

相關問題