Example usage for com.google.common.collect Sets newHashSet

List of usage examples for com.google.common.collect Sets newHashSet

Introduction

In this page you can find the example usage for com.google.common.collect Sets newHashSet.

Prototype

public static <E> HashSet<E> newHashSet(Iterator<? extends E> elements) 

Source Link

Document

Creates a mutable HashSet instance containing the given elements.

Usage

From source file:tech.beshu.ror.utils.MicrobenchMatch.java

public static void main(String[] args) {

    String haystack = RandomStringUtils.random(10) + "logstash-*" + RandomStringUtils.random(10);
    Set<String> needles = Sets.newHashSet("*logstash-*");
    int iterations = (int) Math.pow(10, 7);
    Long start;//ww  w. j a v  a2  s.c  o  m

    // OLD
    MatcherWithWildcards matcher = new MatcherWithWildcards(needles);
    start = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        if (!matcher.match(haystack + i))
            throw new RuntimeException("lol");
    }
    Long timeOld = System.currentTimeMillis() - start;
    System.out.println("old: " + timeOld + " ms");

    // NEW
    MatcherWithWildcards minimatch = new MatcherWithWildcards(needles);
    start = System.currentTimeMillis();
    for (int i = 0; i < iterations; i++) {
        if (!minimatch.match(haystack + i))
            throw new RuntimeException("lol");
    }
    Long timeNew = (System.currentTimeMillis() - start);
    System.out.println("new: " + (System.currentTimeMillis() - start) + " ms");

    System.out.println("Gain: " + (((timeOld - timeNew) * 1f) / timeOld) * 100 + "%");

}

From source file:com.jivesoftware.os.tasmo.test.EventFireGenerator.java

public static void main(String[] args) {
    IdProvider idProvider = new IdProvider() {
        private long number;

        @Override//  w  w w .  ja  v  a2s . co  m
        public Id nextId() {
            ++number;
            return new Id(number);
        }
    };
    EventFireGenerator eventFireGenerator = new EventFireGenerator(new TenantId("test"), new Id("actor"));

    List<ModelPathStep> steps = new ArrayList<>();
    steps.add(new ModelPathStep(true, Sets.newHashSet("A"), "ref_B", ModelPathStepType.ref,
            Sets.newHashSet("B"), null));
    steps.add(new ModelPathStep(false, Sets.newHashSet("C"), "backrefs_B", ModelPathStepType.backRefs,
            Sets.newHashSet("B"), null));
    steps.add(new ModelPathStep(false, Sets.newHashSet("C"), "refs_D", ModelPathStepType.refs,
            Sets.newHashSet("D"), null));
    steps.add(new ModelPathStep(false, Sets.newHashSet("D"), null, ModelPathStepType.value, null,
            Arrays.asList("Value1", "Value2", "Value3")));

    ModelPath path = new ModelPath("testpath", steps);

    EventsAndViewId eventsAndViewId = eventFireGenerator.deriveEventsFromPath(idProvider, path,
            idProvider.nextId(), 2);

    for (EventFire eventFire : eventFireGenerator.generateEventFireCombinationsForPath("TestView",
            eventsAndViewId, path)) {
        StringBuilder builder = new StringBuilder("Event fire: ");
        String sep = "";
        for (Event event : eventFire.getFiredEvents()) {
            builder.append(sep).append(event);
            sep = ", ";
        }

        System.out.println(builder.toString());
    }
}

From source file:com.android.icu4j.srcgen.checker.CheckAndroidIcu4JSource.java

/**
 * Usage:/*  w  ww.j ava2  s. com*/
 * java com.android.icu4j.srcgen.CheckAndroidIcu4JSource {android_icu4j src directories}
 *   {report output file path}
 */
public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        throw new IllegalArgumentException("At least 2 argument required.");
    }

    Main main = new Main(DEBUG);

    // We assume we only need to look at ICU4J code for this for both passes.
    String[] inputDirs = new String[args.length - 1];
    System.arraycopy(args, 0, inputDirs, 0, inputDirs.length);
    InputFileGenerator inputFileGenerator = Icu4jTransformRules.createInputFileGenerator(inputDirs);

    // Pass 1: Establish the set of classes and members that are public in the Android API.
    System.out.println("Establishing Android public ICU4J API");
    RecordPublicApiRules recordPublicApiRulesRules = new RecordPublicApiRules(inputFileGenerator);
    main.execute(recordPublicApiRulesRules);
    List<String> publicMemberLocatorStrings = recordPublicApiRulesRules.publicMembers();
    System.out.println("Public API is:");
    for (String publicMemberLocatorString : publicMemberLocatorStrings) {
        System.out.println(publicMemberLocatorString);
    }

    // Pass 2: Check for issues.
    System.out.println("Checking for issues");
    Set<String> publicMembersSet = Sets.newHashSet(publicMemberLocatorStrings);
    File outputReportFile = new File(args[args.length - 1]);
    FileWriter out = new FileWriter(outputReportFile, false /* append */);
    try (BufferedWriter reportWriter = new BufferedWriter(out)) {
        reportWriter.append("Beginning of report:\n");
        CheckAndroidIcu4jSourceRules reportRules = new CheckAndroidIcu4jSourceRules(inputFileGenerator,
                publicMembersSet);
        main.execute(reportRules, reportWriter);
        reportWriter.append("End of report\n");
    }

    System.out.println("Report file: " + outputReportFile);
}

From source file:org.apache.giraph.debugger.CommandLine.java

/**
 * Main function of the CommandLine./*from  w  w  w .ja  v  a2s  . c o  m*/
 * @param args command line arguments.
 */
public static void main(final String[] args) {
    // Validate
    String mode = args[0];
    if (args.length == 0 || !mode.equalsIgnoreCase("list") && !mode.equalsIgnoreCase("dump")
            && !mode.equalsIgnoreCase("mktest") && !mode.equalsIgnoreCase("dump-master")
            && !mode.equalsIgnoreCase("mktest-master")) {
        printHelp();
    }

    if (args.length <= 1) {
        printHelp();
    }

    String jobId = args[1];

    if (mode.equalsIgnoreCase("list")) {
        try {
            List<Long> superstepsDebuggedMaster = ServerUtils.getSuperstepsMasterDebugged(jobId);
            Set<Long> superstepsDebugged = Sets.newHashSet(ServerUtils.getSuperstepsDebugged(jobId));
            superstepsDebugged.addAll(superstepsDebuggedMaster);
            List<Long> allSupersteps = Lists.newArrayList(superstepsDebugged);
            Collections.sort(allSupersteps);
            for (Long superstepNo : allSupersteps) {
                if (superstepsDebuggedMaster.contains(superstepNo)) {
                    LOG.info(String.format("%-15s  %s  %4d           ", "dump-master", jobId, superstepNo));
                    LOG.info(String.format("%-15s  %s  %4d           TestMaster_%s_S%d", "mktest-master", jobId,
                            superstepNo, jobId, superstepNo));
                }
                List<DebugTrace> debugTraces = Arrays.asList(DebugTrace.INTEGRITY_MESSAGE_SINGLE_VERTEX,
                        DebugTrace.INTEGRITY_VERTEX, DebugTrace.VERTEX_EXCEPTION, DebugTrace.VERTEX_REGULAR);
                for (DebugTrace debugTrace : debugTraces) {
                    for (String vertexId : ServerUtils.getVerticesDebugged(jobId, superstepNo, debugTrace)) {
                        LOG.info(String.format("%-15s  %s  %4d %8s  # %s", "dump", jobId, superstepNo, vertexId,
                                debugTrace.getLabel() == null ? "" : "captured " + debugTrace.getLabel()));
                        LOG.info(String.format("%-15s  %s  %4d %8s  Test_%s_S%d_V%s", "mktest", jobId,
                                superstepNo, vertexId, jobId, superstepNo, vertexId));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (args.length <= 2) {
            printHelp();
        }

        Long superstepNo = Long.parseLong(args[2]);
        try {
            if (mode.equalsIgnoreCase("dump") || mode.equalsIgnoreCase("mktest")) {
                if (args.length <= 3) {
                    printHelp();
                }
                String vertexId = args[3];
                // Read scenario.
                // TODO: rename ServerUtils to Utils
                @SuppressWarnings("rawtypes")
                GiraphVertexScenarioWrapper scenarioWrapper = ServerUtils.readScenarioFromTrace(jobId,
                        superstepNo, vertexId, DebugTrace.VERTEX_ALL);
                if (scenarioWrapper == null) {
                    LOG.error("The trace file does not exist.");
                    System.exit(2);
                }

                if (mode.equalsIgnoreCase("dump")) {
                    LOG.info(scenarioWrapper);
                } else if (mode.equalsIgnoreCase("mktest")) {
                    // Read output prefix and test class.
                    if (args.length <= 4) {
                        printHelp();
                    }
                    String outputPrefix = args[4].trim();
                    String testClassName = new File(outputPrefix).getName();
                    // Generate test case.
                    String generatedTestCase = new ComputationComputeTestGenerator()
                            .generateTest(scenarioWrapper, null, testClassName);
                    outputTestCase(outputPrefix, generatedTestCase);
                }
            } else if (mode.equalsIgnoreCase("dump-master") || mode.equalsIgnoreCase("mktest-master")) {
                GiraphMasterScenarioWrapper scenarioWrapper = ServerUtils.readMasterScenarioFromTrace(jobId,
                        superstepNo, DebugTrace.MASTER_ALL);
                if (scenarioWrapper == null) {
                    LOG.error("The trace file does not exist.");
                    System.exit(2);
                }

                if (mode.equalsIgnoreCase("dump-master")) {
                    LOG.info(scenarioWrapper);
                } else if (mode.equalsIgnoreCase("mktest-master")) {
                    if (args.length <= 3) {
                        printHelp();
                    }
                    String outputPrefix = args[3].trim();
                    String testClassName = new File(outputPrefix).getName();
                    String generatedTestCase = new MasterComputeTestGenerator().generateTest(scenarioWrapper,
                            null, testClassName);
                    outputTestCase(outputPrefix, generatedTestCase);
                }
            } else {
                printHelp();
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.giraph.debugger.instrumenter.InstrumentGiraphClasses.java

/**
 * Main entry point for instrumenting Giraph Computation and MasterCompute
 * classes with Graft classes./* ww  w . j  av a2s  .  com*/
 *
 * @param args Command-line arguments.
 * @throws IOException
 * @throws ClassNotFoundException
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
    // CHECKSTYLE: stop Regexp
    if (args.length < 1) {
        System.err.println(
                "Usage: java ... " + TMP_DIR_NAME_PREFIX + " GIRAPH_COMPUTATION_CLASS_NAME  [OUTPUT_DIR]");
        System.exit(1);
    }

    Collection<String> userComputationClassNames = Sets.newHashSet(args[0]);
    String outputDir = args.length > 1 ? args[1] : null;
    String masterComputeClassName = args.length > 2 ? args[2] : null;
    // Additional Computation classes
    boolean shouldAnalyzeMaster = masterComputeClassName != null;
    for (int i = 3; i < args.length; i++) {
        userComputationClassNames.add(args[i]);
        // Don't analyze the MasterCompute class when a chosen list of
        // Computation classes were given
        shouldAnalyzeMaster = false;
    }

    try {
        Collection<CtClass> classesModified = Sets.newHashSet();
        ClassPool classPool = ClassPool.getDefault();
        if (masterComputeClassName != null) {
            if (shouldAnalyzeMaster) {
                // Collect all Computation class names referenced in
                // masterComputeClassName
                LOG.info("Analyzing MasterCompute class: " + masterComputeClassName);
                userComputationClassNames
                        .addAll(collectComputationClassNames(masterComputeClassName, classPool));
            }
            LOG.info("Instrumenting MasterCompute class: " + masterComputeClassName);
            classesModified.addAll(instrumentSandwich(masterComputeClassName,
                    AbstractInterceptingMasterCompute.class.getName(), UserMasterCompute.class.getName(),
                    BottomInterceptingMasterCompute.class.getName(), classPool));
        }
        for (String userComputationClassName : userComputationClassNames) {
            LOG.info("Instrumenting Computation class: " + userComputationClassName);
            classesModified.addAll(instrumentSandwich(userComputationClassName,
                    AbstractInterceptingComputation.class.getName(), UserComputation.class.getCanonicalName(),
                    BottomInterceptingComputation.class.getName(), classPool));
        }

        // Finally, write the modified classes so that a new jar can be
        // created or an existing one can be updated.
        String jarRoot = outputDir != null ? outputDir
                : Files.createTempDirectory(TMP_DIR_NAME_PREFIX).toString();
        LOG.info("Writing " + classesModified.size() + " instrumented classes to " + jarRoot);
        for (CtClass c : classesModified) {
            LOG.debug(" writing class " + c.getName());
            c.writeFile(jarRoot);
        }

        LOG.info("Finished instrumentation");

        if (outputDir == null) {
            // Show where we produced the instrumented .class files (unless
            // specified)
            System.out.println(jarRoot);
        }
        System.exit(0);
    } catch (NotFoundException e) {
        e.printStackTrace();
        System.err.println("Some Giraph Computation or MasterCompute classes " + "were not found");
        System.exit(1);
    } catch (CannotCompileException e) {
        e.printStackTrace();
        System.err.println("Cannot instrument the given Giraph Computation or " + "MasterCompute classes");
        System.exit(2);
    } catch (IOException e) {
        e.printStackTrace();
        System.err
                .println("Cannot write the instrumented Giraph Computation and/or " + "MasterCompute classes");
        System.exit(4);
    }
    // CHECKSTYLE: resume Regexp
}

From source file:org.prebake.service.tools.ext.JUnitRunner.java

/**
 * @param argv//from  ww  w .j  av a2s . c  o m
 *    [test_listener_lambda, report_output_dir, report_types, test_classes...]
 */
public static void main(String... argv) {
    MobileFunction testReportFilter = !"".equals(argv[0]) ? new MobileFunction(argv[0]) : null;
    Path reportOutputDir = FileSystems.getDefault().getPath(argv[1]);
    Set<String> reportTypes = Sets.newHashSet(argv[2].toLowerCase(Locale.ROOT).split(","));
    String[] testClassNames = new String[argv.length - 3];
    ClassNameFinder classNameFinder = new ClassNameFinder();
    ResultCode okResult = ResultCode.ALL_TESTS_PASSED;
    for (int i = argv.length; --i >= 3;) {
        try {
            testClassNames[i - 3] = classNameFinder.forClassFile(argv[i]);
        } catch (IOException ex) {
            System.err.println("Failed to read class file " + argv[i]);
            okResult = ResultCode.FAILED_TO_IDENTIFY_TEST_CLASSES;
        }
    }
    ResultCode result = run(new JUnitSystem() {
        public void exit(int result) {
            throw new UnsupportedOperationException();
        }

        public PrintStream out() {
            return System.out;
        }
    }, testReportFilter, reportOutputDir, reportTypes, testClassNames);
    if (result == ResultCode.ALL_TESTS_PASSED) {
        result = okResult;
    }
    // 0 all tests passed, -1 reports generated, -2 failed to write reports,
    // -3 failed to identify all test classes
    // If these result codes change, change junit.js
    System.exit(result.processResultCode);
}

From source file:com.ignorelist.kassandra.steam.scraper.TaggerCli.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*  www  .ja  va  2 s .c  o m*/
 * @throws org.antlr.runtime.RecognitionException
 * @throws org.apache.commons.cli.ParseException
 */
public static void main(String[] args) throws IOException, RecognitionException, ParseException {
    Options options = buildOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine;
    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException pe) {
        System.out.println(pe.getMessage());
        System.out.println();
        printHelp(options);
        System.exit(0);
        return;
    }
    if (commandLine.hasOption("h")) {
        printHelp(options);
        System.exit(0);
    }
    final PathResolver pathResolver = new PathResolver();

    Configuration configuration;
    Path configurationFile = pathResolver.findConfiguration();
    if (Files.isRegularFile(configurationFile)) {
        configuration = Configuration.fromPropertiesFile(configurationFile);
    } else {
        configuration = new Configuration();
    }
    configuration = toConfiguration(configuration, commandLine);
    //configuration.toProperties().store(System.err, null);

    if (!Files.isRegularFile(configurationFile)) {
        configuration.writeProperties(configurationFile);
        System.err.println(
                "no configuration file present, write based on CLI options: " + configurationFile.toString());
        configuration.toProperties().store(System.err, null);
    }

    Set<Path> sharedConfigPaths = configuration.getSharedConfigPaths();
    if (sharedConfigPaths.size() > 1 && !commandLine.hasOption("w")) {
        System.err.println("multiple sharedconfig.vdf available:\n" + Joiner.on("\n").join(sharedConfigPaths)
                + "\n, can not write to stdout. Need to specify -w or -f with a single sharedconfig.vdf");
        System.exit(1);
    }

    Tagger.Options taggerOptions = Tagger.Options.fromConfiguration(configuration);

    final String[] removeTagsValues = commandLine.getOptionValues("remove");
    if (null != removeTagsValues) {
        taggerOptions.setRemoveTags(Sets.newHashSet(removeTagsValues));
    }

    Set<TagType> tagTypes = configuration.getTagTypes();
    if (null == tagTypes) {
        System.err.println("no tag types!");
        System.exit(1);
    }

    final boolean printTags = commandLine.hasOption("p");

    final HtmlTagLoader htmlTagLoader = new HtmlTagLoader(pathResolver.findCachePath("html"),
            null == configuration.getCacheExpiryDays() ? 7 : configuration.getCacheExpiryDays());
    final BatchTagLoader tagLoader = new BatchTagLoader(htmlTagLoader, configuration.getDownloadThreads());
    if (true || commandLine.hasOption("v")) {
        tagLoader.registerEventListener(new CliEventLoggerLoaded());
    }
    Tagger tagger = new Tagger(tagLoader);

    if (printTags) {
        Set<String> availableTags = tagger.getAvailableTags(sharedConfigPaths, taggerOptions);
        Joiner.on("\n").appendTo(System.out, availableTags);
    } else {
        for (Path path : sharedConfigPaths) {
            VdfNode tagged = tagger.tag(path, taggerOptions);
            if (commandLine.hasOption("w")) {
                Path backup = path.getParent()
                        .resolve(path.getFileName().toString() + ".bak" + new Date().getTime());
                Files.copy(path, backup, StandardCopyOption.REPLACE_EXISTING);
                System.err.println("backup up " + path + " to " + backup);
                Files.copy(new ByteArrayInputStream(tagged.toPrettyString().getBytes(StandardCharsets.UTF_8)),
                        path, StandardCopyOption.REPLACE_EXISTING);
                try {
                    Files.setPosixFilePermissions(path, SHARED_CONFIG_POSIX_PERMS);
                } catch (Exception e) {
                    System.err.println(e);
                }
                System.err.println("wrote " + path);
            } else {
                System.out.println(tagged.toPrettyString());
                System.err.println("pipe to file and copy to: " + path.toString());
            }
        }
    }
}

From source file:org.deri.iris.queryrewriting.RequiemSymbolsCounter.java

public static void main(final String[] args) {
    try {// w w w  .j  ava  2 s  .  c om
        // Parse the REQUIEM input File
        final BufferedReader br = new BufferedReader(new FileReader(args[0]));
        while (br.ready()) {
            final String curFile = br.readLine();
            System.out.println("Processing Test: " + curFile);

            // Read the file
            final BufferedReader tr = new BufferedReader(new FileReader(curFile));

            final IBasicFactory bf = Factory.BASIC;
            final List<IRule> rules = new UniqueList<IRule>();

            String line, queryFragment, headFragment, bodyFragment;

            // Skip 9 lines (position the cursor to the first query)
            for (int i = 0; i < 9; i++) {
                tr.readLine();
            }

            /*
             * Extract the query from this line. Each query line has the format: "N: head(X) <- body(X,Y)\n"
             */
            int queries = 0;
            while (tr.ready()) {
                line = tr.readLine();
                if (!line.startsWith("==")) {
                    queries++;
                    queryFragment = line.substring(line.indexOf(':') + 1).replace(" ", "");
                    System.out.println("Processing Query: " + queryFragment);
                    headFragment = queryFragment.substring(0, queryFragment.lastIndexOf("<-"));
                    bodyFragment = queryFragment.substring(queryFragment.lastIndexOf("<-") + 2,
                            queryFragment.length());

                    // Generate IRIS Rule Format
                    final List<ILiteral> head = new UniqueList<ILiteral>();
                    final List<ITerm> hTerms = extractTerms(headFragment);
                    final String hSym = extractPredicateSymbol(headFragment);
                    final ILiteral headLit = bf.createLiteral(true, bf.createPredicate(hSym, hTerms.size()),
                            bf.createTuple(hTerms));
                    head.add(headLit);

                    final List<ILiteral> body = new UniqueList<ILiteral>();
                    final StringTokenizer st = new StringTokenizer(bodyFragment, "^");

                    while (st.hasMoreElements()) {
                        final String elem = st.nextToken();
                        final String bSym = extractPredicateSymbol(elem);
                        final List<ITerm> bTerms = extractTerms(elem);
                        body.add(bf.createLiteral(true, bf.createPredicate(bSym, bTerms.size()),
                                bf.createTuple(bTerms)));
                    }

                    rules.add(bf.createRule(head, body));
                }
            }

            // Count the Joins in the rules
            final long joins = RewritingUtils.joinCount(Sets.newHashSet(rules));
            final long symbols = RewritingUtils.atomsCount(Sets.newHashSet(rules));

            // Output the number of symbols
            final BufferedWriter bw = new BufferedWriter(new FileWriter(curFile.replace(".txt", "_count.txt")));
            bw.write("Rewriting Size (queries): " + queries + "\n");
            bw.write("Rewriting Size (atoms): " + symbols + "\n");
            bw.write("Rewriting Size (joins): " + joins + "\n");
            bw.flush();
            bw.close();
        }
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }

}

From source file:pl.edu.icm.cermine.bibref.parsing.examples.HMMBibRefParsingExample.java

public static void main(String[] args) throws URISyntaxException, JDOMException, IOException {

    // 1. construct vector of features builder
    FeatureVectorBuilder<CitationToken, Citation> vectorBuilder = new FeatureVectorBuilder<CitationToken, Citation>();
    vectorBuilder.setFeatureCalculators(Arrays.<FeatureCalculator<CitationToken, Citation>>asList(
            new DigitRelativeCountFeature(), new IsAllDigitsFeature(), new IsAllLettersFeature(),
            new IsAllLettersOrDigitsFeature(), new IsAllLowercaseFeature(), new IsAllRomanDigitsFeature(),
            new IsAllUppercaseFeature(), new IsAndFeature(), new IsCityFeature(),
            new IsClosingParenthesisFeature(), new IsClosingSquareBracketFeature(), new IsCommaFeature(),
            new IsCommonPublisherWordFeature(), new IsCommonSeriesWordFeature(),
            new IsCommonSourceWordFeature(), new IsDashBetweenWordsFeature(), new IsDashFeature(),
            new IsDigitFeature(), new IsDotFeature(), new IsLaquoFeature(), new IsLowercaseLetterFeature(),
            new IsOpeningParenthesisFeature(), new IsOpeningSquareBracketFeature(), new IsQuoteFeature(),
            new IsRaquoFeature(), new IsSingleQuoteBetweenWordsFeature(), new IsSlashFeature(),
            new IsUppercaseLetterFeature(), new IsUppercaseWordFeature(), new IsWordAndFeature(),
            new IsWordDeFeature(), new IsWordHttpFeature(), new IsWordJrFeature(), new IsWordLeFeature(),
            new IsNumberTextFeature(), new IsPagesTextFeature(), new IsWordTheFeature(),
            new IsWordTheoryFeature(), new IsCommonSurnamePartFeature(), new IsVolumeTextFeature(),
            new IsYearFeature(), new LengthFeature(), new LetterRelativeCountFeature(),
            new LowercaseRelativeCountFeature(), new StartsWithUppercaseFeature(),
            new StartsWithWordMcFeature(), new UppercaseRelativeCountFeature()));

    // 2. import and generate training set based on sequences and vector of features
    URL u = HMMBibRefParsingExample.class.getResource(HMM_TRAIN_FILE);

    List<Citation> citations = NlmCitationExtractor.extractCitations(new InputSource(u.openStream()));

    HMMTrainingSample<CitationTokenLabel>[] trainingElements = CitationsToHMMConverter
            .convertToHMM(Sets.newHashSet(citations), vectorBuilder);

    // 3. HMM training. The resulting probabilities object should be
    // serialized for further usage
    HMMProbabilityInfo<CitationTokenLabel> hmmProbabilities = HMMProbabilityInfoFactory.getFVHMMProbability(
            new ArrayList<HMMTrainingSample<CitationTokenLabel>>(Arrays.asList(trainingElements)),
            vectorBuilder);//  w w  w  .ja  v  a2s.  com

    // 4. create an HMM service instance
    HMMService hmmService = new HMMServiceImpl();

    // 5. bibliographic refs parser instance
    HMMBibReferenceParser bibReferenceParser = new HMMBibReferenceParser(hmmService, hmmProbabilities,
            vectorBuilder);

    // 6. find the most probable labels for HMM objects
    BibEntry bibEntry = bibReferenceParser.parseBibReference(
            "[BP] R. Burton and R. Pemantle, Local characteristics, entropy and limit theorems for spanning trees and domino tilings via transfer impedances, Ann. Probab., Vol. 21, 1993, pp. 1329-1371.");
    System.out.println(bibEntry.toBibTeX());
}

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

public static void main(String[] args) throws Exception {
    long curTime = System.nanoTime();
    SearchArgs searchArgs = new SearchArgs();
    CmdLineParser parser = new CmdLineParser(searchArgs, ParserProperties.defaults().withUsageWidth(90));

    try {//  w w w.j  a  v  a 2  s  . co  m
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        System.err.println("Example: SearchTweets" + 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));
    }

    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);

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

    RerankerCascade cascade = new RerankerCascade();
    if (searchArgs.rm3) {
        cascade.add(new Rm3Reranker(IndexTweets.ANALYZER, StatusField.TEXT.name,
                "src/main/resources/io/anserini/rerank/rm3/rm3-stoplist.twitter.txt"));
        cascade.add(new RemoveRetweetsTemporalTiebreakReranker());
    } else {
        cascade.add(new RemoveRetweetsTemporalTiebreakReranker());
    }

    MicroblogTopicSet topics = MicroblogTopicSet.fromFile(new File(searchArgs.topics));

    PrintStream out = new PrintStream(new FileOutputStream(new File(searchArgs.output)));
    LOG.info("Writing output to " + searchArgs.output);

    LOG.info("Initialized complete! (elapsed time = " + (System.nanoTime() - curTime) / 1000000 + "ms)");
    long totalTime = 0;
    int cnt = 0;
    for (MicroblogTopic topic : topics) {
        long curQueryTime = System.nanoTime();

        Filter filter = NumericRangeFilter.newLongRange(StatusField.ID.name, 0L, topic.getQueryTweetTime(),
                true, true);
        Query query = AnalyzerUtils.buildBagOfWordsQuery(StatusField.TEXT.name, IndexTweets.ANALYZER,
                topic.getQuery());

        TopDocs rs = searcher.search(query, filter, searchArgs.hits);

        RerankerContext context = new RerankerContext(searcher, query, topic.getId(), topic.getQuery(),
                Sets.newHashSet(AnalyzerUtils.tokenize(IndexTweets.ANALYZER, topic.getQuery())), filter);
        ScoredDocuments docs = cascade.run(ScoredDocuments.fromTopDocs(rs, searcher), context);

        for (int i = 0; i < docs.documents.length; i++) {
            String qid = topic.getId().replaceFirst("^MB0*", "");
            out.println(String.format("%s Q0 %s %d %f %s", qid,
                    docs.documents[i].getField(StatusField.ID.name).numericValue(), (i + 1), docs.scores[i],
                    searchArgs.runtag));
        }
        long qtime = (System.nanoTime() - curQueryTime) / 1000000;
        LOG.info("Query " + topic.getId() + " (elapsed time = " + qtime + "ms)");
        totalTime += qtime;
        cnt++;
    }

    LOG.info("All queries completed!");
    LOG.info("Total elapsed time = " + totalTime + "ms");
    LOG.info("Average query latency = " + (totalTime / cnt) + "ms");

    reader.close();
    out.close();
}