【发布时间】:2014-04-08 02:58:07
【问题描述】:
我有这段代码可以输出目录中每个文件中所有单词的 tfidf。我正在尝试将其传输到一个矩阵,其中每一行对应于目录中的每个文件,每一列对应于文件中的所有单词,我在做这件事时遇到了一些困难,我需要一些帮助。 当我尝试输出矩阵时,我得到的是 java.lang.NullPointerException。 这些值开始出现,但由于某种原因它们停止并生成 null 错误。
这是代码
public class TestTF_IDF {
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException{
//Test code for TfIdf
TfIdf tf = new TfIdf("E:/Thesis/ThesisWork/data1");
//Contains file name being processed
//String file;
tf.buildAllDocuments();
int numDocuments = tf.documents.size();
Double matrix[][] = new Double[numDocuments][];
int documentIndex = 0;
for (String file : tf.documents.keySet())
{
// System.out.println("File \t" + file);
Map<String, Double[]> myMap =
tf.documents.get(file).getF_TF_TFIDF();
int numWords = myMap.size();
matrix[documentIndex] = new Double[numWords];
int wordIndex = 0;
for (String key : myMap.keySet())
{
Double[] values = myMap.get(key);
matrix[documentIndex][wordIndex] = values[2];
wordIndex++;
//System.out.print("file="+ file+ "term=" +key + values[2]+" ");
}
documentIndex++;
for(int i=0; i<numDocuments;i++){
for(int j=0; j<numWords;j++){
System.out.print("file="+ file+ matrix[i][j]+ " "); //error here
}
}
}
}//public static void main(String[] args)
}//public class TestTF_IDF
任何想法。谢谢
【问题讨论】:
-
tf.documents包含什么?特别是,Double[]数组是什么? (这些是包含频率的 1 元素数组吗?) -
@Marco13 文档是一个 TreeMap (TreeMap
文档),其中包含在 TFIdf 类中声明的文档,而 tf 是该类的一个对象(参见第 5 行),关于数组只是忽略dfIdf 数组不再使用,关于 values 数组是保存 myMay 值的数组。 -
我很确定创建这个矩阵很容易。但只要不清楚您拥有哪些数据(在哪个结构中),以及哪些数据应该包含在矩阵中,就可以了。所以
tf.documents将文件名映射到Document?而getF_TF_TFIDF返回一个映射...单词的映射?到...什么? -
你能告诉我我为输出矩阵写的内容是否正确。谢谢
-
请闭上你的眼睛,想象一下你是另一个人。然后睁开眼睛,阅读问题和代码,并尝试想象:这个问题的答案是什么?。没有人知道您的数据结构、它们包含的内容以及您想要实现的目标。
标签: java file matrix hashmap tf-idf