public static Map<String, List<LinkedHashMap<String, Object>>> similarDocumentMapForClass( GraphDatabaseService db, String className) { Map<String, List<LinkedHashMap<String, Object>>> documents; Map<String, List<LinkedHashMap<String, Object>>> results = new HashMap<>(); List<Integer> featureIndexList; VsmCacheModel vsmCacheModel = new VsmCacheModel(db).invoke(); featureIndexList = vsmCacheModel.getFeatureIndexList(); documents = vsmCacheModel.getDocuments(); final String key = className; List<LinkedHashMap<String, Object>> resultList = new ArrayList<>(); LinkedHashMap<String, Double> classMap = new LinkedHashMap<>(); List<Double> v1 = getFeatureVectorForDocumentClass(documents, featureIndexList, key); documents .keySet() .stream() .filter(otherKey -> !key.equals(otherKey)) .forEach( otherKey -> { List<Double> v2 = getBinaryFeatureVectorForDocumentClass(documents, featureIndexList, otherKey); classMap.put(otherKey, cosineSimilarity(v1, v2)); }); classMap .keySet() .forEach( ks -> { if (!ks.equals(key) && classMap.get(ks) > 0.0) { LinkedHashMap<String, Object> localMap = new LinkedHashMap<>(); localMap.put("class", ks); localMap.put("similarity", classMap.get(ks)); resultList.add(localMap); } }); resultList.sort( (a, b) -> { Double diff = (((double) a.get("similarity")) - ((double) b.get("similarity"))); return diff > 0 ? -1 : diff.equals(0.0) ? 0 : 1; }); results.put("classes", resultList); return results; }
public static Map<String, List<LinkedHashMap<String, Object>>> similarDocumentMapForVector( GraphDatabaseService db, GraphManager graphManager, String input) { Map<String, List<LinkedHashMap<String, Object>>> documents; Map<String, List<LinkedHashMap<String, Object>>> results = new HashMap<>(); List<Integer> featureIndexList; VsmCacheModel vsmCacheModel = new VsmCacheModel(db).invoke(); featureIndexList = vsmCacheModel.getFeatureIndexList(); documents = vsmCacheModel.getDocuments(); List<Double> features = getFeatureVector(db, graphManager, input, featureIndexList); List<LinkedHashMap<String, Object>> resultList = new ArrayList<>(); LinkedHashMap<String, Double> classMap = new LinkedHashMap<>(); documents .keySet() .stream() .forEach( otherKey -> { List<Double> v2 = getWeightVectorForClass(documents, otherKey, featureIndexList, db); classMap.put(otherKey, cosineSimilarity(v2, features)); }); classMap .keySet() .stream() .forEach( ks -> { if (classMap.get(ks) > 0.0) { LinkedHashMap<String, Object> localMap = new LinkedHashMap<>(); localMap.put("class", ks); localMap.put("similarity", classMap.get(ks)); resultList.add(localMap); } }); try { resultList.sort( (a, b) -> { Double diff = (((double) a.get("similarity")) - ((double) b.get("similarity"))); return diff > 0 ? -1 : diff.equals(0.0) ? 0 : 1; }); } catch (NullPointerException ex) { // resultList is empty or null } results.put("classes", resultList); return results; }