我正在为信息检索类创建一个倒置的(位置)索引,并且在我的嵌套Hashmap上遇到了麻烦。如何更新tempMap的键以反映将存储在“内部”并最终存储在“ invertedIndex”中的单词的频率?我认为我正确地填充了tempMap的值部分,但是如果该单词已经在内部,我不知道如何更新else语句以增加该单词的频率。任何帮助将不胜感激!
//inverted index nested hashmap
HashMap<String, HashMap<String, HashMap<Integer, ArrayList<Integer>>>> invertedIndex = new HashMap<String, HashMap<String, HashMap<Integer, ArrayList<Integer>>>>();
//inner hashmap that stores words, their frequencies & their positions in a doucment
HashMap<String, HashMap<Integer, ArrayList<Integer>>> inner = new HashMap<String, HashMap<Integer, ArrayList<Integer>>>();
//stores info for map nested inside "inner"
HashMap<Integer, ArrayList<Integer>> tempMap = new HashMap<Integer, ArrayList<Integer>>();
int pos = 0;
// process one file at a time
for (int index = 0; index < fileCount; index++){
// open the input file, read one line at a time, extract words
// in the line, extract characters in a word, write words and
// character counts to disk files
try {
// get a BufferedReader object, which encapsulates
// access to a (disk) file
br = new BufferedReader(new FileReader(inputFileNames[index]));
// as long as we have more lines to process, read a line
// the following line is doing two things: makes an assignment
// and serves as a boolean expression for while test
while ((line = br.readLine()) != null) {
// process the line by extracting words using the wordPattern
wordMatcher = wordPattern.matcher(line);
// process one word at a time
while ( wordMatcher.find() ) {
// extract the word
word = line.substring(wordMatcher.start(), wordMatcher.end());
word = word.toLowerCase(); //convert to lowercase
pos++;
//use Stemmer class to stem word & convert to lowercase
porterStemmer.stemWord(word);
//add words to inner hashmap and increment frequency
if (!inner.containsKey(word)) {
ArrayList<Integer> ints = new ArrayList<Integer>();
ints.add(pos);
tempMap.put(1, ints);
inner.put(word, tempMap);
}
else
{
ArrayList<Integer> ints = new ArrayList<Integer>();
ints.add(pos);
tempMap.put(inner.get(word) + 1, ints);
inner.put(word, tempMap);
}
} // end one word at a time while
} // end outer while
pos = 0;
//add inner to invertedIndex
invertedIndex.put(inputFileNames[index], inner);
tempMap = new HashMap<Integer, ArrayList<Integer>>(); //create new HashMap
} // end try