代码整个执行流程
- 参数设置
- input数据,设置数据格式
- batchFinished(),处理数据(Tokenzier,Stemming,Stopwords)
- determineDictionary(); 统计计算(TF,IDF)
- 归一化
- output
一些变量和方法的作用
- m_Dictionary , m_DocsCounts 变量与 m_OutputCounts变量 意义
public TreeMap m_Dictionary = new TreeMap(); //TreeMap类型成员变量m_Dictionary 记录<word,新属性index>对;具体类型为TreeMap<String,Integer>, 每一个String 的word所映射的index。
private int[] m_DocsCounts ; // 计算每个单词在多少个文档中出现过,保存在这个数组。数组index是word对应映射的index(与m_Dictionary对应)。
private boolean m_OutputCounts = false; //控制m_Dictionary 中的Integer是输出0/1(表示单词是否在文档中出现),还是count单词在文档中出现的次数。 一般要将它设置为true。
- m_minTermFreq 和 m_WordsToKeep
根据最小词频数(
m_minTermFreq
)和每个类最多保留单词数(
m_WordsToKeep
)过滤单词;另一种过滤单词的方法是通过stopwordlist,见下面。
- 内部类Count 类及变量的意义
里有两个变量public int count, docCount;,count是word在一个文档中出现的次数,docCount指的是这个word在几个文档中出现过。
定义这个内部类及变量计算中间量,主要是为了计算TF*IDF方便。
- 修改具体的TF*IDF公式:
在函数convertInstancewoDocNorm里面如下的两段代码中修改即可。
// Doing TFTransform
if (m_TFTransform == true) {
.....//在这里修改, 源代码为 tf(t,d)= log( f(t,d) +1 ).加一是为了防止tf(t,d)=0
}
// Doing IDFTransform
if (m_IDFTransform == true) {
.....//在这里修改, 源代码是让idf(t,D)= log( |D| / |{d \in D : t\ind}| ). D表示所有的文档集。
log(value)中的value肯定大于等于1
//当然这里需保证分母不为0,即word至少在一个文档中出现过,否则可
idf(t,D)=
log
( |D| / |{d \in D : t\ind}|+1 ).
....// 源代码直接让val (t,d)= tf(t,d) * log( |D| / |{d \in D : t\ind