【发布时间】:2015-09-04 16:16:37
【问题描述】:
我有一个包含字典的 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);
“修复”几个问题后,我仍然收到 NullPointerException..
示例输出:
因此,对于示例数据:
"running": {
"ADJ": 1
},
"dancing": {
"ADP": 28,
"ADV": 57
},
"hopeing": {
"ADJ":14
},
输出将是:
"ing": {
"ADJ": 15
"ADP": 28
"ADV": 57
【问题讨论】:
-
请在您要生成的json中发布示例输出数据。
-
@NikolayRusev 我在问题末尾添加了一个示例。我希望这会有所帮助!