List of usage examples for java.lang String split
public String[] split(String regex)
From source file:com.github.brandtg.stl.StlPlotter.java
public static void main(String[] args) throws Exception { List<Double> times = new ArrayList<Double>(); List<Double> series = new ArrayList<Double>(); List<Double> trend = new ArrayList<Double>(); List<Double> seasonal = new ArrayList<Double>(); List<Double> remainder = new ArrayList<Double>(); // Read from STDIN String line; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while ((line = reader.readLine()) != null) { String[] tokens = line.split(","); times.add(Double.valueOf(tokens[0])); series.add(Double.valueOf(tokens[1])); trend.add(Double.valueOf(tokens[2])); seasonal.add(Double.valueOf(tokens[3])); remainder.add(Double.valueOf(tokens[4])); }/* w w w . ja v a 2s . com*/ StlResult res = new StlResult(convert(times), convert(series), convert(trend), convert(seasonal), convert(remainder)); if (args.length == 1) { plot(res, new File(args[0])); } else { plotOnScreen(res, "Seasonal Decomposition"); } }
From source file:com.oculusinfo.ml.spark.unsupervised.TestKMeans.java
/** * @param args/* w ww . j av a2s. com*/ */ public static void main(String[] args) { int k = 5; try { FileUtils.deleteDirectory(new File("output/clusters")); FileUtils.deleteDirectory(new File("output/centroids")); } catch (IOException e1) { /* ignore (*/ } genTestData(k); JavaSparkContext sc = new JavaSparkContext("local", "OculusML"); SparkDataSet ds = new SparkDataSet(sc); ds.load("test.txt", new SparkInstanceParser() { private static final long serialVersionUID = 1L; @Override public Tuple2<String, Instance> call(String line) throws Exception { Instance inst = new Instance(); String tokens[] = line.split(","); NumericVectorFeature v = new NumericVectorFeature("point"); double x = Double.parseDouble(tokens[0]); double y = Double.parseDouble(tokens[1]); v.setValue(new double[] { x, y }); inst.addFeature(v); return new Tuple2<String, Instance>(inst.getId(), inst); } }); KMeansClusterer clusterer = new KMeansClusterer(k, 10, 0.001, "output/centroids", "output/clusters"); clusterer.registerFeatureType("point", MeanNumericVectorCentroid.class, new EuclideanDistance(1.0)); clusterer.doCluster(ds); try { final List<double[]> instances = readInstances(); final Color[] colors = { Color.red, Color.blue, Color.green, Color.magenta, Color.yellow, Color.black, Color.orange, Color.cyan, Color.darkGray, Color.white }; TestKMeans t = new TestKMeans(); t.add(new JComponent() { private static final long serialVersionUID = 2059497051387104848L; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (double[] inst : instances) { int color = (int) inst[0]; g.setColor(colors[color]); Ellipse2D l = new Ellipse2D.Double(inst[1], inst[2], 5, 5); g2.draw(l); } } }); t.setDefaultCloseOperation(EXIT_ON_CLOSE); t.setSize(400, 400); t.setVisible(true); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.kasabi.data.movies.freebase.FreebaseMovies2RDF.java
public static void main(String[] args) throws Exception { String filename = "/backups/tmp/freebase-datadump-quadruples.tsv.bz2"; BufferedReader in = new BufferedReader( new InputStreamReader(new BZip2CompressorInputStream(new FileInputStream(filename)))); String line; int count = 0; Model model = MoviesCommon.createModel(); String prev_subject = null;//from w ww . j a v a 2 s . co m while ((line = in.readLine()) != null) { count++; String[] tokens = line.split("\\t"); if (tokens.length > 0) { String subject = tokens[0].trim(); if (!subject.equals(prev_subject)) { process(model); model = MoviesCommon.createModel(); } prev_subject = subject; if ((tokens.length == 3) && (tokens[0].trim().length() > 0) && (tokens[1].trim().length() > 0) && (tokens[2].trim().length() > 0)) { output_resource(model, tokens[0], tokens[1], tokens[2]); } else if ((tokens.length == 4) && (tokens[0].trim().length() > 0) && (tokens[1].trim().length() > 0) && (tokens[3].trim().length() > 0)) { if (tokens[2].trim().length() == 0) { output_literal(model, tokens[0], tokens[1], tokens[3]); } else { if (tokens[2].startsWith(LANG)) { output_literal_lang(model, tokens[0], tokens[1], tokens[3], tokens[2]); } else { if (tokens[1].equals("/type/object/key")) { output_literal2(model, tokens[0], tokens[1], tokens[2], tokens[3]); } else if ((tokens[1].equals("/type/object/name")) && (tokens[2].startsWith("/guid/"))) { output_literal2(model, tokens[0], tokens[1], tokens[2], tokens[3]); } else { log.warn("Unexpected data at {}, ignoring: {}", count, line); } } } } else { if (tokens.length < 3) { log.warn("Line {} has only {} tokens: {}", new Object[] { count, tokens.length, line }); } else { log.warn("Line {} has one or more empty tokens: {}", new Object[] { count, line }); } } } if (count % 1000000 == 0) log.info("Processed {} lines...", count); } }
From source file:com.github.ruananswer.stl.StlPlotter.java
public static void main(String[] args) throws Exception { List<Double> times = new ArrayList<Double>(); List<Double> series = new ArrayList<Double>(); List<Double> trend = new ArrayList<Double>(); List<Double> seasonal = new ArrayList<Double>(); List<Double> remainder = new ArrayList<Double>(); // Read from STDIN String line; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while ((line = reader.readLine()) != null) { String[] tokens = line.split(","); times.add(Double.valueOf(tokens[0])); series.add(Double.valueOf(tokens[1])); trend.add(Double.valueOf(tokens[2])); seasonal.add(Double.valueOf(tokens[3])); remainder.add(Double.valueOf(tokens[4])); }//from w w w . j a va2 s .c o m STLResult res = new STLResult(convert(trend), convert(seasonal), convert(remainder)); double[] tmpSeries = convert(series); double[] tmpTimes = convert(times); if (args.length == 1) { plot(res, tmpSeries, tmpTimes, new File(args[0])); } else { plotOnScreen(res, tmpSeries, tmpTimes, "Seasonal Decomposition"); } }
From source file:org.apache.accumulo.maven.plugin.StartMojo.java
public static void main(String[] args) throws MojoExecutionException { int a = 0;//from ww w. ja v a 2 s. co m for (String arg : args) { if (a < 2) { // skip the first two args a++; continue; } StartMojo starter = new StartMojo(); starter.outputDirectory = new File(args[0]); String[] instArgs = arg.split(" "); starter.instanceName = instArgs[0]; starter.rootPassword = instArgs[1]; starter.miniClasspath = args[1]; starter.execute(); } }
From source file:WriteIndex.java
/** * @param args//w w w . jav a 2s. c om */ public static void main(String[] args) throws IOException { File docs = new File("documents"); File indexDir = new File(INDEX_DIRECTORY); Directory directory = FSDirectory.open(indexDir); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, analyzer); IndexWriter writer = new IndexWriter(directory, conf); writer.deleteAll(); for (File file : docs.listFiles()) { Metadata metadata = new Metadata(); ContentHandler handler = new BodyContentHandler(); ParseContext context = new ParseContext(); Parser parser = new AutoDetectParser(); InputStream stream = new FileInputStream(file); try { parser.parse(stream, handler, metadata, context); } catch (TikaException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } finally { stream.close(); } String text = handler.toString(); String fileName = file.getName(); Document doc = new Document(); doc.add(new Field("file", fileName, Store.YES, Index.NO)); for (String key : metadata.names()) { String name = key.toLowerCase(); String value = metadata.get(key); if (StringUtils.isBlank(value)) { continue; } if ("keywords".equalsIgnoreCase(key)) { for (String keyword : value.split(",?(\\s+)")) { doc.add(new Field(name, keyword, Store.YES, Index.NOT_ANALYZED)); } } else if ("title".equalsIgnoreCase(key)) { doc.add(new Field(name, value, Store.YES, Index.ANALYZED)); } else { doc.add(new Field(name, fileName, Store.YES, Index.NOT_ANALYZED)); } } doc.add(new Field("text", text, Store.NO, Index.ANALYZED)); writer.addDocument(doc); } writer.commit(); writer.deleteUnusedFiles(); System.out.println(writer.maxDoc() + " documents written"); }
From source file:edu.msu.cme.rdp.multicompare.Reprocess.java
/** * This class reprocesses the classification results (allrank output) and print out hierarchy output file, based on the confidence cutoff; * and print out only the detail classification results with assignment at certain rank with confidence above the cutoff or/and matching a given taxon. * @param args// ww w .j a v a 2 s . co m * @throws Exception */ public static void main(String[] args) throws Exception { PrintWriter assign_out = new PrintWriter(new NullWriter()); float conf = 0.8f; PrintStream heir_out = null; String hier_out_filename = null; ClassificationResultFormatter.FORMAT format = ClassificationResultFormatter.FORMAT.allRank; String rank = null; String taxonFilterFile = null; String train_propfile = null; String gene = null; List<MCSample> samples = new ArrayList(); try { CommandLine line = new PosixParser().parse(options, args); if (line.hasOption(CmdOptions.HIER_OUTFILE_SHORT_OPT)) { hier_out_filename = line.getOptionValue(CmdOptions.HIER_OUTFILE_SHORT_OPT); heir_out = new PrintStream(hier_out_filename); } else { throw new Exception( "It make sense to provide output filename for " + CmdOptions.HIER_OUTFILE_LONG_OPT); } if (line.hasOption(CmdOptions.OUTFILE_SHORT_OPT)) { assign_out = new PrintWriter(line.getOptionValue(CmdOptions.OUTFILE_SHORT_OPT)); } if (line.hasOption(CmdOptions.RANK_SHORT_OPT)) { rank = line.getOptionValue(CmdOptions.RANK_SHORT_OPT); } if (line.hasOption(CmdOptions.TAXON_SHORT_OPT)) { taxonFilterFile = line.getOptionValue(CmdOptions.TAXON_SHORT_OPT); } if (line.hasOption(CmdOptions.BOOTSTRAP_SHORT_OPT)) { conf = Float.parseFloat(line.getOptionValue(CmdOptions.BOOTSTRAP_SHORT_OPT)); if (conf < 0 || conf > 1) { throw new IllegalArgumentException("Confidence must be in the range [0,1]"); } } if (line.hasOption(CmdOptions.FORMAT_SHORT_OPT)) { String f = line.getOptionValue(CmdOptions.FORMAT_SHORT_OPT); if (f.equalsIgnoreCase("allrank")) { format = ClassificationResultFormatter.FORMAT.allRank; } else if (f.equalsIgnoreCase("fixrank")) { format = ClassificationResultFormatter.FORMAT.fixRank; } else if (f.equalsIgnoreCase("db")) { format = ClassificationResultFormatter.FORMAT.dbformat; } else if (f.equalsIgnoreCase("filterbyconf")) { format = ClassificationResultFormatter.FORMAT.filterbyconf; } else { throw new IllegalArgumentException( "Not valid output format, only allrank, fixrank, filterbyconf and db allowed"); } } if (line.hasOption(CmdOptions.TRAINPROPFILE_SHORT_OPT)) { if (gene != null) { throw new IllegalArgumentException( "Already specified the gene from the default location. Can not specify train_propfile"); } else { train_propfile = line.getOptionValue(CmdOptions.TRAINPROPFILE_SHORT_OPT); } } if (line.hasOption(CmdOptions.GENE_SHORT_OPT)) { if (train_propfile != null) { throw new IllegalArgumentException( "Already specified train_propfile. Can not specify gene any more"); } gene = line.getOptionValue(CmdOptions.GENE_SHORT_OPT).toLowerCase(); if (!gene.equals(ClassifierFactory.RRNA_16S_GENE) && !gene.equals(ClassifierFactory.FUNGALLSU_GENE) && !gene.equals(ClassifierFactory.FUNGALITS_warcup_GENE) && !gene.equals(ClassifierFactory.FUNGALITS_unite_GENE)) { throw new IllegalArgumentException(gene + " is NOT valid, only allows " + ClassifierFactory.RRNA_16S_GENE + ", " + ClassifierFactory.FUNGALLSU_GENE + ", " + ClassifierFactory.FUNGALITS_warcup_GENE + " and " + ClassifierFactory.FUNGALITS_unite_GENE); } } args = line.getArgs(); if (args.length < 1) { throw new Exception("Incorrect number of command line arguments"); } for (String arg : args) { String[] inFileNames = arg.split(","); String inputFile = inFileNames[0]; File idmappingFile = null; if (inFileNames.length == 2) { idmappingFile = new File(inFileNames[1]); if (!idmappingFile.exists()) { System.err.println("Failed to find input file \"" + inFileNames[1] + "\""); return; } } MCSample nextSample = new MCSampleResult(inputFile, idmappingFile); samples.add(nextSample); } } catch (Exception e) { System.out.println("Command Error: " + e.getMessage()); new HelpFormatter().printHelp(120, "Reprocess [options] <Classification_allrank_result>[,idmappingfile] ...", "", options, ""); return; } if (train_propfile == null && gene == null) { gene = ClassifierFactory.RRNA_16S_GENE; } HashSet<String> taxonFilter = null; if (taxonFilterFile != null) { taxonFilter = readTaxonFilterFile(taxonFilterFile); } MultiClassifier multiClassifier = new MultiClassifier(train_propfile, gene); DefaultPrintVisitor printVisitor = new DefaultPrintVisitor(heir_out, samples); MultiClassifierResult result = multiClassifier.multiClassificationParser(samples, conf, assign_out, format, rank, taxonFilter); result.getRoot().topDownVisit(printVisitor); assign_out.close(); heir_out.close(); if (multiClassifier.hasCopyNumber()) { // print copy number corrected counts File cn_corrected_s = new File(new File(hier_out_filename).getParentFile(), "cncorrected_" + hier_out_filename); PrintStream cn_corrected_hier_out = new PrintStream(cn_corrected_s); printVisitor = new DefaultPrintVisitor(cn_corrected_hier_out, samples, true); result.getRoot().topDownVisit(printVisitor); cn_corrected_hier_out.close(); } }
From source file:edu.oregonstate.eecs.mcplan.domains.inventory.InventorySimulator.java
public static void main(final String[] argv) throws IOException { final RandomGenerator rng = new MersenneTwister(42); final int Nproducts = 2; final int max_inventory = 10; final double warehouse_cost = 1; final int min_order = 1; final int max_order = 2; final double delivery_probability = 0.5; final int max_demand = 5; final int[] price = new int[] { 1, 2 }; final InventoryProblem problem = InventoryProblem.TwoProducts(); // Nproducts, price, max_inventory, warehouse_cost, min_order, max_order, delivery_probability, max_demand ); while (true) { final InventoryState s = new InventoryState(rng, problem); final InventorySimulator sim = new InventorySimulator(s); final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (!s.isTerminal()) { System.out.println(sim.state()); final String cmd = reader.readLine(); final String[] tokens = cmd.split(","); final InventoryAction a; if ("n".equals(tokens[0])) { a = new InventoryNothingAction(); } else { final int product = Integer.parseInt(tokens[0]); final int quantity = Integer.parseInt(tokens[1]); a = new InventoryOrderAction(product, quantity, s.problem.cost[product]); }//w ww. java 2 s. co m sim.takeAction(new JointAction<InventoryAction>(a)); System.out.println("r = " + Arrays.toString(sim.reward())); } // System.out.print( "Hand: " ); // System.out.print( sim.state().player_hand ); // System.out.print( " (" ); // final ArrayList<int[]> values = sim.state().player_hand.values(); // for( int i = 0; i < values.size(); ++i ) { // if( i > 0 ) { // System.out.print( ", " ); // } // System.out.print( Arrays.toString( values.get( i ) ) ); // } // System.out.println( ")" ); // // System.out.print( "Reward: " ); // System.out.println( Arrays.toString( sim.reward() ) ); // System.out.print( "Dealer hand: " ); // System.out.print( sim.state().dealerHand().toString() ); // System.out.print( " (" ); // System.out.print( SpBjHand.handValue( sim.state().dealerHand() )[0] ); // System.out.println( ")" ); // System.out.println( "----------------------------------------" ); } }
From source file:org.openplans.delayfeeder.LoadFeeds.java
public static void main(String args[]) throws HibernateException, IOException { if (args.length != 1) { System.out.println("expected one argument: the path to a csv of agency,route,url"); }/*from w w w . j a v a2s. c o m*/ GenericApplicationContext context = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context); xmlReader.loadBeanDefinitions("org/openplans/delayfeeder/application-context.xml"); xmlReader.loadBeanDefinitions("data-sources.xml"); SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory"); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); FileReader fileReader = new FileReader(new File(args[0])); BufferedReader bufferedReader = new BufferedReader(fileReader); while (bufferedReader.ready()) { String line = bufferedReader.readLine().trim(); if (line.startsWith("#")) { continue; } if (line.length() < 3) { //blank or otherwise broken line continue; } String[] parts = line.split(","); String agency = parts[0]; String route = parts[1]; String url = parts[2]; Query query = session.createQuery("from RouteFeed where agency = :agency and route = :route"); query.setParameter("agency", agency); query.setParameter("route", route); @SuppressWarnings("rawtypes") List list = query.list(); RouteFeed feed; if (list.size() == 0) { feed = new RouteFeed(); feed.agency = agency; feed.route = route; feed.lastEntry = new GregorianCalendar(); } else { feed = (RouteFeed) list.get(0); } if (!url.equals(feed.url)) { feed.url = url; feed.lastEntry.setTimeInMillis(0); } session.saveOrUpdate(feed); } tx.commit(); }
From source file:examples.mail.IMAPImportMbox.java
public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println(/*from www . j ava 2s . c o m*/ "Usage: IMAPImportMbox imap[s]://user:password@host[:port]/folder/path <mboxfile> [selectors]"); System.err.println("\tWhere: a selector is a list of numbers/number ranges - 1,2,3-10" + " - or a list of strings to match in the initial From line"); System.exit(1); } final URI uri = URI.create(args[0]); final String file = args[1]; final File mbox = new File(file); if (!mbox.isFile() || !mbox.canRead()) { throw new IOException("Cannot read mailbox file: " + mbox); } String path = uri.getPath(); if (path == null || path.length() < 1) { throw new IllegalArgumentException("Invalid folderPath: '" + path + "'"); } String folder = path.substring(1); // skip the leading / List<String> contains = new ArrayList<String>(); // list of strings to find BitSet msgNums = new BitSet(); // list of message numbers for (int i = 2; i < args.length; i++) { String arg = args[i]; if (arg.matches("\\d+(-\\d+)?(,\\d+(-\\d+)?)*")) { // number,m-n for (String entry : arg.split(",")) { String[] parts = entry.split("-"); if (parts.length == 2) { // m-n int low = Integer.parseInt(parts[0]); int high = Integer.parseInt(parts[1]); for (int j = low; j <= high; j++) { msgNums.set(j); } } else { msgNums.set(Integer.parseInt(entry)); } } } else { contains.add(arg); // not a number/number range } } // System.out.println(msgNums.toString()); // System.out.println(java.util.Arrays.toString(contains.toArray())); // Connect and login final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null); int total = 0; int loaded = 0; try { imap.setSoTimeout(6000); final BufferedReader br = new BufferedReader(new FileReader(file)); // TODO charset? String line; StringBuilder sb = new StringBuilder(); boolean wanted = false; // Skip any leading rubbish while ((line = br.readLine()) != null) { if (line.startsWith("From ")) { // start of message; i.e. end of previous (if any) if (process(sb, imap, folder, total)) { // process previous message (if any) loaded++; } sb.setLength(0); total++; wanted = wanted(total, line, msgNums, contains); } else if (startsWith(line, PATFROM)) { // Unescape ">+From " in body text line = line.substring(1); } // TODO process first Received: line to determine arrival date? if (wanted) { sb.append(line); sb.append(CRLF); } } br.close(); if (wanted && process(sb, imap, folder, total)) { // last message (if any) loaded++; } } catch (IOException e) { System.out.println(imap.getReplyString()); e.printStackTrace(); System.exit(10); return; } finally { imap.logout(); imap.disconnect(); } System.out.println("Processed " + total + " messages, loaded " + loaded); }