Java Code Examples for gnu.trove.set.TIntSet#add()

The following examples show how to use gnu.trove.set.TIntSet#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SparseArrayOfInts.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Array<Integer> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<Integer> builder = ArrayBuilder.of(capacity, Integer.class);
    for (int i=0; i<length(); ++i) {
        final int value = getInt(i);
        if (set.add(value)) {
            builder.addInt(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example 2
Source File: SortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseIndex() {
    final Set<String> blockingKeysSet = invertedIndexD1.keySet();
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    final int[] allEntityIds = getSortedEntities(sortedTerms);

    //slide window over the sorted list of entity ids
    int upperLimit = allEntityIds.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            entityIds.add(allEntityIds[i + j]);
        }

        if (1 < entityIds.size()) {
            blocks.add(new UnilateralBlock(entityIds.toArray()));
        }
    }
}
 
Example 3
Source File: AbstractMetablocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected void setStatistics() {
    distinctComparisons = 0;
    comparisonsPerEntity = new float[noOfEntities];
    final TIntSet distinctNeighbors = new TIntHashSet();
    for (int i = 0; i < noOfEntities; i++) {
        final int[] associatedBlocks = entityIndex.getEntityBlocks(i, 0);
        if (associatedBlocks.length != 0) {
            distinctNeighbors.clear();
            for (int blockIndex : associatedBlocks) {
                for (int neighborId : getNeighborEntities(blockIndex, i)) {
                    distinctNeighbors.add(neighborId);
                }
            }
            comparisonsPerEntity[i] = distinctNeighbors.size();
            if (!cleanCleanER) {
                comparisonsPerEntity[i]--;
            }
            distinctComparisons += comparisonsPerEntity[i];
        }
    }
    distinctComparisons /= 2;
}
 
Example 4
Source File: DenseArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example 5
Source File: MappedArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example 6
Source File: MappedArrayOfInts.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public final Array<Integer> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<Integer> builder = ArrayBuilder.of(capacity, Integer.class);
    for (int i=0; i<length(); ++i) {
        final int value = getInt(i);
        if (set.add(value)) {
            builder.addInt(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example 7
Source File: SparseArrayWithIntCoding.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
@Override
public Array<T> distinct(int limit) {
    final int capacity = limit < Integer.MAX_VALUE ? limit : 100;
    final TIntSet set = new TIntHashSet(capacity);
    final ArrayBuilder<T> builder = ArrayBuilder.of(capacity, type());
    for (int i=0; i<length(); ++i) {
        final int code = getInt(i);
        if (set.add(code)) {
            final T value = getValue(i);
            builder.add(value);
            if (set.size() >= limit) {
                break;
            }
        }
    }
    return builder.toArray();
}
 
Example 8
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnections(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n)) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return true;
        });
    }
    return cs;
}
 
Example 9
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnectionsTraverseStoppingAtTerminals(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n) || topo.getTerminal(n) == null) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return topo.getTerminal(n2) == null;
        });
    }
    return cs;
}
 
Example 10
Source File: GraphUtil.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Remove from the {@param graph} vertices which are not connected to any edge,
 * and which have no associated object.
 */
public static <V, E> void removeIsolatedVertices(UndirectedGraph<V, E> graph) {
    Objects.requireNonNull(graph, "Graph is null.");

    TIntSet connectedVertices = new TIntHashSet();
    for (int e : graph.getEdges()) {
        connectedVertices.add(graph.getEdgeVertex1(e));
        connectedVertices.add(graph.getEdgeVertex2(e));
    }

    for (int v : graph.getVertices()) {
        if (!connectedVertices.contains(v) && graph.getVertexObject(v) == null) {
            graph.removeVertex(v);
        }
    }
}
 
Example 11
Source File: GraphConfidenceEstimator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/**
 * Will return numElements integers from the input elements. If numElements
 * is larger than elements.size(), everything will be returned.
 *
 * @param elements    Elements to choose from.
 * @param numElements Number of elements to choose.
 * @return numElement random integers from elements.
 */
private TIntSet getRandomElements(TIntSet elements, int numElements) {
  TIntList source = new TIntArrayList(elements.toArray());
  TIntSet randomElements = new TIntHashSet();
  for (int i = 0; i < numElements; ++i) {
    if (source.size() == 0) {
      break;
    }
    // TODO: this is not efficient, as deleting from the ArrayList
    // will copy ... make this more efficient when necessary.
    int elementPosition = random_.nextInt(source.size());
    int element = source.get(elementPosition);
    source.remove(element);
    randomElements.add(element);
  }
  return randomElements;
}
 
Example 12
Source File: GraphTracer.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private TIntObjectHashMap<EntityMetaData> loadEntitiesMetaData(String docId) throws EntityLinkingDataAccessException {
  List<String> mentions = new LinkedList<String>(docMentionCandidatesOriginalMap.get(docId).keySet());
  Map<String, List<TracingEntity>> mentionCandidatesOriginalMap = docMentionCandidatesOriginalMap.get(docId);

  TIntSet entities = new TIntHashSet();

  for (String mention : mentions) {
    List<TracingEntity> allCandidites = mentionCandidatesOriginalMap.get(mention);
    for (TracingEntity te : allCandidites) {
      entities.add(te.entityId);
      for (int e : te.connectedEntities.keySet()) {
        entities.add(e);
      }
    }
  }
  return DataAccess.getEntitiesMetaData(entities.toArray());
}
 
Example 13
Source File: SortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseIndices() {
    final Set<String> blockingKeysSet = new HashSet<>();
    blockingKeysSet.addAll(invertedIndexD1.keySet());
    blockingKeysSet.addAll(invertedIndexD2.keySet());
    
    final String[] sortedTerms = blockingKeysSet.toArray(new String[0]);
    Arrays.sort(sortedTerms);

    final int[] allEntityIds = getMixedSortedEntities(sortedTerms);

    int datasetLimit = entityProfilesD1.size();
    //slide window over the sorted list of entity ids
    int upperLimit = allEntityIds.length - windowSize;
    for (int i = 0; i <= upperLimit; i++) {
        final TIntSet entityIds1 = new TIntHashSet();
        final TIntSet entityIds2 = new TIntHashSet();
        for (int j = 0; j < windowSize; j++) {
            if (allEntityIds[i + j] < datasetLimit) {
                entityIds1.add(allEntityIds[i + j]);
            } else {
                entityIds2.add(allEntityIds[i + j] - datasetLimit);
            }
        }

        if (!entityIds1.isEmpty() && !entityIds2.isEmpty()) {
            blocks.add(new BilateralBlock(entityIds1.toArray(), entityIds2.toArray()));
        }
    }
}
 
Example 14
Source File: MaterialUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static TIntSet encodeMaterialSet(Collection<?> materials) {
    TIntSet set = new TIntHashSet(materials.size());
    for(Object material : materials) {
        if(material instanceof MaterialData) {
            set.add(encodeMaterial((MaterialData) material));
        }
    }
    return set;
}
 
Example 15
Source File: MaterialEncoder.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
static TIntSet encodeMaterialSet(Collection<?> materials) {
  TIntSet set = new TIntHashSet(materials.size());
  for (Object material : materials) {
    if (material instanceof MaterialData) {
      set.add(encodeMaterial((MaterialData) material));
    }
  }
  return set;
}
 
Example 16
Source File: SDRCategoryEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
private int[] getSortedSample(final int populationSize, final int sampleLength) {
    TIntSet resultSet = new TIntHashSet();
    while (resultSet.size() < sampleLength) {
        resultSet.add(random.nextInt(populationSize));
    }
    int[] result = resultSet.toArray();
    Arrays.sort(result);
    return result;
}
 
Example 17
Source File: GraphConfidenceEstimator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * Return all mention node ids with more than candidate entity. Others
 * cannot be flipped. 
 *
 * @param g Input graph.
 * @param solution  Disambiguation solution.
 * @return Mention node flipping candidates.
 */
private TIntSet getFlipCandidates(Graph g, Map<Integer, Integer> solution) {
  TIntSet candidates = new TIntHashSet();
  for (int mention : solution.keySet()) {
    if (g.getNode(mention).getSuccessors().size() > 1) {
      candidates.add(mention);
    }
  }
  return candidates;
}
 
Example 18
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/**
 * Removes dangling mentions (where no candidate entity has a coherence edge)
 * from gaph. They will influence the minimum weighted degree but can
 * never be improved. Set the solution to the entity with the highest
 * mention-entity weight.
 *
 * @param solution Solution will be updated, setting the correct entity using
 *                local similarity for dangling mentions.
 * @return Node ids of nodes to remove.
 */
private TIntSet removeUnconnectedMentionEntityPairs(Graph g, Map<ResultMention, List<ResultEntity>> solution) {
  TIntSet mentionsToRemove = new TIntHashSet();
  for (int mentionId : g.getMentionNodesIds().values()) {
    GraphNode mentionNode = g.getNode(mentionId);
    Mention mention = (Mention) mentionNode.getNodeData();
    TIntDoubleHashMap entityCandidates = mentionNode.getSuccessors();
    if (entityCandidates.size() == 0) {
      continue;
    }
    // Remove all mentions without any entities that have coherence edges.
    if (g.isLocalMention(mentionId)) {
      logger.debug("local mention removed: " + mentionId + " " + mention);
      mentionsToRemove.add(mentionId);
      GraphTracer.gTracer.addMentionToDangling(g.getName(), mention.getMention(), mention.getCharOffset());
      // Set solution to best local candidate.
      Pair<Integer, Double> bestEntityScore = getBestLocalCandidateAndScore(entityCandidates);
      int bestEntity = bestEntityScore.getKey();
      double score = bestEntityScore.getValue();
      updateSolution(solution, g, mention, bestEntity, score);
    }

  }
  TIntSet entitiesToRemove = new TIntHashSet();
  // Remove entities that are only connected to removed mentions.
  for (int entityId : g.getEntityNodesIds().values()) {
    GraphNode entityNode = g.getNode(entityId);
    TIntDoubleHashMap successors = entityNode.getSuccessors();
    int removedCount = 0;
    for (TIntDoubleIterator itr = successors.iterator(); itr.hasNext(); ) {
      itr.advance();
      int neighborId = itr.key();
      if (mentionsToRemove.contains(neighborId)) {
        ++removedCount;
      }
    }
    if (removedCount == successors.size()) {
      entitiesToRemove.add(entityId);
    }
  }
  // Remove mentions + entity candidates from graph, trace.
  TIntSet nodesToRemove = new TIntHashSet(mentionsToRemove.size() + entitiesToRemove.size());
  nodesToRemove.addAll(mentionsToRemove);
  nodesToRemove.addAll(entitiesToRemove);
  return nodesToRemove;
}
 
Example 19
Source File: MergeCenterClustering.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
@Override
public EquivalenceCluster[] getDuplicates(SimilarityPairs simPairs) {
    initializeData(simPairs);
    
    // add an edge for every pair of entities with a weight higher than the thrshold
    final Queue<SimilarityEdge> SEqueue = new PriorityQueue<>(simPairs.getNoOfComparisons(), new DecSimilarityEdgeComparator());
    final Iterator<Comparison> iterator = simPairs.getPairIterator();
    while (iterator.hasNext()) {
        final Comparison comparison = iterator.next();
        if (threshold < comparison.getUtilityMeasure()) {
            SEqueue.add(new SimilarityEdge(comparison.getEntityId1(), (comparison.getEntityId2() + datasetLimit), comparison.getUtilityMeasure()));
        }
    }
    
    final TIntSet Center = new TIntHashSet();
    final TIntSet NonCenter = new TIntHashSet();
    while (!SEqueue.isEmpty()) {
        final SimilarityEdge se = SEqueue.remove();
        int v1 = se.getModel1Pos();
        int v2 = se.getModel2Pos();
        
        boolean v1IsCenter = Center.contains(v1);
        boolean v2IsCenter = Center.contains(v2);
        boolean v1IsNonCenter = NonCenter.contains(v1);
        boolean v2IsNonCenter = NonCenter.contains(v2);
        
        if (!(v1IsCenter || v2IsCenter || v1IsNonCenter || v2IsNonCenter)) {
            Center.add(v1);
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if ((v1IsCenter && v2IsCenter) || (v1IsNonCenter && v2IsNonCenter)) {
            continue;
        } else if (v1IsCenter) {
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if (v2IsCenter) {
            NonCenter.add(v1);
            similarityGraph.addEdge(v1, v2);
        }
    }

    return getConnectedComponents();
}
 
Example 20
Source File: CenterClustering.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
@Override
public EquivalenceCluster[] getDuplicates(SimilarityPairs simPairs) {
    initializeData(simPairs);
    
    final float[] edgesWeight = new float[noOfEntities];
    final float[] edgesAttached = new float[noOfEntities];
    final Queue<SimilarityEdge> SEqueue = new PriorityQueue<>(simPairs.getNoOfComparisons(), new DecSimilarityEdgeComparator());

    final Iterator<Comparison> iterator = simPairs.getPairIterator();
    // add a similarity edge to the queue for every pair of entities with a weight higher than the threshold
    while (iterator.hasNext()) { 
        final Comparison comparison = iterator.next();
        if (threshold < comparison.getUtilityMeasure()) {
            SEqueue.add(new SimilarityEdge(comparison.getEntityId1(), comparison.getEntityId2() + datasetLimit, comparison.getUtilityMeasure()));

            edgesWeight[comparison.getEntityId1()] += comparison.getUtilityMeasure();
            edgesWeight[comparison.getEntityId2() + datasetLimit] += comparison.getUtilityMeasure();

            edgesAttached[comparison.getEntityId1()]++;
            edgesAttached[comparison.getEntityId2() + datasetLimit]++;
        }
    }

    final TIntSet Center = new TIntHashSet();
    final TIntSet NonCenter = new TIntHashSet();
    while (!SEqueue.isEmpty()) {
        final SimilarityEdge se = SEqueue.remove();
        int v1 = se.getModel1Pos();
        int v2 = se.getModel2Pos();
        
        boolean v1IsCenter = Center.contains(v1);
        boolean v2IsCenter = Center.contains(v2);
        boolean v1IsNonCenter = NonCenter.contains(v1);
        boolean v2IsNonCenter = NonCenter.contains(v2);

        if (!(v1IsCenter || v2IsCenter || v1IsNonCenter || v2IsNonCenter)) {
            float w1 = edgesWeight[v1] / edgesAttached[v1];
            float w2 = edgesWeight[v2] / edgesAttached[v2];
            if (w1 > w2) {
                Center.add(v1);
                NonCenter.add(v2);
            } else {
                Center.add(v2);
                NonCenter.add(v1);
            }

            similarityGraph.addEdge(v1, v2);
        } else if ((v1IsCenter && v2IsCenter) || (v1IsNonCenter && v2IsNonCenter)) {
            continue;
        } else if (v1IsCenter && !v2IsNonCenter) {
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if (v2IsCenter && !v1IsNonCenter) {
            NonCenter.add(v1);
            similarityGraph.addEdge(v1, v2);
        }
    }

    return getConnectedComponents();
}