Search in sources :

Example 36 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class Util method getAreaChecklists.

public static String[] getAreaChecklists(String[] records) {
    String[] lines = null;
    try {
        if (records != null && records.length > 0) {
            String[][] data = new String[records.length - 1][];
            // header
            for (int i = 1; i < records.length; i++) {
                CSVReader csv = new CSVReader(new StringReader(records[i]));
                data[i - 1] = csv.readNext();
                csv.close();
            }
            java.util.Arrays.sort(data, new Comparator<String[]>() {

                @Override
                public int compare(String[] o1, String[] o2) {
                    // compare WMS urls
                    String s1 = CommonData.getSpeciesChecklistWMSFromSpcode(o1[0])[1];
                    String s2 = CommonData.getSpeciesChecklistWMSFromSpcode(o2[0])[1];
                    if (s1 == null && s2 == null) {
                        return 0;
                    } else if (s1 != null && s2 != null) {
                        return s1.compareTo(s2);
                    } else if (s1 == null) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
            lines = new String[records.length];
            lines[0] = lines[0] = "SPCODE,SCIENTIFIC_NAME,AUTHORITY_FULL,COMMON_NAME,FAMILY,GENUS_NAME,SPECIFIC_NAME,MIN_DEPTH,MAX_DEPTH,METADATA_URL,LSID,AREA_NAME,AREA_SQ_KM,SPECIES_COUNT";
            int len = 1;
            int thisCount = 0;
            for (int i = 0; i < data.length; i++) {
                thisCount++;
                String s1 = CommonData.getSpeciesChecklistWMSFromSpcode(data[i][0])[1];
                String s2 = i + 1 < data.length ? CommonData.getSpeciesChecklistWMSFromSpcode(data[i + 1][0])[1] : null;
                if (i == data.length - 1 || (s1 == null && s2 != null) || (s1 != null && s2 == null) || (s1 != null && s2 != null && !s1.equals(s2))) {
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < data[i].length; j++) {
                        if (j > 0) {
                            sb.append(",");
                        }
                        if (j == 0 || (j >= 9 && j != 10)) {
                            sb.append(Util.wrap(data[i][j]));
                        }
                    }
                    sb.append(",").append(thisCount);
                    lines[len] = sb.toString();
                    len++;
                    thisCount = 0;
                }
            }
            lines = java.util.Arrays.copyOf(lines, len);
        }
    } catch (Exception e) {
        LOGGER.error("error building species checklist", e);
        lines = null;
    }
    return lines;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) StringReader(java.io.StringReader) ParseException(com.vividsolutions.jts.io.ParseException)

Example 37 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class DistributionsController method init.

public void init(String[] text, String type, String count) {
    ((Caption) getFellow(StringConstants.CTITLE)).setLabel(type);
    this.originalCount = count;
    this.type = type;
    this.text = text == null ? null : text.clone();
    if (text != null && text.length > 1) {
        List<String[]> data = new ArrayList<String[]>();
        for (int i = 1; i < text.length; i++) {
            if (text[i] != null && text[i].length() > 0) {
                try {
                    StringReader sreader = new StringReader(text[i]);
                    CSVReader reader = new CSVReader(sreader);
                    // last row indicates if it is mapped or not
                    String[] row = reader.readNext();
                    String[] newrow = java.util.Arrays.copyOf(row, 15);
                    try {
                        String niceAreaSqKm = String.format("%.1f", (float) Double.parseDouble(row[12]));
                        newrow[12] = niceAreaSqKm;
                    } catch (Exception e) {
                        LOGGER.error("error parsing area from: " + ((row != null && row.length > 12) ? row[12] : "bad data"));
                    }
                    data.add(newrow);
                    reader.close();
                    sreader.close();
                } catch (Exception e) {
                    LOGGER.error("error reading distributions row", e);
                }
            }
        }
        if (this.originalData == null) {
            this.originalData = data;
        }
        update(data, originalCount);
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader)

Example 38 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project tika by apache.

the class MetadataResourceTest method testSimpleWord.

@Test
public void testSimpleWord() throws Exception {
    Response response = WebClient.create(endPoint + META_PATH).type("application/msword").accept("text/csv").put(ClassLoader.getSystemResourceAsStream(TikaResourceTest.TEST_DOC));
    Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    CSVReader csvReader = new CSVReader(reader);
    Map<String, String> metadata = new HashMap<String, String>();
    String[] nextLine;
    while ((nextLine = csvReader.readNext()) != null) {
        metadata.put(nextLine[0], nextLine[1]);
    }
    csvReader.close();
    assertNotNull(metadata.get("Author"));
    assertEquals("Maxim Valyanskiy", metadata.get("Author"));
    assertEquals("X-TIKA:digest:MD5", "f8be45c34e8919eedba48cc8d207fbf0", metadata.get("X-TIKA:digest:MD5"));
}
Also used : Response(javax.ws.rs.core.Response) InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) HashMap(java.util.HashMap) CSVReader(au.com.bytecode.opencsv.CSVReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Test(org.junit.Test)

Example 39 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project tika by apache.

the class MetadataResourceTest method testPasswordProtected.

@Test
public void testPasswordProtected() throws Exception {
    Response response = WebClient.create(endPoint + META_PATH).type("application/vnd.ms-excel").accept("text/csv").put(ClassLoader.getSystemResourceAsStream(TikaResourceTest.TEST_PASSWORD_PROTECTED));
    // Won't work, no password given
    assertEquals(500, response.getStatus());
    // Try again, this time with the wrong password
    response = WebClient.create(endPoint + META_PATH).type("application/vnd.ms-excel").accept("text/csv").header("Password", "wrong password").put(ClassLoader.getSystemResourceAsStream(TikaResourceTest.TEST_PASSWORD_PROTECTED));
    assertEquals(500, response.getStatus());
    // Try again, this time with the password
    response = WebClient.create(endPoint + META_PATH).type("application/vnd.ms-excel").accept("text/csv").header("Password", "password").put(ClassLoader.getSystemResourceAsStream(TikaResourceTest.TEST_PASSWORD_PROTECTED));
    // Will work
    assertEquals(200, response.getStatus());
    // Check results
    Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
    CSVReader csvReader = new CSVReader(reader);
    Map<String, String> metadata = new HashMap<String, String>();
    String[] nextLine;
    while ((nextLine = csvReader.readNext()) != null) {
        metadata.put(nextLine[0], nextLine[1]);
    }
    csvReader.close();
    assertNotNull(metadata.get("Author"));
    assertEquals("pavel", metadata.get("Author"));
}
Also used : Response(javax.ws.rs.core.Response) InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) HashMap(java.util.HashMap) CSVReader(au.com.bytecode.opencsv.CSVReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Test(org.junit.Test)

Example 40 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project textdb by TextDB.

the class NltkSentimentOperator method computeClassLabel.

// Process the data file using NLTK
private String computeClassLabel(String filePath) {
    try {
        /*
             *  In order to use the NLTK package to do classification, we start a
             *  new process to run the package, and wait for the result of running
             *  the process as the class label of this text field.
             *  Python call format:
             *      #python3 nltk_sentiment_classify picklePath dataPath resultPath
             * */
        List<String> args = new ArrayList<String>(Arrays.asList(PYTHON, PYTHONSCRIPT, PicklePath, filePath, resultPath));
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process p = processBuilder.start();
        p.waitFor();
        // Read label result from file generated by Python.
        CSVReader csvReader = new CSVReader(new FileReader(resultPath), SEPARATOR, QUOTECHAR, 1);
        List<String[]> allRows = csvReader.readAll();
        idClassMap = new HashMap<String, Integer>();
        // Read CSV line by line
        for (String[] row : allRows) {
            try {
                idClassMap.put(row[0], Integer.parseInt(row[1]));
            } catch (NumberFormatException e) {
                idClassMap.put(row[0], 0);
            }
        }
        csvReader.close();
    } catch (Exception e) {
        throw new DataflowException(e.getMessage(), e);
    }
    return null;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) IOException(java.io.IOException) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) FileReader(java.io.FileReader)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)82 IOException (java.io.IOException)29 InputStreamReader (java.io.InputStreamReader)27 ArrayList (java.util.ArrayList)16 FileReader (java.io.FileReader)11 StringReader (java.io.StringReader)11 HashMap (java.util.HashMap)9 BufferedReader (java.io.BufferedReader)8 InputStream (java.io.InputStream)6 File (java.io.File)5 Reader (java.io.Reader)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)5 DBException (org.jkiss.dbeaver.DBException)4 Query (au.org.ala.spatial.util.Query)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)3 Date (java.util.Date)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONArray (org.json.simple.JSONArray)3