How to Normalize word frequencies of document in Weka

限于喜欢 提交于 2019-12-24 09:17:22

问题


In Weka, class StringToWordVector defines a method called setNormalizeDocLength. It normalizes word frequencies of a document. My questions are:

  1. what is meant by "normalizing word frequency of a document"?
  2. How Weka does this?

A practical example will help me best. Thanks in advance.


回答1:


Looking in the Weka source, this is the method that does the normalising:

private void normalizeInstance(Instance inst, int firstCopy) throws Exception 
{
    double docLength = 0;

    if (m_AvgDocLength < 0) 
    {
        throw new Exception("Average document length not set.");
    }

    // Compute length of document vector
    for(int j=0; j<inst.numValues(); j++) 
    {
        if(inst.index(j)>=firstCopy) 
        {
            docLength += inst.valueSparse(j) * inst.valueSparse(j);
        }
    }     
    docLength = Math.sqrt(docLength);

    // Normalize document vector
    for(int j=0; j<inst.numValues(); j++) 
    {
        if(inst.index(j)>=firstCopy) 
        {
            double val = inst.valueSparse(j) * m_AvgDocLength / docLength;
            inst.setValueSparse(j, val);
            if (val == 0)
            {
                System.err.println("setting value "+inst.index(j)+" to zero.");
                j--;
            }
        }
    }
}

It looks like the most relevant part is

double val = inst.valueSparse(j) * m_AvgDocLength / docLength;
inst.setValueSparse(j, val);

So it looks like the normalisation is value = currentValue * averageDocumentLength / actualDocumentLength.



来源:https://stackoverflow.com/questions/12155550/how-to-normalize-word-frequencies-of-document-in-weka

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!