【问题标题】:nltk pos tagger looks to incorporate '.'nltk pos tagger 看起来包含 '.'
【发布时间】:2013-12-18 15:38:25
【问题描述】:

我是 python、nlp 和 nltk 的新手,所以请多多包涵。我有一些文章(约 200 篇)是手工分类的。我正在寻找一种启发式方法来帮助/推荐类别。首先,我希望在当前类别和文档中的单词之间建立关系。

我的前提是名词比任何其他词性都更重要。例如,“能源”类别可能几乎完全由名词驱动:油、电池、风等。

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

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 下的“漂移”一词。 - 不应该删除期间吗?我需要自己删除它还是我缺少图书馆的东西?

【问题讨论】:

  • 我不确定这是否能解决你的问题,但与其在清理后的文本上调用word_tokenize,假设你的文章不止一个句子,你应该有一行sents = nltk.sent_tokenize(clean)然后在sents上运行word_tokenize
  • 做到了-如果您将其作为答案发布,我将接受它,否则我将在几天后自己发布答案。

标签: python nlp nltk tagging


【解决方案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'

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 2012-01-25
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多