文档的词频-反向文档频率(TF-IDF)计算

简介: TF-IDF计算: TF-IDF反映了在文档集合中一个单词对一个文档的重要性,经常在文本数据挖据与信息 提取中用来作为权重因子。在一份给定的文件里,词频(termfrequency-TF)指的是某一 个给定的词语在该文件中出现的频率。

TF-IDF计算:

TF-IDF反映了在文档集合中一个单词对一个文档的重要性,经常在文本数据挖据与信息

提取中用来作为权重因子。在一份给定的文件里,词频(termfrequency-TF)指的是某一

个给定的词语在该文件中出现的频率。逆向文件频率(inversedocument frequency,

IDF)是一个词语普遍重要性的度量。某一特定词语的IDF,可以由总文件数目除以包含

该词语之文件的数目,再将得到的商取对数得到。

相关代码:

	private static Pattern r = Pattern.compile("([ \\t{}()\",:;. \n])"); 
	private static List<String> documentCollection;

    //Calculates TF-IDF weight for each term t in document d
    private static float findTFIDF(String document, String term)
    {
        float tf = findTermFrequency(document, term);
        float idf = findInverseDocumentFrequency(term);
        return tf * idf;
    }

    private static float findTermFrequency(String document, String term)
    {
    	int count = getFrequencyInOneDoc(document, term);

        return (float)((float)count / (float)(r.split(document).length));
    }
    
    private static int getFrequencyInOneDoc(String document, String term)
    {
    	int count = 0;
        for(String s : r.split(document))
        {
        	if(s.toUpperCase().equals(term.toUpperCase())) {
        		count++;
        	}
        }
        return count;
    }


    private static float findInverseDocumentFrequency(String term)
    {
        //find the  no. of document that contains the term in whole document collection
        int count = 0;
        for(String doc : documentCollection)
        {
        	count += getFrequencyInOneDoc(doc, term);
        }
        /*
         * log of the ratio of  total no of document in the collection to the no. of document containing the term
         * we can also use Math.Log(count/(1+documentCollection.Count)) to deal with divide by zero case; 
         */
        return (float)Math.log((float)documentCollection.size() / (float)count);

    }
建立文档的向量空间模型Vector Space Model并计算余弦相似度。

相关代码:

public static float findCosineSimilarity(float[] vecA, float[] vecB)
{
    float dotProduct = dotProduct(vecA, vecB);
    float magnitudeOfA = magnitude(vecA);
    float magnitudeOfB = magnitude(vecB);
    float result = dotProduct / (magnitudeOfA * magnitudeOfB);
    //when 0 is divided by 0 it shows result NaN so return 0 in such case.
    if (Float.isNaN(result))
        return 0;
    else
        return (float)result;
}

public static float dotProduct(float[] vecA, float[] vecB)
{

    float dotProduct = 0;
    for (int i = 0; i < vecA.length; i++)
    {
        dotProduct += (vecA[i] * vecB[i]);
    }

    return dotProduct;
}

// Magnitude of the vector is the square root of the dot product of the vector with itself.
public static float magnitude(float[] vector)
{
    return (float)Math.sqrt(dotProduct(vector, vector));
}
注意点

零词过滤(stop-words filter)

零词列表

ftp://ftp.cs.cornell.edu/pub/smart/english.stop

关于TF-IDF参考这里:

链接–> http://en.wikipedia.org/wiki/Tf*idf


目录
相关文章
|
JavaScript
esModuleInterop 是如何影响 tsc 的
本文尝试从 CommonJS 和 ES Module 两者规范的差异,详细说明该配置是如何影响 tsc 编译结果的。
385 0
|
2天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
13天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1286 5
|
12天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1318 87
|
1天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
175 82
2025年阿里云域名备案流程(新手图文详细流程)