java.lang.NullPointerException输出术语频率逆文档频率(tfidf)矩阵java
我有这段代码,它为目录中每个文件中的所有单词输出tfidf。我正试图将其转换为一个矩阵,其中每一行对应于目录中的每个文件,每一列对应于文件中的所有单词,我在这方面遇到了一些困难,我需要一些帮助。 当我试图输出矩阵时,得到的是java.lang.NullPointerException。 值开始出现,但由于某种原因,它们停止并生成空错误 这是密码java.lang.NullPointerException输出术语频率逆文档频率(tfidf)矩阵java,java,file,matrix,hashmap,tf-idf,Java,File,Matrix,Hashmap,Tf Idf,我有这段代码,它为目录中每个文件中的所有单词输出tfidf。我正试图将其转换为一个矩阵,其中每一行对应于目录中的每个文件,每一列对应于文件中的所有单词,我在这方面遇到了一些困难,我需要一些帮助。 当我试图输出矩阵时,得到的是java.lang.NullPointerException。 值开始出现,但由于某种原因,它们停止并生成空错误 这是密码 public class TestTF_IDF { public static void main(String[] args) throws Uns
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
公共类TestTF\u IDF{
公共静态void main(字符串[]args)引发不受支持的DencodingException、FileNotFoundException{
//TfIdf的测试代码
TfIdf tf=新的TfIdf(“E:/Thesis/ThesisWork/data1”);
//包含正在处理的文件名
//字符串文件;
tf.buildAllDocuments();
int numDocuments=tf.documents.size();
双矩阵[][]=新的双矩阵[numDocuments][];
int documentIndex=0;
for(字符串文件:tf.documents.keySet())
{
//System.out.println(“文件\t”+文件);
Map myMap=
get(file).getF_tf_TFIDF();
int numWords=myMap.size();
矩阵[documentIndex]=新的双精度[numWords];
int-wordIndex=0;
for(字符串键:myMap.keySet())
{
Double[]value=myMap.get(key);
矩阵[documentIndex][wordIndex]=值[2];
wordIndex++;
//System.out.print(“file=“+file+”term=“+key+values[2]+”);
}
documentIndex++;
对于(int i=0;i虽然问题非常不清楚,但以下是我根据问题和评论试图猜测的内容
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()
{
}
}
import java.util.Map;
公共类TestTF_IDF
{
公共静态void main(字符串[]args)引发异常
{
TfIdf tf=新的TfIdf(“E:/Thesis/ThesisWork/data1”);
tf.buildAllDocuments();
int numDocuments=tf.documents.size();
Double[]矩阵[][]=新的Double[numDocuments][];
int documentIndex=0;
for(字符串文件:tf.documents.keySet())
{
System.out.println(“文件\t”+文件);
Map myMap=
get(file).getF_tf_TFIDF();
int numWords=myMap.size();
矩阵[documentIndex]=新的双精度[numWords][];
int-wordIndex=0;
for(字符串键:myMap.keySet())
{
Double[]value=myMap.get(key);
矩阵[documentIndex][wordIndex]=值;
wordIndex++;
}
documentIndex++;
}
}
}
类文档
{
公共地图getF_TF_TFIDF()
{
返回null;
}
}
类TfIdf
{
公共地图文件;
TfIdf(字符串s)
{
}
public void buildAllDocuments()
{
}
}
tf.documents
包含什么?特别是Double[]
数组是什么?(这些是包含频率的1元素数组吗?@Marco13 documents是一个树映射(TreeMap documents),包含在TFIdf类中声明的文档,tf是该类的对象(参见第5行)关于数组,只需忽略dfIdf数组,它已不再使用,而关于值数组是保存myMay值的数组。我很确定创建此矩阵很容易。但只要不清楚您拥有哪些数据(在哪个结构中),就可以那么tf.documents
将文件名映射到Document
?并且getF_tf_TFIDF
返回一个映射到…单词?到…什么的映射?你能告诉我我为输出矩阵所写的内容是正确的还是不正确的吗?谢谢,请闭上你的眼睛,还有imagi你是另一个人。然后睁开眼睛,阅读问题和代码,试着想象一下:这个问题的答案是什么?没有人知道你的数据结构,它们包含什么,以及你想要实现什么。非常感谢Marco 13。现在我有一些东西要修复并开始。我添加了你的代码,并打印了矩阵,它是rts输出第一个文件中某些单词的值,然后出现java.lang.NullPointerException。也许您可以编辑原始问题并提供有关异常的更多信息。