TFCalculator.java
package edu.odu.cs.cs350;
import java.util.HashMap;
import java.util.Map;
/**
* Calculates term frequency (TF) for words in a given document.
*
* TF(w, d) = (Number of times word w appears in document d) / (Total number of words in d)
*/
public class TFCalculator {
/**
* Computes the term frequency map for a document.
*
* @param document The document to calculate term frequencies for.
* @return A map of words to their TF values.
*/
public static Map<String, Double> computeTF(Document document) {
Map<String, Double> termFrequencyMap = new HashMap<>();
int totalWords = document.getTotalWordCount();
for (Map.Entry<String, Word> entry : document.getWords().entrySet()) {
String word = entry.getKey();
int count = entry.getValue().getCount();
double termFrequency = (double) count / totalWords;
termFrequencyMap.put(word, termFrequency);
}
return termFrequencyMap;
}
}