Exemple #1
0
  public void predictAsLink(
      Graph graph, Iterable<Edge> unknownedges, boolean removenotexist, String existfeature) {
    // Add co-reference edge schema and feature, but do nothing else

    // Add schema, if not already defined
    if (!graph.hasSchema(this.edgeschemaid)) {
      graph.addSchema(this.edgeschemaid, new Schema(SchemaType.UNDIRECTED));
    }

    // Add existence feature, if not already defined
    Schema schema = graph.getSchema(this.edgeschemaid);
    if (!schema.hasFeature(existfeature)) {
      schema.addFeature(existfeature, LinkPredictor.EXISTENCEFEATURE);
      graph.updateSchema(this.edgeschemaid, schema);
    }
  }
Exemple #2
0
 public void predictAsLink(Graph graph, PotentialLinkGenerator generator) {
   // Add co-reference edge schema, but do nothing else
   if (!graph.hasSchema(this.edgeschemaid)) {
     graph.addSchema(this.edgeschemaid, new Schema(SchemaType.UNDIRECTED));
   }
 }
Exemple #3
0
  @Override
  public void addNoise(Graph g) {
    if (initialize) {
      this.initialize();
    }

    Schema schema = g.getSchema(schemaid);

    // Load feature ids, if needed
    if (fids == null) {
      fids =
          FeatureUtils.parseFeatureList(
              this, schema, IteratorUtils.iterator2stringlist(schema.getFeatureIDs()));
    }

    for (String fid : fids) {
      Feature f = schema.getFeature(fid);

      // Flip attributes
      if (f instanceof ExplicitCateg) {
        // Handle explicit categorical features
        UnmodifiableList<String> cats = ((ExplicitCateg) f).getAllCategories();
        int catsize = cats.size();

        // Iterate over graph all graph items with the given schema
        Iterator<GraphItem> gitr = g.getGraphItems(schemaid);
        while (gitr.hasNext()) {
          GraphItem gi = gitr.next();

          String oldvalue = gi.getFeatureValue(fid).getStringValue();
          if (sparsevalue != null && !oldvalue.equals(sparsevalue)) {
            // Only flip the non-sparse values
            // Done so for sparse sets, the common value doesn't become
            // too common.
            continue;
          }

          // Only flip given some probability
          if (rand.nextDouble() > probflip) {
            continue;
          }

          // Randomly select a category
          int index = rand.nextInt(catsize);
          if (changevalue && catsize > 1) {
            while (oldvalue.equals(cats.get(index))) {
              index = rand.nextInt(catsize);
            }
          }

          gi.setFeatureValue(fid, new CategValue(cats.get(index)));
        }
      } else if (f instanceof ExplicitNum) {
        // Iterate over graph all graph items with the given schema
        Iterator<GraphItem> gitr = g.getGraphItems(schemaid);
        while (gitr.hasNext()) {
          GraphItem gi = gitr.next();

          double oldvalue = ((NumValue) gi.getFeatureValue(fid)).getNumber();
          if (oldvalue != 0.0 && oldvalue != 1.0) {
            throw new UnsupportedTypeException(
                "Numeric values must be either 0 or 1: " + gi + "." + fid + "=" + oldvalue);
          }

          if (sparsevalue != null && oldvalue != Double.parseDouble(sparsevalue)) {
            // Only flip the non-sparse values
            // Done so for sparse sets, the common value doesn't become
            // too common.
            continue;
          }

          // Only flip given some probability
          if (rand.nextDouble() > probflip) {
            continue;
          }

          double value = 0;
          if (changevalue && oldvalue == value) {
            value = (oldvalue + 1) % 2;
          } else {
            // Randomly select a category
            value = rand.nextInt(2);
          }

          gi.setFeatureValue(fid, new NumValue(value));
        }
      } else {
        throw new UnsupportedTypeException("Unsupported Type: " + f.getClass().getCanonicalName());
      }
    }
  }