我有一个包含字典的JSON文件。对于每个英语单词,都有一个与之关联的对象。每个对象都是标签和键的列表。一些样本数据:
},
"On": {
"ADP": 345,
"PRT": 1
},
"On-to-Spokane": {
"ADJ": 1
},
"Once": {
"ADP": 28,
"ADV": 57
},
"One": {
"NOUN": 76,
"NUM": 343
},
我正在尝试创建一个新的JSON文件,在那里我转到已经存在的单词集中的每个单词并取出单词的结尾。如果新地图中已经存在,则将结尾的单词放入新地图中,如果它已经存在于新地图中,则需要将该单词中的数据添加到新地图中。
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);
修复&#39;几个问题,我仍然得到一个NullPointerException ..
示例输出:
因此,对于样本数据:
"running": {
"ADJ": 1
},
"dancing": {
"ADP": 28,
"ADV": 57
},
"hopeing": {
"ADJ":14
},
输出将是:
"ing": {
"ADJ": 15
"ADP": 28
"ADV": 57
答案 0 :(得分:1)
这是罪魁祸首:
//get whats actually inside this word
Map<String, Integer> tags = (Map<String, Integer>) inDict.get(entry);
应该是:
Map<String, Integer> tags = (Map<String, Integer>) entry.getValue();