Beispiel #1
0
  private static List<Double> getWeightVectorForClass(
      Map<String, List<LinkedHashMap<String, Object>>> documents,
      String key,
      List<Integer> featureIndexList,
      GraphDatabaseService db) {
    List<Double> weightVector;

    Transaction tx = db.beginTx();
    // Get class id
    Long classId =
        db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key)
            .iterator()
            .next()
            .getId();

    // Get weight vector for class
    List<Long> longs =
        documents
            .get(key)
            .stream()
            .map(a -> ((Integer) a.get("feature")).longValue())
            .collect(Collectors.toList());

    weightVector =
        featureIndexList
            .stream()
            .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0)
            .collect(Collectors.toList());
    tx.success();
    tx.close();
    return weightVector;
  }
 private Node setLabels(Node node, Collection<String> labels) {
     if (labels==null || labels.isEmpty()) return node;
     for (String label : labels) {
         node.addLabel(DynamicLabel.label(label));
     }
     return node;
 }
 private Label[] toLabels(Collection<String> labels) {
     if (labels==null || labels.isEmpty()) return NO_LABELS;
     Label[] labelArray = new Label[labels.size()];
     int i=0;
     for (String label : labels) {
         labelArray[i++]= DynamicLabel.label(label);
     }
     return labelArray;
 }
Beispiel #4
0
  private static List<Node> getAllClasses(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> finalClasses = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Class"))
        .forEach(a -> finalClasses.add(a));
    tx.success();
    tx.close();

    return finalClasses.stream().map(a -> a).collect(Collectors.toList());
  }
Beispiel #5
0
 public static int getDocumentSize(GraphDatabaseService db) {
   int documentSize;
   String cacheKey = "GLOBAL_DOCUMENT_SIZE";
   if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
     documentSize =
         IteratorUtil.count(
             GlobalGraphOperations.at(db).getAllNodesWithLabel(DynamicLabel.label("Class")));
     vectorSpaceModelCache.put(cacheKey, documentSize);
   } else {
     documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey);
   }
   return documentSize;
 }
Beispiel #6
0
  private static List<Integer> getFeatureIndexList(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> patterns = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Pattern"))
        .forEach(a -> patterns.add(a));

    Collections.sort(
        patterns,
        (a, b) ->
            ((Integer) b.getProperty("threshold"))
                .compareTo(((Integer) a.getProperty("threshold"))));

    List<Integer> patternIds =
        patterns.stream().map(a -> ((Long) a.getId()).intValue()).collect(Collectors.toList());

    tx.success();
    tx.close();

    return patternIds;
  }