Python小脚本:计算两个文件内容的相似率
来源:互联网 发布:linux 定时重启tomcat 编辑:程序博客网 时间:2024/06/04 18:03
之前拿到一堆源码文件,在整理的时候需要计算一些文件之间的相似率。与之相关第一个想到的就是Linux上的” diff “命令。把diff命令的输出拿过来简单计算一下,就有结果了。于是用Python调用一下命令行,短短几行代码,就OK了。
# !/usr/bin/python# -*- coding: utf-8 -*-"""create_author : 蛙鳜鸡鹳狸猿create_time : 2017-06-01program : *_* evaluate the similarity between two files *_*"""import commandsdef simieval(lf, rf): """ UDF of evaluating the similarity between two files. Rely on "diff" handler on Linux platform. Default to ignore blanks by "-bB". :param lf: string File to diff at left. :param rf: string File to diff at right. :return: float Similarity ratio between two files. """ le = float(commands.getoutput("diff -bB %s %s | awk 'BEGIN {cnt = 0}; /^[0-9]/ {cnt -= 1}; /^[<>]/ {cnt += 1}; END {print cnt}'" % (lf, rf))) re = float(commands.getoutput("nl %s %s | awk '{print $2}' | grep -v '^$' | wc -l" % (lf, rf))) result = 1 - le / re return resultif __name__ == "__main__": writer = "/home/student/" ll = """ Ubuntu MySQL C JAVA Shell """ wl = open(writer + "lf", 'w') wl.writelines(ll) wl.close() lr = """ CentOS MySQL C Python """ wr = open(writer + "rf", 'w') wr.writelines(lr) wr.close() se = simieval(lf=writer + "lf", rf=writer + "rf") print(se)
阅读全文
0 0
- Python小脚本:计算两个文件内容的相似率
- python文件命名小脚本
- python 备份文件的小脚本
- python一个遍历文件目录的小脚本
- 加速cocospod 的两个小脚本
- 重复内容删除的小脚本
- 入侵渗透专用的python小脚本
- 一些实用的python小脚本
- 简单的python抓图小脚本
- 合并文件的一个小脚本
- 一些python小脚本
- Python字典小脚本
- python统计小脚本
- python小脚本
- Python根据操作系统自动调用创建文件目录小脚本
- 文件转换为utf-8编码(python小脚本)
- 汇总下几个IP计算/转换的shell小脚本
- 有用的小脚本
- 04_程序员必须掌握的8大排序算法_堆排序
- hbase-scala-获取连接
- 开发环境搭建
- Java多线程并发基础面试题回答
- substr截取字符串 不用循环?
- Python小脚本:计算两个文件内容的相似率
- java 手机验证码
- Uboot的简单的概述
- CentOS下安装JDK1.7
- Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
- 使用浏览器访问 Linux 终端的方法
- 05_程序员必须掌握的8大排序算法_冒泡排序
- canvas制作简易祖玛游戏
- vritual box linux桥接网络配置