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

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

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

相关文章
|
1月前
|
自然语言处理 Python
【Python自然语言处理】文本向量化的六种常见模型讲解(独热编码、词袋模型、词频-逆文档频率模型、N元模型、单词-向量模型、文档-向量模型)
【Python自然语言处理】文本向量化的六种常见模型讲解(独热编码、词袋模型、词频-逆文档频率模型、N元模型、单词-向量模型、文档-向量模型)
416 0
|
1月前
TF-IDF 怎样将用单词权重的向量表示一个文档
TF-IDF 怎样将用单词权重的向量表示一个文档
34 1
|
存储 算法 PyTorch
pytorch 给定概率分布的张量,如何利用这个概率进行重复\不重复采样?
在 PyTorch 中,可以使用 torch.distributions.Categorical 来基于给定的概率分布进行采样。
726 0
|
10月前
|
机器学习/深度学习 存储 算法
最邻近规则分类 KNN (K-Nearest Neighbor)算法及python实现
最邻近规则分类 KNN (K-Nearest Neighbor)算法及python实现
103 0
|
10月前
|
自然语言处理 算法 搜索推荐
TF-IDF、TextRank关键字抽取排序算法
TF-IDF称为词频逆文本,结果严重依赖文本分词之后的效果。其公式又可以分成词频(Term Frequency,TF)的计算和逆文档概率(IDF)的计算。
109 0
|
11月前
|
数据挖掘
ENVI:分类后处理_小斑块去除_Majority/Minority处理、聚类处理、过滤处理等
ENVI:分类后处理_小斑块去除_Majority/Minority处理、聚类处理、过滤处理等
826 0
|
算法 Windows
【文本分类】基于类信息的TF-IDF权重分析与改进
【文本分类】基于类信息的TF-IDF权重分析与改进
295 0
【文本分类】基于类信息的TF-IDF权重分析与改进
|
PyTorch 算法框架/工具
pytorch 张量,去掉最后一个维度
可以使用 PyTorch 中的 .squeeze() 函数来去掉张量中大小为 1 的维度。如果要删除最后一个维度,可以指定参数 dim=-1,即对最后一个维度进行处理。下面是示例代码:
1123 0
向量检索/向量相似性计算方法(持续更新ing...)
本文介绍各种用于向量检索的向量相似性计算方法,将会简单介绍各种方法的优缺点等信息,并用toy example给出代码示例。
向量检索/向量相似性计算方法(持续更新ing...)