Я работаю над созданием инвертированного индекса для списка слов в java. В основном это создает список для каждого слова содержит индекс документа, который слово появляется на связанном с частотой слова в этом документе, желаемый результат должен быть такой:Не удалось добавить к карте деревьев, которая находится внутри другой карты (для создания инвертированного индекса)
[word1:[FileNo:frequency],[FileNo:frequency],[FileNo:frequency],word2:[FileNo:frequency],[FileNo:frequency]...etc]
Вот код:
package assigenment2;
import java.io.*;
import java.util.*;
public class invertedIndex {
public static Map<String, Map<Integer,Integer>> wordTodocumentMap;
public static BufferedReader buffer;
public static BufferedReader br;
public static BufferedReader reader;
public static List<String> files = new ArrayList<String>();
public static List<String>[] tokens;
public static void main(String[] args) throws IOException {
//read the token file and store the token in list
String tokensPath="/Users/Manal/Documents/workspace/Information Retrieval/tokens.txt";
int k=0;
String[] tokens = new String[8500];
String sCurrentLine;
try
{
FileReader fr=new FileReader(tokensPath);
BufferedReader br= new BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null)
{
tokens[k]=sCurrentLine;
k++;
}
System.out.println("the number of token are:"+k+" words");
br.close();
}
catch(Exception ex)
{System.out.println(ex);}
пока он работает правильно, я считаю, что проблема заключается в манипулировании вложенной карты в следующей части:
TreeMap<Integer,Integer> documentToCount = new TreeMap<Integer,Integer>();
//read files
System.out.print("Enter the path of files you want to process:\n");
Scanner InputPath = new Scanner(System.in);
String cranfield = InputPath.nextLine();
File cranfieldFiles = new File(cranfield);
for (File file: cranfieldFiles.listFiles())
{
int fileno = files.indexOf(file.getPath());
if (fileno == -1) //the current file isn't in the files list \
{
files.add(file.getPath());// add file to the files list
fileno = files.size() - 1;//the index of file will start from 0 to size-1
}
int frequency = 0;
BufferedReader reader = new BufferedReader(new FileReader(file));
for (String line = reader.readLine(); line != null; line = reader.readLine())
{
for (String _word : line.split(" "))
{
String word = _word.toLowerCase();
if (Arrays.asList(tokens).contains(word))
if (wordTodocumentMap.get(word) == null)//check whether word is new word
{
documentToCount = new TreeMap<Integer,Integer>();
wordTodocumentMap.put(word, documentToCount);
}
documentToCount.put(fileno, frequency+1);//add the location and frequency
}
}
}
reader.close();
}
}
ошибки я получаю:
Исключение в потоке "главный" java.lang.NullPointerException
в assigenment2.invertedIndex.main (invertedIndex.java:65)
Какая строка кода является линией 65? (Я спрашиваю, потому что трассировка стека относится к строке invertedIndex.java 65) –
Что в ваших входных файлах? –
Когда я смотрю на вопросы, которые вы задали в Stack Overflow, мне кажется, что вы выиграете от [Как задать хороший вопрос?] (Http://stackoverflow.com/help/how-to-ask) и также, пожалуйста, помните [Что делать, если кто-то отвечает на мой вопрос?] (http://stackoverflow.com/help/someone-answers). Благодарю. –