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;
}
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);
}
}
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"));
}
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"));
}
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;
}
Aggregations