`
cooliufang
  • 浏览: 129894 次
社区版块
存档分类
最新评论

【Similarity calculation】Jaccard Distance

阅读更多
Jaccard token distance
It  is simply given by the number of common tokens in two names and the count of total number of tokens in those names.


Jaccard distance (simplify)
To reduce the computational complexity

 
Jaccard distance (weighted)
weighted Jaccard distance is equal to the following expression


then Jaccard similarity function only need to take last past from the above each function.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JaccardDistance {
	public static Map<String, Double> weightMap = new HashMap<String, Double>();

	/**
	 * intersection between two strings
	 * @param source
	 * @param target
	 * @return
	 */
	public static List<String> intersection(String source, String target) {
		List<String> slist = Arrays.asList(source.split(" "));
		List<String> tlist = Arrays.asList(target.split(" "));
		List<String> intersection = new ArrayList<String>();		
		
		for (String s: slist) {
			if (tlist.contains(s)) {
				if (!intersection.contains(s)) {
					intersection.add(s);
				}
			}
		}
		
		return intersection;
	}
	/**
	 * J(s,t) = 1 - intersection(s, t).size()) / (s.size() + t.size() - intersection.size())	 * 
	 * @param source
	 * @param target
	 */
	public static double Jaccard1(String source, String target) {
		List<String> slist = Arrays.asList(source.split(" "));
		List<String> tlist = Arrays.asList(target.split(" "));
		List<String> intersection = intersection(source, target);		
		
		return (double) 1 - intersection.size() / (double)(slist.size() + tlist.size() - intersection.size());
	}
	
	/**
	 * J(s,t) = 1 - 2 * intersection(s, t).size()) / (s.size() + t.size())
	 * @param source
	 * @param target
	 * @return
	 */
	public static double Jaccard2(String source, String target) {
		List<String> slist = Arrays.asList(source.split(" "));
		List<String> tlist = Arrays.asList(target.split(" "));
		List<String> intersection = intersection(source, target);			
		
		return (double) 1 - 2 * intersection.size() / (double)(slist.size() + tlist.size());
	}
	
	/**
	 * J(s,t) each token has weight value.
	 * @param stringList
	 * @param token
	 * @return
	 */
	public static void JaccardWeight(List<String> stringList) {
		Map<String, Integer> freqMap = new HashMap<String, Integer>();
		
		for (String string : stringList) {
			List<String> slist = Arrays.asList(string.split(" "));
			for (String s : slist)  {
				s = s.trim();
				if (freqMap.containsKey(s)) {
					freqMap.put(s, freqMap.get(s)+1);
				} else {
					freqMap.put(s, 1);
				}
			}
		}
		
		for (String key : freqMap.keySet()) {
			int freq = freqMap.get(key);
			double weight = (double) 1 / (Math.log(freq) + 1);
			weightMap.put(key, weight);
		}		
//		return weightMap;		
	}
	
	
	public static double Jaccard3(String source, String target) {
		List<String> slist = Arrays.asList(source.split(" "));
		List<String> tlist = Arrays.asList(target.split(" "));
		List<String> intersection = intersection(source, target);
		
		double intersectionWeight = 0;
		double sourceWeight = 0;
		double targetWeight = 0;
		for (String s : intersection) {
			intersectionWeight += weightMap.get(s);
		}
		for (String s : slist) {
			sourceWeight += weightMap.get(s);
		}
		for (String s: tlist) {
			targetWeight += weightMap.get(s);
		}
		
		return 1 - 2 * intersectionWeight / (sourceWeight + targetWeight);
	}
	
	
	//main
	public static void main(String[] args) {
		String s1 = "AAE HOLDING";
		String s2 = "AAE TECHNOLOGY INTERNATIONAL";
		String s3 = "AGRIPA HOLDING";
		System.out.println("J1(s1, s2) = " + Jaccard1(s1, s2));
		System.out.println("J1(s1, s3) = " + Jaccard1(s1, s3));
		System.out.println("J1(s2, s3) = " + Jaccard1(s2, s3));

		System.out.println();
		System.out.println("J2(s1, s2) = " + Jaccard2(s1, s2));
		System.out.println("J2(s1, s3) = " + Jaccard2(s1, s3));
		System.out.println("J2(s2, s3) = " + Jaccard2(s2, s3));
		
		System.out.println();
		List<String> stringList = new ArrayList<String>();
		Collections.addAll(stringList, s1, s2, s3);
		JaccardWeight(stringList);
		System.out.println(weightMap);
		System.out.println("J3(s1, s2) = " + Jaccard3(s1, s2));
		System.out.println("J3(s1, s3) = " + Jaccard3(s1, s3));
		System.out.println("J3(s2, s3) = " + Jaccard3(s2, s3));
	}

}


Output:
J1(s1, s2) = 0.75
J1(s1, s3) = 0.6666666666666667
J1(s2, s3) = 1.0

J2(s1, s2) = 0.6
J2(s1, s3) = 0.5
J2(s2, s3) = 1.0

{AAE=0.5906161091496412, TECHNOLOGY=1.0, AGRIPA=1.0, INTERNATIONAL=1.0, HOLDING=0.5906161091496412}
J3(s1, s2) = 0.6868293431358082
J3(s1, s3) = 0.5738467337473576
J3(s2, s3) = 1.0



  • 大小: 1.4 KB
  • 大小: 1.4 KB
  • 大小: 1.7 KB
分享到:
评论

相关推荐

    关于文本相似度计算-JaccardSimilarity和哈希签名函数.pdf

    计算文本相似度的JaccardSimilarity和哈希签名函数 文本相似度计算是自然语言处理和信息检索领域中的一个重要研究方向,它可以应用于过滤相似度很高的新闻、考试防作弊系统、论文抄袭检查等多个领域。文本相似度...

    Naxi sentence similarity calculation based on improved chunking edit-distance

    Aiming at the characteristics of Naxi language, a method is proposed for Naxi sentence similarity calculation. First, according to the characteristics of Naxi language that verbs set back, and nouns ...

    Text similarity calculation method based on ontology model

    - **Hybrid Word Similarity Calculation (HWSC) 方法**:这是一种混合词相似度计算方法,结合了HowNet(汉语知识库)和TongYiCi CiLin(同义词词林)两种资源。 - **HowNet**:提供了丰富的语义信息,包括词汇的...

    关于文本相似度计算-JaccardSimilarity和哈希签名函数.docx

    文本相似度计算-JaccardSimilarity 和哈希签名函数 文本相似度计算是目前信息时代非常重要的一种技术,它可以应用于过滤掉大量相似的新闻、考试防作弊系统、论文抄袭检查等领域。文本相似度计算的方法有很多,主要...

    Indexing for Subtree Similarity-Search using Edit Distance

    Sara Cohen在其2013年发表于SIGMOD的文章中,探讨了如何使用树编辑距离(Tree Edit Distance)来解决大规模树集合中的子树相似性搜索问题。 首先,文章定义了树编辑距离。在比较树结构时,编辑距离是指将一棵树转换...

    An Integrated Item Similarity Calculation Method for Collaborative Filtering

    推荐系统是互联网技术发展的一个重要分支,它通过智能分析用户行为和偏好,为用户主动提供个性化的信息和服务,极大提升了用户体验。在众多推荐系统技术中,协同过滤(Collaborative Filtering, CF)算法因其出色的...

    MapReduce在聚类算法中的应用论文.rar

    MapReduce在聚类算法中的应用论文.rarMapReduce在聚类算法中的应用论文.rarMapReduce在聚类算法中的应用论文.rarMapReduce在聚类算法中的应用论文.rarMapReduce在聚类算法中的应用论文.rarMapReduce在聚类算法中的...

    WordNet Similarity 词语相似度

    4. **Jaccard相似度(Jaccard Similarity)**:Jaccard相似度常用于比较集合的相似度,计算的是两个词汇的共享同义词集合与它们各自同义词集合并集的比例。 5. **Wu-Palmer相似度(Wu-Palmer Similarity)**:Wu-...

    Entropy of interval-valued fuzzy sets based on distance and its relationship with similarity measure

    First, we propose an axiomatic definition of entropy for IVFS based on distance which is consistent with the axiomatic definition of entropy of a fuzzy set introduced by De Luca, Termini and Liu....

    Similarity 文本比对程序java文本比较算法

    "Similarity.jar" 提供了三种经典的方法,方便开发者直接使用。下面将详细解释这些知识点: 1. **文本比对**:文本比对是指通过计算两个文本之间的相似度来确定它们的关联程度。在信息检索、自然语言处理、抄袭检测...

    wordnet::similarity

    3. **Jaccard相似度(Jaccard Similarity)**:基于两个词汇共享的同义词集合大小,计算它们的交集与并集的比例。 4. **Resnik相似度(Resnik Similarity)**:使用信息内容最大互信息(MI)的概念,找到两个词汇的...

    java-string-similarity-master.zip_similarity

    1. 各种字符串距离度量算法的实现,如Levenshtein距离、Jaccard相似度、Jaro-Winkler距离等。 2. 余弦相似度计算,可能包括对预处理后的词频向量进行处理的方法,如TF-IDF(词频-逆文档频率)转换。 3. 本体相关的...

    bioinformatic similarity mehtod using python(cosin, jaccard...)

    python 编写 cosin jaccard and smith-waterman函数

    Similarity V1.8.4 beta ...

    《Similarity V1.8.4 beta:音乐文件相似度检测工具详解》 在数字化的音乐世界里,我们经常遇到一个问题:如何有效地管理和查找重复或相似的音乐文件。"Similarity V1.8.4 beta"就是为此目的设计的一款强大工具,它...

    ngraph.jaccard:计算图上的jaccard相似度

    ngraph.jaccard 羽毛的鸟儿聚集在一起。 原来在我的编程生涯中激发我灵感的人也激发我去追随他们追随的其他人。 我们可以解决反向问题吗? 在我追随的所有人中,谁...// compute jaccard similarity for each conn

    text similarity

    在文本领域,这通常涉及词汇、短语、句子乃至整个文档的比较,通过计算如余弦相似度、Jaccard相似度、编辑距离等不同的相似度指标。 在“Text-Similarity-0.07”这个压缩包文件名中,我们可以推测这可能是一个特定...

    java-string-similarity

    各种字符串相似度和距离算法的实现:Levenshtein,Jaro-winkler,n-Gram,Q-Gram,Jaccard索引,最长公共子序列编辑距离,余弦相似度......

    PyPI 官网下载 | pytextdist-0.1.4.tar.gz

    similarity = jaccard_similarity(set1, set2) print(similarity) # 输出:0.5 ``` 此外,库中可能还包含了其他文本距离计算方法,如Hamming距离,它用于衡量两个相同长度字符串中对应位置的不同字符数。这些算法的...

    Candidate Label-aware Similarity Graph for Partial Label Data

    In this paper, we proposes a.candidate label-aware similarity graph constructing method for.partial label data which effectively combines candidate label.information using Jaccard distance and linear...

Global site tag (gtag.js) - Google Analytics