Как получить доступ / изменить ключ во вложенной хэш-карте - PullRequest
0 голосов
/ 03 марта 2019

Я создаю инвертированный (позиционный) индекс для класса поиска информации, и у меня возникли проблемы с моими вложенными хэш-картами.Как мне обновить ключ tempMap, чтобы отразить частоты слова, которое будет сохранено во «внутреннем» и в конечном итоге «в инвертированном индексе»?Я думаю, что я правильно заполняю часть значения 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...