Пример #1
0
  private TIntHashSet getUnion(TIntHashSet contextA, TIntHashSet contextB) {
    TIntHashSet union = new TIntHashSet();

    for (int a : contextB.toArray()) {
      union.add(a);
    }

    for (int a : contextA.toArray()) {
      if (!union.contains(a) && !union.contains(DataAccess.expandTerm(a))) {
        union.add(a);
      }
    }

    return union;
  }
Пример #2
0
 /**
  * Returns a dense array representing the potential pool bits with the connected bits set to 1.
  *
  * <p>Note: Only called from tests for now...
  *
  * @param c
  * @return
  */
 public int[] getDenseConnections(Connections c) {
   int[] retVal = new int[c.getNumInputs()];
   for (int inputIndex : synapseConnections.toArray()) {
     retVal[inputIndex] = 1;
   }
   return retVal;
 }
Пример #3
0
 /**
  * Creates a propagator for Among: Counts the number of decision variables which take a value in
  * the input value set
  *
  * @param variables {decision variables, cardinality variable}
  * @param values input value set
  */
 public PropAmongGAC(IntVar[] variables, int[] values) {
   super(variables, PropagatorPriority.LINEAR, true);
   nb_vars = variables.length - 1;
   IEnvironment environment = model.getEnvironment();
   this.setValues = new TIntHashSet(values);
   this.values = setValues.toArray();
   Arrays.sort(this.values);
   poss = SetFactory.makeStoredSet(SetType.BIPARTITESET, 0, model);
   nbSure = environment.makeInt(0);
 }
Пример #4
0
  private TIntHashSet getIntersection(TIntHashSet contextA, TIntHashSet contextB) {
    TIntHashSet is = new TIntHashSet();

    for (int a : contextA.toArray()) {
      if (contextB.contains(a) || contextB.contains(DataAccess.expandTerm(a))) {
        is.add(a);
      }
    }

    return is;
  }
  /**
   * Computes the intersection of two <b>unsorted</b> arrays. Will deduplicate the items, so the
   * return is a set of integers.
   */
  public static int[] intersectionUnsorted(int[] arr, int[] arr2) {
    TIntHashSet set = new TIntHashSet();
    TIntHashSet toReturn = new TIntHashSet();
    for (int a : arr) {
      set.add(a);
    }

    for (int a : arr2) {
      if (set.contains(a)) {
        toReturn.add(a);
      }
    }

    return toReturn.toArray();
  }
 /** Computes the union of two arrays. */
 public static int[] union(int[] a, int[] b) {
   TIntHashSet set = new TIntHashSet();
   set.addAll(a);
   set.addAll(b);
   return set.toArray();
 }
  @Override
  public double calcSimilarity(Entity a, Entity b, EntitiesContext entitiesContext) {
    kpc = (WeightedKeyphrasesContext) entitiesContext;

    Map<String, Double> matches = new HashMap<String, Double>();
    double dotprod = 0.0;

    int[] e1kps = kpc.getEntityKeyphraseIds(a);
    int[] e2kps = kpc.getEntityKeyphraseIds(b);

    TIntHashSet e1kpsForIntersect = new TIntHashSet(e1kps);
    TIntHashSet e2kpsForIntersect = new TIntHashSet(e2kps);
    e1kpsForIntersect.retainAll(e2kpsForIntersect);

    // iterate through intersection
    for (int kp : e1kpsForIntersect.toArray()) {
      double v1 = kpc.getCombinedKeyphraseMiIdfWeight(a, kp);
      double v2 = kpc.getCombinedKeyphraseMiIdfWeight(b, kp);

      if (v1 > 0 && v2 > 0) {
        double tmp = v1 * v2;
        dotprod += tmp;

        matches.put(kpc.getKeyphraseForId(kp), tmp);
      }
    }

    double norm1 = calcNorm(a, e1kps);
    double norm2 = calcNorm(b, e2kps);

    double sim = 0.0;
    double denom = norm1 * norm2;

    if (denom != 0) {
      sim = dotprod / denom;
    }

    if (!(tracer.eeTracing() instanceof NullEntityEntityTracing)) {
      Map<String, Double> e1keyphrases = new HashMap<String, Double>();
      for (int kp : e1kps) {
        if (kpc.getCombinedKeyphraseMiIdfWeight(a, kp) > 0.0) {
          e1keyphrases.put(kpc.getKeyphraseForId(kp), kpc.getCombinedKeyphraseMiIdfWeight(a, kp));
        }
      }
      e1keyphrases = CollectionUtils.sortMapByValue(e1keyphrases, true);
      Map<String, Double> e1top = new LinkedHashMap<String, Double>();
      for (Entry<String, Double> e : e1keyphrases.entrySet()) {
        e1top.put(e.getKey(), e.getValue());
      }
      e1keyphrases = e1top;

      Map<String, Double> e2keyphrases = new HashMap<String, Double>();
      for (int kp : e2kps) {
        if (kpc.getCombinedKeyphraseMiIdfWeight(b, kp) > 0.0) {
          e2keyphrases.put(kpc.getKeyphraseForId(kp), kpc.getCombinedKeyphraseMiIdfWeight(b, kp));
        }
      }
      e2keyphrases = CollectionUtils.sortMapByValue(e2keyphrases, true);
      Map<String, Double> e2top = new LinkedHashMap<String, Double>();
      for (Entry<String, Double> e : e2keyphrases.entrySet()) {
        e2top.put(e.getKey(), e.getValue());
      }
      e2keyphrases = e2top;

      Map<String, TermTracer> matchedKeywords = new HashMap<String, TermTracer>();
      for (String kp : matches.keySet()) {
        TermTracer tt = new TermTracer();
        tt.setTermWeight(matches.get(kp));
        matchedKeywords.put(kp, tt);
      }

      tracer.eeTracing().addEntityContext(a.getName(), e1keyphrases);
      tracer.eeTracing().addEntityContext(b.getName(), e2keyphrases);

      KeytermEntityEntityMeasureTracer mt =
          new KeytermEntityEntityMeasureTracer(
              "KeyphraseCosineSim", 0.0, e2keyphrases, matchedKeywords);
      mt.setScore(sim);
      tracer.eeTracing().addEntityEntityMeasureTracer(a.getName(), b.getName(), mt);

      KeytermEntityEntityMeasureTracer mt2 =
          new KeytermEntityEntityMeasureTracer(
              "KeyphraseCosineSim", 0.0, e1keyphrases, matchedKeywords);
      mt2.setScore(sim);
      tracer.eeTracing().addEntityEntityMeasureTracer(b.getName(), a.getName(), mt2);
    }

    return sim;
  }