python - nltk pos 标记器看起来包含 '.'

标签 python nlp nltk tagging

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

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

我想做的第一件事是标记零件并对其进行评估。在第一篇文章中,我遇到了一些问题。一些标记绑定(bind)了标点符号。

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 下的“漂移”一词。 - 句点不应该被删除吗?我需要自己删除它还是我在库中遗漏了什么?

最佳答案

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', ' .', '史密斯')

关于python - nltk pos 标记器看起来包含 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662286/

相关文章:

python - 使用 nltk 的没有上下文的词性标记

python - Flask SQLAlchemy 连接到 MySQL 数据库

python - 更新列表中的匹配行

python - 打印选择的 scipy.optimize.minimize 方法

python - 如何不将整个单词 "king"匹配到 "king?"?

python - 如何从文本语料库构建 PPMI 矩阵?

python - 从列表中提取出现在特定单词之后的第一个元素

Python获取完整程序 "self"

java - 从 ANTLR 文法创建树

python - POS 标记的 NLTK 语料库中的正则表达式