我有这段代码,可以为目录中每个文件中的所有单词输出tfidf。我正在尝试将其传输到一个矩阵中,其中每一行对应于目录中的每个文件,每一列对应于文件中的所有单词,我这样做有些困难,我需要一些帮助。当我尝试输出矩阵时,我得到的是一个java.lang.NullPointerException。这些值开始出现,但由于某种原因,它们停止并生成空错误。
这是代码
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
任何想法。谢谢
虽然这个问题非常不清楚,但这是我试图根据问题和评论猜测的。
import java.util.Map;
public class TestTF_IDF
{
public static void main(String[] args) throws Exception
{
TfIdf tf = new TfIdf("E:/Thesis/ThesisWork/data1");
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;
wordIndex++;
}
documentIndex++;
}
}
}
class Document
{
public Map<String, Double[]> getF_TF_TFIDF()
{
return null;
}
}
class TfIdf
{
public Map<String, Document> documents;
TfIdf(String s)
{
}
public void buildAllDocuments()
{
}
}