How to Normalize word frequencies of document in W

2019-08-11 23:30发布

问题:

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.