2013-12-18 49 views
2

我是python,nlp和nltk的新手,请耐心等待。我有一些文章(〜200),这些文章都是用手分类的。我期待开发一种启发式辅助/推荐类别。首先,我希望能够在当前类别和文档中的单词之间建立关系。nltk pos tagger看起来合并'。'

我的前提是名词比其他词类更重要。例如,类别“能量”可能几乎完全通过名词来驱动:油,电池,风等。

我想要做的第一件事是标记零件并评估它们。在第一篇文章中,我遇到了一些问题。一些令牌绑定到标点符号。

for articles in articles[1]: 
    articles_id, content = articles 
    clean = nltk.clean_html(content).replace('’', "'") 
    tokens = nltk.word_tokenize(clean) 
    pos_document = nltk.pos_tag(tokens) 
    pos ={} 
    for pos_word in pos_document: 
     word, part = pos_word 
     if pos.has_key(part): 
      pos[part].append(word) 
     else: 
      pos[part] = [word] 

格式化输出:

{ 
'VBG': ['continuing', 'paying', 'falling', 'starting'], 
'VBD': ['made', 'ended'], 'VBN': ['been', 'leaned', 'been', 'been'], 
'VBP': ['know', 'hasn', 'have', 'continue', 'expect', 'take', 'see', 'have', 'are'], 
'WDT': ['which', 'which'], 'JJ': ['negative', 'positive', 'top', 'modest', 'negative', 'real', 'financial', 'isn', 'important', 'long', 'short', 'next'], 
'VBZ': ['is', 'has', 'is', 'leads', 'is', 'is'], 'DT': ['Another', 'the', 'the', 'any', 'any', 'the', 'the', 'a', 'the', 'the', 'the', 'the', 'a', 'the', 'a', 'a', 'the', 'a', 'the', 'any'], 
'RP': ['back'], 
'NN': [ 'listless', 'day', 'rsquo', 'll', 'progress', 'rsquo', 't', 'news', 'season', 'corner', 'surprise', 'stock', 'line', 'growth', 'question', 
     'stop', 'engineering', 'growth', 'isn', 'rsquo', 't', 'rsquo', 't', 'stock', 'market', 'look', 'junk', 'bond', 'market', 'turning', 'junk', 
     'rock', 'history', 'guide', 't', 'day', '%', '%', '%', 'level', 'move', 'isn', 'rsquo', 't', 'indication', 'way'], 
',': [',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ','], '.': ['.'], 
'TO': ['to', 'to', 'to', 'to', 'to', 'to', 'to'], 
'PRP': ['them', 'they', 'they', 'we', 'you', 'they', 'it'], 
'RB': ['then', 'there', 'just', 'just', 'always', 'so', 'so', 'only', 'there', 'right', 'there', 'much', 'typically', 'far', 'certainly'], 
':': [';', ';', ';', ';', ';', ';', ';'], 
'NNS': ['folks', 'companies', 'estimates', 'covers', 's', 'equities', 'bonds', 'equities', 'flats'], 
'NNP': ['drift.', 'We', 'Monday', 'DC', 'note.', 'Earnings', 'EPS', 'same.', 'The', 'Street', 'now.', 'Since', 'points.', 'What', 'behind.', 'We', 'flat.', 'The'], 
'VB': ['get', 'manufacture', 'buy', 'boost', 'look', 'see', 'say', 'let', 'rsquo', 'rsquo', 'be', 'build', 'accelerate', 'be'], 
'WRB': ['when', 'where'], 
'CC': ['&', 'and', '&', 'and', 'and', 'or', 'and', '&', '&', '&', 'and', '&', 'and', 'but', '&'], 
'CD': ['47', '23', '30'], 
'EX': ['there'], 
'IN': ['on', 'if', 'until', 'of', 'around', 'as', 'on', 'down', 'since', 'of', 'for', 'under', 'that', 'about', 'at', 'at', 'that', 'like', 'if'], 
'MD': ['can', 'will', 'can', 'can', 'will'], 
'JJR': ['more'] 
} 

的NMP字下通知 '漂移'。 - 不应该删除这段时间吗?我是否需要自行删除这些内容或者是否缺少与库中的内容?

+0

我不确定这是否能解决您的问题,而不是在已清理的文本上调用'word_tokenize',假设您的文章长度超过一个句子,则应该有一行'sents = nltk.sent_tokenize(clean)'然后在'sents'上运行'word_tokenize' – aelfric5578

+0

这样做 - 如果您将它作为答案发布,我会接受它,否则我会在几天内发布答案。 – akaphenom

回答

1

NLTK的词标记器假定它的输入已经被分离成句子。因此,为了使其起作用,您需要首先在您的输入上拨打sent_tokenize。我认为您可以使用sent_tokenize的输出作为word_tokenize的输入,但通常您会想要迭代您的句子。

for articles in articles[1]: 
    articles_id, content = articles 
    clean = nltk.clean_html(content).replace('’', "'") 
    sents = nltk.sent_tokenize(clean) 
    pos ={} 
    for sent in sents: 
     tokens = nltk.word_tokenize(sent) 
     pos_document = nltk.pos_tag(tokens) 
     for pos_word in pos_document: 
      word, part = pos_word 
      if pos.has_key(part): 
       pos[part].append(word) 
      else: 
       pos[part] = [word] 

我相信这是必要的原因是帮助从缩写使用的时间段区分的句子结束标点符号周期(即你不希望“史密斯先生”被分成'Mr', '.', 'Smith'