How to extract noun adjective pairs from a sentence

前端 未结 1 1204
说谎
说谎 2021-01-03 06:14

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

fr         


        
1条回答
  •  半阙折子戏
    2021-01-03 06:53

    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
    

    output

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

    0 讨论(0)
提交回复
热议问题