2012-10-25 110 views
1

这里只是一个快速的初学者问题与NLTK。 我想标记并从一个语料库生成bigrams,trigrams和quadgram。NLTK标记问题

我需要将<s>添加到我的列表开头,</s>到最后代替一个句点。

该列表取自nltk中的棕色语料库。 (在那个特定的部分)

所以..我有

from nltk.corpus import brown 
news = brown.sents(categories = 'editorial') 

我是不是使这个太难了? 非常感谢。

回答

3
import nltk.corpus as corpus 

def mark_sentence(row): 
    if row[-1] == '.': 
     row[-1] = '</s>' 
    else: 
     row.append('</s>') 
    return ['<s>'] + row 

news = corpus.brown.sents(categories = 'editorial') 
for row in news[:5]: 
    print(mark_sentence(row)) 

产量

['<s>', 'Assembly', 'session', 'brought', 'much', 'good', '</s>'] 
['<s>', 'The', 'General', 'Assembly', ',', 'which', 'adjourns', 'today', ',', 'has', 'performed', 'in', 'an', 'atmosphere', 'of', 'crisis', 'and', 'struggle', 'from', 'the', 'day', 'it', 'convened', '</s>'] 
['<s>', 'It', 'was', 'faced', 'immediately', 'with', 'a', 'showdown', 'on', 'the', 'schools', ',', 'an', 'issue', 'which', 'was', 'met', 'squarely', 'in', 'conjunction', 'with', 'the', 'governor', 'with', 'a', 'decision', 'not', 'to', 'risk', 'abandoning', 'public', 'education', '</s>'] 
['<s>', 'There', 'followed', 'the', 'historic', 'appropriations', 'and', 'budget', 'fight', ',', 'in', 'which', 'the', 'General', 'Assembly', 'decided', 'to', 'tackle', 'executive', 'powers', '</s>'] 
['<s>', 'The', 'final', 'decision', 'went', 'to', 'the', 'executive', 'but', 'a', 'way', 'has', 'been', 'opened', 'for', 'strengthening', 'budgeting', 'procedures', 'and', 'to', 'provide', 'legislators', 'information', 'they', 'need', '</s>'] 
+0

哦!谢谢你的理解,我坐在这里搔着我的头。我想现在我只需要弄清楚如何将bigrams,tri等列出来并计算概率。 – user1378618

+0

nltk的书告诉你如何制作ngrams,并且还指出nltk自己的bigram,trigram和ngram [functions。](http://nltk.googlecode.com/svn/trunk/doc/api/nltk。 UTIL-module.html) – alexis