List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:ffx.utilities.BlockAverager.java
/** * Constructor grabs all histograms from the file and loads them into data * structures. TODO: figure out how to disregard histogram-bin combos that * aren't (currently) changing per time. * @param filename//from w w w .j a v a2 s .c o m * @param testMode * @param grepCmd * @param psPerHisto * @param blockSizeStep * @param maxBlockSize * @throws java.io.IOException */ public BlockAverager(String filename, boolean testMode, Optional<String> grepCmd, Optional<Double> psPerHisto, Optional<Integer> blockSizeStep, Optional<Integer> maxBlockSize) throws IOException { this.TEST = testMode; this.psPerHisto = (psPerHisto.isPresent()) ? psPerHisto.get() : 1.0; this.blockSizeStep = (blockSizeStep.isPresent()) ? blockSizeStep.get() : 100; int linesPerHistogram = (System.getProperty("ba-lph") == null) ? 201 : Integer.parseInt(System.getProperty("ba-lph")); if (TEST) { logger.info(" Testing Mode "); linesPerHistogram = 1; } File parallelInFile = new File(filename); int nThreads = ParallelTeam.getDefaultThreadCount(); parallelTeam = new ParallelTeam(nThreads); numThreads = parallelTeam.getThreadCount(); BlockRegion parallelBlock = new BlockRegion(parallelInFile); try { parallelTeam.execute(parallelBlock); } catch (Exception ex) { Logger.getLogger(BlockAverager.class.getName()).log(Level.SEVERE, null, ex); } // Step 1: Find histograms and create a stream. Scanner scan = null; File outFile = null; if (preGrep != null) { File file = new File(preGrep); BufferedReader br = new BufferedReader(new FileReader(file)); scan = new Scanner(br); } else { outFile = new File(filename + "-ba.tmp"); if (outFile.exists()) { logger.info(format(" Previous temp file exists: %s", outFile.getName())); if (!outFile.canWrite()) { logger.severe(format("Lacked write permissions to temp file.")); } System.out.print(format(" Delete it? (Y/N) ")); Scanner kb = new Scanner(System.in); if (kb.nextLine().toUpperCase().startsWith("Y")) { outFile.delete(); logger.info(""); } else { logger.severe("Aborted by user."); } } // Manually accomplish a 'grep -A 201 Bins filename'. File inFile = new File(filename); BufferedReader br = new BufferedReader(new FileReader(inFile)); scan = new Scanner(br); BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); logger.info(" Parsing logfile... "); int numFound = 0; while (scan.hasNextLine()) { String line = scan.nextLine(); if (TEST) { // No headers in test data. if (++numFound % 100 == 0) { logger.info(format(" Parsed %d histograms.", numFound)); } bw.write(line); bw.newLine(); continue; } if (line.contains("Lambda Bins")) { if (++numFound % 100 == 0) { logger.info(format(" Parsed %d histograms.", numFound)); } bw.write(line); bw.newLine(); for (int i = 0; i < linesPerHistogram; i++) { if (!scan.hasNextLine() && i < linesPerHistogram) { logger.warning(format("Found incomplete histogram: %d, %s", numFound, line)); } bw.write(scan.nextLine()); bw.newLine(); } } } bw.flush(); scan = new Scanner(outFile); } // Parse stream into data structures. List<Bin> binList = new ArrayList<>(); Histogram histo = null; while (scan.hasNextLine()) { String line = scan.nextLine(); String[] tokens = line.split("\\s+"); // Catch grep flotsam. if (tokens[0].startsWith("--")) { continue; } // Header line signals time for a new histogram. if (line.contains("Lambda Bins") || TEST) { if (histo != null) { histoList.add(histo); } histo = new Histogram(++histoIndexer); if (histoIndexer % 100 == 0) { if (psPerHisto.isPresent()) { logger.info(format(" BlockAverager loaded %d histograms (simTime %.2f ps).", histoIndexer, histoIndexer * this.psPerHisto)); } else { logger.info(format(" BlockAverager loaded %d histograms.", histoIndexer)); } } if (TEST) { // No headers in test data. histo.bins.add(new Bin(tokens)); } continue; } histo.bins.add(new Bin(tokens)); } histoList.add(histo); Collections.sort(histoList); logger.info(format("")); numObs = histoList.size(); this.maxBlockSize = (maxBlockSize.isPresent()) ? maxBlockSize.get() : numObs; // Validate for (int i = 1; i < histoList.size(); i++) { if (histoList.get(i).index != histoList.get(i - 1).index + 1 || histoList.get(i).bins.size() != histoList.get(i - 1).bins.size()) { logger.warning(format("Improper indexing or bin size mismatch. i,i-1,binsi,binsi-1: %d %d %d %d", histoList.get(i).index, histoList.get(i - 1).index, histoList.get(i).bins.size(), histoList.get(i - 1).bins.size())); throw new ArithmeticException(); } } if (outFile != null && outFile.exists()) { outFile.delete(); } numBins = histoList.get(0).bins.size(); this.describe(); }
From source file:Creator.WidgetPanel.java
public void readWidgetVars() { groupNamesWidget = new HashMap<>(); String path = "/Creator/textFiles/Default Widget Names.txt"; InputStream loc = this.getClass().getResourceAsStream(path); Scanner scan = new Scanner(loc); String line, groupName = ""; while (scan.hasNextLine()) { line = scan.nextLine();// ww w . ja va 2 s .c o m if (line == null) { // make sure it doesnt break } else if (line.startsWith("`")) { groupName = line.substring(1); if (!groupNamesWidget.containsKey(groupName)) { groupNamesWidget.put(groupName, new ArrayList<>()); } //System.out.println("Grouping name: " + groupName); } else { groupNamesWidget.get(groupName).add(line); } } scan.close(); loadWidgetVarsComboBox(); }
From source file:GUI.PizzaCat.java
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked // TODO add your handling code here: String inp = ""; String path;// w w w . j ava2 s .c o m path = jTextPane1.getText(); //Change to uploaded file & rip Python code for regex + extraction of IOCs before TR File file = new File(path); System.out.println("Processing file" + path); try { Scanner scan = new Scanner(file); ArrayList<String> thelist = new ArrayList<String>(); int countz = 0; while (scan.hasNextLine()) { inp = scan.nextLine(); Pattern pattern2; //PATTERN BELOW IS TEH WINZ FOR OUR DATA USE String domainPattern = "^?([a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+.*)$"; pattern2 = Pattern.compile(domainPattern, Pattern.CASE_INSENSITIVE); //System.out.println("Processing Domain Pattern(s)..."); Matcher match2 = pattern2.matcher(inp); while (match2.find()) { String wordz = match2.group(); if (wordz.indexOf(' ') > 0) { String[] result = wordz.split(" ", 2); String first = result[0]; wordz = first; String rest = result[1]; //System.out.println("First: " + first); //System.out.println("Rest: " + rest); } if (countz <= 99) { jTable1.setValueAt(wordz, countz, 0); } System.out.println("Indicator found: " + countz + " " + wordz); thelist.add(wordz); countz++; } } label7.setText(" Numebr of Queries : " + countz); //THIS IS WHERE TO UPDATE TABLE String[] arrayz = thelist.toArray(new String[thelist.size()]); //System.out.println(Arrays.toString(arrayz)); //IGNORE NEXT WARNING AND DON'T REMOVE EXCEPTION } catch (FileNotFoundException f) { f.printStackTrace(); } //System.out.println("FIN"); }
From source file:com.joliciel.talismane.TalismaneConfig.java
/** * TokenFilters to be applied during analysis. * @return//from ww w . j av a 2 s . c om */ public List<PosTagSequenceFilter> getDefaultPosTagSequenceFilters() { try { if (posTaggerPostProcessingFilters == null) { List<String> posTaggerPostProcessingFilterDescriptors = new ArrayList<String>(); posTaggerPostProcessingFilters = new ArrayList<PosTagSequenceFilter>(); if (posTagSequenceFilterPath != null) { File filterFile = new File(posTagSequenceFilterPath); Scanner scanner = new Scanner(filterFile); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); LOG.debug(descriptor); posTaggerPostProcessingFilterDescriptors.add(descriptor); if (descriptor.length() > 0 && !descriptor.startsWith("#")) { PosTagSequenceFilter filter = this.getPosTagFilterService() .getPosTagSequenceFilter(descriptor); posTaggerPostProcessingFilters.add(filter); } } } this.getDescriptors().put(PosTagFilterService.POSTAG_POSTPROCESSING_FILTER_DESCRIPTOR_KEY, posTaggerPostProcessingFilterDescriptors); } return posTaggerPostProcessingFilters; } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:com.joliciel.talismane.TalismaneConfig.java
/** * TokenFilters to be applied during analysis. * @return/*from w w w .j av a 2 s .c o m*/ */ public List<TokenSequenceFilter> getTokenSequenceFilters() { try { if (tokenSequenceFilters == null) { List<String> tokenSequenceFilterDescriptors = new ArrayList<String>(); tokenSequenceFilters = new ArrayList<TokenSequenceFilter>(); if (tokenSequenceFilterPath != null) { File tokenSequenceFilterFile = new File(tokenSequenceFilterPath); Scanner scanner = new Scanner(tokenSequenceFilterFile); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); LOG.debug(descriptor); tokenSequenceFilterDescriptors.add(descriptor); if (descriptor.length() > 0 && !descriptor.startsWith("#")) { TokenSequenceFilter tokenSequenceFilter = this.getTokenFilterService() .getTokenSequenceFilter(descriptor); tokenSequenceFilters.add(tokenSequenceFilter); } } } tokenSequenceFilters.addAll(this.implementation.getDefaultTokenSequenceFilters()); this.getDescriptors().put(TokenFilterService.TOKEN_SEQUENCE_FILTER_DESCRIPTOR_KEY, tokenSequenceFilterDescriptors); } return tokenSequenceFilters; } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:com.joliciel.talismane.TalismaneConfig.java
/** * A regex used to process the input, when pre-annotated. * @return/* w w w . j ava 2 s .c om*/ */ public String getInputRegex() { try { if (inputRegex == null && inputPatternFilePath != null && inputPatternFilePath.length() > 0) { Scanner inputPatternScanner = null; File inputPatternFile = new File(inputPatternFilePath); inputPatternScanner = new Scanner(inputPatternFile); if (inputPatternScanner.hasNextLine()) { inputRegex = inputPatternScanner.nextLine(); } inputPatternScanner.close(); if (inputRegex == null) throw new TalismaneException("No input pattern found in " + inputPatternFilePath); } return inputRegex; } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:com.joliciel.talismane.TalismaneConfig.java
/** * A regex used to process the evaluation corpus. * @return/*from www . j ava 2 s . c o m*/ */ public String getEvaluationRegex() { try { if (evaluationRegex == null) { if (evaluationPatternFilePath != null && evaluationPatternFilePath.length() > 0) { Scanner evaluationPatternScanner = null; File evaluationPatternFile = new File(evaluationPatternFilePath); evaluationPatternScanner = new Scanner(evaluationPatternFile); if (evaluationPatternScanner.hasNextLine()) { evaluationRegex = evaluationPatternScanner.nextLine(); } evaluationPatternScanner.close(); if (evaluationRegex == null) throw new TalismaneException("No evaluation pattern found in " + evaluationPatternFilePath); } else { evaluationRegex = this.getInputRegex(); } } return evaluationRegex; } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:com.joliciel.talismane.TalismaneConfig.java
private TokeniserPatternManager getTokeniserPatternManager() { if (tokeniserPatternManager == null) { if (tokeniserPatternFilePath.length() == 0) throw new RuntimeException("Missing argument: tokeniserPatterns"); try {/*from w w w . j a va 2 s . c o m*/ File tokeniserPatternFile = new File(tokeniserPatternFilePath); Scanner scanner = new Scanner(new BufferedReader( new InputStreamReader(new FileInputStream(tokeniserPatternFile), this.getInputCharset()))); List<String> patternDescriptors = new ArrayList<String>(); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); patternDescriptors.add(descriptor); LOG.debug(descriptor); } scanner.close(); tokeniserPatternManager = this.getTokeniserPatternService().getPatternManager(patternDescriptors); } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } } return tokeniserPatternManager; }
From source file:com.joliciel.talismane.TalismaneConfig.java
public Set<ParseConfigurationFeature<?>> getParserFeatures() { if (parserFeatures == null) { try {/*from w ww.j a va 2 s. c om*/ if (parserFeaturePath != null) { LOG.debug("Found setting to change parser features"); File parserFeatureFile = new File(parserFeaturePath); Scanner scanner = new Scanner(new BufferedReader( new InputStreamReader(new FileInputStream(parserFeatureFile), this.getInputCharset()))); List<String> featureDescriptors = new ArrayList<String>(); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); featureDescriptors.add(descriptor); LOG.debug(descriptor); } parserFeatures = this.getParserFeatureService().getFeatures(featureDescriptors); this.getDescriptors().put(MachineLearningModel.FEATURE_DESCRIPTOR_KEY, featureDescriptors); } } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } } return parserFeatures; }
From source file:com.joliciel.talismane.TalismaneConfig.java
public Set<SentenceDetectorFeature<?>> getSentenceDetectorFeatures() { if (sentenceFeatures == null) { try {// w w w.ja va 2s .c o m if (sentenceFeaturePath != null) { LOG.debug("Found setting to change sentence detector features"); File sentenceFeatureFile = new File(sentenceFeaturePath); Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader( new FileInputStream(sentenceFeatureFile), this.getInputCharset()))); List<String> featureDescriptors = new ArrayList<String>(); while (scanner.hasNextLine()) { String descriptor = scanner.nextLine(); featureDescriptors.add(descriptor); LOG.debug(descriptor); } sentenceFeatures = this.getSentenceDetectorFeatureService().getFeatureSet(featureDescriptors); this.getDescriptors().put(MachineLearningModel.FEATURE_DESCRIPTOR_KEY, featureDescriptors); } } catch (Exception e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } } return sentenceFeatures; }