文件夹内容相似度统计

  需求:比对两源码文件的相似性。
  思路1:利用Beyond Compare 4工具,文件夹比较,直接统计两源码文件相同文件(哈希值及文件名)数量,再分别计算占比。
  思路2:先匹配出相对路径且文件名相同的文件,利用Beyond Compare 4工具,文本比较,分别得到文本比较结果,再依次统计出各文本内容的相似度(相同行/文本所有行)。
  注:这里仅展示思路2的逻辑。

1. 匹配出相对路径且文件名相同的文件

def left_right_path_file(path):
    '''文件夹(左右)下,所有文件的路径。
    path 文件夹路径; keynum 相对路径截取位置
    '''
    file_path = []
    keynum = len(path)
    for cur_path, cur_dirs, cur_files in os.walk(path):
        for name in cur_files:
            file_path.append(os.path.join(cur_path[int(keynum):], name))         
    return file_path
    
if __name__ == '__main__':
	path_left = r'D:\***'
	path_right = r'D:\***'
	left_path_files = left_right_path_file(path_left)     # 源文件夹(左)下 所有文件相对路径
    right_path_files = left_right_path_file(path_right)    # 源文件夹(右)下 所有文件相对路径
    same_path_file = set(left_path_files).intersection(set(right_path_files))     # 源文件夹(左右)下,相对路径且文件名相同

2. Beyond Compare 4,依次文本比较

def sta_main(path_left, path_right):
    '''匹配同路径同名文件;遍历完成bcompare比对,生成html比对结果;读取html比对结果,整理成表格'''
    bc_path = r'D:\Program Files\Beyond Compare 4'  # 调用本地BCompare                 #  ******* ----- ********
    os.chdir(bc_path)

    left_path_files = left_right_path_file(path_left)     # 源文件夹(左)下 所有文件相对路径
    right_path_files = left_right_path_file(path_right)    # 源文件夹(右)下 所有文件相对路径
    same_path_file = set(left_path_files).intersection(set(right_path_files))     # 源文件夹(左右)下,相对路径且文件名相同

    kk = 1
    for path_file in same_path_file:
        print(path_file)
        time.sleep(0.3)
        left_path_file = path_left + '\\' + path_file
        right_path_file = path_right + '\\' + path_file
        compare_file = r'D:\文件夹内容相似度比对\bc.txt'            
        out_file = r'D:\文件夹内容相似度比对\%d.html' % kk          
        bcompare_file(compare_file, left_path_file, right_path_file, out_file)

先得安装Beyond Compare 4,其中bc.txt文本内容,以及文本比较结果(html),见下。

bc.txt内容:
text-report layout:summary options:display-mismatches,line-numbers output-to:"%3" output-options:wrap-word,html-color "%1" "%2"

文本比较html结果:
文本比较(T)
已产生: 2022/9/26 15:10:42
   
左边文件: F:\同路径同名文件内容比对\***\logic\af.go  
右边文件: F:\同路径同名文件内容比对\***\logic\af.go  
56 个相同行  
1 个不重要的左边独有行  
3 个不重要的差异行  
3 个重要的左边独有行  
10 个重要差异行  
   
7 个差异部分  

3. 汇总、统计结果

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#中可以使用余弦相似度算法来计算文本的相似度。余弦相似度算法是一种常用的文本相似度计算方法,它可以通过计算两个文本向量之间的夹角余弦值来判断文本的相似程度。 以下是一个简单的C#代码示例,展示如何使用余弦相似度算法计算两个字符串之间的相似度: ``` using System; using System.Collections.Generic; namespace CosineSimilarity { class Program { static void Main(string[] args) { string text1 = "This is the first text"; string text2 = "This is the second text"; double similarity = CalculateCosineSimilarity(text1, text2); Console.WriteLine("The similarity between the two texts is: " + similarity); } static double CalculateCosineSimilarity(string text1, string text2) { Dictionary<string, int> vector1 = GetTermFrequencies(text1); Dictionary<string, int> vector2 = GetTermFrequencies(text2); double dotProduct = 0.0; double magnitude1 = 0.0; double magnitude2 = 0.0; foreach (KeyValuePair<string, int> entry in vector1) { string term = entry.Key; int frequency1 = entry.Value; int frequency2 = 0; if (vector2.TryGetValue(term, out frequency2)) { dotProduct += frequency1 * frequency2; } magnitude1 += Math.Pow(frequency1, 2); } foreach (KeyValuePair<string, int> entry in vector2) { int frequency2 = entry.Value; magnitude2 += Math.Pow(frequency2, 2); } magnitude1 = Math.Sqrt(magnitude1); magnitude2 = Math.Sqrt(magnitude2); if (magnitude1 == 0.0 || magnitude2 == 0.0) { return 0.0; } else { return dotProduct / (magnitude1 * magnitude2); } } static Dictionary<string, int> GetTermFrequencies(string text) { Dictionary<string, int> frequencies = new Dictionary<string, int>(); string[] words = text.Split(' '); foreach (string word in words) { if (frequencies.ContainsKey(word)) { frequencies[word]++; } else { frequencies[word] = 1; } } return frequencies; } } } ``` 在此示例中,我们首先定义了两个字符串 `text1` 和 `text2`,然后调用了 `CalculateCosineSimilarity` 方法来计算这两个字符串之间的相似度。该方法接受两个字符串作为输入,并返回一个 double 类型的相似度值。在方法中,我们首先调用了 `GetTermFrequencies` 方法来计算每个字符串中每个词语的出现频率,并将其存储在一个字典中。然后,我们通过计算这两个字符串的向量之间的余弦相似度计算它们之间的相似度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值