我刚开始使用 spacy。我有一个场景,我必须在句子中获取句子开始和结束的索引。如果我使用文档。发送然后我得到一个发送列表。 sent.beg 和 sent.end 打印 token 索引,但我想要字符索引。
for sent in doc.sents:
print(sent.start,sent.end) #prints token index
例子:
completeText = "Hi, I am using StackOverflow. The community is great."
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe(nlp.create_pipe('sentencizer'))
doc = nlp(completeText)
for sent in doc.sents:
print(sent.start,sent.end) #prints 0,7 and 7,12 the token indices
上面的打印语句只打印标记索引,不打印字符索引。我想要的输出是 0,29 和 30, 54。
我试过按如下方式获取句子的长度。我在最后添加了一个 if 语句,因为句号后的空格在句子中被忽略了。
start = [0] * len(list(doc.sents))
end = [0] * len(list(doc.sents))
for index, i in enumerate(doc.sents):
if index !=0:
start[index] = end[index-1] + 1
length += len(str(i))
if index == 0:
end[index] = length
else:
end[index] = length
if end[index] + 1 < len(sent) and sent[end[index]+1] == " ":
length += 1
当句号后只有空格时,这很有效。但是在我的全文中(超过 10,000 行)我没有得到正确的答案。 spacy 是否会忽略上面提到的包含在发送中的任何其他字符?
有更好的方法吗?
最佳答案
您可以只使用 start_char
和 end_char
。
for sent in doc.sents:
print(sent.start_char,sent.end_char)
一个句子是 spaCy 中的一个 Span,它带有许多有用的属性,这些属性在 docs 中有介绍。 .
关于python - 如何获得发送者开始和结束的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64202958/