簡體   English   中英

如何使用Spacy獲得兩個對齊文本的相似度的行級度量?

[英]How to obtain a line-level measure of the similarity of two aligned texts with Spacy?

我有兩個對齊的英文文檔,每個文檔的行數相同(大約 30k)。 我想獲得每行的相似性,即,在LINE_1text_aLINE_1 text_b,LINE_2text_aLINE_2text_b等的測量。 (每一行可能包含不止一個句子)我已經這樣做了:

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))

它運行良好

nlp 需要一個字符串,而不是一個文件處理程序對象。

嘗試這個

a_doc = nlp("".join(file_a.readlines()))
b_doc = nlp("".join(file_b.readlines()))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM