Coreference resolution in python nltk using Stanford coreNLP

后端 未结 4 1960
情话喂你
情话喂你 2020-12-16 00:07

Stanford CoreNLP provides coreference resolution as mentioned here, also this thread, this, provides some insights about its implementation in Java.

However, I am

4条回答
  •  不知归路
    2020-12-16 00:27

    stanfordcorenlp, the relatively new wrapper, may work for you.

    Suppose the text is "Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008."

    The code:

    # coding=utf-8
    
    import json
    from stanfordcorenlp import StanfordCoreNLP
    
    nlp = StanfordCoreNLP(r'G:\JavaLibraries\stanford-corenlp-full-2017-06-09', quiet=False)
    props = {'annotators': 'coref', 'pipelineLanguage': 'en'}
    
    text = 'Barack Obama was born in Hawaii.  He is the president. Obama was elected in 2008.'
    result = json.loads(nlp.annotate(text, properties=props))
    
    num, mentions = result['corefs'].items()[0]
    for mention in mentions:
        print(mention)
    

    Every "mention" above is a Python dict like this:

    {
      "id": 0,
      "text": "Barack Obama",
      "type": "PROPER",
      "number": "SINGULAR",
      "gender": "MALE",
      "animacy": "ANIMATE",
      "startIndex": 1,
      "endIndex": 3,
      "headIndex": 2,
      "sentNum": 1,
      "position": [
        1,
        1
      ],
      "isRepresentativeMention": true
    }
    

提交回复
热议问题