本文介绍了如何从句子中提取名词形容词对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从该sentence中提取名词形容词对.所以,基本上我想要这样的东西:(Mark,sincere) (John,sincere).

I wish to extract noun-adjective pairs from this sentence. So, basically I want something like :(Mark,sincere) (John,sincere).

from nltk import word_tokenize, pos_tag, ne_chunk
sentence = "Mark and John are sincere employees at Google."
print ne_chunk(pos_tag(word_tokenize(sentence)))

推荐答案

Spacy的 POS标记NLTK更好.更快更好.这是您想做的事的一个例子

Spacy's POS tagging would be a better than NLTK. It's faster and better. Here is an example of what you want to do

import spacy
nlp = spacy.load('en')
doc = nlp(u'Mark and John are sincere employees at Google.')
noun_adj_pairs = []
for i,token in enumerate(doc):
    if token.pos_ not in ('NOUN','PROPN'):
        continue
    for j in range(i+1,len(doc)):
        if doc[j].pos_ == 'ADJ':
            noun_adj_pairs.append((token,doc[j]))
            break
noun_adj_pairs

输出

[(Mark, sincere), (John, sincere)]

这篇关于如何从句子中提取名词形容词对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:30