Domanda

I have a public class named "InvertedIndex" which has two private data members:

private HashMap<String, ArrayList<Integer>> invertedList;
private ArrayList<String> documents;

I have generated getter and setter functions for them. I have a function named "buildFromTextFile" which fills both of the data members, and I have another function called "processQuery". I wrote a test unit in class "InvertedIndexTest" for processQuery which is as follows:

@Test
public void testProcessQuery() throws IOException{
    InvertedIndex invertedIndex = new InvertedIndex();
    String query = "ryerson award";
    ArrayList<String> expectedResult = new ArrayList<String>();
    expectedResult.add("ryerson award ??..23847");
    invertedIndex.buildFromTextFile("input.tsv");
    ArrayList<String> result = processQuery(query, 5);
    Assert.assertEquals(expectedResult, result);        
}

In this function, in debugging mode, when the function "buildFromTextFile" is called, the code will go to the class "InvertedIndex" and fill the data members, so at the end of this function the data members has correct data in them. When the running comes back to this unit test function again if I watch invertedIndex.getInvertedList().ToString(), I can see that the data is still correct. Then the function processQuery is called, and when the running goes to the "InvertedIndex" class, and inside this function, the invertedList().ToString() is empty. It seems that all the data is lost somewhere, but I don't know where. I would appreciate your help.

This is the processQuery method:

public ArrayList<String> processQuery(String query, int k){
    ArrayList<String> result = new ArrayList<String>();
    ArrayList<Integer> resultIds;
    String[] queryWords = query.split("\\W+");
    ArrayList<Integer> list1;
    resultIds = invertedList.get(queryWords[0]);
    for (int i = 1; i < queryWords.length; i++) {       
        list1 = invertedList.get(queryWords[i]);
        resultIds = intersect(list1, resultIds);
    }

    Collections.sort(resultIds);
    for (Integer item : resultIds) {
        result.add(documents.get(item));
    }
    return result;
}

resultIds is null when this line runs:

resultIds = invertedList.get(queryWords[0]);

I put a breaking point at the very first line of queryProcess function, and both data members are empty.

È stato utile?

Soluzione

More problems with your invertedList, I see. :-)

This is actually being caused by the exact same problem you had previously: Hashmap get function returns null

Line

ArrayList<String> result = processQuery(query, 5);

Should read

ArrayList<String> result = invertedIndex.processQuery(query, 5);

Recommend moving all your tests to a completely separate file. That would save you these field reference headaches.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top