List of usage examples for java.util Scanner hasNextInt
public boolean hasNextInt()
From source file:org.apache.mahout.classifier.sequencelearning.hmm.BaumWelchTrainer.java
public static void main(String[] args) throws IOException { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputOption = DefaultOptionCreator.inputOption().create(); Option outputOption = DefaultOptionCreator.outputOption().create(); Option stateNumberOption = optionBuilder.withLongName("nrOfHiddenStates") .withDescription("Number of hidden states").withShortName("nh") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("number").create()) .withRequired(true).create(); Option observedStateNumberOption = optionBuilder.withLongName("nrOfObservedStates") .withDescription("Number of observed states").withShortName("no") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("number").create()) .withRequired(true).create(); Option epsilonOption = optionBuilder.withLongName("epsilon").withDescription("Convergence threshold") .withShortName("e") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("number").create()) .withRequired(true).create(); Option iterationsOption = optionBuilder.withLongName("max-iterations") .withDescription("Maximum iterations number").withShortName("m") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("number").create()) .withRequired(true).create(); Group optionGroup = new GroupBuilder().withOption(inputOption).withOption(outputOption) .withOption(stateNumberOption).withOption(observedStateNumberOption).withOption(epsilonOption) .withOption(iterationsOption).withName("Options").create(); try {/*ww w . j a v a2 s .c o m*/ Parser parser = new Parser(); parser.setGroup(optionGroup); CommandLine commandLine = parser.parse(args); String input = (String) commandLine.getValue(inputOption); String output = (String) commandLine.getValue(outputOption); int nrOfHiddenStates = Integer.parseInt((String) commandLine.getValue(stateNumberOption)); int nrOfObservedStates = Integer.parseInt((String) commandLine.getValue(observedStateNumberOption)); double epsilon = Double.parseDouble((String) commandLine.getValue(epsilonOption)); int maxIterations = Integer.parseInt((String) commandLine.getValue(iterationsOption)); //constructing random-generated HMM HmmModel model = new HmmModel(nrOfHiddenStates, nrOfObservedStates, new Date().getTime()); List<Integer> observations = Lists.newArrayList(); //reading observations Scanner scanner = new Scanner(new FileInputStream(input), "UTF-8"); try { while (scanner.hasNextInt()) { observations.add(scanner.nextInt()); } } finally { scanner.close(); } int[] observationsArray = new int[observations.size()]; for (int i = 0; i < observations.size(); ++i) { observationsArray[i] = observations.get(i); } //training HmmModel trainedModel = HmmTrainer.trainBaumWelch(model, observationsArray, epsilon, maxIterations, true); //serializing trained model DataOutputStream stream = new DataOutputStream(new FileOutputStream(output)); try { LossyHmmSerializer.serialize(trainedModel, stream); } finally { Closeables.close(stream, false); } //printing tranied model System.out.println("Initial probabilities: "); for (int i = 0; i < trainedModel.getNrOfHiddenStates(); ++i) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < trainedModel.getNrOfHiddenStates(); ++i) { System.out.print(trainedModel.getInitialProbabilities().get(i) + " "); } System.out.println(); System.out.println("Transition matrix:"); System.out.print(" "); for (int i = 0; i < trainedModel.getNrOfHiddenStates(); ++i) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < trainedModel.getNrOfHiddenStates(); ++i) { System.out.print(i + " "); for (int j = 0; j < trainedModel.getNrOfHiddenStates(); ++j) { System.out.print(trainedModel.getTransitionMatrix().get(i, j) + " "); } System.out.println(); } System.out.println("Emission matrix: "); System.out.print(" "); for (int i = 0; i < trainedModel.getNrOfOutputStates(); ++i) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < trainedModel.getNrOfHiddenStates(); ++i) { System.out.print(i + " "); for (int j = 0; j < trainedModel.getNrOfOutputStates(); ++j) { System.out.print(trainedModel.getEmissionMatrix().get(i, j) + " "); } System.out.println(); } } catch (OptionException e) { CommandLineUtil.printHelp(optionGroup); } }
From source file:org.apache.mahout.classifier.sequencelearning.hmm.ViterbiEvaluator.java
public static void main(String[] args) throws IOException { DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder(); ArgumentBuilder argumentBuilder = new ArgumentBuilder(); Option inputOption = DefaultOptionCreator.inputOption().create(); Option outputOption = DefaultOptionCreator.outputOption().create(); Option modelOption = optionBuilder.withLongName("model").withDescription("Path to serialized HMM model") .withShortName("m") .withArgument(argumentBuilder.withMaximum(1).withMinimum(1).withName("path").create()) .withRequired(true).create(); Option likelihoodOption = optionBuilder.withLongName("likelihood") .withDescription("Compute likelihood of observed sequence").withShortName("l").withRequired(false) .create();/*from w w w. j a v a 2s .co m*/ Group optionGroup = new GroupBuilder().withOption(inputOption).withOption(outputOption) .withOption(modelOption).withOption(likelihoodOption).withName("Options").create(); try { Parser parser = new Parser(); parser.setGroup(optionGroup); CommandLine commandLine = parser.parse(args); String input = (String) commandLine.getValue(inputOption); String output = (String) commandLine.getValue(outputOption); String modelPath = (String) commandLine.getValue(modelOption); boolean computeLikelihood = commandLine.hasOption(likelihoodOption); //reading serialized HMM DataInputStream modelStream = new DataInputStream(new FileInputStream(modelPath)); HmmModel model; try { model = LossyHmmSerializer.deserialize(modelStream); } finally { Closeables.close(modelStream, true); } //reading observations List<Integer> observations = Lists.newArrayList(); Scanner scanner = new Scanner(new FileInputStream(input), "UTF-8"); try { while (scanner.hasNextInt()) { observations.add(scanner.nextInt()); } } finally { scanner.close(); } int[] observationsArray = new int[observations.size()]; for (int i = 0; i < observations.size(); ++i) { observationsArray[i] = observations.get(i); } //decoding int[] hiddenStates = HmmEvaluator.decode(model, observationsArray, true); //writing output PrintWriter writer = new PrintWriter( new OutputStreamWriter(new FileOutputStream(output), Charsets.UTF_8), true); try { for (int hiddenState : hiddenStates) { writer.print(hiddenState); writer.print(' '); } } finally { Closeables.close(writer, false); } if (computeLikelihood) { System.out.println("Likelihood: " + HmmEvaluator.modelLikelihood(model, observationsArray, true)); } } catch (OptionException e) { CommandLineUtil.printHelp(optionGroup); } }
From source file:org.apache.sysml.api.mlcontext.MLContextUtil.java
/** * Compare two version strings (ie, "1.4.0" and "1.4.1"). * /*from ww w .jav a2 s . co m*/ * @param versionStr1 * First version string. * @param versionStr2 * Second version string. * @return If versionStr1 is less than versionStr2, return {@code -1}. If * versionStr1 equals versionStr2, return {@code 0}. If versionStr1 * is greater than versionStr2, return {@code 1}. * @throws MLContextException * if versionStr1 or versionStr2 is {@code null} */ private static int compareVersion(String versionStr1, String versionStr2) { if (versionStr1 == null) { throw new MLContextException("First version argument to compareVersion() is null"); } if (versionStr2 == null) { throw new MLContextException("Second version argument to compareVersion() is null"); } Scanner scanner1 = null; Scanner scanner2 = null; try { scanner1 = new Scanner(versionStr1); scanner2 = new Scanner(versionStr2); scanner1.useDelimiter("\\."); scanner2.useDelimiter("\\."); while (scanner1.hasNextInt() && scanner2.hasNextInt()) { int version1 = scanner1.nextInt(); int version2 = scanner2.nextInt(); if (version1 < version2) { return -1; } else if (version1 > version2) { return 1; } } return scanner1.hasNextInt() ? 1 : 0; } finally { scanner1.close(); scanner2.close(); } }
From source file:org.igov.service.business.action.task.core.ActionTaskService.java
private void buildID_Protected(String sID_order, StringBuilder ID_Protected, int i) { String ch = "" + sID_order.charAt(i); Scanner scanner = new Scanner(ch); if (scanner.hasNextInt()) { ID_Protected.append(ch);//from w w w. j av a 2s. c o m } }
From source file:org.kiji.bento.tools.MiniClusterTool.java
/** * Reads a pid from a file.// ww w.j a v a 2s . co m * * @return The pid extracted from the file. * @throws IOException If the file couldn't be read. */ private int readPidFile() throws IOException { int pid; Scanner scanner = new Scanner(mPidFile, "UTF-8"); if (!scanner.hasNextInt()) { throw new IOException("Invalid pid file format."); } pid = scanner.nextInt(); scanner.close(); return pid; }
From source file:steamgameselector.SteamUtils.java
private void populateMap(Map map, String file) throws FileNotFoundException { map.clear();//from w w w . j av a 2s . c o m Scanner sc = new Scanner(new File(file)); while (sc.hasNextInt() && sc.hasNextLine()) { int id = sc.nextInt(); sc.nextLine(); if (sc.hasNextLine()) { map.put(id, sc.nextLine()); } } }
From source file:testing.test.java
private static void testIFSExtraction() { String[] coordIndexArray = null; String coordIndex = "0 1 2 3 -1"; String points = "-1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 1 -1 -1 -1 -1 1 -1 -1"; List totalPointParts = null;/*from ww w . j a v a 2 s .co m*/ coordIndex = coordIndex.replaceAll("\\r|\\n", " ").trim().replaceAll(" +", " ").replaceAll(",", ""); System.out.println(coordIndex); points = points.replaceAll("\\r|\\n", " ").trim().replaceAll(" +", " ").replaceAll(",", ""); System.out.println(points); Scanner sc = new Scanner(coordIndex).useDelimiter(" "); int maxCoordIndex = 0; while (sc.hasNextInt()) { int thisVal = sc.nextInt(); if (thisVal > maxCoordIndex) { maxCoordIndex = thisVal; } } System.out.println("maxCoordIndex: " + maxCoordIndex); totalPointParts = new ArrayList(); totalPointParts = getPointParts(points, maxCoordIndex); if (coordIndex.contains("-1")) { coordIndex = coordIndex.substring(0, coordIndex.lastIndexOf("-1")); coordIndexArray = coordIndex.split(" -1 "); } else { coordIndexArray = new String[1]; coordIndexArray[0] = coordIndex; } System.out.println("coordIndexArray"); System.out.println("coordIndexArray.length: " + coordIndexArray.length); for (int i = 0; i < coordIndexArray.length; i++) { System.out.println(i + ": " + coordIndexArray[i]); } System.out.println("totalPointsParts"); for (int j = 0; j < totalPointParts.size(); j++) { System.out.println(j + ": " + totalPointParts.get(j)); } }
From source file:uk.co.jwlawson.jcluster.demos.Mutator.java
/** * @param args/*from w ww . java 2s .c o m*/ */ public static void main(String[] args) { System.out.println("Mutator demo from jCluster Copyright (C) 2014 John Lawson"); System.out.println("This program comes with ABSOLUTELY NO WARRANTY. This is free"); System.out.println("software, and you are welcome to redistribute it under certain"); System.out.println("conditions."); if (Mutator.isValidArgs(args)) { Mutator mut = new Mutator(args); Scanner scan = new Scanner(System.in); System.out.println("Initial matrix:"); mut.printMatrix(); while (true) { System.out.print("Enter vertex to mutate at or q to quit: "); if (scan.hasNextInt()) { int k = scan.nextInt(); mut.mutate(k); mut.printMatrix(); System.out.println(); } else { break; } } scan.close(); } else { System.out.println("Usage:"); System.out.println(" Mutator <diagram>"); System.out.println(" Where diagram is the name of a Dynkin diagram"); System.out.println(); System.out.println(" e.g. Mutator A6"); } }