Example usage for com.google.common.math IntMath binomial

List of usage examples for com.google.common.math IntMath binomial

Introduction

In this page you can find the example usage for com.google.common.math IntMath binomial.

Prototype

@GwtIncompatible("need BigIntegerMath to adequately test")
public static int binomial(int n, int k) 

Source Link

Document

Returns n choose k , also known as the binomial coefficient of n and k , or Integer#MAX_VALUE if the result does not fit in an int .

Usage

From source file:eu.project.ttc.engines.GraphicalVariantGatherer.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    logger.info("Starting graphical term gathering for TermIndex {}",
            this.termIndexResource.getTermIndex().getName());

    if (termIndexResource.getTermIndex().getTerms().isEmpty())
        return;/*from   w w  w.j av a2  s .  c om*/

    // create the index
    String indexName = String.format("_%d_first_letters_", n);
    final TermIndex termIndex = this.termIndexResource.getTermIndex();
    CustomTermIndex customIndex = termIndex.createCustomIndex(indexName, nFirstLettersProvider);

    // clean singleton classes
    logger.debug("Cleaning singleton keys");
    customIndex.cleanSingletonKeys();

    logger.debug("Graphical gathering over {} classes", customIndex.size());

    // get the total number of comparisons
    for (String key : customIndex.keySet())
        totalComparisons += IntMath.binomial(customIndex.getTerms(key).size(), 2);

    if (taskObserver.isPresent())
        taskObserver.get().setTotalTaskWork(totalComparisons);

    logger.debug("Number of distance edition pairs to compute: {}", totalComparisons);

    // Log the progress every 5 seconds
    Timer progressLoggerTimer = new Timer("Syn. Variant Gathering Timer");
    progressLoggerTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            GraphicalVariantGatherer.logger.info("progress for graphical variant gathering: {}%",
                    String.format("%.2f", ((float) nbComparisons * 100) / totalComparisons));
        }
    }, 5000l, 5000l);

    // do the distance computation on each class
    List<Term> terms;
    Term t1, t2;
    int i, j;
    int gatheredCnt = 0;
    double dist = 0d;
    for (String key : customIndex.keySet()) {
        terms = customIndex.getTerms(key);
        logger.trace("Graphical gathering over term class {} of size: {}", key, terms.size());
        for (i = 0; i < terms.size(); i++) {
            t1 = terms.get(i);
            for (j = i + 1; j < terms.size(); j++) {
                nbComparisons++;
                if (nbComparisons % OBSERVER_STEP == 0 && taskObserver.isPresent())
                    taskObserver.get().work(OBSERVER_STEP);
                t2 = terms.get(j);
                dist = distance.computeNormalized(t1.getLemma(), t2.getLemma());
                if (dist >= this.threshold) {
                    gatheredCnt++;
                    if (t1.getLemma().compareTo(t2.getLemma()) <= 0)
                        t1.addTermVariation(t2, VariationType.GRAPHICAL, dist);
                    else
                        t2.addTermVariation(t1, VariationType.GRAPHICAL, dist);

                    watch(t1, t2, dist);
                    watch(t2, t1, dist);

                }
            }
        }
    }

    // log some stats
    logger.debug("Graphical gathering {} terms gathered / {} pairs compared", gatheredCnt, nbComparisons);

    // free memory taken by the index
    termIndex.dropCustomIndex(indexName);

    progressLoggerTimer.cancel();
}