Example usage for edu.stanford.nlp.util BinaryHeapPriorityQueue removeFirst

List of usage examples for edu.stanford.nlp.util BinaryHeapPriorityQueue removeFirst

Introduction

In this page you can find the example usage for edu.stanford.nlp.util BinaryHeapPriorityQueue removeFirst.

Prototype

@Override
public E removeFirst() 

Source Link

Document

Finds the E with the highest priority, removes it, and returns it.

Usage

From source file:de.up.ling.irtg.automata.condensed.CondensedBestFirstIntersectionAutomaton.java

@Override
public void makeAllRulesExplicit() {
    if (!isExplicit) {
        isExplicit = true;/* www. j  a  va 2s  .  com*/

        getStateInterner().setTrustingMode(true);

        right.makeAllRulesCondensedExplicit();

        BinaryHeapPriorityQueue<Integer> agenda = new BinaryHeapPriorityQueue<>();

        IntSet seenStates = new IntOpenHashSet(); // all states that have ever been added to the agenda
        IntSet removedStates = new IntOpenHashSet(); // all states that have ever been dequeued from the agenda

        // initialize agenda with nullary rules
        int[] emptyChildren = new int[0];

        for (CondensedRule rightRule : right.getCondensedRulesBottomUpFromExplicit(emptyChildren)) {
            //                System.err.println("right: " + rightRule.toString(right));

            IntSet rightLabels = rightRule.getLabels(right);
            for (int rightLabel : rightLabels) {
                int leftLabel = leftToRightSignatureMapper.remapBackward(rightLabel);
                for (Rule leftRule : left.getRulesBottomUp(leftLabel, emptyChildren)) {
                    //                        System.err.println("left: " + leftRule.toString(left));
                    Rule rule = combineRules(leftRule, rightRule);
                    storeRule(rule);

                    if (Math.log(rule.getWeight()) > viterbiScore.getOrDefault(rule.getParent(),
                            Double.NEGATIVE_INFINITY)) {
                        viterbiScore.put(rule.getParent(), Math.log(rule.getWeight()));
                    }

                    double estimate = edgeEvaluator.evaluate(leftRule.getParent(), rightRule.getParent(),
                            rule.getWeight());

                    agenda.add(rule.getParent(), estimate);
                    seenStates.add(rule.getParent());
                    //                        System.err.println("Addding " + rule.getParent() + " with estimate " + estimate);

                }
            }
        }

        // iterate until agenda is empty
        List<IntSet> remappedChildren = new ArrayList<IntSet>();

        while (!agenda.isEmpty()) {

            int statePairID = agenda.removeFirst();
            int rightState = stateToRightState.get(statePairID);
            int leftState = stateToLeftState.get(statePairID);

            // register statePairID as "removed from agenda"
            removedStates.add(statePairID);

            // If left & right state are final, leave the loop.
            if (right.getFinalStates().contains(rightState) && left.getFinalStates().contains(leftState)) {
                break;
            }

            rightRuleLoop: for (CondensedRule rightRule : right.getCondensedRulesForRhsState(rightState)) {
                remappedChildren.clear();

                // iterate over all children in the right rule
                for (int i = 0; i < rightRule.getArity(); ++i) {
                    IntSet partners = getPartners(rightRule.getChildren()[i]);

                    if (partners == null) {
                        continue rightRuleLoop;
                    } else {
                        remappedChildren.add(new IntArraySet(partners)); //!(1) Make a copy of partners, it could be changed while iterating over it
                    }
                }

                left.foreachRuleBottomUpForSets(rightRule.getLabels(right), remappedChildren,
                        leftToRightSignatureMapper, leftRule -> {
                            Rule rule = combineRules(leftRule, rightRule); //!(1) 'partners' could be changed here (-> addStatePair)

                            storeRule(rule);

                            double insideScore = Math.log(rule.getWeight());

                            for (int child : rule.getChildren()) {
                                assert viterbiScore.containsKey(child);
                                insideScore += viterbiScore.get(child);
                            }

                            double estimate = edgeEvaluator.evaluate(leftRule.getParent(),
                                    rightRule.getParent(), insideScore);
                            // Update viterbi score if needed
                            if (insideScore > viterbiScore.getOrDefault(rule.getParent(),
                                    Double.NEGATIVE_INFINITY)) {
                                viterbiScore.put(rule.getParent(), insideScore);
                            }

                            // add new parent state to chart
                            seenStates.add(rule.getParent());

                            // if parent state is new or still on the agenda,
                            // give it a chance to move closer to the front of the agenda
                            if (!removedStates.contains(rule.getParent())) {
                                agenda.relaxPriority(rule.getParent(), estimate);
                            }
                        });
            }
        }

        finalStates = null;
    }
}