Search in sources :

Example 1 with RuntimeInterruptedException

use of edu.stanford.nlp.util.RuntimeInterruptedException in project CoreNLP by stanfordnlp.

the class AbstractBatchOptimizer method optimize.

public <T> ConcatVector optimize(T[] dataset, AbstractDifferentiableFunction<T> fn, ConcatVector initialWeights, double l2regularization, double convergenceDerivativeNorm, boolean quiet) {
    if (!quiet)
        log.info("\n**************\nBeginning training\n");
    else
        log.info("[Beginning quiet training]");
    TrainingWorker<T> mainWorker = new TrainingWorker<>(dataset, fn, initialWeights, l2regularization, convergenceDerivativeNorm, quiet);
    new Thread(mainWorker).start();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    if (!quiet) {
        log.info("NOTE: you can press any key (and maybe ENTER afterwards to jog stdin) to terminate learning early.");
        log.info("The convergence criteria are quite aggressive if left uninterrupted, and will run for a while");
        log.info("if left to their own devices.\n");
        while (true) {
            if (mainWorker.isFinished) {
                log.info("training completed without interruption");
                return mainWorker.weights;
            }
            try {
                if (br.ready()) {
                    log.info("received quit command: quitting");
                    log.info("training completed by interruption");
                    mainWorker.isFinished = true;
                    return mainWorker.weights;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        while (!mainWorker.isFinished) {
            synchronized (mainWorker.naturalTerminationBarrier) {
                try {
                    mainWorker.naturalTerminationBarrier.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeInterruptedException(e);
                }
            }
        }
        log.info("[Quiet training complete]");
        return mainWorker.weights;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException)

Example 2 with RuntimeInterruptedException

use of edu.stanford.nlp.util.RuntimeInterruptedException in project CoreNLP by stanfordnlp.

the class NERCombinerAnnotator method doOneSentence.

@Override
public void doOneSentence(Annotation annotation, CoreMap sentence) {
    List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
    // only used if try assignment works.
    List<CoreLabel> output;
    if (tokens.size() <= this.maxSentenceLength) {
        try {
            output = this.ner.classifySentenceWithGlobalInformation(tokens, annotation, sentence);
        } catch (RuntimeInterruptedException e) {
            // If we get interrupted, set the NER labels to the background
            // symbol if they are not already set, then exit.
            output = null;
        }
    } else {
        output = null;
    }
    if (output == null) {
        doOneFailedSentence(annotation, sentence);
    } else {
        for (int i = 0, sz = tokens.size(); i < sz; ++i) {
            // add the named entity tag to each token
            String neTag = output.get(i).get(CoreAnnotations.NamedEntityTagAnnotation.class);
            String normNeTag = output.get(i).get(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class);
            Map<String, Double> neTagProbMap = output.get(i).get(CoreAnnotations.NamedEntityTagProbsAnnotation.class);
            tokens.get(i).setNER(neTag);
            tokens.get(i).set(CoreAnnotations.NamedEntityTagProbsAnnotation.class, neTagProbMap);
            tokens.get(i).set(CoreAnnotations.CoarseNamedEntityTagAnnotation.class, neTag);
            if (normNeTag != null)
                tokens.get(i).set(CoreAnnotations.NormalizedNamedEntityTagAnnotation.class, normNeTag);
            NumberSequenceClassifier.transferAnnotations(output.get(i), tokens.get(i));
        }
        if (VERBOSE) {
            boolean first = true;
            StringBuilder sb = new StringBuilder("NERCombinerAnnotator output: [");
            for (CoreLabel w : tokens) {
                if (first) {
                    first = false;
                } else {
                    sb.append(", ");
                }
                sb.append(w.toShorterString("Text", "NamedEntityTag", "NormalizedNamedEntityTag"));
            }
            sb.append(']');
            log.info(sb);
        }
    }
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations)

Example 3 with RuntimeInterruptedException

use of edu.stanford.nlp.util.RuntimeInterruptedException in project CoreNLP by stanfordnlp.

the class BeamBestSequenceFinder method bestSequence.

public int[] bestSequence(SequenceModel ts, int size) {
    // Set up tag options
    int length = ts.length();
    int leftWindow = ts.leftWindow();
    int rightWindow = ts.rightWindow();
    int padLength = length + leftWindow + rightWindow;
    int[][] tags = new int[padLength][];
    int[] tagNum = new int[padLength];
    for (int pos = 0; pos < padLength; pos++) {
        tags[pos] = ts.getPossibleValues(pos);
        tagNum[pos] = tags[pos].length;
    }
    Beam newBeam = new Beam(beamSize, ScoredComparator.ASCENDING_COMPARATOR);
    TagSeq initSeq = new TagSeq();
    newBeam.add(initSeq);
    for (int pos = 0; pos < padLength; pos++) {
        if (Thread.interrupted()) {
            // Allow interrupting
            throw new RuntimeInterruptedException();
        }
        //System.out.println("scoring word " + pos + " / " + (leftWindow + length) + ", tagNum = " + tagNum[pos] + "...");
        //System.out.flush();
        Beam oldBeam = newBeam;
        if (pos < leftWindow + rightWindow && exhaustiveStart) {
            newBeam = new Beam(100000, ScoredComparator.ASCENDING_COMPARATOR);
        } else {
            newBeam = new Beam(beamSize, ScoredComparator.ASCENDING_COMPARATOR);
        }
        // each hypothesis gets extended and beamed
        for (Object anOldBeam : oldBeam) {
            if (Thread.interrupted()) {
                // Allow interrupting
                throw new RuntimeInterruptedException();
            }
            // System.out.print("#"); System.out.flush();
            TagSeq tagSeq = (TagSeq) anOldBeam;
            for (int nextTagNum = 0; nextTagNum < tagNum[pos]; nextTagNum++) {
                TagSeq nextSeq = tagSeq.tclone();
                if (pos >= leftWindow + rightWindow) {
                    nextSeq.extendWith(tags[pos][nextTagNum], ts, size);
                } else {
                    nextSeq.extendWith(tags[pos][nextTagNum]);
                }
                //System.out.println("Created: "+nextSeq.score()+" %% "+arrayToString(nextSeq.tags(), nextSeq.size()));
                newBeam.add(nextSeq);
            //		System.out.println("Beam size: "+newBeam.size()+" of "+beamSize);
            //System.out.println("Best is: "+((Scored)newBeam.iterator().next()).score());
            }
        }
        // System.out.println(" done");
        if (recenter) {
            double max = Double.NEGATIVE_INFINITY;
            for (Object aNewBeam1 : newBeam) {
                TagSeq tagSeq = (TagSeq) aNewBeam1;
                if (tagSeq.score > max) {
                    max = tagSeq.score;
                }
            }
            for (Object aNewBeam : newBeam) {
                TagSeq tagSeq = (TagSeq) aNewBeam;
                tagSeq.score -= max;
            }
        }
    }
    try {
        TagSeq bestSeq = (TagSeq) newBeam.iterator().next();
        int[] seq = bestSeq.tags();
        return seq;
    } catch (NoSuchElementException e) {
        log.info("Beam empty -- no best sequence.");
        return null;
    }
/*
    int[] tempTags = new int[padLength];

    // Set up product space sizes
    int[] productSizes = new int[padLength];

    int curProduct = 1;
    for (int i=0; i<leftWindow+rightWindow; i++)
      curProduct *= tagNum[i];
    for (int pos = leftWindow+rightWindow; pos < padLength; pos++) {
      if (pos > leftWindow+rightWindow)
	curProduct /= tagNum[pos-leftWindow-rightWindow-1]; // shift off
      curProduct *= tagNum[pos]; // shift on
      productSizes[pos-rightWindow] = curProduct;
    }

    // Score all of each window's options
    double[][] windowScore = new double[padLength][];
    for (int pos=leftWindow; pos<leftWindow+length; pos++) {
      windowScore[pos] = new double[productSizes[pos]];
      Arrays.fill(tempTags,tags[0][0]);
      for (int product=0; product<productSizes[pos]; product++) {
	int p = product;
	int shift = 1;
	for (int curPos = pos+rightWindow; curPos >= pos-leftWindow; curPos--) {
	  tempTags[curPos] = tags[curPos][p % tagNum[curPos]];
	  p /= tagNum[curPos];
	  if (curPos > pos)
	    shift *= tagNum[curPos];
	}
	if (tempTags[pos] == tags[pos][0]) {
	  // get all tags at once
	  double[] scores = ts.scoresOf(tempTags, pos);
	  // fill in the relevant windowScores
	  for (int t = 0; t < tagNum[pos]; t++) {
	    windowScore[pos][product+t*shift] = scores[t];
	  }
	}
      }
    }


    // Set up score and backtrace arrays
    double[][] score = new double[padLength][];
    int[][] trace = new int[padLength][];
    for (int pos=0; pos<padLength; pos++) {
      score[pos] = new double[productSizes[pos]];
      trace[pos] = new int[productSizes[pos]];
    }

    // Do forward Viterbi algorithm

    // loop over the classification spot
    //log.info();
    for (int pos=leftWindow; pos<length+leftWindow; pos++) {
      //log.info(".");
      // loop over window product types
      for (int product=0; product<productSizes[pos]; product++) {
	// check for initial spot
	if (pos==leftWindow) {
	  // no predecessor type
	  score[pos][product] = windowScore[pos][product];
	  trace[pos][product] = -1;
	} else {
	  // loop over possible predecessor types
	  score[pos][product] = Double.NEGATIVE_INFINITY;
	  trace[pos][product] = -1;
	  int sharedProduct = product / tagNum[pos+rightWindow];
	  int factor = productSizes[pos] / tagNum[pos+rightWindow];
	  for (int newTagNum=0; newTagNum<tagNum[pos-leftWindow-1]; newTagNum++) {
	    int predProduct = newTagNum*factor+sharedProduct;
	    double predScore = score[pos-1][predProduct]+windowScore[pos][product];
	    if (predScore > score[pos][product]) {
	      score[pos][product] = predScore;
	      trace[pos][product] = predProduct;
	    }
	  }
	}
      }
    }

    // Project the actual tag sequence
    double bestFinalScore = Double.NEGATIVE_INFINITY;
    int bestCurrentProduct = -1;
    for (int product=0; product<productSizes[leftWindow+length-1]; product++) {
      if (score[leftWindow+length-1][product] > bestFinalScore) {
	bestCurrentProduct = product;
	bestFinalScore = score[leftWindow+length-1][product];
      }
    }
    int lastProduct = bestCurrentProduct;
    for (int last=padLength-1; last>=length-1; last--) {
      tempTags[last] = tags[last][lastProduct % tagNum[last]];
      lastProduct /= tagNum[last];
    }
    for (int pos=leftWindow+length-2; pos>=leftWindow; pos--) {
      int bestNextProduct = bestCurrentProduct;
      bestCurrentProduct = trace[pos+1][bestNextProduct];
      tempTags[pos-leftWindow] = tags[pos-leftWindow][bestCurrentProduct / (productSizes[pos]/tagNum[pos-leftWindow])];
    }
    return tempTags;
    */
}
Also used : Beam(edu.stanford.nlp.util.Beam) RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with RuntimeInterruptedException

use of edu.stanford.nlp.util.RuntimeInterruptedException in project CoreNLP by stanfordnlp.

the class StatisticalCorefAlgorithm method runCoref.

@Override
public void runCoref(Document document) {
    Compressor<String> compressor = new Compressor<>();
    if (Thread.interrupted()) {
        // Allow interrupting
        throw new RuntimeInterruptedException();
    }
    Map<Pair<Integer, Integer>, Boolean> pairs = new HashMap<>();
    for (Map.Entry<Integer, List<Integer>> e : CorefUtils.heuristicFilter(CorefUtils.getSortedMentions(document), maxMentionDistance, maxMentionDistanceWithStringMatch).entrySet()) {
        for (int m1 : e.getValue()) {
            pairs.put(new Pair<>(m1, e.getKey()), true);
        }
    }
    DocumentExamples examples = extractor.extract(0, document, pairs, compressor);
    Counter<Pair<Integer, Integer>> pairwiseScores = new ClassicCounter<>();
    for (Example mentionPair : examples.examples) {
        if (Thread.interrupted()) {
            // Allow interrupting
            throw new RuntimeInterruptedException();
        }
        pairwiseScores.incrementCount(new Pair<>(mentionPair.mentionId1, mentionPair.mentionId2), classifier.predict(mentionPair, examples.mentionFeatures, compressor));
    }
    List<Pair<Integer, Integer>> mentionPairs = new ArrayList<>(pairwiseScores.keySet());
    mentionPairs.sort((p1, p2) -> {
        double diff = pairwiseScores.getCount(p2) - pairwiseScores.getCount(p1);
        return diff == 0 ? 0 : (int) Math.signum(diff);
    });
    Set<Integer> seenAnaphors = new HashSet<>();
    for (Pair<Integer, Integer> pair : mentionPairs) {
        if (seenAnaphors.contains(pair.second)) {
            continue;
        }
        if (Thread.interrupted()) {
            // Allow interrupting
            throw new RuntimeInterruptedException();
        }
        seenAnaphors.add(pair.second);
        MentionType mt1 = document.predictedMentionsByID.get(pair.first).mentionType;
        MentionType mt2 = document.predictedMentionsByID.get(pair.second).mentionType;
        if (pairwiseScores.getCount(pair) > thresholds.get(new Pair<>(mt1 == MentionType.PRONOMINAL, mt2 == MentionType.PRONOMINAL))) {
            CorefUtils.mergeCoreferenceClusters(pair, document);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Pair(edu.stanford.nlp.util.Pair) HashSet(java.util.HashSet) MentionType(edu.stanford.nlp.coref.data.Dictionaries.MentionType) RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with RuntimeInterruptedException

use of edu.stanford.nlp.util.RuntimeInterruptedException in project CoreNLP by stanfordnlp.

the class ShiftReduceParserQuery method parseInternal.

private boolean parseInternal() {
    final int maxBeamSize;
    if (parser.op.testOptions().beamSize == 0) {
        maxBeamSize = Math.max(parser.op.trainOptions().beamSize, 1);
    } else {
        maxBeamSize = parser.op.testOptions().beamSize;
    }
    success = true;
    unparsable = false;
    PriorityQueue<State> oldBeam = new PriorityQueue<>(maxBeamSize + 1, ScoredComparator.ASCENDING_COMPARATOR);
    PriorityQueue<State> beam = new PriorityQueue<>(maxBeamSize + 1, ScoredComparator.ASCENDING_COMPARATOR);
    // nextBeam will keep track of an unused PriorityQueue to cut down on the number of PriorityQueue objects created
    PriorityQueue<State> nextBeam = new PriorityQueue<>(maxBeamSize + 1, ScoredComparator.ASCENDING_COMPARATOR);
    beam.add(initialState);
    while (beam.size() > 0) {
        if (Thread.interrupted()) {
            // Allow interrupting the parser
            throw new RuntimeInterruptedException();
        }
        // log.info("================================================");
        // log.info("Current beam:");
        // log.info(beam);
        PriorityQueue<State> temp = oldBeam;
        oldBeam = beam;
        beam = nextBeam;
        beam.clear();
        nextBeam = temp;
        State bestState = null;
        for (State state : oldBeam) {
            if (Thread.interrupted()) {
                // Allow interrupting the parser
                throw new RuntimeInterruptedException();
            }
            Collection<ScoredObject<Integer>> predictedTransitions = parser.model.findHighestScoringTransitions(state, true, maxBeamSize, constraints);
            // log.info("Examining state: " + state);
            for (ScoredObject<Integer> predictedTransition : predictedTransitions) {
                Transition transition = parser.model.transitionIndex.get(predictedTransition.object());
                State newState = transition.apply(state, predictedTransition.score());
                // log.info("  Transition: " + transition + " (" + predictedTransition.score() + ")");
                if (bestState == null || bestState.score() < newState.score()) {
                    bestState = newState;
                }
                beam.add(newState);
                if (beam.size() > maxBeamSize) {
                    beam.poll();
                }
            }
        }
        if (beam.size() == 0) {
            // will result in some sort of parse.
            for (State state : oldBeam) {
                Transition transition = parser.model.findEmergencyTransition(state, constraints);
                if (transition != null) {
                    State newState = transition.apply(state);
                    if (bestState == null || bestState.score() < newState.score()) {
                        bestState = newState;
                    }
                    beam.add(newState);
                }
            }
        }
        // If the bestState is finished, we are done
        if (bestState == null || bestState.isFinished()) {
            break;
        }
    }
    bestParses = beam.stream().filter((state) -> state.isFinished()).collect(Collectors.toList());
    if (bestParses.size() == 0) {
        success = false;
        unparsable = true;
        debinarized = null;
        finalState = null;
        bestParses = Collections.emptyList();
    } else {
        Collections.sort(bestParses, beam.comparator());
        Collections.reverse(bestParses);
        finalState = bestParses.get(0);
        debinarized = debinarizer.transformTree(finalState.stack.peek());
        debinarized = Tsurgeon.processPattern(rearrangeFinalPunctuationTregex, rearrangeFinalPunctuationTsurgeon, debinarized);
    }
    return success;
}
Also used : RuntimeInterruptedException(edu.stanford.nlp.util.RuntimeInterruptedException) ScoredObject(edu.stanford.nlp.util.ScoredObject) PriorityQueue(java.util.PriorityQueue) ParserConstraint(edu.stanford.nlp.parser.common.ParserConstraint)

Aggregations

RuntimeInterruptedException (edu.stanford.nlp.util.RuntimeInterruptedException)14 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)2 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 ParserConstraint (edu.stanford.nlp.parser.common.ParserConstraint)2 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)2 Pair (edu.stanford.nlp.util.Pair)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MentionType (edu.stanford.nlp.coref.data.Dictionaries.MentionType)1 Compressor (edu.stanford.nlp.coref.statistical.Compressor)1 DocumentExamples (edu.stanford.nlp.coref.statistical.DocumentExamples)1 Example (edu.stanford.nlp.coref.statistical.Example)1 HasTag (edu.stanford.nlp.ling.HasTag)1 HasWord (edu.stanford.nlp.ling.HasWord)1 Label (edu.stanford.nlp.ling.Label)1 TaggedWord (edu.stanford.nlp.ling.TaggedWord)1 Word (edu.stanford.nlp.ling.Word)1