自定义字符串匹配度函数 解决字符串相似
自定义字符串匹配度函数 解决字符串相似
因为日志项目名称不同格式(如.xlsx .csv .txt)匹配需要,最近看到一篇比较老的博客
https://www.cnblogs.com/Demcia/p/5453906.html
据此 给出了一个很不错的匹配字符串的思路
// str1= "算法证明" , str2= "个人算法证明复印件"
//这里手动分词 ,可以考虑用第三方库 或者自定义分词方法
ArrayListString strs1 = new ArrayList();
strs1.add("身份");
strs1.add("证明");
ArrayListString strs2 = new ArrayList();
strs2.add("个人");
strs2.add("身份");
strs2.add("证明");
strs2.add("复印件");
T1 并 T2 ,并附加分数
*
定义如下函数定义T1和T2的相似度 ,可以感知如果交集大、相似的成分多,则最后字符串匹配度高
while (it.hasNext()) {
double[] c = T.get(it.next());
Ssum += c[0] * c[1];
s1 += c[0] * c[0];
s2 += c[1] * c[1];
}
//自定义的字符串匹配度
return Ssum / Math.sqrt(s1 * s2);
//// 相似度: 0.8320502943378436
完整代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class Test1 {
public static double YUZHI = 0.2;
public static double getSimilarity(ArrayListString T1, ArrayListString T2) {
int size = 0, size2 = 0;
MapString, double[] T = new HashMapString, double[]();
//在T1 不在T2的 分数标记为 [1,0.2]
String index = null;
for (int i = 0; i T1.size(); i++) {
index = T1.get(i);
double[] c = new double[2];
c[0] = 1; //T1的语义分数Ci
c[1] = YUZHI;//T2的语义分数Ci
T.put(index, c);
}
//遍历T2
for (int i = 0; i T2.size(); i++) {
index = T2.get(i);
double[] c = T.get(index);
if (c != null) {
c[1] = 1; //T2中也存在,T2的语义分数=1
} else {
c = new double[2]; //说明是T2独有
c[0] = YUZHI; //T1的语义分数Ci
c[1] = 1; //T2的语义分数Ci
T.put(index, c);
}
}
//开始计算,百分比
IteratorString it = T.keySet().iterator();
double s1 = 0, s2 = 0, Ssum = 0; //S1、S2
while (it.hasNext()) {
double[] c = T.get(it.next());
Ssum += c[0] * c[1];
s1 += c[0] * c[0];
s2 += c[1] * c[1];
}
//百分比
return Ssum / Math.sqrt(s1 * s2);
// } else {
// throw new Exception("传入参数有问题!");
// }
}
public static void main(String[] args) {
//dd
ArrayListString strs1 = new ArrayList();
strs1.add("身份");
strs1.add("证明");
ArrayListString strs2 = new ArrayList();
strs2.add("个人");
strs2.add("身份");
strs2.add("证明");
strs2.add("复印件");
//根据分词返回相似度
double same = 0;
same = getSimilarity(strs1, strs2);
// 相似度: 0.8320502943378436
System.out.println("相似度:" + same);
//end dd
}
自定义字符串匹配度函数 解决字符串相似 相关文章
- Element upload 组件实现自定义上传功能
el-form-item label="照片" el-upload v-if="operType !== 'details'" ref="upload" class="upload-demo" accept='.jpg,.png' :action="`${httpConfig.hashUrl}/sys/core/file/upload.do`" :show-file-list="false" :before-upload="beforeAvatarUpload" :on
- php如何把array转换成字符串
php把array转换成字符串的方法:可以利用implode函数来实现将array转换成数组,如【$arr = array(Hello,World!); echo implode( ,$ayy);】。 本文操作环境:windows10系统、php 7.3、thinkpad t480电脑。 函数介绍: implode() 函数返回一个由数组元素组合成
- python基础(2)字符串常用方法
python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub ?找到,则返回最左端的索引值,未找到,则返回-1 ?start和end都可省略,省略start说明从字符串开头找 省略end说明查找到字符串结尾,全部省略则查找全部字符串 source_st
- 创建自定义ClassLoader,绕过双亲委派
1.什么是类加载 通过javac将.java文件编译成.class字节码文件后,则需要将.class加载到JVM中运行,哪么是谁将.class加载到JVM的呢那就是类加载器啦。 2.类加载器类型 Bootstrap ClassLoader(启动类加载器): 该类加载器由C++实现的。负责加载Java基础类,
- scratch-ui自定义开发笔记
scratch-gui是代码开源在github上,具体地址为:https://github.com/LLK/scratch-gui.git 1.执行yarn build的时候报错 ERROR in ./src/playground/index.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js): Error [ERR_PACKAGE_PATH_
- dremio sql server 自定义connector开发解决中文乱码问题
在通过反编译查看dremio ce 版本的jdbc 插件,发现对于sql server 的乱码是因为一个配置引起的 所以以下是尝试基于官方的机制,写一个sql server 的扩展,方便集成使用 项目结构 pom.xml xml version="1.0" encoding="UTF-8" project xmlns="http://maven.ap
- DEV C++自定义函数顺序与printf用法
#include stdio.hint gys(int a,int b)//此函数只能放在main上面;如果放在main下面,会报错“没有定义这个函数" 此函数的功能为求最大公约数{ if (b==0) return a; return gys(b,a%b);}int main(){ int a = 520; int c1=98; int c2=56; char b = 'F'; float
- switch case 字符串表达式支持
根据业务需求,在编码过程中,经常会遇到switch case表达式是字符串的场景,现支持如下。 【1】实现文件 支持实现的文件 1 #pragma once 2 3 #include cstddef 4 #include cstdint 5 #include type_traits 6 7 namespace prefab 8 { 9 templatetypename T10
- 正则
正则表达式 位置字符: ^ : 匹配字符串开始位置 $ : 匹配字符串结束位置 | : 中|美国 匹配 "中" 和 "美国" , (中|美)国 匹配 "中国" 和 美国 元字符: \ : 隐藏特殊字符的特殊含义。 "\\" 打印出来就是 \ , r"\\" 打印出来就是 \\ , \\\\ 模式才能匹配 "\
- 安卓自动化测试--Momkey 自定义脚本实现自动化
MonkeyScript MS 是官方提供的,除了像猴子一样随机乱点之外,还可以通过编写脚本的形式,完成一系列固定的操作。MS 提供一整套完善的 API 来进行支持,主要还是基于坐标点的操作,包含常用的:点击、长按、输入、等待等操作。 脚本用法 1、LaunchActivity(p