Advertisement

余弦相似度 机器学习_深度学习-5.短文本相似度计算

阅读量:

在nlp自然语言处理领域中进行短文本相似度评估是一项占据重要地位的理论基础研究。这种技术被广泛应用于多种应用场景中,包括但不限于文本分类和文本去噪等任务。

在当今信息时代,了解各种文本相似度的计算方法是一个重要的课题。其中一些文献会详细阐述其理论基础,而另一些则直接给出了具体的数学公式和求解方法

基于文本字符的相似度评估
Jaccard’s similarity coefficient
The longest common subsequence (LCS)
The minimum edit distance based on word frequency vectors, the similarity assessment
Cosine similarity measure

主要有这么几种吧,接下来我们一一介绍。

基于文本字符的相似度计算

Jaccard :

The Jaccard index, also referred to as the Jaccard similarity coefficient (also known as JSC), is a measure of similarity between two sets. Given any two strings A and B, by performing word segmentation to obtain their respective word sets SA and SB, the Jaccard coefficient is calculated as the size of the intersection of SA and SB divided by the size of their union. This mathematical framework effectively transforms text into a set-based model for analysis.

05b6ad8f0be8467cd2a55c834f4db205.png

基础代码如下:

复制代码
 def jaccard_similarity(str_a,str_b):

    
  	seta = split_words(str_a)
    
  	setb = split_words(str_b)
    
  	print(seta)
    
  	print(setb)
    
   	sa_sb = 1.0 * len(seta & setb) / len(seta | setb)
    
   	return sa_sb

最长公共子序列(LCS)

  • 1. 子序列为:从一个特定顺序排列好的元素集合中删除零个或多个元素后生成的新集合(不改变剩余元素之间的相对位置)。
  • 2. 公共子序列为:对于两个给定的有序数据集X和Y来说, 如果存在另一个数据集Z, 使得Z是X的一个子集也是Y的一个子集, 那么我们称Z为X和Y的一个公共子集。
  • 3. 最长公共子序列问题是:即寻找在公共子序列中最长的那个特定的数据块。
复制代码
 def lcs_similarity(str_a, str_b):

    
      # 得到一个二维的数组,类似用dp[lena+1][lenb+1],并且初始化为0
    
      lengths = [[0 for j in range(len(str_b)+1)] for i in range(len(str_a)+1)]
    
      for i, x in enumerate(str_a):
    
      for j, y in enumerate(str_b):
    
          if x == y:
    
              lengths[i+1][j+1] = lengths[i][j] + 1
    
          else:
    
              lengths[i+1][j+1] = max(lengths[i+1][j], lengths[i][j+1])
    
      result = ""
    
      x, y = len(str_a), len(str_b)
    
      while x != 0 and y != 0:
    
      if lengths[x][y] == lengths[x-1][y]:
    
          x -= 1
    
      elif lengths[x][y] == lengths[x][y-1]:
    
          y -= 1
    
      else:
    
          assert str_a[x-1] == str_b[y-1] #后面语句为真,类似于if(a[x-1]==b[y-1]),执行后条件下的语句
    
          result = str_a[x-1] + result #注意这一句,这是一个从后向前的过程
    
          x -= 1
    
          y -= 1
    
      longestdist = lengths[len(str_a)][len(str_b)]
    
      ratio = longestdist/min(len(str_a),len(str_b))
    
  
    
      return ratio

最小可编辑距离

最小编辑距离问题涉及两个字符串之间的差异性。举例说明如下:比如"abcd"与"acd"之间存在差异;或者"happy"与"huppy"之间也存在差异。可用操作包括插入、删除以及替换等基本操作。

代码如下:

复制代码
 def med_similarity(str_a,str_b):

    
      len_sum = float(len(str_a) + len(str_b))
    
      long_str = None
    
      short_str = None
    
      if len(str_a) > len(str_b): 
    
      long_str,short_str = str_a,str_b
    
      else:         long_str,short_str = str_b,str_a      distances = range(len(short_str) + 1)      for long_str_index,char_in_long_str in enumerate(long_str):         new_distances = [long_str_index+1]          for short_str_index,char_in_short_str in enumerate(short_str):             if char_in_short_str == char_in_long_str:                 new_distances.append(distances[short_str_index])             else:                  del_distance = distances[short_str_index] # 删除                 insert_distance = distances[short_str_index+1] # 插入                 replace_distance = new_distances[-1] # 变换                 new_distances.append(1 + min((del_distance,insert_distance,replace_distance)))         distances = new_distances      mindist = distances[-1]     ratio = (len_sum - mindist)/len_sum          return ratio

基于词频向量的相似度计算

余弦相似度

还是老规矩将两个字符串转换成数组或者说是向量,ok。

85a5c97af059481e6893236e562715cc.png

当两向量之间的夹角为零时,则意味着a向量与b向量是相同的;考虑到其基本性质,并利用余弦定理进行推导

61ad81882b044fa8b9878fecd3d9056f.png
复制代码
      a = np.array(vec_a)

    
      b = np.array(vec_b)
    
      print(a)
    
      print(b)
    
      ret =  np.sum(a*b) / (np.sqrt(np.sum(a ** 2)) * np.sqrt(np.sum(b ** 2)))
    
      print('分数:'+str(ret))
    
      return ret

代码如上,至于你用什么把文本转换成向量 有多种方法。

以下是大体的结果

句子:
Hello, I'm from the UK<--->Hello, I'm from China
各种算法结果:
Jaccard coefficient is 0.6
Longest common subsequence (LCS) ratio is approximately 82%, which is quite high and indicates a strong similarity between the two sequences under comparison in this experiment.
Minimum edit distance ratio is approximately 84%, suggesting that transforming one sequence into another requires minimal changes.
Cosine similarity ratio is approximately 75%, indicating a moderate level of similarity between the two sequences.

其他

关注我不迷路,目前只是一些入门级的小文章,后面会有AI系列文章推送。

复制代码
    https://github.com/yazone/ai_learning_path
39999086f058dea4be6d165fbc779f8d.png

全部评论 (0)

还没有任何评论哟~