Java Code Examples for gnu.trove.map.hash.TIntDoubleHashMap#iterator()
The following examples show how to use
gnu.trove.map.hash.TIntDoubleHashMap#iterator() .
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: CollectionUtils.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
/** * Normalizes values so that they sum up to 1. * * @param values * @return Normalized values. */ public static TIntDoubleHashMap normalizeValuesToSum(TIntDoubleHashMap values) { TIntDoubleHashMap normalizedScores = new TIntDoubleHashMap(); double total = 0; for (TIntDoubleIterator itr = values.iterator(); itr.hasNext(); ) { itr.advance(); total += itr.value(); } if (total == 0) { return values; } for (TIntDoubleIterator itr = values.iterator(); itr.hasNext(); ) { itr.advance(); Double normalizedScore = itr.value() / total; normalizedScores.put(itr.key(), normalizedScore); } return normalizedScores; }
Example 2
Source File: PriorProbability.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
/** * Returns the prior probability for the given mention-entity pair. * If smoothing is true, it will return the lowest prior among all entities if * there is no real prior. * * @param mention * @param entity * @param smoothing * @return */ public double getPriorProbability(Mention mention, Entity entity, boolean smoothing) { Integer id = RunningTimer.recordStartTime("PriorProbability"); TIntDoubleHashMap allMentionPriors = priors.get(mention); double entityPrior = allMentionPriors.get(entity.getId()); if (smoothing && entityPrior == 0.0) { double smallestPrior = 1.0; for (TIntDoubleIterator it = allMentionPriors.iterator(); it.hasNext(); ) { it.advance(); double currentPrior = it.value(); if (currentPrior < smallestPrior) { smallestPrior = currentPrior; } } entityPrior = smallestPrior; } RunningTimer.recordEndTime("PriorProbability", id); return entityPrior; }
Example 3
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
private boolean isNodeRemovable(Graph graph, int nodeId) { GraphNode node = graph.getNode(nodeId); if (node.getType() == GraphNodeTypes.MENTION) // this is a mention node return false; // Check if the entity is removable TIntDoubleHashMap successorsMap = node.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorNodeId = successorsIterator.key(); GraphNode successorNode = graph.getNode(successorNodeId); // if mention and mention connected to only one entity if (successorNode.getType() == GraphNodeTypes.MENTION && mentionDegrees.get(successorNodeId) == 1) { return false; } } return true; }
Example 4
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
private Map<Integer, Double> getConnectedEntities(Graph graph, int nodeId) { Map<Integer, Double> entities = new HashMap<Integer, Double>(); GraphNode entityNode = graph.getNode(nodeId); TIntDoubleHashMap successorsMap = entityNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorId = successorsIterator.key(); GraphNode successorNode = graph.getNode(successorId); if (successorNode.getType() == GraphNodeTypes.ENTITY) { int entity = (int) successorNode.getNodeData(); double weight = successorsIterator.value(); entities.put(entity, weight); } } return entities; }
Example 5
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
private TIntLinkedList getEntityMentionsNodesIds(Graph graph, int entityNodeId) { TIntLinkedList mentions = new TIntLinkedList(); GraphNode entityNode = graph.getNode(entityNodeId); TIntDoubleHashMap successorsMap = entityNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorId = successorsIterator.key(); GraphNode successorNode = graph.getNode(successorId); if (successorNode.getType() == GraphNodeTypes.MENTION) { mentions.add(successorId); } } return mentions; }
Example 6
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 6 votes |
/** * Get the best candidate and (normalized) score from the given entity-score map. * */ private Pair<Integer, Double> getBestLocalCandidateAndScore(TIntDoubleHashMap entityCandidates) { if (entityCandidates.size() == 0) { return new Pair<Integer, Double>(-100, 0.0); } double bestScore = -1.0; int bestCandidate = -10; for (TIntDoubleIterator itr = entityCandidates.iterator(); itr.hasNext(); ) { itr.advance(); int entityId = itr.key(); double score = itr.value(); if (score > bestScore) { bestScore = score; bestCandidate = entityId; } } if (computeConfidence) { TIntDoubleHashMap normalizedScores = CollectionUtils.normalizeValuesToSum(entityCandidates); bestScore = normalizedScores.get(bestCandidate); } return new Pair<>(new Integer(bestCandidate), new Double(bestScore)); }
Example 7
Source File: CollectionUtils.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
public static double getMaxValue(TIntDoubleHashMap map) { if (map.isEmpty()) { return 0.0; } double max = -Double.MAX_VALUE; for (TIntDoubleIterator itr = map.iterator(); itr.hasNext(); ) { itr.advance(); max = Math.max(itr.value(), max); } return max; }
Example 8
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
private void traceFinalGraphStructure(Graph graph) { for (int menNodeId : mentionDegrees.keySet()) { GraphNode menNode = graph.getNode(menNodeId); Mention mention = (Mention) menNode.getNodeData(); TIntDoubleHashMap successorsMap = menNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorNodeId = successorsIterator.key(); if (!bestRemoved[successorNodeId]) { double sim = 0; double weight = 0.0; if (bestWeightedDegrees.containsKey(successorNodeId)) { weight = bestWeightedDegrees.get(successorNodeId); } else if (notRemovableEntityWeightedDegrees.containsKey(successorNodeId)) { weight = notRemovableEntityWeightedDegrees.get(successorNodeId); } else { weight = GraphTracer.gTracer.getRemovedEntityDegree(graph.getName(), (int) graph.getNode(successorNodeId).getNodeData()); } GraphNode entityNode = graph.getNode(successorNodeId); int entity = (int) entityNode.getNodeData(); GraphTracer.gTracer.addCandidateEntityToFinalGraph(graph.getName(), mention.getIdentifiedRepresentation(), entity, weight, sim); } } } GraphTracer.gTracer.cleanRemovalSteps(graph.getName()); }
Example 9
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
private void traceCleanedGraphStructure(Graph graph) { for (int menNodeId : mentionDegrees.keySet()) { GraphNode menNode = graph.getNode(menNodeId); Mention mention = (Mention) menNode.getNodeData(); TIntDoubleHashMap successorsMap = menNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorNodeId = successorsIterator.key(); if (!graph.isRemoved(successorNodeId)) { double sim = 0; double weight = 0.0; if (entityWeightedDegrees.containsKey(successorNodeId)) { weight = entityWeightedDegrees.get(successorNodeId); } else { weight = notRemovableEntityWeightedDegrees.get(successorNodeId); } GraphNode entityNode = graph.getNode(successorNodeId); int entity = (int) entityNode.getNodeData(); GraphTracer.gTracer.addCandidateEntityToCleanedGraph(graph.getName(), mention.getIdentifiedRepresentation(), entity, weight, sim); } } } }
Example 10
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
private void traceIntitialGraphStructure(Graph graph) { for (int menNodeId : mentionDegrees.keySet()) { GraphNode menNode = graph.getNode(menNodeId); Mention mention = (Mention) menNode.getNodeData(); TIntDoubleHashMap successorsMap = menNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorNodeId = successorsIterator.key(); double sim = successorsIterator.value(); double weight = 0.0; if (entityWeightedDegrees.containsKey(successorNodeId)) { weight = entityWeightedDegrees.get(successorNodeId); } else { weight = notRemovableEntityWeightedDegrees.get(successorNodeId); } GraphNode entityNode = graph.getNode(successorNodeId); int entity = (int) entityNode.getNodeData(); GraphTracer.gTracer.addCandidateEntityToOriginalGraph(graph.getName(), mention.getIdentifiedRepresentation(), entity, weight, sim, getConnectedEntities(graph, successorNodeId)); } } }
Example 11
Source File: GraphConfidenceEstimator.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
/** * Returns all local scores computed during the graph creation. As some * entities are dropped (graph coherence), they will be missing from the * current graph. These entities will still be present in the returned * map with negative scores. This is necessary for proper normalization. */ private Map<Integer, Double> getMentionEntityLocalScores(Graph g, int mentionId) { Map<Integer, Double> scores = new HashMap<Integer, Double>(); // Don't get the local scores from the graph edges, they are incomplete when // candidate entities are dropped due to the coherence robustness test. // The graph contains all scores as well in a different variable. Mention mention = (Mention) g.getNode(mentionId).getNodeData(); TIntDoubleHashMap entitySims = g.getMentionEntitySims(mention); if (entitySims == null) { return new HashMap<Integer, Double>(); } TIntIntHashMap entity2id = g.getEntityNodesIds(); for (TIntDoubleIterator itr = entitySims.iterator(); itr.hasNext(); ) { itr.advance(); // If the entity is not present in the graph anymore, assign a new, // negative one. The negative ids will never be queried, they are // just there for the score normalization. Integer entityId = 0; if (!entity2id.contains(itr.key())) { entityId = outOfGraphEntityId; --outOfGraphEntityId; } else { entityId = entity2id.get(itr.key()); } scores.put(entityId, itr.value()); } return scores; }
Example 12
Source File: GraphGenerator.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
private double calcL1(TIntDoubleHashMap priorDistribution, TIntDoubleHashMap simDistribution) { double l1 = 0.0; for (TIntDoubleIterator itr = priorDistribution.iterator(); itr.hasNext(); ) { itr.advance(); double prior = itr.value(); double sim = simDistribution.get(itr.key()); double diff = Math.abs(prior - sim); l1 += diff; } return l1; }
Example 13
Source File: CollectionUtils.java From ambiverse-nlu with Apache License 2.0 | 5 votes |
/** * Convenience method for the call above. * * @param elementProbabilities Map with elements as keys and their * probabilities as values. Values are expected to sum up to 1. * @param rand Random generator to use. * @return Randomly selected element according to probabilities. */ public static Integer getConditionalElement(TIntDoubleHashMap elementProbabilities, Random rand) { Integer[] elements = new Integer[elementProbabilities.size()]; double[] probs = new double[elementProbabilities.size()]; double currentProb = 0.0; int i = 0; for (TIntDoubleIterator itr = elementProbabilities.iterator(); itr.hasNext(); ) { itr.advance(); elements[i] = itr.key(); currentProb += itr.value(); probs[i] = currentProb; ++i; } return getConditionalElement(elements, probs, rand); }
Example 14
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
/** * 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 15
Source File: CocktailPartySizeConstrained.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
protected void removeInitialEntitiesByDistance(Graph graph) { ArrayList<Integer> toRemove = new ArrayList<Integer>(); int nodesCount = graph.getNodesCount(); double[][] allDistances = new double[nodesCount][nodesCount]; fillDistances(graph, allDistances); Map<Integer, Double> entityDistances = new HashMap<Integer, Double>(); for (int q : entityWeightedDegrees.keySet()) { if (graph.isRemoved(q)) continue; double entityDistance = calcEntityDistance(allDistances[q]); entityDistances.put(q, entityDistance); } List<Entry<Integer, Double>> entries = new ArrayList<Entry<Integer, Double>>(entityDistances.entrySet()); Collections.sort(entries, new Comparator<Entry<Integer, Double>>() { @Override public int compare(Entry<Integer, Double> e0, Entry<Integer, Double> e1) { return Double.compare(e0.getValue(), e1.getValue()); } }); Map<Integer, Double> sortedEntityDistances = new LinkedHashMap<Integer, Double>(); for (Entry<Integer, Double> entry : entries) { sortedEntityDistances.put(entry.getKey(), entry.getValue()); } HashMap<Integer, Integer> checkMentionDegree = new HashMap<Integer, Integer>(); HashMap<Integer, Double> mentionMaxWeightedDegree = new HashMap<Integer, Double>(); HashMap<Integer, Integer> mentionMaxEntity = new HashMap<Integer, Integer>(); int numberToKeep = (int) Math.ceil(mentionDegrees.size() * initialGraphSize); int i = 0; for (int entityNodeId : sortedEntityDistances.keySet()) { i++; if (i > numberToKeep) { toRemove.add(entityNodeId); GraphNode entityNode = graph.getNode(entityNodeId); TIntDoubleHashMap successorsMap = entityNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int s = successorsMap.size(); s-- > 0; ) { successorsIterator.advance(); int succId = successorsIterator.key(); if (!graph.isEntityNode(succId)) { if (checkMentionDegree.get(succId) == null) checkMentionDegree.put(succId, 1); else checkMentionDegree.put(succId, 1 + checkMentionDegree.get(succId)); double weightedDegree = entityWeightedDegrees.get(entityNodeId); if (mentionMaxWeightedDegree.get(succId) == null) { mentionMaxWeightedDegree.put(succId, weightedDegree); mentionMaxEntity.put(succId, entityNodeId); } else { if (weightedDegree > mentionMaxWeightedDegree.get(succId)) { mentionMaxWeightedDegree.put(succId, weightedDegree); mentionMaxEntity.put(succId, entityNodeId); } } } // end mention neighbor }// end scanning neighbors of the entity selected // for // removal. } } removeAndUpdateEntities(graph, toRemove, checkMentionDegree, mentionMaxEntity, mentionMaxWeightedDegree); }
Example 16
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
/** * Fill in the solution, compute average closeness. Return the mapping * in the original graph as byproduct. * * @param finalEntities * @param allCloseness * @return mention-entity mapping in the original graph. */ private Map<Integer, Integer> fillInSolutionObject(Graph graph, HashSet<Integer> finalEntities, double[][] allCloseness) { Map<Integer, Integer> graphMapping = new HashMap<Integer, Integer>(); for (int mentionNodeId : bestMentionDegrees.keySet()) { GraphNode mentionNode = graph.getNode(mentionNodeId); Mention mention = (Mention) mentionNode.getNodeData(); ResultMention rm = new ResultMention(mention.getMention(), mention.getCharOffset(), mention.getCharLength()); int mentionOutdegree = graph.getNodeOutdegree(mentionNodeId); if (mentionOutdegree == 0) { solution.put(rm, ResultEntity.getResultEntityAsList(ResultEntity.getNoMatchingEntity())); graphMapping.put(mentionNodeId, -1); } else { TIntDoubleHashMap successorsMap = mentionNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int entityNodeId = successorsIterator.key(); double mentionEntitySimilarity = successorsIterator.value(); if (finalEntities.contains(entityNodeId)) { double confidence = mentionEntitySimilarity; double averageCloseness = 0.0; for (int otherMention : bestMentionDegrees.keySet()) { if (otherMention == mentionNodeId || allCloseness[entityNodeId][otherMention] == Double.NEGATIVE_INFINITY) { continue; } averageCloseness += allCloseness[entityNodeId][otherMention]; } int numOtherMentions = bestMentionDegrees.keySet().size() - 1; if (numOtherMentions > 0) { averageCloseness = averageCloseness / numOtherMentions; } confidence += averageCloseness; GraphNode entityNode = graph.getNode(entityNodeId); int entityInternalId = (int) entityNode.getNodeData(); Entity entity = allEntities_.getEntityById(entityInternalId); List<ResultEntity> res = new ArrayList<ResultEntity>(1); res.add(new ResultEntity(entity, confidence)); graphMapping.put(mentionNodeId, entityNodeId); solution.put(rm, res); } } } } return graphMapping; }
Example 17
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
protected void removeInitialEntitiesByDistance(Graph graph) { ArrayList<Integer> toRemove = new ArrayList<Integer>(); double[][] allDistances = new double[graph.getNodesCount()][graph.getNodesCount()]; HashMap<Integer, Integer> checkMentionDegree = new HashMap<Integer, Integer>(); HashMap<Integer, Double> mentionMaxWeightedDegree = new HashMap<Integer, Double>(); HashMap<Integer, Integer> mentionMaxEntity = new HashMap<Integer, Integer>(); for (int m : mentionDegrees.keySet()) { double[] shortest = shortestPath.run(m, graph); for (int e : entityWeightedDegrees.keySet()) { allDistances[e][m] = shortest[e]; } } // end distance loop for (GraphNode node : graph.getNodes()) { int nodeId = node.getId(); if (graph.isRemoved(nodeId)) continue; // If the node is a mention, skip. if (node.getType() == GraphNodeTypes.MENTION) { continue; } double entityDistance = calcEntityDistance(allDistances[nodeId]); if (entityDistance > distanceThreshold_) { TIntDoubleHashMap successorsMap = node.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorNodeId = successorsIterator.key(); if (!graph.isEntityNode(successorNodeId)) { if (checkMentionDegree.get(successorNodeId) == null) checkMentionDegree.put(successorNodeId, 1); else checkMentionDegree.put(successorNodeId, 1 + checkMentionDegree.get(successorNodeId)); double weightedDegree = entityWeightedDegrees.get(nodeId); if (mentionMaxWeightedDegree.get(successorNodeId) == null) { mentionMaxWeightedDegree.put(successorNodeId, weightedDegree); mentionMaxEntity.put(successorNodeId, nodeId); } else { if (weightedDegree > mentionMaxWeightedDegree.get(successorNodeId)) { mentionMaxWeightedDegree.put(successorNodeId, weightedDegree); mentionMaxEntity.put(successorNodeId, nodeId); } } } // end mention neighbor }// end scanning neighbors of the entity selected // for // removal. if (!toRemove.contains(nodeId)) toRemove.add(nodeId); } } removeAndUpdateEntities(graph, toRemove, checkMentionDegree, mentionMaxEntity, mentionMaxWeightedDegree); }
Example 18
Source File: CocktailParty.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
private double firstScanAndCalculateInitialObjective(Graph graph) throws IOException { double initialObjective = Double.POSITIVE_INFINITY; for (GraphNode node : graph.getNodes()) { if (node.getId() == null) { logger.error("ERROR: node has no id: " + node.getNodeData() + " " + node.getType()); } int nodeId = node.getId(); int degree = graph.getNodeOutdegree(nodeId); if (graph.isMentionNode(nodeId)) { // mention node mentionDegrees.put(nodeId, degree); bestMentionDegrees.put(nodeId, degree); } else { // entity node double weightedDegree = graph.getNodeWeightedDegrees(nodeId); boolean notRemovable = false; TIntDoubleHashMap successorsMap = node.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorId = successorsIterator.key(); if (graph.isMentionNode(successorId)) { // The current successor is a mention if (graph.getNodeOutdegree(successorId) == 1) notRemovable = true; } } if (notRemovable) { notRemovableEntityWeightedDegrees.put(nodeId, weightedDegree); notRemovableEntitySortedDegrees.add(nodeId + ":::" + weightedDegree); } else { entitySortedDegrees.add(nodeId + ":::" + weightedDegree); entityWeightedDegrees.put(nodeId, weightedDegree); } if (weightedDegree < initialObjective) { initialObjective = weightedDegree; } } } return initialObjective; }
Example 19
Source File: GreedyHillClimbing.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
/** * Exhaustive enumeration of all the possible combinations * * @param graphName */ public Map<Integer, Integer> runExhaustive(String graphName) throws Exception { long possibleCombinations = 1; logger.debug("Computing the initial solution..."); Map<Integer, Integer> bestChoice = new HashMap<Integer, Integer>(); double bestTotalWeight = 0.0; for (int mentionNodeId : mentionNodes) { // This should be needed, as we cannot remove mentions if (inputGraph.isRemoved(mentionNodeId)) continue; GraphNode mentionNode = inputGraph.getNode(mentionNodeId); int actualOutdegree = 0; List<Integer> actualSuccessors = new LinkedList<Integer>(); TIntDoubleHashMap successorsMap = mentionNode.getSuccessors(); TIntDoubleIterator successorsIterator = successorsMap.iterator(); for (int i = successorsMap.size(); i-- > 0; ) { successorsIterator.advance(); int successorId = successorsIterator.key(); if (inputGraph.isRemoved(successorId)) continue; actualOutdegree++; actualSuccessors.add(successorId); } if (actualOutdegree > 0) { actualMentionDegrees.put(mentionNodeId, actualOutdegree); actualMentionSuccessors.put(mentionNodeId, actualSuccessors.toArray(new Integer[actualOutdegree])); possibleCombinations *= actualOutdegree; if (possibleCombinations < 0) { // overflow possibleCombinations = Long.MAX_VALUE; } } } // end choosing first entity for every mention if (possibleCombinations > this.maxCombinationsExhaustive) { logger.debug("The combinations to check are " + possibleCombinations); logger.debug("Applying local search"); GraphTracer.gTracer.addStat(graphName, "Exhaustive search not applied, too many combinations", Long.toString(possibleCombinations)); return null; } logger.debug("Number of possible combinations that need to be checked: " + possibleCombinations); GraphTracer.gTracer.addStat(graphName, "Exhaustively searching number of combinations", Long.toString(possibleCombinations)); Integer[] mentionIds = actualMentionDegrees.keySet().toArray(new Integer[actualMentionDegrees.keySet().size()]); if (possibleCombinations > 0) { Map<Integer, Integer> currentConfiguration = new HashMap<Integer, Integer>(); GraphConfiguration gc = permuteRecursive(mentionIds, 0, currentConfiguration); bestChoice = gc.getMapping(); bestTotalWeight = gc.getWeight(); } logger.debug("Checked " + possibleCombinations + " combinations"); logger.debug("The final solution has total weight " + bestTotalWeight); GraphTracer.gTracer.addStat(graphName, "Objective value after Exhaustive Search", Double.toString(bestTotalWeight)); return bestChoice; }
Example 20
Source File: EncoderDecoderKryoTest.java From ambiverse-nlu with Apache License 2.0 | 4 votes |
@Test public void test() throws IOException { EncoderDecoderKryo<Integer> encoderInteger = new EncoderDecoderKryo(Integer.class); int a = 845739038; byte[] bytes = encoderInteger.encode(a); int resultInt = encoderInteger.decode(bytes); assertEquals(a, resultInt); EncoderDecoderKryo<Double> encoderDouble = new EncoderDecoderKryo(Double.class); double b = 845739038; bytes = encoderDouble.encode(b); double resultDouble = encoderDouble.decode(bytes); assertEquals(b, resultDouble, 0.00001); EncoderDecoderKryo<String> encoderString = new EncoderDecoderKryo(String.class); String str = "hola"; bytes = encoderString.encode(str); String resultString = encoderString.decode(bytes); assertEquals(str, resultString); EncoderDecoderKryo<String[]> encoderStringArray = new EncoderDecoderKryo(String[].class); String[] strArray = new String[] { "hola", "y", "chau" }; bytes = encoderStringArray.encode(strArray); String[] resultStringArray = encoderStringArray.decode(bytes); for (int i = 0; i < strArray.length; i++) { assertEquals(strArray[i], resultStringArray[i]); } EncoderDecoderKryo<KeyValueStoreRow[]> keyvalueStoreRowArray = new EncoderDecoderKryo(KeyValueStoreRow[].class); Object[] elements = new Object[] { a, b, str }; KeyValueStoreRow kvs1 = new KeyValueStoreRow(elements); Object[] elements2 = new Object[] { str, b, a }; KeyValueStoreRow kvs2 = new KeyValueStoreRow(elements2); KeyValueStoreRow[] kvsr = new KeyValueStoreRow[] { kvs1, kvs2 }; bytes = keyvalueStoreRowArray.encode(kvsr); KeyValueStoreRow[] kvsrResultl = keyvalueStoreRowArray.decode(bytes); assertEquals(kvsr[0].getInt(0), kvsrResultl[0].getInt(0)); assertEquals(kvsr[0].getDouble(1), kvsrResultl[0].getDouble(1), 0.00001); assertEquals(kvsr[0].getString(2), kvsr[0].getString(2)); assertEquals(kvsr[1].getInt(2), kvsrResultl[1].getInt(2)); assertEquals(kvsr[1].getDouble(1), kvsrResultl[1].getDouble(1), 0.00001); assertEquals(kvsr[1].getString(0), kvsr[1].getString(0)); EncoderDecoderKryo<TIntDoubleHashMap> tintDoubleHashMap = new EncoderDecoderKryo(TIntDoubleHashMap.class); TIntDoubleHashMap object = new TIntDoubleHashMap(2, Constants.DEFAULT_LOAD_FACTOR); object.put(7, 8.19); object.put(15, 7.9); bytes = tintDoubleHashMap.encode(object); TIntDoubleHashMap result = tintDoubleHashMap.decode(bytes); TIntDoubleIterator it = result.iterator(); int index = 0; while (it.hasNext()) { it.advance(); if (index == 0) { assertEquals(7, it.key()); assertEquals(it.value(), 8.19, 0.00001); } else { assertEquals(15, it.key()); assertEquals(it.value(), 7.9, 0.00001); } index++; } }