Example usage for java.util SortedMap size

List of usage examples for java.util SortedMap size

Introduction

In this page you can find the example usage for java.util SortedMap size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:io.anserini.search.SearchWebCollection.java

public static void main(String[] args) throws Exception {

    SearchArgs searchArgs = new SearchArgs();
    CmdLineParser parser = new CmdLineParser(searchArgs, ParserProperties.defaults().withUsageWidth(90));

    try {// w w w  .  j av a 2 s .c om
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        System.err.println("Example: SearchWebCollection" + parser.printExample(OptionHandlerFilter.REQUIRED));
        return;
    }

    LOG.info("Reading index at " + searchArgs.index);
    Directory dir;
    if (searchArgs.inmem) {
        LOG.info("Using MMapDirectory with preload");
        dir = new MMapDirectory(Paths.get(searchArgs.index));
        ((MMapDirectory) dir).setPreload(true);
    } else {
        LOG.info("Using default FSDirectory");
        dir = FSDirectory.open(Paths.get(searchArgs.index));
    }

    Similarity similarity = null;

    if (searchArgs.ql) {
        LOG.info("Using QL scoring model");
        similarity = new LMDirichletSimilarity(searchArgs.mu);
    } else if (searchArgs.bm25) {
        LOG.info("Using BM25 scoring model");
        similarity = new BM25Similarity(searchArgs.k1, searchArgs.b);
    } else {
        LOG.error("Error: Must specify scoring model!");
        System.exit(-1);
    }

    RerankerCascade cascade = new RerankerCascade();
    boolean useQueryParser = false;
    if (searchArgs.rm3) {
        cascade.add(new Rm3Reranker(new EnglishAnalyzer(), FIELD_BODY,
                "src/main/resources/io/anserini/rerank/rm3/rm3-stoplist.gov2.txt"));
        useQueryParser = true;
    } else {
        cascade.add(new IdentityReranker());
    }
    FeatureExtractors extractors = null;
    if (searchArgs.extractors != null) {
        extractors = FeatureExtractors.loadExtractor(searchArgs.extractors);
    }

    if (searchArgs.dumpFeatures) {
        PrintStream out = new PrintStream(searchArgs.featureFile);
        Qrels qrels = new Qrels(searchArgs.qrels);
        cascade.add(new WebCollectionLtrDataGenerator(out, qrels, extractors));
    }

    Path topicsFile = Paths.get(searchArgs.topics);

    if (!Files.exists(topicsFile) || !Files.isRegularFile(topicsFile) || !Files.isReadable(topicsFile)) {
        throw new IllegalArgumentException(
                "Topics file : " + topicsFile + " does not exist or is not a (readable) file.");
    }

    TopicReader tr = (TopicReader) Class
            .forName("io.anserini.search.query." + searchArgs.topicReader + "TopicReader")
            .getConstructor(Path.class).newInstance(topicsFile);
    SortedMap<Integer, String> topics = tr.read();

    final long start = System.nanoTime();
    SearchWebCollection searcher = new SearchWebCollection(searchArgs.index);
    searcher.search(topics, searchArgs.output, similarity, searchArgs.hits, cascade, useQueryParser,
            searchArgs.keepstop);
    searcher.close();
    final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    LOG.info("Total " + topics.size() + " topics searched in "
            + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss"));
}

From source file:Main.java

public static <T, U> SortedMap<T, U> copySortedMap(SortedMap<T, U> m) {
    if (m == null || m.size() == 0) {
        return new TreeMap<T, U>();
    } else {//from w w  w  .  ja v  a2  s.c om
        return new TreeMap<T, U>(m);
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T, U> U[] getSortedValueArray(SortedMap<T, U> m, Class<U> clazz) {
    if (m == null || m.size() == 0) {
        U[] a = (U[]) java.lang.reflect.Array.newInstance(clazz, 0);
        return Collections.<U>emptyList().toArray(a);
    } else {/*from   ww  w.  j  a  v a2 s . com*/
        U[] a = (U[]) java.lang.reflect.Array.newInstance(clazz, m.size());
        return m.values().toArray(a);
    }
}

From source file:co.rsk.peg.BridgeSerializationUtils.java

public static byte[] serializeMap(SortedMap<Sha3Hash, BtcTransaction> map) {
    int ntxs = map.size();

    byte[][] bytes = new byte[ntxs * 2][];
    int n = 0;/*from  www  .ja v  a2s  . co m*/

    for (Map.Entry<Sha3Hash, BtcTransaction> entry : map.entrySet()) {
        bytes[n++] = RLP.encodeElement(entry.getKey().getBytes());
        bytes[n++] = RLP.encodeElement(entry.getValue().bitcoinSerialize());
    }

    return RLP.encodeList(bytes);
}

From source file:co.rsk.peg.BridgeSerializationUtils.java

public static byte[] serializePairMap(SortedMap<Sha3Hash, Pair<BtcTransaction, Long>> map) {
    int ntxs = map.size();

    byte[][] bytes = new byte[ntxs * 3][];
    int n = 0;/*from  w w w .j ava 2  s . com*/

    for (Map.Entry<Sha3Hash, Pair<BtcTransaction, Long>> entry : map.entrySet()) {
        bytes[n++] = RLP.encodeElement(entry.getKey().getBytes());
        bytes[n++] = RLP.encodeElement(entry.getValue().getLeft().bitcoinSerialize());
        bytes[n++] = RLP.encodeBigInteger(BigInteger.valueOf(entry.getValue().getRight()));
    }

    return RLP.encodeList(bytes);
}

From source file:com.cloudera.oryx.kmeans.common.Weighted.java

/**
 * Sample items from a {@code List<Weighted>} where items with higher weights
 * have a higher probability of being included in the sample.
 * //from  w w  w .jav  a  2 s.  c  o  m
 * @param things The iterable to sample from
 * @param size The number of items to sample
 * @return A list containing the sampled items
 */
public static <T extends Weighted<?>> List<T> sample(Iterable<T> things, int size, RandomGenerator random) {
    if (random == null) {
        random = RandomManager.getRandom();
    }
    SortedMap<Double, T> sampled = Maps.newTreeMap();
    for (T thing : things) {
        if (thing.weight() > 0) {
            double score = Math.log(random.nextDouble()) / thing.weight();
            if (sampled.size() < size || score > sampled.firstKey()) {
                sampled.put(score, thing);
            }
            if (sampled.size() > size) {
                sampled.remove(sampled.firstKey());
            }
        }
    }
    return Lists.newArrayList(sampled.values());
}

From source file:edu.uci.ics.crawler4j.url.URLCanonicalizer.java

public static String getCanonicalURL(String href, String context) {

    try {//from  w w  w. java2 s  .c om
        URL canonicalURL = new URL(UrlResolver.resolveUrl(context == null ? "" : context, href));

        String host = canonicalURL.getHost().toLowerCase();
        if (StringUtils.isBlank(host)) {
            // This is an invalid Url.
            return null;
        }

        String path = canonicalURL.getPath();

        /*
         * Normalize: no empty segments (i.e., "//"), no segments equal to
         * ".", and no segments equal to ".." that are preceded by a segment
         * not equal to "..".
         */
        path = new URI(path).normalize().toString();

        /*
         * Convert '//' -> '/'
         */
        int idx = path.indexOf("//");
        while (idx >= 0) {
            path = path.replace("//", "/");
            idx = path.indexOf("//");
        }

        /*
         * Drop starting '/../'
         */
        while (path.startsWith("/../")) {
            path = path.substring(3);
        }

        /*
         * Trim
         */
        path = path.trim();

        final SortedMap<String, String> params = createParameterMap(canonicalURL.getQuery());
        final String queryString;

        if (params != null && params.size() > 0) {
            String canonicalParams = canonicalize(params);
            queryString = (canonicalParams.isEmpty() ? "" : "?" + canonicalParams);
        } else {
            queryString = "";
        }

        /*
         * Add starting slash if needed
         */
        if (path.length() == 0) {
            path = "/" + path;
        }

        /*
         * Drop default port: example.com:80 -> example.com
         */
        int port = canonicalURL.getPort();
        if (port == canonicalURL.getDefaultPort()) {
            port = -1;
        }

        String protocol = canonicalURL.getProtocol().toLowerCase();
        String pathAndQueryString = normalizePath(path) + queryString;

        URL result = new URL(protocol, host, port, pathAndQueryString);
        return result.toExternalForm();

    } catch (MalformedURLException ex) {
        return null;
    } catch (URISyntaxException ex) {
        return null;
    }
}

From source file:ee.ria.xroad.opmonitordaemon.HealthDataMetricsUtil.java

/**
 * @param registry the metric registry where the gauge should be looked up
 * @param expectedGaugeName the gauge name to find
 * @return the found gauge or null if it does not exist
 *//*from  w ww .  j  a v  a2s  . co m*/
static Gauge findGauge(MetricRegistry registry, String expectedGaugeName) {
    SortedMap<String, Gauge> gauges = registry.getGauges(
            (name, metric) -> name.matches(HealthDataMetricsUtil.formatMetricMatchRegexp(expectedGaugeName)));

    if (gauges.size() > 1) {
        // Should not happen because we use a strict regexp.
        log.warn("Multiple gauges matched the name " + expectedGaugeName);
    }

    return gauges.isEmpty() ? null : gauges.values().iterator().next();
}

From source file:ee.ria.xroad.opmonitordaemon.HealthDataMetricsUtil.java

/**
 * @param registry the metric registry where the counter should be looked up
 * @param expectedCounterName the counter name to find
 * @return the found counter or null if it does not exist
 *//*w  w  w.j av  a  2  s .c o m*/
static Counter findCounter(MetricRegistry registry, String expectedCounterName) {
    SortedMap<String, Counter> counters = registry.getCounters(
            (name, metric) -> name.matches(HealthDataMetricsUtil.formatMetricMatchRegexp(expectedCounterName)));

    if (counters.size() > 1) {
        // Should not happen because we use a strict regexp.
        log.warn("Multiple counters matched the name " + expectedCounterName);
    }

    return counters.isEmpty() ? null : counters.values().iterator().next();
}

From source file:ee.ria.xroad.opmonitordaemon.HealthDataMetricsUtil.java

/**
 * @param registry the metric registry where the histogram should be
 * looked up// w w w.j  a v a2  s .  c o m
 * @param expectedHistogramName the counter name to find
 * @return the found histogram or null if it does not exist
 */
static Histogram findHistogram(MetricRegistry registry, String expectedHistogramName) {
    SortedMap<String, Histogram> histograms = registry.getHistograms((name, metric) -> name
            .matches(HealthDataMetricsUtil.formatMetricMatchRegexp(expectedHistogramName)));

    if (histograms.size() > 1) {
        // Should not happen because we use a strict regexp.
        log.warn("Multiple histograms matched the name " + expectedHistogramName);
    }

    return histograms.isEmpty() ? null : histograms.values().iterator().next();
}