Example usage for java.io BufferedReader readLine

List of usage examples for java.io BufferedReader readLine

Introduction

In this page you can find the example usage for java.io BufferedReader readLine.

Prototype

public String readLine() throws IOException 

Source Link

Document

Reads a line of text.

Usage

From source file:StringTaggerDemo.java

public static void main(String args[]) {
    try {//  w ww . j a  v a  2s.  c  o  m
        System.setProperty("sen.home", "../Sen1221/senhome-ipadic");
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "FATAL");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please input Japanese sentence:");

        StringTagger tagger = StringTagger.getInstance();
        // You can also get StringTagger instance by following code:
        //
        // String confPath = System.getProperty("sen.home")
        // + System.getProperty("file.separator") + "conf/sen.xml";
        // tagger = StringTagger.getInstance(confPath);

        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
            Token[] token = tagger.analyze(s);
            if (token != null) {
                for (int i = 0; i < token.length; i++) {
                    System.out.println(token[i].toString() + "\t(" + token[i].getBasicString() + ")" + "\t"
                            + token[i].getPos() + "(" + token[i].start() + "," + token[i].end() + ","
                            + token[i].length() + ")\t" + token[i].getReading() + "\t"
                            + token[i].getPronunciation());
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:nab.detectors.htmjava.HTMModel.java

/**
 * Launch htm.java NAB detector/*from w w  w .j  a va2 s.co m*/
 *
 * Usage:
 *      As a standalone application (for debug purpose only):
 *
 *          java -jar htm.java-nab.jar "{\"modelParams\":{....}}" < nab_data.csv > anomalies.out
 *
 *      For complete list of command line options use:
 *
 *          java -jar htm.java-nab.jar --help
 *
 *      As a NAB detector (see 'htmjava_detector.py'):
 *
 *          python run.py --detect --score --normalize -d htmjava
 *
 *      Logging options, see "log4j.properties":
 *
 *          - "LOGLEVEL": Controls log output (default: "OFF")
 *          - "LOGGER": Either "CONSOLE" or "FILE" (default: "CONSOLE")
 *          - "LOGFILE": Log file destination (default: "htmjava.log")
 *
 *      For example:
 *
 *          java -DLOGLEVEL=TRACE -DLOGGER=FILE -jar htm.java-nab.jar "{\"modelParams\":{....}}" < nab_data.csv > anomalies.out
 *
 */
@SuppressWarnings("resource")
public static void main(String[] args) {
    try {
        LOGGER.trace("main({})", Arrays.asList(args));
        // Parse command line args
        OptionParser parser = new OptionParser();
        parser.nonOptions("OPF parameters object (JSON)");
        parser.acceptsAll(Arrays.asList("p", "params"),
                "OPF parameters file (JSON).\n(default: first non-option argument)").withOptionalArg()
                .ofType(File.class);
        parser.acceptsAll(Arrays.asList("i", "input"), "Input data file (csv).\n(default: stdin)")
                .withOptionalArg().ofType(File.class);
        parser.acceptsAll(Arrays.asList("o", "output"), "Output results file (csv).\n(default: stdout)")
                .withOptionalArg().ofType(File.class);
        parser.acceptsAll(Arrays.asList("s", "skip"), "Header lines to skip").withOptionalArg()
                .ofType(Integer.class).defaultsTo(0);
        parser.acceptsAll(Arrays.asList("h", "?", "help"), "Help");
        OptionSet options = parser.parse(args);
        if (args.length == 0 || options.has("h")) {
            parser.printHelpOn(System.out);
            return;
        }

        // Get in/out files
        final PrintStream output;
        final InputStream input;
        if (options.has("i")) {
            input = new FileInputStream((File) options.valueOf("i"));
        } else {
            input = System.in;
        }
        if (options.has("o")) {
            output = new PrintStream((File) options.valueOf("o"));
        } else {
            output = System.out;
        }

        // Parse OPF Model Parameters
        JsonNode params;
        ObjectMapper mapper = new ObjectMapper();
        if (options.has("p")) {
            params = mapper.readTree((File) options.valueOf("p"));
        } else if (options.nonOptionArguments().isEmpty()) {
            try {
                input.close();
            } catch (Exception ignore) {
            }
            if (options.has("o")) {
                try {
                    output.flush();
                    output.close();
                } catch (Exception ignore) {
                }
            }
            throw new IllegalArgumentException("Expecting OPF parameters. See 'help' for more information");
        } else {
            params = mapper.readTree((String) options.nonOptionArguments().get(0));
        }

        // Number of header lines to skip
        int skip = (int) options.valueOf("s");

        // Force timezone to UTC
        DateTimeZone.setDefault(DateTimeZone.UTC);

        // Create NAB Network Model
        HTMModel model = new HTMModel(params);
        Network network = model.getNetwork();
        network.observe().subscribe((inference) -> {
            double score = inference.getAnomalyScore();
            int record = inference.getRecordNum();
            LOGGER.trace("record = {}, score = {}", record, score);
            // Output raw anomaly score
            output.println(score);
        }, (error) -> {
            LOGGER.error("Error processing data", error);
        }, () -> {
            LOGGER.trace("Done processing data");
            if (LOGGER.isDebugEnabled()) {
                model.showDebugInfo();
            }
        });
        network.start();

        // Pipe data to network
        Publisher publisher = model.getPublisher();
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
        String line;
        while ((line = in.readLine()) != null && line.trim().length() > 0) {
            // Skip header lines
            if (skip > 0) {
                skip--;
                continue;
            }
            publisher.onNext(line);
        }
        publisher.onComplete();
        in.close();
        LOGGER.trace("Done publishing data");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:markov.java

/**
 * @param args/*from   ww  w  . jav a 2s  . c  o  m*/
 */
public static void main(String[] args) {

    // hack: eclipse don't support IO redirection worth a shit
    // try {
    // System.setIn(new FileInputStream("./json"));
    // } catch (FileNotFoundException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }

    boolean graphMode = false;
    boolean jsonMode = false;
    boolean jsonRecoverMode = false;
    boolean endNode = false;

    int count = -1;

    long n = 0;
    long sumOfSqr = 0;
    long sum = 0;

    for (String s : args) {

        if (!s.matches("^-[vegjJh]*(c[0-9]*)?$")) {
            System.out.println("invalid argument");
            return;
        }

        if (s.matches("^-.*h.*")) {
            System.out.println(HELP);
            return;
        }
        if (s.matches("^-.*v.*")) {
            verbose = true;
            log("verbose mode");
        }
        if (s.matches("^-.*g.*")) {
            graphMode = true;
            log("graph mode");
        }
        if (s.matches("^-.*j.*")) {
            jsonMode = true;
            log("json mode");
        }
        if (s.matches("^-.*J.*")) {
            jsonRecoverMode = true;
            log("json recover mode");
        }
        if (s.matches("^-.*e.*")) {
            endNode = true;
            log("include end node");
        }
        if (s.matches("^-.*c[0-9]*$")) {
            log("counted output mode");
            count = Integer.parseInt(s.replaceAll("^-.*c", ""));
        }

        boolean error = (graphMode == true && jsonMode == true);
        if (!error) {
            error = (count > -1) && (graphMode == true || jsonMode == true);
        }

        if (error) {
            System.err.println("[error] switches j, g and, c are mutualy exclusive.");
            return;
        }

    }

    StateTransitionDiagram<Character> std;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    try {
        if (!jsonRecoverMode) {
            Trainer<Character> trainer = new Trainer<Character>();
            String s = br.readLine();
            while (s != null) {
                trainer.train(string2List(s));
                n++;
                sumOfSqr += s.length() * s.length();
                sum += s.length();
                s = br.readLine();
            }
            if (n == 0) {
                System.err
                        .println("Invalid corpus: At least one sample is required, two to make it interesting");
                return;
            }
            std = trainer.getTransitionDiagram();
        } else {
            std = new StateTransitionDiagram<Character>();
            GsonStub gstub = new Gson().fromJson(br, GsonStub.class);
            n = gstub.meta.n;
            sum = gstub.meta.sum;
            sumOfSqr = gstub.meta.sumOfSqr;

            for (Entry<String, StateStub> entry : gstub.states.entrySet()) {
                State<Character> state;
                if (entry.getKey().equals("null")) {
                    state = std.getGuard();
                } else {
                    state = std.getState(Character.valueOf(entry.getKey().charAt(0)));
                }
                for (Entry<String, Integer> transitions : entry.getValue().transitions.entrySet()) {
                    State<Character> tranny;
                    if (transitions.getKey().equals("null")) {
                        tranny = std.getGuard();
                    } else {
                        tranny = std.getState(Character.valueOf(transitions.getKey().charAt(0)));
                    }

                    state.addTransition(tranny.getValue(), transitions.getValue());
                }
            }
        }
        if (graphMode) {
            if (endNode) {
                System.out.println(std.toString());
            } else {
                System.out.println(std.removeEndGuards().toString());
            }
            return;
        }
        if (jsonMode) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

            String partialJson;
            if (endNode) {
                partialJson = gson.toJson(std);
            } else {
                partialJson = gson.toJson(std.removeEndGuards());
            }
            GsonStub gstub = new Gson().fromJson(partialJson, GsonStub.class);
            gstub.meta = new Meta();
            gstub.meta.n = n;
            gstub.meta.sum = sum;
            gstub.meta.sumOfSqr = sumOfSqr;

            System.out.println(gson.toJson(gstub));
            return;
        }

        Generator<Character> generator;
        if (endNode) {
            generator = new EndTagGenerator<Character>(std);
        } else {
            double sd = ((double) sumOfSqr - (double) (sum * sum) / (double) n) / (double) (n - 1);
            double mean = (double) sum / (double) n;
            log(String.format("mean: %.4f sd: %.4f", mean, sd));
            NormalDistributionImpl dist = new NormalDistributionImpl(mean, sd);
            generator = new NormalizedGenerator<Character>(std.removeEndGuards(), dist);
        }
        if (count >= 0) {
            for (int c = 0; c < count; c++) {
                output(generator);
            }
        } else {
            while (true) {
                output(generator);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:ivory.core.tokenize.Tokenizer.java

@SuppressWarnings("static-access")
public static void main(String[] args) {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("full path to model file or directory").hasArg()
            .withDescription("model file").create("model"));
    options.addOption(OptionBuilder.withArgName("full path to input file").hasArg()
            .withDescription("input file").isRequired().create("input"));
    options.addOption(OptionBuilder.withArgName("full path to output file").hasArg()
            .withDescription("output file").isRequired().create("output"));
    options.addOption(OptionBuilder.withArgName("en | zh | de | fr | ar | tr | es").hasArg()
            .withDescription("2-character language code").isRequired().create("lang"));
    options.addOption(OptionBuilder.withArgName("path to stopwords list").hasArg()
            .withDescription("one stopword per line").create("stopword"));
    options.addOption(OptionBuilder.withArgName("path to stemmed stopwords list").hasArg()
            .withDescription("one stemmed stopword per line").create("stemmed_stopword"));
    options.addOption(OptionBuilder.withArgName("true|false").hasArg().withDescription("turn on/off stemming")
            .create("stem"));
    options.addOption(OptionBuilder.withDescription("Hadoop option to load external jars")
            .withArgName("jar packages").hasArg().create("libjars"));

    CommandLine cmdline;/*  ww  w  .j ava  2  s .c  om*/
    CommandLineParser parser = new GnuParser();
    try {
        String stopwordList = null, stemmedStopwordList = null, modelFile = null;
        boolean isStem = true;
        cmdline = parser.parse(options, args);
        if (cmdline.hasOption("stopword")) {
            stopwordList = cmdline.getOptionValue("stopword");
        }
        if (cmdline.hasOption("stemmed_stopword")) {
            stemmedStopwordList = cmdline.getOptionValue("stemmed_stopword");
        }
        if (cmdline.hasOption("stem")) {
            isStem = Boolean.parseBoolean(cmdline.getOptionValue("stem"));
        }
        if (cmdline.hasOption("model")) {
            modelFile = cmdline.getOptionValue("model");
        }

        ivory.core.tokenize.Tokenizer tokenizer = TokenizerFactory.createTokenizer(
                cmdline.getOptionValue("lang"), modelFile, isStem, stopwordList, stemmedStopwordList, null);
        BufferedWriter out = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(cmdline.getOptionValue("output")), "UTF8"));
        BufferedReader in = new BufferedReader(
                new InputStreamReader(new FileInputStream(cmdline.getOptionValue("input")), "UTF8"));

        String line = null;
        while ((line = in.readLine()) != null) {
            String[] tokens = tokenizer.processContent(line);
            String s = "";
            for (String token : tokens) {
                s += token + " ";
            }
            out.write(s.trim() + "\n");
        }
        in.close();
        out.close();

    } catch (Exception exp) {
        System.out.println(exp);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Tokenizer", options);
        System.exit(-1);
    }
}

From source file:edu.harvard.med.iccbl.dev.HibernateConsole.java

public static void main(String[] args) {
    BufferedReader br = null;
    try {/*from  w  w  w  .j  a  v  a2  s  .  c o  m*/
        CommandLineApplication app = new CommandLineApplication(args);
        app.processOptions(true, false);
        br = new BufferedReader(new InputStreamReader(System.in));

        EntityManagerFactory emf = (EntityManagerFactory) app.getSpringBean("entityManagerFactory");
        EntityManager em = emf.createEntityManager();

        do {
            System.out.println("Enter HQL query (blank to quit): ");
            String input = br.readLine();
            if (input.length() == 0) {
                System.out.println("Goodbye!");
                System.exit(0);
            }
            try {
                List list = ((Session) em.getDelegate()).createQuery(input).list(); // note: this uses the Hibernate Session object, to allow HQL (and JPQL)
                // List list = em.createQuery(input).getResultList();  // note: this JPA method supports JPQL

                System.out.println("Result:");
                for (Iterator iter = list.iterator(); iter.hasNext();) {
                    Object item = iter.next();
                    // format output from multi-item selects ("select a, b, c, ... from ...")
                    if (item instanceof Object[]) {
                        List<Object> fields = Arrays.asList((Object[]) item);
                        System.out.println(StringUtils.makeListString(fields, ", "));
                    }
                    // format output from single-item selected ("select a from ..." or "from ...")
                    else {
                        System.out.println("[" + item.getClass().getName() + "]: " + item);
                    }
                }
                System.out.println("(" + list.size() + " rows)\n");
            } catch (Exception e) {
                System.out.println("Hibernate Error: " + e.getMessage());
                log.error("Hibernate error", e);
            }
            System.out.println();
        } while (true);
    } catch (Exception e) {
        System.err.println("Fatal Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(br);
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.toy.RelevantIrrelevant.java

public static void main(final String[] argv) throws NumberFormatException, IOException {
    final RandomGenerator rng = new MersenneTwister(42);
    final int T = 30;
    final int nr = 3;
    final int ni = 3;

    final Parameters params = new Parameters(T, nr, ni);
    final Actions actions = new Actions(params);
    final FsssModel model = new FsssModel(rng, params);

    State s = model.initialState();
    while (!s.isTerminal()) {
        System.out.println(s);/*  w  w  w. ja  v  a 2 s.  c o  m*/
        System.out.println("R(s): " + model.reward(s));
        actions.setState(s, 0);
        final ArrayList<Action> action_list = Fn.takeAll(actions);
        for (int i = 0; i < action_list.size(); ++i) {
            System.out.println(i + ": " + action_list.get(i));
        }
        System.out.print(">>> ");
        final BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        final int choice = Integer.parseInt(cin.readLine());
        final Action a = action_list.get(choice);
        System.out.println("R(s, a): " + model.reward(s, a));
        s = model.sampleTransition(s, a);
    }

    //      // Estimate the value of a "good" policy.
    //      // Note: The "good" policy is to Invest when you can, and Sell if the
    //      // price is >= 2. This is not necessarily optimal because:
    //      //    1. You should Borrow once the episode will end before the loan must be repaid
    //      //   2. For some values of invest_period, you should pass on a low price
    //      //      early in the period to try to get a better one later.
    //      final int Ngames = 10000;
    //      double V = 0;
    //      int Ninvest = 0;
    //      for( int i = 0; i < Ngames; ++i ) {
    //         State s = model.initialState();
    //         double Vi = model.reward( s );
    //         while( !s.isTerminal() ) {
    //            final Action a;
    //
    //            // "Good" policy
    //            if( s.investment == 0 ) {
    //               a = new InvestAction();
    //               Ninvest += 1;
    //            }
    //            else if( s.investment > 0 && s.price >= 2 ) {
    //               if( s.invest_t < (params.invest_period - 1) || s.price > 2 ) {
    //                  a = new SellAction();
    //               }
    //               else {
    //                  a = new SaveAction();
    //               }
    ////               a = new SellAction();
    //            }
    //            else {
    //               a = new SaveAction();
    //            }
    //
    //            // "Borrow" policy
    ////            if( s.loan == 0 ) {
    ////               a = new BorrowAction();
    ////            }
    ////            else {
    ////               a = new SaveAction();
    ////            }
    //
    //            final double ra = model.reward( s, a );
    //            s = model.sampleTransition( s, a );
    //            Vi += ra + model.reward( s );
    //         }
    //         V += Vi;
    //      }
    //
    //      final double Vavg = V / Ngames;
    //      final double Navg = (Ninvest / ((double) Ngames));
    //      System.out.println( "Avg. value: " + Vavg );
    //      System.out.println( "Avg. Invest actions: " + Navg );
    //      System.out.println( "V(Invest) ~= " + ( 1 + (Vavg - params.T)/Navg ) );
}

From source file:jmxbf.java

public static void main(String[] args) throws IOException, MalformedObjectNameException {

    String HOST = "";
    String PORT = "";
    String usersFile = "";
    String pwdFile = "";

    CommandLine cmd = getParsedCommandLine(args);

    if (cmd != null) {

        HOST = cmd.getOptionValue("host");
        PORT = cmd.getOptionValue("port");
        usersFile = cmd.getOptionValue("usernames-file");
        pwdFile = cmd.getOptionValue("passwords-file");

    } else {//from   w  ww. j  ava  2 s.  c om

        System.exit(1);
    }

    String finalResults = "";

    BufferedReader users = new BufferedReader(new FileReader(usersFile));
    BufferedReader pwds = new BufferedReader(new FileReader(pwdFile));

    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + HOST + ":" + PORT + "/jmxrmi");
    //new JMXServiceURL("service:jmx:remoting-jmx://" + HOST + ":" + PORT);

    String user = null;
    boolean found = false;
    while ((user = users.readLine()) != null) {
        String pwd = null;
        while ((pwd = pwds.readLine()) != null) {
            //System.out.println(user+":"+pwd);

            Map<String, String[]> env = new HashMap<>();
            String[] credentials = { user, pwd };
            env.put(JMXConnector.CREDENTIALS, credentials);
            try {

                JMXConnector jmxConnector = JMXConnectorFactory.connect(url, env);

                System.out.println();
                System.out.println();
                System.out.println();
                System.out.println(
                        "[+] ###SUCCESS### - We got a valid connection for: " + user + ":" + pwd + "\r\n\r\n");
                finalResults = finalResults + "\n" + user + ":" + pwd;
                jmxConnector.close();
                found = true;
                break;

            } catch (java.lang.SecurityException e) {
                System.out.println("Auth failed!!!\r\n");

            }
        }
        if (found) {
            System.out.println("Found some valid credentials - continuing brute force");
            found = false;

        }
        //closing and reopening pwds
        pwds.close();
        pwds = new BufferedReader(new FileReader(pwdFile));

    }

    users.close();
    //print final results
    if (finalResults.length() != 0) {
        System.out.println("The following valid credentials were found:\n");
        System.out.println(finalResults);
    }

}

From source file:net.socket.bio.TimeClient.java

/**
 * @param args//from   w w w  .ja va2  s. c o m
 */
public static void main(String[] args) {

    int port = 8089;
    if (args != null && args.length > 0) {

        try {
            port = Integer.valueOf(args[0]);
        } catch (NumberFormatException e) {
            // 
        }

    }
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try {
        socket = new Socket("127.0.0.1", port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
        out.println("QUERY TIME ORDER");
        out.println("QUERY TIME ORDER");
        String test = StringUtils.repeat("hello tcp", 1000);
        out.println(test);
        System.out.println("Send order 2 server succeed.");
        String resp = in.readLine();
        System.out.println("Now is : " + resp);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(socket);
    }
}

From source file:CalcoloRitardiLotti.java

public static void main(String[] args) {
    String id_ref = "cbededce-269f-48d2-8c25-2359bf246f42";
    String requestString = "http://dati.openexpo2015.it/catalog/api/action/datastore_search?resource_id="
            + id_ref;//  w w w.  j a v a  2  s. co  m
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(requestString);
    try {

        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String result = "";
        String resline = "";
        Calendar c = Calendar.getInstance();
        Date current = Date.valueOf(
                c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH));
        while ((resline = rd.readLine()) != null)
            result += resline;

        //System.out.println(jsonObject.toString());
        if (result != null) {
            JSONObject jsonObject = new JSONObject(result);
            JSONObject resultJson = (JSONObject) jsonObject.get("result");
            JSONArray records = (JSONArray) resultJson.get("records");
            Date temp1, temp2;
            //System.out.printf(records.toString());
            long diffInizioFineLavori;
            long ritardo;
            long den = (24 * 60 * 60 * 1000);
            JSONObject temp;

            DefaultCategoryDataset cdata = new DefaultCategoryDataset();
            String partialQuery;
            DefaultPieDataset data = new DefaultPieDataset();

            String totalQuery = "";
            int countSospesi = 0;
            int countConclusi = 0;
            int countVerifica = 0;
            int countInCorso = 0;
            int countCollaudo = 0;
            String stato;
            for (int i = 0; i < records.length(); i++) {
                temp = (JSONObject) records.get(i);
                temp1 = Date.valueOf((temp.getString("Data Consegna Lavori")).substring(0, 10));
                temp2 = Date.valueOf((temp.getString("Data Fine lavori")).substring(0, 10));
                diffInizioFineLavori = (long) (temp2.getTime() - temp1.getTime()) / den;
                stato = temp.getString("STATO");
                if (stato.equals("Concluso"))
                    countConclusi++;
                else if (stato.equals("In corso"))
                    countInCorso++;
                else if (stato.contains("Verifiche"))
                    countVerifica++;
                else if (stato.contains("Collaudo sospeso") || stato.contains("sospeso"))
                    countSospesi++;
                else
                    countCollaudo++;

                if (!temp.getString("STATO").equals("Concluso") && temp2.getTime() < current.getTime())
                    ritardo = (long) (current.getTime() - temp2.getTime()) / den;
                else
                    ritardo = 0;

                cdata.setValue(ritardo, String.valueOf(i + 1), String.valueOf(i + 1));
                System.out.println(
                        "Opera: " + temp.getString("Oggetto del lotto") + " | id: " + temp.getInt("_id"));
                System.out.println("Data consegna lavoro: " + temp.getString("Data Consegna Lavori")
                        + " | Data fine lavoro: " + temp.getString("Data Fine lavori"));
                System.out.println("STATO: " + temp.getString("STATO"));
                System.out.println("Differenza in giorni: " + diffInizioFineLavori
                        + " | Numero giorni contrattuali: " + temp.getString("numero di giorni contrattuali"));
                System.out.println("Ritardo accumulato: " + ritardo);

                System.out.println("----------------------------------");

                partialQuery = "\nid: " + temp.getInt("_id") + "\nOpera:" + temp.getString("Oggetto del lotto")
                        + "\n" + "Data consegna lavoro: " + temp.getString("Data Consegna Lavori")
                        + "Data fine lavoro: " + temp.getString("Data Fine lavori") + "\n" + "STATO: "
                        + temp.getString("STATO") + "\n" + "Differenza in giorni: " + diffInizioFineLavori
                        + " - Numero giorni contrattuali: " + temp.getString("numero di giorni contrattuali")
                        + "\n" + "Ritardo accumulato: " + ritardo + "\n"
                        + "----------------------------------\n";
                totalQuery = totalQuery + partialQuery;

            }

            JFreeChart chart1 = ChartFactory.createBarChart3D("RITARDI AL " + current, "Id lotto",
                    "ritardo(in giorni)", cdata);
            ChartRenderingInfo info = null;
            ChartUtilities.saveChartAsPNG(
                    new File(System.getProperty("user.dir") + "/istogramma" + current + ".png"), chart1, 1500,
                    1500, info, true, 10);
            FileUtils.writeStringToFile(new File(current + "_1.txt"), totalQuery);

            data.setValue("Conclusi: " + countConclusi, countConclusi);
            data.setValue("Sospeso: " + countSospesi, countSospesi);
            data.setValue("In Corso: " + countInCorso, countInCorso);
            data.setValue("Verifica: " + countVerifica, countVerifica);
            data.setValue("Collaudo: " + countCollaudo, countCollaudo);
            JFreeChart chart2 = ChartFactory.createPieChart3D("Statistiche del " + current, data, true, true,
                    true);
            ChartUtilities.saveChartAsPNG(new File(System.getProperty("user.dir") + "/pie" + current + ".png"),
                    chart2, 800, 450);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.citec.csra.elancsv.parser.SimpleParser.java

public static void main(String[] args) throws IOException, ParseException {

    Options opts = new Options();
    opts.addOption("file", true, "Tab-separated ELAN export file to load.");
    opts.addOption("tier", true,
            "Tier to analyze. Optional: Append ::num to interpret annotations numerically.");
    opts.addOption("format", true,
            "How to read information from the file name. %V -> participant, %A -> annoatator, %C -> condition, e.g. \"%V - %A\"");
    opts.addOption("help", false, "Print this help and exit");

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(opts, args);
    if (cmd.hasOption("help")) {
        helpExit(opts, "where OPTION includes:");
    }//w  ww  .  j  ava2 s . c  o  m

    String infile = cmd.getOptionValue("file");
    if (infile == null) {
        helpExit(opts, "Error: no file given.");
    }

    String format = cmd.getOptionValue("format");
    if (format == null) {
        helpExit(opts, "Error: no format given.");
    }

    String tier = cmd.getOptionValue("tier");
    if (tier == null) {
        helpExit(opts, "Error: no tier given.");
    }

    //      TODO count values in annotations (e.g. search all robot occurrences)
    String[] tn = tier.split("::");
    boolean numeric = false;
    if (tn.length == 2 && tn[1].equals("num")) {
        numeric = true;
        tier = tn[0];
    }

    format = "^" + format + "$";
    format = format.replaceFirst("%V", "(?<V>.*?)");
    format = format.replaceFirst("%A", "(?<A>.*?)");
    format = format.replaceFirst("%C", "(?<C>.*?)");
    Pattern pa = Pattern.compile(format);

    Map<String, Participant> participants = new HashMap<>();
    BufferedReader br = new BufferedReader(new FileReader(infile));
    String line;
    int lineno = 0;
    while ((line = br.readLine()) != null) {
        String[] parts = line.split("\t");
        lineno++;
        if (parts.length < 5) {
            System.err.println("WARNING: line '" + lineno + "' too short '" + line + "'");
            continue;
        }
        Annotation a = new Annotation(Long.valueOf(parts[ElanFormat.START.field]),
                Long.valueOf(parts[ElanFormat.STOP.field]), Long.valueOf(parts[ElanFormat.DURATION.field]),
                parts[ElanFormat.VALUE.field]);
        String tname = parts[ElanFormat.TIER.field];
        String file = parts[ElanFormat.FILE.field].replaceAll(".eaf", "");

        Matcher m = pa.matcher(file);
        String vp = file;
        String condition = "?";
        String annotator = "?";
        String participantID = vp;

        if (m.find()) {
            vp = m.group("V");
            if (format.indexOf("<A>") > 0) {
                annotator = m.group("A");
            }

            if (format.indexOf("<C>") > 0) {
                condition = m.group("C");
            }
        }
        participantID = vp + ";" + annotator;

        if (!participants.containsKey(participantID)) {
            participants.put(participantID, new Participant(vp, condition, annotator));
        }
        Participant p = participants.get(participantID);

        if (!p.tiers.containsKey(tname)) {
            p.tiers.put(tname, new Tier(tname));
        }

        p.tiers.get(tname).annotations.add(a);

    }

    Map<String, Map<String, Number>> values = new HashMap<>();
    Set<String> rownames = new HashSet<>();

    String allCountKey = "c: all values";
    String allDurationKey = "d: all values";
    String allMeanKey = "m: all values";

    for (Map.Entry<String, Participant> e : participants.entrySet()) {
        //         System.out.println(e);
        Tier t = e.getValue().tiers.get(tier);
        String participantID = e.getKey();

        if (!values.containsKey(participantID)) {
            values.put(participantID, new HashMap<String, Number>());
        }
        Map<String, Number> row = values.get(participantID); //participant id

        if (t != null) {

            row.put(allCountKey, 0l);
            row.put(allDurationKey, 0l);
            row.put(allMeanKey, 0l);

            for (Annotation a : t.annotations) {

                long countAll = (long) row.get(allCountKey) + 1;
                long durationAll = (long) row.get(allDurationKey) + a.duration;
                long meanAll = durationAll / countAll;

                row.put(allCountKey, countAll);
                row.put(allDurationKey, durationAll);
                row.put(allMeanKey, meanAll);

                if (!numeric) {
                    String countKey = "c: " + a.value;
                    String durationKey = "d: " + a.value;
                    String meanKey = "m: " + a.value;

                    if (!row.containsKey(countKey)) {
                        row.put(countKey, 0l);
                    }
                    if (!row.containsKey(durationKey)) {
                        row.put(durationKey, 0l);
                    }
                    if (!row.containsKey(meanKey)) {
                        row.put(meanKey, 0d);
                    }

                    long count = (long) row.get(countKey) + 1;
                    long duration = (long) row.get(durationKey) + a.duration;
                    double mean = duration * 1.0 / count;

                    row.put(countKey, count);
                    row.put(durationKey, duration);
                    row.put(meanKey, mean);

                    rownames.add(countKey);
                    rownames.add(durationKey);
                    rownames.add(meanKey);
                } else {
                    String countKey = "c: " + t.name;
                    String sumKey = "s: " + t.name;
                    String meanKey = "m: " + t.name;

                    if (!row.containsKey(countKey)) {
                        row.put(countKey, 0l);
                    }
                    if (!row.containsKey(sumKey)) {
                        row.put(sumKey, 0d);
                    }
                    if (!row.containsKey(meanKey)) {
                        row.put(meanKey, 0d);
                    }

                    double d = 0;
                    try {
                        d = Double.valueOf(a.value);
                    } catch (NumberFormatException ex) {

                    }

                    long count = (long) row.get(countKey) + 1;
                    double sum = (double) row.get(sumKey) + d;
                    double mean = sum / count;

                    row.put(countKey, count);
                    row.put(sumKey, sum);
                    row.put(meanKey, mean);

                    rownames.add(countKey);
                    rownames.add(sumKey);
                    rownames.add(meanKey);
                }

            }
        }

    }

    ArrayList<String> list = new ArrayList(rownames);
    Collections.sort(list);
    StringBuilder header = new StringBuilder("ID;Annotator;");
    header.append(allCountKey);
    header.append(";");
    header.append(allDurationKey);
    header.append(";");
    header.append(allMeanKey);
    header.append(";");
    for (String l : list) {
        header.append(l);
        header.append(";");
    }
    System.out.println(header);

    for (Map.Entry<String, Map<String, Number>> e : values.entrySet()) {
        StringBuilder row = new StringBuilder(e.getKey());
        row.append(";");
        if (e.getValue().containsKey(allCountKey)) {
            row.append(e.getValue().get(allCountKey));
        } else {
            row.append("0");
        }
        row.append(";");
        if (e.getValue().containsKey(allDurationKey)) {
            row.append(e.getValue().get(allDurationKey));
        } else {
            row.append("0");
        }
        row.append(";");
        if (e.getValue().containsKey(allMeanKey)) {
            row.append(e.getValue().get(allMeanKey));
        } else {
            row.append("0");
        }
        row.append(";");
        for (String l : list) {
            if (e.getValue().containsKey(l)) {
                row.append(e.getValue().get(l));
            } else {
                row.append("0");
            }
            row.append(";");
        }
        System.out.println(row);
    }
}