Example usage for org.apache.commons.io LineIterator hasNext

List of usage examples for org.apache.commons.io LineIterator hasNext

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator hasNext.

Prototype

public boolean hasNext() 

Source Link

Document

Indicates whether the Reader has more lines.

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.io.brat.internal.model.BratAnnotationDocument.java

public static BratAnnotationDocument read(Reader aReader) {
    BratAnnotationDocument doc = new BratAnnotationDocument();

    List<BratEventAnnotation> events = new ArrayList<>();

    // Read from file
    LineIterator lines = IOUtils.lineIterator(aReader);
    while (lines.hasNext()) {
        String line = lines.next();
        switch (line.charAt(0)) {
        case 'T':
            doc.addAnnotation(BratTextAnnotation.parse(line));
            break;
        case 'A':
        case 'M':
            doc.addAttribute(BratAttribute.parse(line));
            break;
        case 'R':
            doc.addAnnotation(BratRelationAnnotation.parse(line));
            break;
        case 'E': {
            BratEventAnnotation e = BratEventAnnotation.parse(line);
            events.add(e);/*from ww  w . jav  a  2 s .com*/
            doc.addAnnotation(e);
            break;
        }
        default:
            throw new IllegalStateException("Unknown annotation format: [" + line + "]");
        }
    }

    // Attach attributes to annotations
    for (BratAttribute attr : doc.attributes.values()) {
        BratAnnotation target = doc.annotations.get(attr.getTarget());

        if (target == null) {
            throw new IllegalStateException(
                    "Attribute refers to unknown annotation [" + attr.getTarget() + "]");
        }

        target.addAttribute(attr);
    }

    // FIXME this is currently inconsistent between reading and manual creation / writing
    // when we read the triggers, they no longer appear as individual annotations
    // when we manually create the brat annotations, we leave the triggers

    // Attach triggers to events and remove them as individual annotations
    List<String> triggersIds = new ArrayList<>();
    for (BratEventAnnotation event : events) {
        BratTextAnnotation trigger = (BratTextAnnotation) doc.annotations.get(event.getTrigger());

        if (trigger == null) {
            throw new IllegalStateException(
                    "Trigger refers to unknown annotation [" + event.getTrigger() + "]");
        }

        triggersIds.add(trigger.getId());
        event.setTriggerAnnotation(trigger);
    }

    // Remove trigger annotations, they are owned by the event
    doc.annotations.keySet().removeAll(triggersIds);

    return doc;
}

From source file:de.tudarmstadt.lt.nlkg.EvaluatePreds.java

static void evaluate(String file) throws IllegalArgumentException, FileNotFoundException {
    DT dt = new DT() {
        {/*from ww  w. j a v  a  2  s  .co m*/
            _mysql_dbname = "nlkg_1";
        }
    };
    LineIterator iter = new LineIterator(new FileReader(file));
    iter.nextLine(); // skip first line
    int lineno = 1;
    double tp = 0d, tn = 0d, fp = 0d, fn = 0d;
    while (iter.hasNext() && (lineno < 100 || true)) {
        lineno++;
        String line = iter.nextLine();
        if (line.trim().isEmpty())
            continue;

        String[] splits = line.split("\t");
        String x = splits[0].trim();
        String y = splits[1].trim();
        String pred_l = splits[2].trim();
        String pred_r = splits[3].trim();
        boolean entailing_trueclass = Boolean.valueOf(splits[4].trim());

        _X.add(x);
        _Y.add(y);
        _PRED_L.add(pred_l);
        _PRED_R.add(pred_r);
        _ENTAILING.add(entailing_trueclass);

        boolean entailing_predicted = predictEntailing(dt, pred_l, pred_r);
        if (lineno % 100 == 0)
            Evaluate.log_progress();

        if (entailing_predicted && entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tp", pred_l, pred_r,
                    entailing_trueclass));
            tp++;
        }
        if (!entailing_predicted && !entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tn", pred_l, pred_r,
                    entailing_trueclass));
            tn++;
        }
        if (entailing_predicted && !entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fp", pred_l, pred_r,
                    entailing_trueclass));
            fp++;
        }
        if (!entailing_predicted && entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fn", pred_l, pred_r,
                    entailing_trueclass));
            fn++;
        }

    }

    System.out.format("tp: %d; fp: %d; fn: %d; tn: %d; %n", (int) tp, (int) fp, (int) fn, (int) tn);
    System.out.println("Precision = " + (tp / (tp + fp)));
    System.out.println("Recall    = " + (tp / (tp + fn)));
    System.out.println("F1        = " + ((2 * tp) / ((2 * tp) + fn + fp)));

    dt.disconnect();

}

From source file:controllers.FileHandler.java

/**
* brief: read content of a given file to the proper list
* @param strFilePath    - path of given file          e.g. "D:\\EGYETEM\\Szakdolgozat\\Mernoki_tervezes\\update files\\sr28upd\\ADD_NUTR.txt"
* @param strFileType    - type of the given file      e.g. AddFood
* @param TList          - list to fill with data      e.g. List<FileFoodStruct> ListFFS
*///from   w w w  .  j av a 2s .co  m
public static <T> void readFile(String strFilePath, List<T> TList, String strFileType) {
    TList.clear();

    try {

        File file = FileUtils.getFile(strFilePath);
        LineIterator iter = FileUtils.lineIterator(file);

        while (iter.hasNext()) {
            String strLine = iter.next();

            switch (strFileType) {
            case "ADD_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(2), strList.get(3),
                        Integer.valueOf(strList.get(8)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        Double.valueOf(strList.get(2)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(2), strList.get(3),
                        Integer.valueOf(strList.get(8)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        Double.valueOf(strList.get(2)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "CHG_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_FOOD": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructFood Object = new FileStructFood(strList.get(0), strList.get(1));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_NUTR": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "DEL_WGT": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructWeight Object = new FileStructWeight(strList.get(0), Double.valueOf(strList.get(2)),
                        strList.get(3), Double.valueOf(strList.get(4)));
                TList.add((T) Object);
                //System.out.println(Object.toString());
                break;
            }
            case "ADD_NDEF": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        strList.get(2), strList.get(3), Integer.valueOf(strList.get(4)));
                TList.add((T) Object);
                System.out.println(strList.get(0) + " " + strList.get(1) + " " + strList.get(2) + " "
                        + strList.get(3) + " " + Integer.valueOf(strList.get(4)));
                break;
            }
            case "CHG_NDEF": {
                ArrayList<String> strList = ParseUpdateFile(strLine);
                FileStructNutrient Object = new FileStructNutrient(strList.get(0), strList.get(1),
                        strList.get(2), strList.get(3), Integer.valueOf(strList.get(4)));
                TList.add((T) Object);
                System.out.println(strList.get(0) + " " + strList.get(1) + " " + strList.get(2) + " "
                        + strList.get(3) + " " + Integer.valueOf(strList.get(4)));
                break;
            }
            case "LOG_FILE": {
                ArrayList<String> strList = ParseLogFile(strLine);
                TraceMessage Object = new TraceMessage(strList.get(0), strList.get(1));
                TList.add((T) Object);
                break;
            }
            }
        }
        iter.close();

    } catch (IOException ex) {
        Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        return;
    }
}

From source file:mitm.common.postfix.SaslPasswordManager.java

protected static List<SaslPassword> parseContent(String input) {
    List<SaslPassword> passwords = new LinkedList<SaslPassword>();

    if (StringUtils.isEmpty(input)) {
        return passwords;
    }/* w  w w.  j a  v  a 2s.co m*/

    LineIterator lineIterator = IOUtils.lineIterator(new StringReader(input));

    while (lineIterator.hasNext()) {
        String line = lineIterator.nextLine();

        if (!StringUtils.isBlank(line)) {
            Matcher m = SASL_PASSWORD_PATTERN.matcher(line);

            if (m.matches()) {
                boolean mxLookup = !("[".equals(m.group(1)) && "]".equals(m.group(3)));
                String server = StringUtils.trim(m.group(2));
                Integer port = NumberUtils.toInt(StringUtils.trim(m.group(4)), 0);
                String username = StringUtils.trim(m.group(5));
                String password = StringUtils.trim(m.group(6));

                SaslPassword saslPassword = new SaslPassword();

                saslPassword.setMxLookup(mxLookup);
                saslPassword.setServer(server);
                saslPassword.setPort(port != 0 ? port : null);
                saslPassword.setUsername(username);
                saslPassword.setPassword(password);

                passwords.add(saslPassword);
            }
        }
    }

    return passwords;
}

From source file:net.ostis.scpdev.debug.core.model.M4SCPUtils.java

/**
 * Iterate over line in source file to line with lineNumber
 * and find program or procedure declaration with less or equal line number.
 * @return program name if success else null.
 *//*from w  w  w .  j  a va  2 s.  co  m*/
public static String getProgramNameFromLine(final IFile file, final int lineNumber) throws CoreException {
    try {
        LineIterator iter = FileUtils.lineIterator(file.getLocation().toFile(), file.getCharset());
        String curProgram = null;
        int curLineNumber = 0;
        while (iter.hasNext() && curLineNumber < lineNumber) {
            ++curLineNumber;
            String line = iter.nextLine();

            // Find start of declaration program or procedure in line.
            Matcher startMatcher = programStartDeclrPattern.matcher(line);
            if (startMatcher.matches())
                curProgram = startMatcher.group(2);

            // Find end of declaration program or procedure in line.
            Matcher endMatcher = programEndDeclrPattern.matcher(line);
            if (endMatcher.matches())
                curProgram = null;
        }

        return curProgram;
    } catch (IOException e) {
        throw new CoreException(StatusUtils.makeStatus(IStatus.ERROR, "Error find m4scp program name", e));
    }
}

From source file:io.cloudslang.lang.tools.build.ArgumentProcessorUtils.java

public static Set<String> loadChangedItems(String filePath) throws IOException {
    Set<String> changedItems = new HashSet<>();
    try (InputStream fileInputStream = new FileInputStream(filePath)) {
        LineIterator lineIterator = IOUtils.lineIterator(fileInputStream, SlangSource.getCloudSlangCharset());
        while (lineIterator.hasNext()) {
            changedItems.add(lineIterator.next());
        }/* w  w  w. ja v a  2 s  .com*/
    }
    return changedItems;
}

From source file:com.comcast.cats.monitor.util.FileSearchUtil.java

public static Integer countHitsByRegex(String filePath, String expression) throws IOException {
    int hits = 0;

    if ((null == expression) || (expression.isEmpty()) || (null == filePath) || (filePath.isEmpty())) {
        throw new IllegalArgumentException("Expression/FilePath is NULL of EMPTY !!!");
    } else {/*from  w  w  w .  j a va2  s. c  o m*/
        File file = new File(filePath);
        Pattern pattern = Pattern.compile(expression);

        if (file.exists()) {
            LineIterator lineIterator = FileUtils.lineIterator(file, UTF_8_ENCODING);

            LOGGER.info("Expression under search = " + expression);

            try {
                while (lineIterator.hasNext()) {
                    String line = lineIterator.nextLine();

                    if (pattern.matcher(line).find()) {
                        hits++;
                    }
                }
            } finally {
                LineIterator.closeQuietly(lineIterator);
            }
        }
    }

    return hits;
}

From source file:de.tudarmstadt.lt.nlkg.EvaluateArgs.java

static void evaluate(String file) throws IllegalArgumentException, FileNotFoundException {
    DT dt = new DT() {
        {//from   w ww  . j  a  v  a  2s. c o  m
            _mysql_host = "localhost";
            _mysql_dbname = "nlkg_1";
        }
    };
    LineIterator iter = new LineIterator(new FileReader(file));
    iter.nextLine(); // skip first line
    int lineno = 1;
    double tp = 0d, tn = 0d, fp = 0d, fn = 0d;
    while (iter.hasNext() && (lineno < 100 || true)) {
        lineno++;
        String line = iter.nextLine();
        if (line.trim().isEmpty())
            continue;

        String[] splits = line.split("\t");
        //         String context = splits[0].trim();
        String arg_l = splits[0].trim();
        String arg_r = splits[1].trim();
        boolean entailing_trueclass = Boolean.valueOf(splits[2].trim());

        //         _CONTEXT.add(context);
        _ARG_L.add(arg_l);
        _ARG_R.add(arg_r);
        _ENTAILING.add(entailing_trueclass);

        boolean entailing_predicted = predictEntailing(dt, arg_l, arg_r);
        if (lineno % 100 == 0)
            Evaluate.log_progress();

        if (entailing_predicted && entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tp", arg_l, arg_r,
                    entailing_trueclass));
            tp++;
        }
        if (!entailing_predicted && !entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tn", arg_l, arg_r,
                    entailing_trueclass));
            tn++;
        }
        if (entailing_predicted && !entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fp", arg_l, arg_r,
                    entailing_trueclass));
            fp++;
        }
        if (!entailing_predicted && entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fn", arg_l, arg_r,
                    entailing_trueclass));
            fn++;
        }

    }

    dt.disconnect();

    System.out.format("%ntp: %d; fp: %d; fn: %d; tn: %d; %n", (int) tp, (int) fp, (int) fn, (int) tn);
    System.out.println("Precision = " + (tp / (tp + fp)));
    System.out.println("Recall    = " + (tp / (tp + fn)));
    System.out.println("F1        = " + ((2 * tp) / ((2 * tp) + fn + fp)));
}

From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java

/**
 * List of lines are generated from a given file. then this list is used to generate a NodeNamerImpl
 *
 * @param file//from w  ww.  j a v  a 2s  .  c o  m
 * @return INodeNamer
 * @throws IOException
 */
public static INodeNamer load(final File file) throws IOException {
    final int numNodes = countLines(file, Encoding.DEFAULT_CHARSET_NAME);

    final Int2ObjectOpenHashMap<String> id2Label = new Int2ObjectOpenHashMap<String>(numNodes);
    final Object2IntOpenHashMap<String> label2Id = new Object2IntOpenHashMap<String>(numNodes);

    final LineIterator it = FileUtils.lineIterator(file, Encoding.DEFAULT_CHARSET_NAME);
    try {
        int curId = 0;
        while (it.hasNext()) {
            final String nodeName = it.next();
            id2Label.put(curId, nodeName);
            label2Id.put(nodeName, curId);
            curId++;
        }

        return new NodeNamerImpl(id2Label, label2Id);
    } finally {
        it.close();
    }
}

From source file:edu.umn.msi.tropix.proteomics.test.VerifyUtils.java

public static void verifyDTAList(final DTAList dtaList) {
    for (final DTAList.Entry entry : dtaList) {
        final byte[] bytes = entry.getContents();
        final LineIterator lineIterator = IOUtils.lineIterator(new StringReader(new String(bytes)));
        final String firstLine = lineIterator.nextLine();
        assert firstLine.matches("^\\d+\\.\\d+\\s+\\d+\\s*$");
        while (lineIterator.hasNext()) {
            final String line = lineIterator.nextLine();
            assert line.matches(RegexUtils.FLOATING_POINT_LITERAL + "\\s+" + RegexUtils.FLOATING_POINT_LITERAL
                    + "\\s*") : "Line is not of form -- <double> <double>";
        }//from w w  w .  j av a 2s .c  o  m
        final String name = entry.getName();
        assert name.matches("^.*\\.\\d+\\.\\d+\\.\\d+\\.[dD][tT][aA]$");
    }
}