Skip to content

Instantly share code, notes, and snippets.

@herdianf
Created June 9, 2017 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herdianf/c92aa1f166217d7494f9e0687ee3de57 to your computer and use it in GitHub Desktop.
Save herdianf/c92aa1f166217d7494f9e0687ee3de57 to your computer and use it in GitHub Desktop.
Cia Deduplication
private Map<String, Object> processResponse(Map<String, Object> map,
final String payload) {
//noinspection unchecked
final List<Map<String, Object>> namedEntities = (List<Map<String, Object>>) map.remove("namedEntities");
final List<Map<String, Object>> semanticKeywords = (List<Map<String, Object>>) map.remove("semanticKeywords");
final List<Map<String, Object>> taxonomies = (List<Map<String, Object>>) map.remove("taxonomy");
//filter duplicates
List<Map<String,Object>> entities = new ArrayList<Map<String, Object>>();
Map<String,Object> keys = new HashMap<String, Object>();
int total = 0;
for(Map<String,Object> result: namedEntities) {
String key = ((String)result.get("name")).toLowerCase();
if (!keys.containsKey(key)) {
result.put("weight", result.get("confidenceScore"));
entities.add(result);
keys.put(key, result);
if (total++ > LIMIT) break;
}
}
for(Map<String,Object> result: semanticKeywords) {
String key = ((String)result.get("name")).toLowerCase();
result.put("type", "semantic");
if (!keys.containsKey(key)) {
entities.add(result);
keys.put(key, result);
if (total++ > LIMIT) break;
}
}
total=0;
Map<String,Object> taxonomyKeys = new HashMap<String, Object>();
for(Map<String,Object> result: taxonomies) {
String key = ((String)result.get("name")).toLowerCase();
result.put("type", "taxonomy");
if (!taxonomyKeys.containsKey(key)) {
entities.add(result);
taxonomyKeys.put(key, result);
if (total++ > LIMIT) break;
}
}
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("named", namedEntities);
resultMap.put("semantic", semanticKeywords);
resultMap.put("taxonomy", taxonomies);
resultMap.put("entities", entities);
resultMap.put("payload", payload);
return resultMap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment