List<Path> paths = IteratorUtil.asCollection(db.traversalDescription() .depthFirst() .relationships(withName("HAS_AFFINITY"), Direction.BOTH)
public static Map<Long, Integer> getTermFrequencyMapForDocument(GraphDatabaseService db, Long classId) { Map<Long, Integer> termDocumentMatrix; String cacheKey = "TERM_DOCUMENT_FREQUENCY_" + classId; if(vectorSpaceModelCache.getIfPresent(cacheKey) == null) { Node classNode = db.getNodeById(classId); termDocumentMatrix = new HashMap<>(); IteratorUtil.asCollection(db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.INCOMING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(classNode)).stream() .forEach(p -> { int matches = (Integer) p.lastRelationship().getProperty("matches"); termDocumentMatrix.put(p.endNode().getId(), matches); }); vectorSpaceModelCache.put(cacheKey, termDocumentMatrix); } else { termDocumentMatrix = (Map<Long, Integer>)vectorSpaceModelCache.getIfPresent(cacheKey); } return termDocumentMatrix; }
public static double getFeatureMatchDistribution(GraphDatabaseService db, Long patternId) { Transaction tx = db.beginTx(); Node startNode = db.getNodeById(patternId); // Feature match distribution List<Double> matches = IteratorUtil.asCollection(db.traversalDescription() .depthFirst() .relationships(withName("HAS_CLASS"), Direction.OUTGOING) .evaluator(Evaluators.fromDepth(1)) .evaluator(Evaluators.toDepth(1)) .traverse(startNode) .relationships()) .stream() .map(p -> ((Integer)p.getProperty("matches")).doubleValue()) .collect(Collectors.toList()); tx.success(); tx.close(); double variance = 1.0; if(matches.size() > 1) { Double[] matchArr = matches.toArray(new Double[matches.size()]); // Get the standard deviation DescriptiveStatistics ds = new DescriptiveStatistics(); matches.forEach(m -> ds.addValue(m.doubleValue() / StatUtils.sum(ArrayUtils.toPrimitive(matchArr)))); variance = ds.getStandardDeviation(); } return variance; }
Relationship rel = IteratorUtil.asCollection(db.traversalDescription() .depthFirst() .relationships(withName(getRelationshipType()), Direction.OUTGOING)