Java Code Examples for gnu.trove.map.hash.TIntDoubleHashMap#size()

The following examples show how to use gnu.trove.map.hash.TIntDoubleHashMap#size() . 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: GraphConfidenceEstimator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private Map<Integer, Double> getMentionEntityWeightedDegreeScores(Graph g, Configuration configuration, int mentionId) {
  Map<Integer, Double> scores = new HashMap<Integer, Double>();
  // When coherence robustness is enabled, all but one candidate entities
  // are dropped and the (normalized) score is always 1 (and thus useless).
  // Use the original scores if they are available.
  GraphNode mentionNode = g.getNode(mentionId);
  TIntDoubleHashMap successors = mentionNode.getSuccessors();
  if (successors.size() > 1) {
    for (TIntDoubleIterator itr = g.getNode(mentionId).getSuccessors().iterator(); itr.hasNext(); ) {
      itr.advance();
      int entity = itr.key();
      scores.put(entity, computeWeightedDegree(g, configuration, entity));
    }
  } else {
    // Use local scores.
    scores = getMentionEntityLocalScores(g, mentionId);
  }
  return scores;
}
 
Example 2
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/**
 * 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 3
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
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 4
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
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 vote down vote up
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 6
Source File: CollectionUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: GraphConfidenceEstimator.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private Configuration getRandomConfiguration(Graph g, Map<Integer, Integer> solution, float mentionFlipPercentage) {
  Configuration flippedConfiguration = new Configuration();

  // Solution has at least 2 mentions, other case is handled in estimate().
  // Decide number of mentions to switch - at least 1, at most 20%.
  int mentionSize = Math.round(solution.size() * mentionFlipPercentage);
  mentionSize = Math.max(1, mentionSize);
  int numFlips = Math.max(1, random_.nextInt(mentionSize));
  TIntSet flipCandidates = getFlipCandidates(g, solution);
  TIntSet flippedMentions = getRandomElements(flipCandidates, numFlips);
  flippedConfiguration.flippedMentions_ = flippedMentions;
  Map<Integer, Integer> flippedSolution = new HashMap<Integer, Integer>(solution);
  for (TIntIterator itr = flippedMentions.iterator(); itr.hasNext(); ) {
    int mentionId = itr.next();
    TIntDoubleHashMap entityCandidates = new TIntDoubleHashMap(getConnectedEntitiesWithScores(g_, mentionId));
    // Remove correct solution from candidates - it should not be chosen
    // when flipping.
    entityCandidates.remove(solution.get(mentionId));
    // Put placeholder if resembling a missing entity (will not contribute
    // to coherence at all).
    Integer flippedEntity = -1;
    if (entityCandidates.size() > 0) {
      TIntDoubleHashMap entityCandidateProbabilities = CollectionUtils.normalizeValuesToSum(entityCandidates);
      flippedEntity = getRandomEntity(mentionId, entityCandidateProbabilities, random_);
    }
    flippedSolution.put(mentionId, flippedEntity);
  }
  flippedConfiguration.mapping_ = flippedSolution;
  // Store active nodes in graph for faster lookup.
  flippedConfiguration.presentGraphNodes_ = new TIntHashSet();
  for (Entry<Integer, Integer> entry : flippedSolution.entrySet()) {
    flippedConfiguration.presentGraphNodes_.add(entry.getKey());
    flippedConfiguration.presentGraphNodes_.add(entry.getValue());
  }
  //    logger_.debug("Flipped " + flippedMentions.size() + " mentions: " +
  //                  flippedMentions);
  return flippedConfiguration;
}
 
Example 8
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
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 9
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
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 vote down vote up
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 11
Source File: GreedyHillClimbing.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/**
 * 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 12
Source File: CocktailPartySizeConstrained.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
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 13
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 14
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/**
 * 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 15
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
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 16
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
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;

}