如何从句子中提取名词形容词对

时间:2018-03-08 05:30:38

标签: python nltk

我想从这个sentence中提取名词 - 形容词对。所以,基本上我想要的东西: (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)))

1 个答案:

答案 0 :(得分:7)

Spacy的POS tagging会比NLTK更好。它越来越快。以下是您要做的事情的示例

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)]