List of usage examples for java.io LineNumberReader getLineNumber
public int getLineNumber()
From source file:org.wso2.carbon.ml.rest.api.neuralNetworks.FeedForwardNetwork.java
/** * method to createFeedForwardNetwork.// w ww.ja v a 2 s . co m * @param seed * @param learningRate * @param analysisID * @param bachSize * @param backprop * @param hiddenList * @param inputLayerNodes * @param iterations * @param versionID * @param momentum * @param nepoches * @param datasetId * @param noHiddenLayers * @param optimizationAlgorithms * @param outputList * @param pretrain * @param updater * @return an String object with evaluation result. */ public String createFeedForwardNetwork(long seed, double learningRate, int bachSize, double nepoches, int iterations, String optimizationAlgorithms, String updater, double momentum, boolean pretrain, boolean backprop, int noHiddenLayers, int inputLayerNodes, int datasetId, int versionID, int analysisID, List<HiddenLayerDetails> hiddenList, List<OutputLayerDetails> outputList) throws IOException, InterruptedException { String evaluationDetails = null; int numLinesToSkip = 0; String delimiter = ","; mlDataSet = getDatasetPath(datasetId, versionID); analysisFraction = getAnalysisFraction(analysisID); analysisResponceVariable = getAnalysisResponseVariable(analysisID); responseIndex = getAnalysisResponseVariableIndex(analysisID); SplitTestAndTrain splitTestAndTrain; DataSet currentDataset; DataSet trainingSet = null; DataSet testingSet = null; INDArray features = null; INDArray labels = null; INDArray predicted = null; Random rnd = new Random(); int labelIndex = 0; int numClasses = 0; int fraction = 0; //Initialize RecordReader RecordReader rr = new CSVRecordReader(numLinesToSkip, delimiter); //read the dataset rr.initialize(new FileSplit(new File(mlDataSet))); labelIndex = responseIndex; numClasses = outputList.get(0).outputNodes; //Get the fraction to do the spliting data to training and testing FileReader fr = new FileReader(mlDataSet); LineNumberReader lineNumberReader = new LineNumberReader(fr); //Get the total number of lines lineNumberReader.skip(Long.MAX_VALUE); int lines = lineNumberReader.getLineNumber(); //handling multiplication of 0 error if (analysisFraction == 0) { return null; } //Take floor value to set the numHold of training data fraction = ((int) Math.floor(lines * analysisFraction)); org.nd4j.linalg.dataset.api.iterator.DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, lines, labelIndex, numClasses); //Create NeuralNetConfiguration object having basic settings. NeuralNetConfiguration.ListBuilder neuralNetConfiguration = new NeuralNetConfiguration.Builder().seed(seed) .iterations(iterations).optimizationAlgo(mapOptimizationAlgorithm(optimizationAlgorithms)) .learningRate(learningRate).updater(mapUpdater(updater)).momentum(momentum) .list(noHiddenLayers + 1); //Add Hidden Layers to the network with unique settings for (int i = 0; i < noHiddenLayers; i++) { int nInput = 0; if (i == 0) nInput = inputLayerNodes; else nInput = hiddenList.get(i - 1).hiddenNodes; neuralNetConfiguration.layer(i, new DenseLayer.Builder().nIn(nInput).nOut(hiddenList.get(i).hiddenNodes) .weightInit(mapWeightInit(hiddenList.get(i).weightInit)) .activation(hiddenList.get(i).activationAlgo).build()); } //Add Output Layers to the network with unique settings neuralNetConfiguration.layer(noHiddenLayers, new OutputLayer.Builder(mapLossFunction(outputList.get(0).lossFunction)) .nIn(hiddenList.get(noHiddenLayers - 1).hiddenNodes).nOut(outputList.get(0).outputNodes) .weightInit(mapWeightInit(outputList.get(0).weightInit)) .activation(outputList.get(0).activationAlgo).build()); //Create MultiLayerConfiguration network MultiLayerConfiguration conf = neuralNetConfiguration.pretrain(pretrain).backprop(backprop).build(); MultiLayerNetwork model = new MultiLayerNetwork(conf); model.init(); model.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1))); while (trainIter.hasNext()) { currentDataset = trainIter.next(); splitTestAndTrain = currentDataset.splitTestAndTrain(fraction, rnd); trainingSet = splitTestAndTrain.getTrain(); testingSet = splitTestAndTrain.getTest(); features = testingSet.getFeatureMatrix(); labels = testingSet.getLabels(); } //Train the model with the training data for (int n = 0; n < nepoches; n++) { model.fit(trainingSet); } //Do the evaluations of the model including the Accuracy, F1 score etc. log.info("Evaluate model...."); Evaluation eval = new Evaluation(outputList.get(0).outputNodes); predicted = model.output(features, false); eval.eval(labels, predicted); evaluationDetails = "{\"Accuracy\":\"" + eval.accuracy() + "\", \"Pecision\":\"" + eval.precision() + "\",\"Recall\":\"" + eval.recall() + "\",\"F1Score\":\"" + eval.f1() + "\"}"; return evaluationDetails; }
From source file:com.saptarshidebnath.lib.processrunner.process.RunnerFactoryTest.java
private int getFileLineNumber(final File fileToCountLineNumber) throws IOException { LineNumberReader lnr = null; int lineNumber; try {/*from w w w. ja v a 2 s . c o m*/ lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(fileToCountLineNumber))); long skipValue = Long.MAX_VALUE; while (skipValue != 0) { skipValue = lnr.skip(Long.MAX_VALUE); } lineNumber = lnr.getLineNumber() + 1; // Add 1 because line index starts at 0 } finally { if (lnr != null) { lnr.close(); } } return lineNumber; }
From source file:org.gofleet.module.routing.RoutingMap.java
private int getNumberLines(File f) { LineNumberReader lineCounter; try {/*from ww w . j a v a 2 s . c o m*/ lineCounter = new LineNumberReader(new InputStreamReader(new FileInputStream(f.getPath()))); while (lineCounter.readLine() != null) ; return lineCounter.getLineNumber(); } catch (Exception done) { log.error(done, done); return -1; } }
From source file:org.apache.cocoon.generation.TextGenerator.java
/** * Generate XML data./*from w w w . j a v a 2 s . co m*/ * * @throws IOException * @throws ProcessingException * @throws SAXException */ public void generate() throws IOException, SAXException, ProcessingException { InputStreamReader in = null; try { final InputStream sis = this.inputSource.getInputStream(); if (sis == null) { throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); } if (encoding != null) { in = new InputStreamReader(sis, encoding); } else { in = new InputStreamReader(sis); } } catch (SourceException se) { throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); } LocatorImpl locator = new LocatorImpl(); locator.setSystemId(this.inputSource.getURI()); locator.setLineNumber(1); locator.setColumnNumber(1); contentHandler.setDocumentLocator(locator); contentHandler.startDocument(); contentHandler.startPrefixMapping("", URI); AttributesImpl atts = new AttributesImpl(); if (localizable) { atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId()); atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber())); atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber())); } contentHandler.startElement(URI, "text", "text", atts); LineNumberReader reader = new LineNumberReader(in); String line; String newline = null; while (true) { if (newline == null) { line = convertNonXmlChars(reader.readLine()); } else { line = newline; } if (line == null) { break; } newline = convertNonXmlChars(reader.readLine()); if (newline != null) { line += SystemUtils.LINE_SEPARATOR; } locator.setLineNumber(reader.getLineNumber()); locator.setColumnNumber(1); contentHandler.characters(line.toCharArray(), 0, line.length()); if (newline == null) { break; } } reader.close(); contentHandler.endElement(URI, "text", "text"); contentHandler.endPrefixMapping(""); contentHandler.endDocument(); }
From source file:org.apache.cocoon.generation.TextGenerator2.java
/** * Generate XML data./* ww w .ja va2s . c o m*/ * * @throws IOException * @throws ProcessingException * @throws SAXException */ public void generate() throws IOException, SAXException, ProcessingException { InputStreamReader in = null; try { final InputStream sis = this.inputSource.getInputStream(); if (sis == null) { throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found"); } if (encoding != null) { in = new InputStreamReader(sis, encoding); } else { in = new InputStreamReader(sis); } } catch (SourceException se) { throw new ProcessingException("Error during resolving of '" + this.source + "'.", se); } LocatorImpl locator = new LocatorImpl(); locator.setSystemId(this.inputSource.getURI()); locator.setLineNumber(1); locator.setColumnNumber(1); /* Do not pass the source URI to the contentHandler, assuming that that is the LexicalTransformer. It does not have to be. contentHandler.setDocumentLocator(locator); */ contentHandler.startDocument(); AttributesImpl atts = new AttributesImpl(); if (localizable) { atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId()); atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber())); atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber())); } String nsPrefix = this.element.contains(":") ? this.element.replaceFirst(":.+$", "") : ""; String localName = this.element.replaceFirst("^.+:", ""); if (this.namespace.length() > 1) contentHandler.startPrefixMapping(nsPrefix, this.namespace); contentHandler.startElement(this.namespace, localName, this.element, atts); LineNumberReader reader = new LineNumberReader(in); String line; String newline = null; while (true) { if (newline == null) { line = convertNonXmlChars(reader.readLine()); } else { line = newline; } if (line == null) { break; } newline = convertNonXmlChars(reader.readLine()); if (newline != null) { line += SystemUtils.LINE_SEPARATOR; } locator.setLineNumber(reader.getLineNumber()); locator.setColumnNumber(1); contentHandler.characters(line.toCharArray(), 0, line.length()); if (newline == null) { break; } } reader.close(); contentHandler.endElement(this.namespace, localName, this.element); if (this.namespace.length() > 1) contentHandler.endPrefixMapping(nsPrefix); contentHandler.endDocument(); }
From source file:stroom.util.StreamGrepTool.java
private void processFile(final StreamStore streamStore, final long streamId, final String match) { try {//www. ja va 2s . com final StreamSource streamSource = streamStore.openStreamSource(streamId); if (streamSource != null) { final InputStream inputStream = streamSource.getInputStream(); // Build up 2 buffers so we can output the content either side // of // the matching line final ArrayDeque<String> preBuffer = new ArrayDeque<>(); final ArrayDeque<String> postBuffer = new ArrayDeque<>(); final LineNumberReader lineNumberReader = new LineNumberReader( new InputStreamReader(inputStream, StreamUtil.DEFAULT_CHARSET)); String aline = null; while ((aline = lineNumberReader.readLine()) != null) { String lines[] = new String[] { aline }; if (addLineBreak != null) { lines = aline.split(addLineBreak); } for (final String line : lines) { if (match == null) { System.out.println(lineNumberReader.getLineNumber() + ":" + line); } else { postBuffer.add(lineNumberReader.getLineNumber() + ":" + line); if (postBuffer.size() > 5) { final String searchLine = postBuffer.pop(); checkMatch(match, preBuffer, postBuffer, searchLine); preBuffer.add(searchLine); if (preBuffer.size() > 5) { preBuffer.pop(); } } } } } // Look at the end while (postBuffer.size() > 0) { final String searchLine = postBuffer.pop(); checkMatch(match, preBuffer, postBuffer, searchLine); preBuffer.add(searchLine); if (preBuffer.size() > 5) { preBuffer.pop(); } } inputStream.close(); streamStore.closeStreamSource(streamSource); } } catch (final Exception ex) { ex.printStackTrace(); } }
From source file:com.jkoolcloud.tnt4j.streams.configure.state.FileStreamStateHandlerTest.java
@Test public void findStreamingFile() throws Exception { FileStreamStateHandler rwd = new FileStreamStateHandler(); File testFilesDir = new File(samplesDir, "/multiple-logs/"); File[] testFiles = testFilesDir.listFiles((FilenameFilter) new WildcardFileFilter("orders*")); // NON-NLS FileAccessState newFAS = new FileAccessState(); int count = 0; File fileToSearchFor = null;/*from w ww . java 2 s . com*/ int lineLastRead = 0; File fileWritten = null; for (File testFile : testFiles) { count++; FileReader in; LineNumberReader reader; Long fileCRC = rwd.getFileCrc(testFile); if (count == 2) { newFAS.currentFileCrc = fileCRC; fileToSearchFor = testFile; } in = new FileReader(testFile); reader = new LineNumberReader(in); reader.setLineNumber(0); String line = reader.readLine(); int count2 = 0; while (line != null) { count2++; Checksum crcLine = new CRC32(); final byte[] bytes4Line = line.getBytes(); crcLine.update(bytes4Line, 0, bytes4Line.length); final long lineCRC = crcLine.getValue(); final int lineNumber = reader.getLineNumber(); System.out.println("for " + lineNumber + " line CRC is " + lineCRC); // NON-NLS if (count2 == 3) { newFAS.currentLineCrc = lineCRC; newFAS.currentLineNumber = lineNumber; newFAS.lastReadTime = System.currentTimeMillis(); lineLastRead = lineNumber; } line = reader.readLine(); } fileWritten = AbstractFileStreamStateHandler.writeState(newFAS, testFilesDir, "TestStream"); // NON-NLS Utils.close(reader); } final File findLastProcessed = rwd.findStreamingFile(newFAS, testFiles); assertEquals(fileToSearchFor, findLastProcessed); final int lineLastReadRecorded = rwd.checkLine(findLastProcessed, newFAS); assertEquals(lineLastRead, lineLastReadRecorded); fileWritten.delete(); }
From source file:core.Inject.java
private String[] loadPayloadsFromfile(String pathTo_payloads) { String[] _payloads = null;// ww w . jav a 2s . co m try { FileInputStream fstream = new FileInputStream(pathTo_payloads); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; int i = 0; LineNumberReader lnr = new LineNumberReader(new FileReader(pathTo_payloads)); lnr.skip(Long.MAX_VALUE); _payloads = new String[lnr.getLineNumber()]; while ((strLine = br.readLine()) != null) { _payloads[i] = strLine; i++; } in.close(); } catch (Exception e) { Debug.printError("ERROR: unable to load the attack payloads"); HaltHandler.quit_nok(); } return _payloads; }
From source file:net.flamefeed.ftb.modpackupdater.FileOperator.java
/** * This method will download the hash-file from the server and * will extract data from it and populate the remoteHashes 2d array. * * @throws java.lang.IOException//from w w w . j av a2 s . co m */ private void parseHashfile() throws IOException { int numRemoteFiles; String[] currentLine; FileReader fr; BufferedReader br; LineNumberReader lnr; downloadFile(HASHFILE_NAME); /* * Compute the number of lines in the hash-file. This corresponds to the * number of files on the remote server, and therefore the length of the * hashFiles outer array. */ fr = new FileReader(pathMinecraft + "/" + HASHFILE_NAME); lnr = new LineNumberReader(fr); lnr.skip(Long.MAX_VALUE); numRemoteFiles = lnr.getLineNumber(); fr.close(); /* * Populate the remoteHashes[][] array with data from the hash file */ remoteHashes = new String[numRemoteFiles][2]; fr = new FileReader(pathMinecraft + "/" + HASHFILE_NAME); br = new BufferedReader(fr); /* * The hash-file format is as follows: * <MD5 hash><tab character><relative filename><newline character> * Splitting each line using a tab character "\t" separates the filename * from the MD5 hash. */ for (int i = 0; i < numRemoteFiles; i++) { currentLine = br.readLine().split("\t", 2); remoteHashes[i][MD5HASH] = currentLine[MD5HASH]; remoteHashes[i][FILENAME] = currentLine[FILENAME]; } fr.close(); }
From source file:org.kalypso.kalypsomodel1d2d.sim.IterationInfo.java
@Override public void readIterFile() throws IOException { m_itrFile.refresh();/*from w w w .ja va2s. c o m*/ if (!m_itrFile.exists()) return; /* Read file and write outputs */ LineNumberReader lnr = null; try { // final InputStream inputStream = m_itrFile.getContent().getInputStream(); final byte[] content = FileUtil.getContent(m_itrFile); // lnr = new LineNumberReader( new BufferedReader( new InputStreamReader( inputStream ) ) ); lnr = new LineNumberReader(new StringReader(new String(content, Charset.defaultCharset()))); while (lnr.ready()) { final String line = lnr.readLine(); if (line == null) break; processLine(line, lnr.getLineNumber()); } } catch (final FileNotFoundException e) { // FIXME: stati are never used; what happened here?! // if( lnr == null ) // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.1" ), e ); //$NON-NLS-1$ // // final String msg = Messages.getString( "org.kalypso.kalypsomodel1d2d.sim.IterationInfo.2", lnr.getLineNumber() ); //$NON-NLS-1$ // StatusUtilities.createStatus( IStatus.WARNING, ISimulation1D2DConstants.CODE_RMA10S, msg, e ); } finally { IOUtils.closeQuietly(lnr); } }