List of usage examples for java.lang Integer parseInt
public static int parseInt(String s) throws NumberFormatException
From source file:eu.bitm.NominatimReverseGeocoding.NominatimReverseGeocodingJAPI.java
public static void main(String[] args) { if (args.length < 1) { System.out.println("use -help for instructions"); } else if (args.length < 2) { if (args[0].equals("-help")) { System.out.println("Mandatory parameters:"); System.out.println(" -lat [latitude]"); System.out.println(" -lon [longitude]"); System.out.println("\nOptional parameters:"); System.out.println(" -zoom [0-18] | from 0 (country) to 18 (street address), default 18"); System.out.println(" -osmid | show also osm id and osm type of the address"); System.out.println("\nThis page:"); System.out.println(" -help"); } else//from w ww. j av a 2 s. c o m System.err.println("invalid parameters, use -help for instructions"); } else { boolean latSet = false; boolean lonSet = false; boolean osm = false; double lat = -200; double lon = -200; int zoom = 18; for (int i = 0; i < args.length; i++) { if (args[i].equals("-lat")) { try { lat = Double.parseDouble(args[i + 1]); } catch (NumberFormatException nfe) { System.out.println("Invalid latitude"); return; } latSet = true; i++; continue; } else if (args[i].equals("-lon")) { try { lon = Double.parseDouble(args[i + 1]); } catch (NumberFormatException nfe) { System.out.println("Invalid longitude"); return; } lonSet = true; i++; continue; } else if (args[i].equals("-zoom")) { try { zoom = Integer.parseInt(args[i + 1]); } catch (NumberFormatException nfe) { System.out.println("Invalid zoom"); return; } i++; continue; } else if (args[i].equals("-osm")) { osm = true; } else { System.err.println("invalid parameters, use -help for instructions"); return; } } if (latSet && lonSet) { NominatimReverseGeocodingJAPI nominatim = new NominatimReverseGeocodingJAPI(zoom); Address address = nominatim.getAdress(lat, lon); System.out.println(address); if (osm) { System.out.print("OSM type: " + address.getOsmType() + ", OSM id: " + address.getOsmId()); } } else { System.err.println("please specifiy -lat and -lon, use -help for instructions"); } } }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.BuildRetrofitLexicons.java
public static void main(String[] args) { Options options = new Options(); options.addOption(CommonParams.GIZA_ROOT_DIR_PARAM, null, true, CommonParams.GIZA_ROOT_DIR_DESC); options.addOption(CommonParams.GIZA_ITER_QTY_PARAM, null, true, CommonParams.GIZA_ITER_QTY_DESC); options.addOption(CommonParams.MEMINDEX_PARAM, null, true, CommonParams.MEMINDEX_DESC); options.addOption(OUT_FILE_PARAM, null, true, OUT_FILE_DESC); options.addOption(MIN_PROB_PARAM, null, true, MIN_PROB_DESC); options.addOption(FORMAT_PARAM, null, true, FORMAT_DESC); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); try {//from ww w . j a v a 2s .com CommandLine cmd = parser.parse(options, args); String gizaRootDir = cmd.getOptionValue(CommonParams.GIZA_ROOT_DIR_PARAM); int gizaIterQty = -1; if (cmd.hasOption(CommonParams.GIZA_ITER_QTY_PARAM)) { gizaIterQty = Integer.parseInt(cmd.getOptionValue(CommonParams.GIZA_ITER_QTY_PARAM)); } else { Usage("Specify: " + CommonParams.GIZA_ITER_QTY_PARAM, options); } String outFileName = cmd.getOptionValue(OUT_FILE_PARAM); if (null == outFileName) { Usage("Specify: " + OUT_FILE_PARAM, options); } String indexDir = cmd.getOptionValue(CommonParams.MEMINDEX_PARAM); if (null == indexDir) { Usage("Specify: " + CommonParams.MEMINDEX_DESC, options); } FormatType outType = FormatType.kOrig; String outTypeStr = cmd.getOptionValue(FORMAT_PARAM); if (null != outTypeStr) { if (outTypeStr.equals(ORIG_TYPE)) { outType = FormatType.kOrig; } else if (outTypeStr.equals(WEIGHTED_TYPE)) { outType = FormatType.kWeighted; } else if (outTypeStr.equals(UNWEIGHTED_TYPE)) { outType = FormatType.kUnweighted; } else { Usage("Unknown format type: " + outTypeStr, options); } } float minProb = 0; if (cmd.hasOption(MIN_PROB_PARAM)) { minProb = Float.parseFloat(cmd.getOptionValue(MIN_PROB_PARAM)); } else { Usage("Specify: " + MIN_PROB_PARAM, options); } System.out.println(String.format( "Saving lexicon to '%s' (output format '%s'), keep only entries with translation probability >= %f", outFileName, outType.toString(), minProb)); // We use unlemmatized text here, because lemmatized dictionary is going to be mostly subset of the unlemmatized one. InMemForwardIndex textIndex = new InMemForwardIndex(FeatureExtractor.indexFileName(indexDir, FeatureExtractor.mFieldNames[FeatureExtractor.TEXT_UNLEMM_FIELD_ID])); InMemForwardIndexFilterAndRecoder filterAndRecoder = new InMemForwardIndexFilterAndRecoder(textIndex); String prefix = gizaRootDir + "/" + FeatureExtractor.mFieldNames[FeatureExtractor.TEXT_UNLEMM_FIELD_ID] + "/"; GizaVocabularyReader answVoc = new GizaVocabularyReader(prefix + "source.vcb", filterAndRecoder); GizaVocabularyReader questVoc = new GizaVocabularyReader(prefix + "target.vcb", filterAndRecoder); GizaTranTableReaderAndRecoder gizaTable = new GizaTranTableReaderAndRecoder(false, // we don't need to flip the table for the purpose prefix + "/output.t1." + gizaIterQty, filterAndRecoder, answVoc, questVoc, (float) FeatureExtractor.DEFAULT_PROB_SELF_TRAN, minProb); BufferedWriter outFile = new BufferedWriter(new FileWriter(outFileName)); for (int srcWordId = 0; srcWordId <= textIndex.getMaxWordId(); ++srcWordId) { GizaOneWordTranRecs tranRecs = gizaTable.getTranProbs(srcWordId); if (null != tranRecs) { String wordSrc = textIndex.getWord(srcWordId); StringBuffer sb = new StringBuffer(); sb.append(wordSrc); for (int k = 0; k < tranRecs.mDstIds.length; ++k) { float prob = tranRecs.mProbs[k]; if (prob >= minProb) { int dstWordId = tranRecs.mDstIds[k]; if (dstWordId == srcWordId && outType != FormatType.kWeighted) continue; // Don't duplicate the word, unless it's probability weighted sb.append(' '); String dstWord = textIndex.getWord(dstWordId); if (null == dstWord) { throw new Exception( "Bug or inconsistent data: Couldn't retriev a word for wordId = " + dstWordId); } if (dstWord.indexOf(':') >= 0) throw new Exception( "Illegal dictionary word '" + dstWord + "' b/c it contains ':'"); sb.append(dstWord); if (outType != FormatType.kOrig) { sb.append(':'); sb.append(outType == FormatType.kWeighted ? prob : 1); } } } outFile.write(sb.toString()); outFile.newLine(); } } outFile.close(); } catch (ParseException e) { e.printStackTrace(); Usage("Cannot parse arguments", options); } catch (Exception e) { e.printStackTrace(); System.err.println("Terminating due to an exception: " + e); System.exit(1); } System.out.println("Terminated successfully!"); }
From source file:jlp.aspectj.test.MainDBCPtest.java
public static void main(String[] args) { // TODO Auto-generated method stub MainDBCPtest[] tabRunners = new MainDBCPtest[Integer.parseInt(args[0])]; for (int i = 0; i < tabRunners.length; i++) { tabRunners[i] = new MainDBCPtest(); tabRunners[i].thread.start();// w w w.j a va2 s. c om } }
From source file:com.hazelcast.jet.benchmark.trademonitor.FlinkTradeMonitor.java
public static void main(String[] args) throws Exception { if (args.length != 13) { System.err.println("Usage:"); System.err.println(" " + FlinkTradeMonitor.class.getSimpleName() + " <bootstrap.servers> <topic> <offset-reset> <maxLagMs> <windowSizeMs> <slideByMs> <outputPath> <checkpointInterval> <checkpointUri> <doAsyncSnapshot> <stateBackend> <kafkaParallelism> <windowParallelism>"); System.err.println("<stateBackend> - fs | rocksDb"); System.exit(1);//ww w .j a v a 2 s. c o m } String brokerUri = args[0]; String topic = args[1]; String offsetReset = args[2]; int lagMs = Integer.parseInt(args[3]); int windowSize = Integer.parseInt(args[4]); int slideBy = Integer.parseInt(args[5]); String outputPath = args[6]; int checkpointInt = Integer.parseInt(args[7]); String checkpointUri = args[8]; boolean doAsyncSnapshot = Boolean.parseBoolean(args[9]); String stateBackend = args[10]; int kafkaParallelism = Integer.parseInt(args[11]); int windowParallelism = Integer.parseInt(args[12]); System.out.println("bootstrap.servers: " + brokerUri); System.out.println("topic: " + topic); System.out.println("offset-reset: " + offsetReset); System.out.println("lag: " + lagMs); System.out.println("windowSize: " + windowSize); System.out.println("slideBy: " + slideBy); System.out.println("outputPath: " + outputPath); System.out.println("checkpointInt: " + checkpointInt); System.out.println("checkpointUri: " + checkpointUri); System.out.println("doAsyncSnapshot: " + doAsyncSnapshot); System.out.println("stateBackend: " + stateBackend); System.out.println("kafkaParallelism: " + kafkaParallelism); System.out.println("windowParallelism: " + windowParallelism); // set up the execution environment StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); if (checkpointInt > 0) { env.enableCheckpointing(checkpointInt); env.getCheckpointConfig().setMinPauseBetweenCheckpoints(checkpointInt); } env.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, 5000)); if ("fs".equalsIgnoreCase(stateBackend)) { env.setStateBackend(new FsStateBackend(checkpointUri, doAsyncSnapshot)); } else if ("rocksDb".equalsIgnoreCase(stateBackend)) { env.setStateBackend(new RocksDBStateBackend(checkpointUri)); } else { System.err.println("Bad value for stateBackend: " + stateBackend); System.exit(1); } DeserializationSchema<Trade> schema = new AbstractDeserializationSchema<Trade>() { TradeDeserializer deserializer = new TradeDeserializer(); @Override public Trade deserialize(byte[] message) throws IOException { return deserializer.deserialize(null, message); } }; DataStreamSource<Trade> trades = env .addSource(new FlinkKafkaConsumer010<>(topic, schema, getKafkaProperties(brokerUri, offsetReset))) .setParallelism(kafkaParallelism); AssignerWithPeriodicWatermarks<Trade> timestampExtractor = new BoundedOutOfOrdernessTimestampExtractor<Trade>( Time.milliseconds(lagMs)) { @Override public long extractTimestamp(Trade element) { return element.getTime(); } }; WindowAssigner window = windowSize == slideBy ? TumblingEventTimeWindows.of(Time.milliseconds(windowSize)) : SlidingEventTimeWindows.of(Time.milliseconds(windowSize), Time.milliseconds(slideBy)); trades.assignTimestampsAndWatermarks(timestampExtractor).keyBy((Trade t) -> t.getTicker()).window(window) .aggregate(new AggregateFunction<Trade, MutableLong, Long>() { @Override public MutableLong createAccumulator() { return new MutableLong(); } @Override public MutableLong add(Trade value, MutableLong accumulator) { accumulator.increment(); return accumulator; } @Override public MutableLong merge(MutableLong a, MutableLong b) { a.setValue(Math.addExact(a.longValue(), b.longValue())); return a; } @Override public Long getResult(MutableLong accumulator) { return accumulator.longValue(); } }, new WindowFunction<Long, Tuple5<String, String, Long, Long, Long>, String, TimeWindow>() { @Override public void apply(String key, TimeWindow window, Iterable<Long> input, Collector<Tuple5<String, String, Long, Long, Long>> out) throws Exception { long timeMs = System.currentTimeMillis(); long count = input.iterator().next(); long latencyMs = timeMs - window.getEnd() - lagMs; out.collect( new Tuple5<>(Instant.ofEpochMilli(window.getEnd()).atZone(ZoneId.systemDefault()) .toLocalTime().toString(), key, count, timeMs, latencyMs)); } }).setParallelism(windowParallelism).writeAsCsv(outputPath, WriteMode.OVERWRITE); env.execute("Trade Monitor Example"); }
From source file:bbs.monitor.ListNode.java
public static void main(String[] args) throws Exception { boolean printRawForm = false; String transport = null;/* w ww . ja v a 2s.co m*/ String selfAddressAndPort = null; // parse command-line arguments Options opts = new Options(); opts.addOption("h", "help", false, "print help"); opts.addOption("r", "raw", false, "print nodes in the raw form (hostname:port)"); opts.addOption("t", "transport", true, "transpoft, UDP or TCP"); opts.addOption("s", "selfipaddress", true, "self IP address (and port)"); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(opts, args); } catch (ParseException e) { System.out.println("There is an invalid option."); e.printStackTrace(); System.exit(1); } String optVal; if (cmd.hasOption('h')) { usage(COMMAND); System.exit(1); } if (cmd.hasOption('r')) { printRawForm = true; } optVal = cmd.getOptionValue('t'); if (optVal != null) { transport = optVal; } optVal = cmd.getOptionValue('s'); if (optVal != null) { selfAddressAndPort = optVal; } args = cmd.getArgs(); // parse initial contact String contactHostAndPort = null; int contactPort = -1; if (args.length < 1) { usage(COMMAND); System.exit(1); } contactHostAndPort = args[0]; if (args.length >= 2) contactPort = Integer.parseInt(args[1]); // // prepare a NodeCollector and invoke it // // prepare StatConfiguration StatConfiguration config = StatFactory.getDefaultConfiguration(); config.setDoUPnPNATTraversal(false); if (transport != null) { config.setMessagingTransport(transport); } if (selfAddressAndPort != null) { MessagingUtility.HostAndPort hostAndPort = MessagingUtility.parseHostnameAndPort(selfAddressAndPort, config.getSelfPort()); config.setSelfAddress(hostAndPort.getHostName()); config.setSelfPort(hostAndPort.getPort()); } // prepare MessageReceiver and initial contact MessagingProvider provider = config.deriveMessagingProvider(); MessageReceiver receiver = config.deriveMessageReceiver(provider); MessagingAddress contact; try { contact = provider.getMessagingAddress(contactHostAndPort, contactPort); } catch (IllegalArgumentException e) { contact = provider.getMessagingAddress(contactHostAndPort, config.getContactPort()); } // prepare a callback NodeCollectorCallback cb; if (printRawForm) { cb = new NodeCollectorCallback() { public void addNode(ID id, MessagingAddress address) { System.out.println(new MessagingUtility.HostAndPort(address.getHostname(), address.getPort())); // <hostname>:<port> } public void removeNode(ID id) { } }; } else { cb = new NodeCollectorCallback() { public void addNode(ID id, MessagingAddress address) { //System.out.println(HTMLUtil.convertMessagingAddressToURL(address)); System.out.println("http://" + address.getHostAddress() + ":" + address.getPort() + "/"); // http://<hostname>:port/ } public void removeNode(ID id) { } }; } // instantiate and invoke a NodeCollector NodeCollector collector = StatFactory.getNodeCollector(config, contact, cb, receiver); collector.investigate(); // stop MessageReceiver to prevent // handling incoming messages and submissions to a thread pool receiver.stop(); }
From source file:com.joliciel.talismane.terminology.Main.java
public static void main(String[] args) throws Exception { String termFilePath = null;/*from w w w. j ava 2 s . c o m*/ String outFilePath = null; Command command = Command.extract; int depth = -1; String databasePropertiesPath = null; String projectCode = null; Map<String, String> argMap = TalismaneConfig.convertArgs(args); String logConfigPath = argMap.get("logConfigFile"); if (logConfigPath != null) { argMap.remove("logConfigFile"); Properties props = new Properties(); props.load(new FileInputStream(logConfigPath)); PropertyConfigurator.configure(props); } Map<String, String> innerArgs = new HashMap<String, String>(); for (Entry<String, String> argEntry : argMap.entrySet()) { String argName = argEntry.getKey(); String argValue = argEntry.getValue(); if (argName.equals("command")) command = Command.valueOf(argValue); else if (argName.equals("termFile")) termFilePath = argValue; else if (argName.equals("outFile")) outFilePath = argValue; else if (argName.equals("depth")) depth = Integer.parseInt(argValue); else if (argName.equals("databaseProperties")) databasePropertiesPath = argValue; else if (argName.equals("projectCode")) projectCode = argValue; else innerArgs.put(argName, argValue); } if (termFilePath == null && databasePropertiesPath == null) throw new TalismaneException("Required argument: termFile or databasePropertiesPath"); if (termFilePath != null) { String currentDirPath = System.getProperty("user.dir"); File termFileDir = new File(currentDirPath); if (termFilePath.lastIndexOf("/") >= 0) { String termFileDirPath = termFilePath.substring(0, termFilePath.lastIndexOf("/")); termFileDir = new File(termFileDirPath); termFileDir.mkdirs(); } } long startTime = new Date().getTime(); try { TerminologyServiceLocator terminologyServiceLocator = TerminologyServiceLocator.getInstance(); TerminologyService terminologyService = terminologyServiceLocator.getTerminologyService(); TerminologyBase terminologyBase = null; if (projectCode == null) throw new TalismaneException("Required argument: projectCode"); File file = new File(databasePropertiesPath); FileInputStream fis = new FileInputStream(file); Properties dataSourceProperties = new Properties(); dataSourceProperties.load(fis); terminologyBase = terminologyService.getPostGresTerminologyBase(projectCode, dataSourceProperties); if (command.equals(Command.analyse) || command.equals(Command.extract)) { if (depth < 0) throw new TalismaneException("Required argument: depth"); if (command.equals(Command.analyse)) { innerArgs.put("command", "analyse"); } else { innerArgs.put("command", "process"); } TalismaneFrench talismaneFrench = new TalismaneFrench(); TalismaneConfig config = new TalismaneConfig(innerArgs, talismaneFrench); PosTagSet tagSet = TalismaneSession.getPosTagSet(); Charset outputCharset = config.getOutputCharset(); TermExtractor termExtractor = terminologyService.getTermExtractor(terminologyBase); termExtractor.setMaxDepth(depth); termExtractor.setOutFilePath(termFilePath); termExtractor.getIncludeChildren().add(tagSet.getPosTag("P")); termExtractor.getIncludeChildren().add(tagSet.getPosTag("P+D")); termExtractor.getIncludeChildren().add(tagSet.getPosTag("CC")); termExtractor.getIncludeWithParent().add(tagSet.getPosTag("DET")); if (outFilePath != null) { if (outFilePath.lastIndexOf("/") >= 0) { String outFileDirPath = outFilePath.substring(0, outFilePath.lastIndexOf("/")); File outFileDir = new File(outFileDirPath); outFileDir.mkdirs(); } File outFile = new File(outFilePath); outFile.delete(); outFile.createNewFile(); Writer writer = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outFilePath), outputCharset)); TermAnalysisWriter termAnalysisWriter = new TermAnalysisWriter(writer); termExtractor.addTermObserver(termAnalysisWriter); } Talismane talismane = config.getTalismane(); talismane.setParseConfigurationProcessor(termExtractor); talismane.process(); } else if (command.equals(Command.list)) { List<Term> terms = terminologyBase.getTermsByFrequency(2); for (Term term : terms) { LOG.debug("Term: " + term.getText()); LOG.debug("Frequency: " + term.getFrequency()); LOG.debug("Heads: " + term.getHeads()); LOG.debug("Expansions: " + term.getExpansions()); LOG.debug("Contexts: " + term.getContexts()); } } } finally { long endTime = new Date().getTime(); long totalTime = endTime - startTime; LOG.info("Total time: " + totalTime); } }
From source file:edu.oregonstate.eecs.mcplan.domains.voyager.Main.java
/** * @param args/*from ww w . j a v a 2 s . c om*/ */ public static void main(final String[] args) { System.out.println(args.toString()); final String batch_name = args[0]; final String[] instance_args = args[1].split(","); final String[] pi_args = args[2].split(","); final String[] phi_args = args[3].split(","); final File root_directory = createDirectory(args); final int Nplanets = Integer.parseInt(instance_args[0]); final int policy_epoch = Integer.parseInt(instance_args[1]); final int Nworlds = Integer.parseInt(instance_args[2]); final int max_time = Integer.parseInt(instance_args[3]); final int Nanytime = Integer.parseInt(instance_args[4]); // FIXME: This default_params thing is too error-prone! There's no // easy way to know whether you need to set a parameter in // 1) default_params // 2) an element of ps // 3) both places final VoyagerParameters default_params = new VoyagerParameters.Builder().Nplanets(Nplanets) .policy_epoch(policy_epoch).finish(); final Environment default_environment = new Environment.Builder().root_directory(root_directory) .rng(new MersenneTwister(default_params.master_seed)).finish(); final int[] anytime_times = new int[Nanytime]; anytime_times[Nanytime - 1] = max_time; for (int i = Nanytime - 2; i >= 0; --i) { anytime_times[i] = anytime_times[i + 1] / 2; } final List<VoyagerParameters> ps = new ArrayList<VoyagerParameters>(Nanytime); for (final int t : anytime_times) { ps.add(new VoyagerParameters.Builder().max_time(t).Nplanets(Nplanets).policy_epoch(policy_epoch) .finish()); } final List<VoyagerInstance> ws = new ArrayList<VoyagerInstance>(Nworlds); for (int i = 0; i < Nworlds; ++i) { // FIXME: Why default_params and not ps.get( i ) ? ws.add(new VoyagerInstance(default_params, default_environment.rng.nextInt())); } final MultipleInstanceMultipleWorldGenerator<VoyagerParameters, VoyagerInstance> experimental_setups = new MultipleInstanceMultipleWorldGenerator<VoyagerParameters, VoyagerInstance>( default_environment, ps, ws); final ArrayList<EpisodeListener<VoyagerState, VoyagerEvent>> extra_listeners = new ArrayList<EpisodeListener<VoyagerState, VoyagerEvent>>(); if (default_params.use_monitor) { // TODO: Actually create the viewport! extra_listeners.add(new VisualizationUpdater()); } final Experiment<VoyagerParameters, VoyagerInstance> experiment = new PolicyComparison<VoyagerState, VoyagerEvent, VoyagerParameters, VoyagerInstance>( createPolicy(pi_args), createPolicy(phi_args), extra_listeners); while (experimental_setups.hasNext()) { final ExperimentalSetup<VoyagerParameters, VoyagerInstance> setup = experimental_setups.next(); experiment.setup(setup.environment, setup.parameters, setup.world); experiment.run(); experiment.finish(); } System.exit(0); }
From source file:cc.twittertools.search.api.TrecSearchThriftServer.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(new Option(HELP_OPTION, "show help")); options.addOption(OptionBuilder.withArgName("port").hasArg().withDescription("port").create(PORT_OPTION)); options.addOption(/*from w w w .j a v a 2 s. c o m*/ OptionBuilder.withArgName("index").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption(OptionBuilder.withArgName("num").hasArg() .withDescription("max number of threads in thread pool").create(MAX_THREADS_OPTION)); options.addOption(OptionBuilder.withArgName("file").hasArg() .withDescription("file containing access tokens").create(CREDENTIALS_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(INDEX_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(TrecSearchThriftServer.class.getName(), options); System.exit(-1); } int port = cmdline.hasOption(PORT_OPTION) ? Integer.parseInt(cmdline.getOptionValue(PORT_OPTION)) : DEFAULT_PORT; int maxThreads = cmdline.hasOption(MAX_THREADS_OPTION) ? Integer.parseInt(cmdline.getOptionValue(MAX_THREADS_OPTION)) : DEFAULT_MAX_THREADS; File index = new File(cmdline.getOptionValue(INDEX_OPTION)); Map<String, String> credentials = null; if (cmdline.hasOption(CREDENTIALS_OPTION)) { credentials = Maps.newHashMap(); File cfile = new File(cmdline.getOptionValue(CREDENTIALS_OPTION)); if (!cfile.exists()) { System.err.println("Error: " + cfile + " does not exist!"); System.exit(-1); } for (String s : Files.readLines(cfile, Charsets.UTF_8)) { try { String[] arr = s.split(":"); credentials.put(arr[0], arr[1]); } catch (Exception e) { // Catch any exceptions from parsing file contain access tokens System.err.println("Error reading access tokens from " + cfile + "!"); System.exit(-1); } } } if (!index.exists()) { System.err.println("Error: " + index + " does not exist!"); System.exit(-1); } TServerSocket serverSocket = new TServerSocket(port); TrecSearch.Processor<TrecSearch.Iface> searchProcessor = new TrecSearch.Processor<TrecSearch.Iface>( new TrecSearchHandler(index, credentials)); TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverSocket); serverArgs.maxWorkerThreads(maxThreads); TServer thriftServer = new TThreadPoolServer( serverArgs.processor(searchProcessor).protocolFactory(new TBinaryProtocol.Factory())); thriftServer.serve(); }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.QueryGenNMSLIB.java
public static void main(String[] args) { Options options = new Options(); options.addOption(CommonParams.QUERY_FILE_PARAM, null, true, CommonParams.QUERY_FILE_DESC); options.addOption(CommonParams.MEMINDEX_PARAM, null, true, CommonParams.MEMINDEX_DESC); options.addOption(CommonParams.KNN_QUERIES_PARAM, null, true, CommonParams.KNN_QUERIES_DESC); options.addOption(CommonParams.NMSLIB_FIELDS_PARAM, null, true, CommonParams.NMSLIB_FIELDS_DESC); options.addOption(CommonParams.MAX_NUM_QUERY_PARAM, null, true, CommonParams.MAX_NUM_QUERY_DESC); options.addOption(CommonParams.SEL_PROB_PARAM, null, true, CommonParams.SEL_PROB_DESC); CommandLineParser parser = new org.apache.commons.cli.GnuParser(); BufferedWriter knnQueries = null; int maxNumQuery = Integer.MAX_VALUE; Float selProb = null;/*from ww w .j a v a 2 s .c om*/ try { CommandLine cmd = parser.parse(options, args); String queryFile = null; if (cmd.hasOption(CommonParams.QUERY_FILE_PARAM)) { queryFile = cmd.getOptionValue(CommonParams.QUERY_FILE_PARAM); } else { Usage("Specify 'query file'", options); } String knnQueriesFile = cmd.getOptionValue(CommonParams.KNN_QUERIES_PARAM); if (null == knnQueriesFile) Usage("Specify '" + CommonParams.KNN_QUERIES_DESC + "'", options); String tmpn = cmd.getOptionValue(CommonParams.MAX_NUM_QUERY_PARAM); if (tmpn != null) { try { maxNumQuery = Integer.parseInt(tmpn); } catch (NumberFormatException e) { Usage("Maximum number of queries isn't integer: '" + tmpn + "'", options); } } String tmps = cmd.getOptionValue(CommonParams.NMSLIB_FIELDS_PARAM); if (null == tmps) Usage("Specify '" + CommonParams.NMSLIB_FIELDS_DESC + "'", options); String nmslibFieldList[] = tmps.split(","); knnQueries = new BufferedWriter(new FileWriter(knnQueriesFile)); knnQueries.write("isQueryFile=1"); knnQueries.newLine(); knnQueries.newLine(); String memIndexPref = cmd.getOptionValue(CommonParams.MEMINDEX_PARAM); if (null == memIndexPref) { Usage("Specify '" + CommonParams.MEMINDEX_DESC + "'", options); } String tmpf = cmd.getOptionValue(CommonParams.SEL_PROB_PARAM); if (tmpf != null) { try { selProb = Float.parseFloat(tmpf); } catch (NumberFormatException e) { Usage("A selection probability isn't a number in the range (0,1)'" + tmpf + "'", options); } if (selProb < Float.MIN_NORMAL || selProb + Float.MIN_NORMAL >= 1) Usage("A selection probability isn't a number in the range (0,1)'" + tmpf + "'", options); } BufferedReader inpText = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(queryFile))); String docText = XmlHelper.readNextXMLIndexEntry(inpText); NmslibQueryGenerator queryGen = new NmslibQueryGenerator(nmslibFieldList, memIndexPref); Random rnd = new Random(); for (int docNum = 1; docNum <= maxNumQuery && docText != null; ++docNum, docText = XmlHelper.readNextXMLIndexEntry(inpText)) { if (selProb != null) { if (rnd.nextFloat() > selProb) continue; } Map<String, String> docFields = null; try { docFields = XmlHelper.parseXMLIndexEntry(docText); String queryObjStr = queryGen.getStrObjForKNNService(docFields); knnQueries.append(queryObjStr); knnQueries.newLine(); } catch (SAXException e) { System.err.println("Parsing error, offending DOC:" + NL + docText + " doc # " + docNum); throw new Exception("Parsing error."); } } knnQueries.close(); } catch (ParseException e) { Usage("Cannot parse arguments", options); if (null != knnQueries) try { knnQueries.close(); } catch (IOException e1) { e1.printStackTrace(); } } catch (Exception e) { System.err.println("Terminating due to an exception: " + e); try { if (knnQueries != null) knnQueries.close(); } catch (IOException e1) { e1.printStackTrace(); } System.exit(1); } System.out.println("Terminated successfully!"); }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.FilterVocabulary.java
public static void main(String[] args) { String optKeys[] = { IN_VOC_FILE_PARAM, OUT_VOC_FILE_PARAM, CommonParams.MEM_FWD_INDEX_PARAM, CommonParams.MAX_WORD_QTY_PARAM }; String optDescs[] = { IN_VOC_FILE_DESC, OUT_VOC_FILE_DESC, CommonParams.MEM_FWD_INDEX_DESC, CommonParams.MAX_WORD_QTY_DESC }; boolean hasArg[] = { true, true, true, true }; ParamHelper mParamHelper = null;// w w w . jav a 2 s. c o m try { mParamHelper = new ParamHelper(args, optKeys, optDescs, hasArg); CommandLine cmd = mParamHelper.getCommandLine(); String outputFile = cmd.getOptionValue(OUT_VOC_FILE_PARAM); if (null == outputFile) { UsageSpecify(OUT_VOC_FILE_DESC, mParamHelper.getOptions()); } String inputFile = cmd.getOptionValue(IN_VOC_FILE_PARAM); if (null == inputFile) { UsageSpecify(IN_VOC_FILE_DESC, mParamHelper.getOptions()); } int maxWordQty = Integer.MAX_VALUE; String tmpi = cmd.getOptionValue(CommonParams.MAX_WORD_QTY_PARAM); if (null != tmpi) { maxWordQty = Integer.parseInt(tmpi); } String memFwdIndxName = cmd.getOptionValue(CommonParams.MEM_FWD_INDEX_PARAM); if (null == memFwdIndxName) { UsageSpecify(CommonParams.MEM_FWD_INDEX_DESC, mParamHelper.getOptions()); } VocabularyFilterAndRecoder filter = new FrequentIndexWordFilterAndRecoder(memFwdIndxName, maxWordQty); BufferedReader finp = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(inputFile))); BufferedWriter fout = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outputFile))); try { String line; int wordQty = 0; long addedQty = 0; long totalQty = 0; for (totalQty = 0; (line = finp.readLine()) != null;) { ++totalQty; // Skip empty lines line = line.trim(); if (line.isEmpty()) continue; GizaVocRec rec = new GizaVocRec(line); if (filter.checkWord(rec.mWord)) { rec.save(fout); addedQty++; } if (totalQty % REPORT_INTERVAL_QTY == 0) System.out.println(String.format( "Processed %d lines (%d source word entries) from '%s', added %d lines", totalQty, wordQty, inputFile, addedQty)); } } finally { finp.close(); fout.close(); } } catch (ParseException e) { Usage("Cannot parse arguments", mParamHelper != null ? mParamHelper.getOptions() : null); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.err.println("Terminating due to an exception: " + e); System.exit(1); } }