【问题标题】:Sentence similarity using universal sentence encoder by passing threshold通过阈值使用通用句子编码器的句子相似度
【发布时间】:2020-09-10 07:36:05
【问题描述】:

我有一个超过 1500 行的数据。每一行都有一个句子。我正在尝试找出在所有句子中找到最相似句子的最佳方法。我已经尝试过example,但处理速度非常慢,1500 行数据大约需要 20 分钟。

我使用了上一个问题中的代码,并尝试了多种类型来提高速度,但影响不大。我遇到了使用 tensorflow 的 通用句子编码器,它看起来很快并且具有很好的准确性。我正在开发 colab,你可以查看 here

import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import re
import seaborn as sns

module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" #@param ["https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5", "https://tfhub.dev/google/universal-sentence-encoder-lite/2"]
model = hub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
  return model(input)

df = pd.DataFrame(columns=["ID","DESCRIPTION"], data=np.matrix([[10,"Cancel ASN WMS Cancel ASN"],
                                                                [11,"MAXPREDO Validation is corect"],
                                                                [12,"Move to QC"],
                                                                [13,"Cancel ASN WMS Cancel ASN"],
                                                                [14,"MAXPREDO Validation is right"],
                                                                [15,"Verify files are sent every hours for this interface from Optima"],
                                                                [16,"MAXPREDO Validation are correct"],
                                                                [17,"Move to QC"],
                                                                [18,"Verify files are not sent"]
                                                                ]))

message_embeddings = embed(messages)

for i, message_embedding in enumerate(np.array(message_embeddings).tolist()):
  print("Message: {}".format(messages[i]))
  print("Embedding size: {}".format(len(message_embedding)))
  message_embedding_snippet = ", ".join(
      (str(x) for x in message_embedding[:3]))
  print("Embedding: [{}, ...]\n".format(message_embedding_snippet))

我在寻找什么

我想要一种方法,我可以通过阈值示例 0.90 的所有行中彼此相似的数据应在 0.90% 以上作为结果返回。

Data Sample
ID    |   DESCRIPTION
-----------------------------
10    | Cancel ASN WMS Cancel ASN   
11    | MAXPREDO Validation is corect
12    | Move to QC  
13    | Cancel ASN WMS Cancel ASN   
14    | MAXPREDO Validation is right
15    | Verify files are sent every hours for this interface from Optima
16    | MAXPREDO Validation are correct
17    | Move to QC  
18    | Verify files are not sent 

预期结果

Above data which are similar upto 0.90% should get as a result with ID

ID    |   DESCRIPTION
-----------------------------
10    | Cancel ASN WMS Cancel ASN
13    | Cancel ASN WMS Cancel ASN
11    | MAXPREDO Validation is corect  # even spelling is not correct
14    | MAXPREDO Validation is right
16    | MAXPREDO Validation are correct
12    | Move to QC  
17    | Move to QC 

【问题讨论】:

    标签: python tensorflow word-embedding tensorflow-hub sentence-similarity


    【解决方案1】:

    您可以通过多种方式找到两个嵌入向量之间的相似性。 最常见的是cosine_similarity

    因此你要做的第一件事就是计算相似度矩阵:

    代码:

    message_embeddings = embed(list(df['DESCRIPTION']))
    cos_sim = sklearn.metrics.pairwise.cosine_similarity(message_embeddings)
    

    您会得到一个具有相似值的9*9 矩阵。 您可以创建此矩阵的热图以将其可视化。

    代码:

    def plot_similarity(labels, corr_matrix):
      sns.set(font_scale=1.2)
      g = sns.heatmap(
          corr_matrix,
          xticklabels=labels,
          yticklabels=labels,
          vmin=0,
          vmax=1,
          cmap="YlOrRd")
      g.set_xticklabels(labels, rotation=90)
      g.set_title("Semantic Textual Similarity")
    
    plot_similarity(list(df['DESCRIPTION']), cos_sim)
    

    输出:

    较暗的框意味着更多的相似性。

    最后,您使用阈值遍历这个 cos_sim 矩阵以获得所有相似的句子:

    threshold = 0.8
    row_index = []
    for i in range(cos_sim.shape[0]):
      if i in row_index:
        continue
      similar = [index for index in range(cos_sim.shape[1]) if (cos_sim[i][index] > threshold)]
      if len(similar) > 1:
        row_index += similar
    
    sim_df = pd.DataFrame()
    sim_df['ID'] = [df['ID'][i] for i in row_index]
    sim_df['DESCRIPTION'] = [df['DESCRIPTION'][i] for i in row_index]
    sim_df
    

    数据框如下所示。
    输出:

    您可以使用不同的方法生成相似度矩阵。 更多方法可以看this

    【讨论】:

    • 我已经完成了代码并执行了相同的操作。我想知道为什么在 0.8 阈值时我得到的索引 [17] [18] 上的术语相似。虽然不是很相似
    • 它们不相似,只是列表中的下一个术语。
    • 我已经更新了我的答案。现在,只有当它们具有相似的术语时才会包含这些术语。
    • 我建议您针对上述主题提出一个关于 SO 的新问题。
    • 你能告诉我,当我仍然设置阈值 1 时,我得到了一些不相似的行,它们甚至不完全相同。根据阈值,我们应该得到完全相同的结果。
    猜你喜欢
    • 2021-05-23
    • 2020-04-14
    • 1970-01-01
    • 2017-01-10
    • 2020-12-27
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多