【问题标题】:java.lang.NullPointerException output term frequency-inverse document frequency (tfidf) matrix javajava.lang.NullPointerException 输出词频-逆文档频率(tfidf)矩阵java
【发布时间】: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


【解决方案1】:

虽然这个问题非常不清楚,但这是我根据问题和 cmets 尝试猜测的内容。

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()
    {
    }
}

【讨论】:

  • 非常感谢 Marco 13.. 现在我有一些东西要修复并开始。我添加了您的代码并打印了矩阵,它开始输出第一个文件的某些单词的值,然后出现 java.lang.NullPointerException。
  • 也许您可以编辑原始问题并提供有关异常的更多信息。
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 2015-11-23
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多