用共指分解法求stanfordNLP输出中的字符位置

2024-04-25 01:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用stanfordNLP来解析共指,正如它所解释的here。我正在运行上面的代码(提供here):

from stanfordnlp.server import CoreNLPClient

text = 'Barack was born in Hawaii. His wife Michelle was born in Milan. He says that she is very smart.'
print(f"Input text: {text}")

# set up the client
client = CoreNLPClient(properties={'annotators': 'coref', 'coref.algorithm' : 'statistical'}, timeout=60000, memory='16G')

# submit the request to the server
ann = client.annotate(text)    

mychains = list()
chains = ann.corefChain
for chain in chains:
    mychain = list()
    # Loop through every mention of this chain
    for mention in chain.mention:
        # Get the sentence in which this mention is located, and get the words which are part of this mention
        # (we can have more than one word, for example, a mention can be a pronoun like "he", but also a compound noun like "His wife Michelle")
        words_list = ann.sentence[mention.sentenceIndex].token[mention.beginIndex:mention.endIndex]
        #build a string out of the words of this mention
        ment_word = ' '.join([x.word for x in words_list])
        mychain.append(ment_word)
    mychains.append(mychain)

for chain in mychains:
    print(' <-> '.join(chain))

安装库后:

pip3 install stanfordcorenlp

下载模型

wget http://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip

设置$CORENLP\u HOME变量

os.environ['CORENLP_HOME'] = "path/to/stanford-corenlp-full-2018-10-05"

这段代码对我来说运行得很好,但是,输出只包含标记而不是字符的信息。例如,对于上述代码,输出为:

Barack <-> His <-> He
His wife Michelle <-> she

在皮带扣内部打印变量是:

mentionID: 0
mentionType: "PROPER"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
beginIndex: 0
endIndex: 1
headIndex: 0
sentenceIndex: 0
position: 1

mentionID: 4
mentionType: "PRONOMINAL"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
beginIndex: 0
endIndex: 1
headIndex: 0
sentenceIndex: 1
position: 3

mentionID: 5
mentionType: "PRONOMINAL"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
beginIndex: 0
endIndex: 1
headIndex: 0
sentenceIndex: 2
position: 1

mentionID: 3
mentionType: "PROPER"
number: "SINGULAR"
gender: "FEMALE"
animacy: "ANIMATE"
beginIndex: 0
endIndex: 3
headIndex: 2
sentenceIndex: 1
position: 2

mentionID: 6
mentionType: "PRONOMINAL"
number: "SINGULAR"
gender: "FEMALE"
animacy: "ANIMATE"
beginIndex: 3
endIndex: 4
headIndex: 3
sentenceIndex: 2
position: 2

我在搜索其他属性,例如打印安·斯福科雷夫哦

mentionType: "PROPER"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
person: "UNKNOWN"
startIndex: 0
endIndex: 1
headIndex: 0
headString: "barack"
nerString: "PERSON"
originalRef: 4294967295
goldCorefClusterID: -1
corefClusterID: 5
mentionNum: 0
sentNum: 0
utter: 0
paragraph: 1
isSubject: false
isDirectObject: true
isIndirectObject: false
isPrepositionObject: false
hasTwin: false
generic: false
isSingleton: false
hasBasicDependency: true
hasEnhancedDepenedncy: true
hasContextParseTree: true

尽管这个属性提供了大量的信息,但是没有关于单词的字符位置的信息。我可以用空格来划分句子,但这不是一般性的,我认为这可能会失败。有人能帮我吗??你知道吗


Tags: theinfalsenumberforgendersingularanimate