文档的词频-反向文档频率(TF-IDF)计算
来源:互联网 发布:重庆忽米网络 编辑:程序博客网 时间:2024/04/29 13:27
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
- 文档的词频-反向文档频率(TF-IDF)计算
- 文档的词频-反向文档频率(TF-IDF)计算
- 文档的词频-反向文档频率(TF-IDF)计算
- Python 评估字词在文件集的重要程度 (文档频率和逆向文档频率 TF-IDF)
- Spark词频-逆文档频率
- python 分词计算文档TF-IDF值并排序
- spark mllib 中的tf-idf算法计算文档相似度
- NLP计算文档相似度之TF-IDF
- TF-IDF(文档向量化)
- [转]搜索引擎的文档相关性计算和检索模型(BM25/TF-IDF)
- 分享自用小工具:TF-IDF计算文档相似性的python实现
- 文本词频TF-IDF
- TF-IDF与文档相似度的应用
- 使用tf*idf实现对文档集合的检索
- TF-IDF 的计算二
- 使用TF-IDF进行文档分类
- 计算分词的Tf-idf值
- 计算分词的tf*idf算法
- Ext is not defined
- 如何制作一个横版格斗过关游戏 Cocos2d-x 2.0.4
- JNI byteArray 传递与返回
- 你需要知道的三个CSS技巧
- SeIDEReleaseNotes
- 文档的词频-反向文档频率(TF-IDF)计算
- Eclipse Java EE IDE中jsp页面编码修改
- JNI高级教程之数据类型转换
- padding和margin的区别?
- HTML 学习手册(常用的标签)
- PKU-1724(DFS + 剪枝)
- 《MySQL必知必会》学习笔记
- 用TeeChart时遇到未能找到类型或命名空间名称“Steema”(是否缺少 using 指令或程序集引用?)
- Solr连接数据库配置