对python中的句子相似性排序
我试图找到标记文档的句子和将结果保存在列表中的句子之间的相似性。我想根据相似性分数对结果进行排序。当我尝试根据相似性分数对输出进行分类时,会出现错误吗?
results=[]
#embedding all the documents and find the similarity between search text and all the tokenize sentences
for docs_sent_token in docs_sent_tokens:
sentence_embeddings = model.encode(docs_sent_token)
sim_score1 = cosine_sim(search_sentence_embeddings, sentence_embeddings)
if sim_score1 > 0:
results.append({
sim_score1,
docs_sent_token,
})
results.sort(key=lambda k : k['sim_score1'] , reverse=True)
print(results)
这是我遇到的错误。
TypeError: 'set' object is not subscriptable
可以使用字典解决此问题。
if sim_score1 > 0:
results.append({
'Score':sim_score1,
'Token':docs_sent_token,
})
results.sort(key=lambda k : k['Score'] , reverse=True)
print(results)
但是,有什么可能的方法可以使用列表完成分类?我想以这种格式获得结果。
[{0.91, 'Sentence 1'}, {0.87, 'Sentence 2'}, {0.33, 'Sentence 3'}, {0.30, 'Sentence 4'},
I am trying to find the similarity between the sentences tokenised document and a sentence getting the result saved in a list. I want to sort the results based on the similarity score. When I try to sort the output based on the similarity score I get an error?
results=[]
#embedding all the documents and find the similarity between search text and all the tokenize sentences
for docs_sent_token in docs_sent_tokens:
sentence_embeddings = model.encode(docs_sent_token)
sim_score1 = cosine_sim(search_sentence_embeddings, sentence_embeddings)
if sim_score1 > 0:
results.append({
sim_score1,
docs_sent_token,
})
results.sort(key=lambda k : k['sim_score1'] , reverse=True)
print(results)
This is the error I get.
TypeError: 'set' object is not subscriptable
This issue can be solved using dictionaries.
if sim_score1 > 0:
results.append({
'Score':sim_score1,
'Token':docs_sent_token,
})
results.sort(key=lambda k : k['Score'] , reverse=True)
print(results)
But is there any possible way to get the sorting done using the list? I want to get the result in this format.
[{0.91, 'Sentence 1'}, {0.87, 'Sentence 2'}, {0.33, 'Sentence 3'}, {0.30, 'Sentence 4'},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
set
s没有索引或密钥来指示要排序的值。您可以创建元组
s或dict
s的列表,对其进行排序,然后将其转换为set
s稍后输出
set
s don't have indices or keys to indicate a value to sort by. You can create a list oftuple
s ordict
s instead, sort it and convert it toset
s later onOutput