예제 #1
0
  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 ->
                  termDocumentMatrix.put(
                      p.endNode().getId(), (Integer) p.lastRelationship().getProperty("matches")));

      vectorSpaceModelCache.put(cacheKey, termDocumentMatrix);
    } else {
      termDocumentMatrix = (Map<Long, Integer>) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return termDocumentMatrix;
  }
예제 #2
0
  private static List<LinkedHashMap<String, Object>> getFeaturesForClass(
      GraphDatabaseService db, Node classNode) {
    List<LinkedHashMap<String, Object>> patternIds = new ArrayList<>();

    for (Path p :
        db.traversalDescription()
            .depthFirst()
            .relationships(withName("HAS_CLASS"), Direction.INCOMING)
            .evaluator(Evaluators.fromDepth(1))
            .evaluator(Evaluators.toDepth(1))
            .traverse(classNode)) {

      LinkedHashMap<String, Object> featureMap = new LinkedHashMap<>();

      if (p.relationships().iterator().hasNext()) {
        featureMap.put("frequency", p.relationships().iterator().next().getProperty("matches"));
      } else {
        featureMap.put("frequency", 0);
      }

      featureMap.put("feature", ((Long) p.endNode().getId()).intValue());

      patternIds.add(featureMap);
    }
    return patternIds;
  }
예제 #3
0
  public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) {
    int documentSize;

    String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id;

    if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
      Node startNode = db.getNodeById(id);

      Iterator<Node> classes =
          db.traversalDescription()
              .depthFirst()
              .relationships(withName("HAS_CLASS"), Direction.OUTGOING)
              .evaluator(Evaluators.fromDepth(1))
              .evaluator(Evaluators.toDepth(1))
              .traverse(startNode)
              .nodes()
              .iterator();

      documentSize = IteratorUtil.count(classes);

      vectorSpaceModelCache.put(cacheKey, documentSize);
    } else {
      documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return documentSize;
  }
  private void canPruneTraversalAtSpecificDepth(TraversalDescription description) {
    Traverser traverser =
        description
            .uniqueness(Uniqueness.NONE)
            .evaluator(Evaluators.toDepth(1))
            .traverse(referenceNode());

    expectNodes(traverser, "1", "2", "3", "4", "5");
  }
예제 #5
0
 private void checkGeometryNodeIterator() {
   TraversalDescription td =
       database
           .traversalDescription()
           .depthFirst()
           .relationships(RTreeRelationshipTypes.RTREE_REFERENCE, Direction.OUTGOING)
           .evaluator(Evaluators.excludeStartPosition())
           .evaluator(Evaluators.toDepth(1));
   while ((geometryNodeIterator == null || !geometryNodeIterator.hasNext())
       && allIndexNodeIterator.hasNext()) {
     geometryNodeIterator = td.traverse(allIndexNodeIterator.next()).nodes().iterator();
   }
 }
 @SuppressWarnings("deprecation")
 @Test
 @Transactional
 public void testTraverse() throws Exception {
   // final TraversalDescription description =
   // Traversal.description().relationships(KNOWS).prune(Traversal.pruneAfterDepth(1)).filter(Traversal.returnAllButStartNode());
   final TraversalDescription description =
       Traversal.description()
           .relationships(KNOWS)
           .evaluator(Evaluators.toDepth(1))
           .evaluator(Evaluators.excludeStartPosition());
   assertSingleResult(
       "node1",
       neo4jTemplate
           .traverse(referenceNode, description)
           .to(String.class, new PathNodeNameMapper()));
 }
예제 #7
0
 /** @deprecated See {@link org.neo4j.graphdb.traversal.BranchCollisionPolicies#SHORTEST_PATH} */
 @Deprecated
 public static BranchCollisionDetector shortestPathsCollisionDetector(int maxDepth) {
   return new ShortestPathsBranchCollisionDetector(Evaluators.toDepth(maxDepth));
 }