Nate Cook3
Nate Cook3

Reputation: 596

Problems Merging JSON Data -- ObjectMapper (HashMap)

I have a JSON file that contains a dictionary. For each English word there is an Object associated with it. Each object is a list of tags and keys. Some sample data:

}, 
"On": {
    "ADP": 345, 
    "PRT": 1
}, 
"On-to-Spokane": {
    "ADJ": 1
}, 
"Once": {
    "ADP": 28, 
    "ADV": 57
}, 
"One": {
    "NOUN": 76, 
    "NUM": 343
},

I am trying to create a new JSON file where I go to each word in the already existing word-set and take the word's ending. The words ending is placed into a new Map if it doesn't exist already in the new map, if it already exists in the new map, the data from the word needs to be added to the new map.

    InvertedStemmerDB data = new InvertedStemmerDB();
    inDict = mapper.readValue(new File(dictionary), Map.class); 

    //Hold Stem Data... creating a whole new look-up dictionary for word endings
    Map<String,Object> stemData = new HashMap<String,Object>();

    //Open up existing data
    ObjectMapper mapper = new ObjectMapper(); 
    //Map<String,Object> data1 = mapper.readValue(new File("*****.json"), Map.class);


    //Go through All the Data
    for (Map.Entry<String, Object> entry : inDict.entrySet()) {

         //this is how I'm stripping the word.. this isn't the issue
        String key = entry.getKey();
        stemmer.setCurrent(key);
        stemmer.stem();
        String stemKey = stemmer.getCurrent();  
        String suffix = suffix(key, stemKey);

        //get whats actually inside this word
        Map<String, Integer> tags = (Map<String, Integer>) inDict.get(entry);

        if (stemData.containsKey(suffix)) {//This means it needs to be updated, the stem already exists

            //get object sub-value
            Map<String, Integer> stemTag = (Map<String, Integer>) stemData.get(suffix);

            double value = 0;
            for (String tag : tags.keySet()) {
                //add to stemData
                if(stemTag.containsKey(tag)){
                    int previousValue = stemTag.get(tag);
                    int resultantValue = tags.get(tag);
                    stemTag.put(tag, resultantValue+previousValue);
                }
                else

                stemTag.put(tag, tags.get(tag));
            }
        }

        else {
            //needs to be created, not updated
                stemData.put(suffix, inDict.get(key));
            }   
    }

    mapper.writeValue(new File("test_write.json"), stemData);

After 'fixing' several problems, I"m still getting a NullPointerException..

Sample output:

So, for a sample data:

"running": {
    "ADJ": 1
}, 
"dancing": {
    "ADP": 28, 
    "ADV": 57
}, 
"hopeing": {
  "ADJ":14
 },

Output will be:

 "ing": {
    "ADJ": 15
    "ADP": 28
    "ADV": 57

Upvotes: 1

Views: 290

Answers (1)

ttzn
ttzn

Reputation: 2613

Here is the culprit :

//get whats actually inside this word
Map<String, Integer> tags = (Map<String, Integer>) inDict.get(entry);

It should be :

Map<String, Integer> tags = (Map<String, Integer>) entry.getValue();

Upvotes: 1

Related Questions