我有两个对齐的英文文档,每个文档都有相同的行数(大约 30k)。我想要获取每行相似度的度量,即 text_a 中的 line_1 与 text_b 中的 line_1 、text_a 中的 line_2 与 text_b 中的 line_2 等等。 (每一行可能包含多个句子)我已经这样做了:
import spacy
nlp = spacy.load('en_core_web_lg')
file_a = open('text-1.txt', 'r')
file_b = open ('text-2.txt', 'r')
a_doc = nlp(file_a)
b_doc = nlp(file_b)
for a,b in zip(a_doc, b_doc):
print("similarity:", a.similarity(b))
但我收到以下错误:
if len(text) > self.max_length:
TypeError: object of type '_io.TextIOWrapper' has no len()
你能帮我吗?非常感谢
最佳答案
nlp()
需要一个字符串,而不是文件对象。我稍微编辑了你的代码:
import spacy
nlp = spacy.load('en_core_web_sm')
file_a = open('text-1.txt', 'r').read()
file_b = open ('text-2.txt', 'r').read()
a_doc = nlp(file_a)
b_doc = nlp(file_b)
for a,b in zip(a_doc, b_doc):
print("similarity:", a.similarity(b))
运行良好
关于python - 如何使用 Spacy 获得两个对齐文本相似度的行级度量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670548/