`

TF-IDF[2]

ESA 
阅读更多

package com.sap.research.semantic;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.print.attribute.standard.OutputDeviceAssigned;

import cc.mallet.types.Vector;
import cc.mallet.util.CommandOption.Set;

import com.sap.research.util.Pair;

public class SemanticInterpretor {
	private InvertedIndex invertedIndex;

	public SemanticInterpretor(InvertedIndex invertedIndex) {
		this.invertedIndex = invertedIndex;
	}

	// vector representation for a document based on concepts in DB
	public HashMap<String, Double> vectorRepreforDoc(File file) {
		HashMap<String, Pair<String, Integer>> docVec = invertedIndex
				.termCount(file);
		// vector representation for a document, only term frequency considered
		// HashMap<term, frequency>
		HashMap<String, Integer> vec = new HashMap<String, Integer>();
		for (Map.Entry<String, Pair<String, Integer>> entry : docVec.entrySet()) {
			vec.put(entry.getKey(), entry.getValue().second);
		}

		// semantical vector representation for a document
		// HashMap<concept, relatedness>
		HashMap<String, Double> semanticVec = new HashMap<String, Double>();
		// vector representation based on concepts
		HashMap<String, ArrayList<Pair<String, Double>>> indices = invertedIndex
				.getInvertedIndicesDouble();
		for (Map.Entry<String, Integer> entry : vec.entrySet()) {
			if (indices.keySet().contains(entry.getKey())) {
				// if certain word is contained in the invertedIndex
				ArrayList<Pair<String, Double>> pairList = indices.get(entry
						.getKey());
				for (Pair<String, Double> pair : pairList) {
					double relatedness = entry.getValue() * pair.second;
					if (!semanticVec.keySet().contains(pair.first)) {
						semanticVec.put(pair.first, relatedness);
					} else {
						semanticVec.put(pair.first, semanticVec.get(pair.first)
								+ relatedness);
					}

				}
			} else {
				// if certain word is not contained in the invertedIndex
				// simply ignore?
			}
		}
		output(semanticVec);
		return semanticVec;
	}

	private void output(HashMap<String, Double> semanticVec) {
		System.out.println(semanticVec);
	}

	public double semanticalSimilarity(HashMap<String, Double> docVec1,
			HashMap<String, Double> docVec2) {
		ArrayList<String> base = new ArrayList<String>();
		for (Map.Entry<String, Double> entry : docVec1.entrySet()) {
			base.add(entry.getKey());
		}
		for (Map.Entry<String, Double> entry : docVec2.entrySet()) {
			if (!base.contains(entry.getKey())) {
				base.add(entry.getKey());
			}
		}

		HashMap<String, Double> docVec1Std = new HashMap<String, Double>();
		ArrayList<Double> docVec1Double = new ArrayList<Double>();
		HashMap<String, Double> docVec2Std = new HashMap<String, Double>();
		ArrayList<Double> docVec2Double = new ArrayList<Double>();

		for (String string : base) {
			if (docVec1.keySet().contains(string)) {
				docVec1Std.put(string, docVec1.get(string));
				docVec1Double.add(docVec1.get(string));
			} else {
				docVec1Std.put(string, 0.0);
				docVec1Double.add(0.0);
			}

			if (docVec2.keySet().contains(string)) {
				docVec2Std.put(string, docVec2.get(string));
				docVec2Double.add(docVec2.get(string));
			} else {
				docVec2Std.put(string, 0.0);
				docVec2Double.add(0.0);
			}
		}
		
		output(docVec1Std);
		output(docVec2Std);

		double similarity = 0.0;
		if (docVec1Double.size() == docVec2Double.size()) {
			double prod = 0.0;
			double doc1Sqr = 0.0;
			double doc2Sqr = 0.0;
			for (int i = 0; i < docVec1Double.size(); i++) {
				prod += docVec1Double.get(i) * docVec2Double.get(i);
				doc1Sqr += docVec1Double.get(i) * docVec1Double.get(i);
				doc2Sqr += docVec2Double.get(i) * docVec2Double.get(i);
			}

			similarity = prod / (Math.sqrt(doc1Sqr) * Math.sqrt(doc2Sqr));
		} else {
			System.err.println("cannot compare vectors of different length!");
		}
		return similarity;
	}

	public double computeSemanticRelatedness(File fileA, File fileB) {
		// invertedIndex.addIndices(dirPath, filePath, recursive)
		return semanticalSimilarity(vectorRepreforDoc(fileA),
				vectorRepreforDoc(fileB));
	}

	public static void main(String[] args) {
		String propertyFilePath = "settings/html.properties";
		InvertedIndex invertedIndex = new InvertedIndex(propertyFilePath);
		invertedIndex.addIndices("test/input/html_20", "html", true);
		SemanticInterpretor interpretor = new SemanticInterpretor(invertedIndex);
		File fileA = new File("test/input/html/topic1.html");
		File fileB = new File("test/input/html/topic2.html");
		double similarity = interpretor
				.computeSemanticRelatedness(fileA, fileB);
		System.out.println("Semantic relatedness of " + fileA.getName()
				+ " and " + fileB.getName() + " is: " + similarity);

	}

}
 
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    tf-idf_tf-idf_

    2. **逆文档频率(Inverse Document Frequency, IDF)**: IDF是通过计算包含特定词的文档数量的倒数来降低常见词汇的权重。公式为IDF(w) = log(N/df(w)),其中N是总文档数,df(w)是包含词w的文档数。如果一个词在很多...

    TF-IDF与余弦相似性的应用

    TF-IDF与余弦相似性的应用 TF-IDF(Term Frequency-Inverse Document Frequency)是一种常用的关键词提取算法,应用于自动关键词提取、信息检索等领域。该算法的优点是简单快速,结果比较符合实际情况。TF-IDF 算法...

    LDA和TF-IDF算法的相关论文

    《LDA与TF-IDF算法:深度探讨与应用》 在信息检索和自然语言处理领域,LDA(Latent Dirichlet Allocation)和TF-IDF(Term Frequency-Inverse Document Frequency)是两种至关重要的算法,它们在文本分析、文档分类...

    Using_TF-IDF_to_Determine_Word_Relevance_in_Document_Queries

    ### 使用TF-IDF确定文档查询中的词相关性 在当今数据驱动的世界中,从大量文本信息中高效地检索相关信息是一项至关重要的技能。本文探讨了如何应用TF-IDF(Term Frequency-Inverse Document Frequency)来确定文档...

    NLP:基于TF-IDF的中文关键词提取.zip

    在这个主题中,"NLP:基于TF-IDF的中文关键词提取"是一个项目,它利用TF-IDF算法来从中文文本中提取具有代表性的关键词。TF-IDF是一种经典的文本特征权重计算方法,广泛应用于信息检索、文档分类和关键词提取等领域...

    基于TF-IDF算法抽取

    ### 基于TF-IDF算法抽取文章关键词 #### 一、引言 TF-IDF(Term Frequency-Inverse Document Frequency)是一种广泛应用于信息检索与文本挖掘领域的统计方法,用于评估单词对于一个文档集或者语料库中单个文档的...

    基于特定语料库的TF-IDF的中文关键词提取

    在"tf-idf-keyword-master"这个压缩包文件中,很可能包含了实现TF-IDF关键词提取的代码框架或者示例。用户可能需要进一步了解代码结构,学习如何加载特定语料库,如何进行预处理,如何计算TF-IDF值,以及如何输出和...

    C语言、Python实现TF-IDF算法

    2. **逆文档频率(Inverse Document Frequency, IDF)**:IDF用来衡量一个词的普遍性,即这个词在多少文档中出现。如果一个词在很多文档中都出现,那么它的IDF值较低,因为它不太能区分文档;反之,如果只在少数文档中...

    TF-IDF.zip_TF-IDF java_java tf idf_tf idf_tf-idf

    TF-IDF(Term Frequency-Inverse Document Frequency)是一种在信息检索和自然语言处理中广泛使用的统计方法,用于评估一个词在文档集合中的重要性。在Java编程环境下,TF-IDF可以帮助我们提取文本的关键信息,理解...

    TF-IDF.py.zip_TF-IDF WEIGHT_tf-idf_tf_idf_特征提取

    TF-IDF(Term Frequency-Inverse Document Frequency)是一种在信息检索和自然语言处理中广泛使用的文本特征表示方法。它能够衡量一个词在文档中的重要性,适用于文档集合的特征提取,帮助我们理解文档的主题和区分...

    基于TF-IDF文本向量化的SQL注入攻击检测.pdf

    基于 TF-IDF 文本向量化的 SQL 注入攻击检测 SQL 注入攻击是最常见的 Web 应用程序攻击手段,利用机器学习检测 SQL 注入攻击已成为一种趋势。该论文提出了基于 TF-IDF 文本向量化的 SQL 注入攻击检测方法,旨在提高...

    基于Python实现TF-IDF矩阵表示(人工智能实验)【100011921】

    TF-IDF(Term Frequency-Inverse Document Frequency)是一种在信息检索和文本挖掘领域广泛使用的权重计算方法,用于评估一个词在文档中的重要性。这个概念基于两个原则:词频(Term Frequency, TF)和逆文档频率...

    tf-idf.zip_Information Retrival_python IR_python TF-IDF_tf-idf

    2. **逆文档频率(Inverse Document Frequency, IDF)**:反映了一个词在整个文档集合中的稀有程度。IDF是所有文档数除以包含该词的文档数,再取对数。如果一个词在很多文档中都出现,其IDF值会较低;反之,如果只在...

    TF-IDF.zip_tf-idf_tfidf

    2. **搜索引擎排名**:搜索引擎会利用TF-IDF来决定搜索结果的相关性,高TF-IDF值的词更可能与查询相关。 3. **信息检索**:在海量文档中查找相关信息时,TF-IDF有助于找出最相关的文档。 4. **文本摘要**:通过识别...

    基于改进TF-IDF算法的牛疾病智能诊断系统.pdf

    2. 将改进的TF-IDF算法应用于牛疾病诊断系统中,提高了疾病诊断的准确率和可信度。 3. 证明了改进的TF-IDF算法在疾病诊断领域中的应用价值和前景。 本文的研究结果对智能诊断系统和疾病诊断领域具有重要的贡献和...

    关键词提取TF-IDF算法综述

    TF-IDF算法,即词频-逆文档频率(Term Frequency-Inverse Document Frequency)算法,是关键词提取中最常用的方法之一。该算法综合了词频(TF)和逆文档频率(IDF)两个因子来评估词汇在文档集合中的重要性。 在...

    TF-IDF.rar_TF-IDF algorithm

    2. **文本分类和聚类**:TF-IDF可以作为特征向量,用于区分不同类别的文本,帮助实现文本分类和聚类。 3. **关键词提取**:通过计算TF-IDF值,可以找出文档中的重要关键词,有助于摘要生成和文档理解。 4. **推荐...

    使用python进行朴素贝叶斯的数据分析,使用TF-IDF方法整理数据

    2. **创建词汇表**:使用`TfidfVectorizer`构建词汇表,转换文本数据为TF-IDF向量。 3. **训练模型**:使用`sklearn.naive_bayes.GaussianNB`(或其他类型的朴素贝叶斯分类器)训练模型,输入是TF-IDF向量,输出是...

    GetFileTimes.rar_IF-IDF_TF_java TF-IDF_tf idf_tf idf java

    2. 计算词频(Term Frequency, TF):统计每个词在文档中出现的次数。 3. 计算逆文档频率(Inverse Document Frequency, IDF):估算词的普遍性,反比于包含该词的文档数量。 4. 计算TF-IDF值:将词频与逆文档频率...

Global site tag (gtag.js) - Google Analytics