检索原始文档中的开始和结束字符索引,对于由 Spacy 返回的那些句子

GuS*_*uku 2 nlp spacy

我正在使用类似于以下模式的东西来检索原始文档中 Spacy 句子的开始和结束索引:

nlp = spacy.en.English()
doc = nlp(fulltext)

tot = 0
prev_end=0
for sent in doc.sents:
    x = re.search(re.escape(sent.text), fulltext)
    print (x.start(), x.end(), ">>>", sent.text)
    tot += (x.end()-prev_end)
    prev_end = x.end()

if len(fulltext) == tot: print ("works")
Run Code Online (Sandbox Code Playgroud)

这似乎适用于我使用的那几个测试文档。但是担心我是否忽略了像 spacy 这样的任何“陷阱”,有时会剥离一些我不知道的字符。我是吗?

PS:如果有帮助,我需要这些索引与我从 Brat 的注释文件中获得的索引进行比较。

syl*_*sm_ 7

您应该只能使用sent.start_charsent.end_char属性。这些给出了你所追求的索引:https : //spacy.io/docs/api/span#attributes

doc.text始终等于原始全文。如果没有,请提交错误报告。