所以我正在使用stanfordnlp
库,我想创建一种方法来将给定句子的VBD
和NN
配对。我已经阅读了所有相关问题,但我认为我还需要其他内容。
这是我到目前为止的内容:
def extract_pos(doc):
parsed_text = {'word':[], 'POS':[], 'expl':[]}
for sent in doc.sentences:
for wrd in sent.words:
if wrd.pos in pos_dict.keys():
pos_exp = pos_dict[wrd.pos]
else:
pos_exp = 'NA'
parsed_text['word'].append(wrd.text)
parsed_text['POS'].append(wrd.pos)
parsed_text['expl'].append(pos_exp)
return parsed_text
nlp = stanfordnlp.Pipeline()
sentence = 'The quick brown fox jumped over the lazy dog.'
doc = nlp(sentence)
for d in doc.sentences[0].words:
if d.dependency_relation == 'nsubj':
print(d.text)
我曾考虑过为VBD
和NN
创建空列表,然后尝试使用它们,但是我无处可去。
假设我有The quick brown fox jumped over the lazy dog
。我应该有像(jumped, fox), (jumped, dog)
这样的对。我需要一些指导。任何帮助表示赞赏。
您只需要将相关性编号映射到原始短语。只需记住,斯坦福大学nlp中的令牌头寸是基于1的(因此,需要对每个头寸减去1)。
然后将根匹配为其nsubjs。
这里是一个快速解决方案:
sentence = 'The quick brown fox jumped over the lazy dog'
words = sentence.split()
parsed = nlp.dependency_parse(sentence)
root = ""
for tup in parsed:
if (tup[0] == 'ROOT'):
root = words[tup[2]-1]
else:
if(parsed[tup[1]-1][0] in ['nsubj']):