adjustements to the csv parser method
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngine@82436 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
1b156b62d8
commit
71847c3b20
10
input.csv
10
input.csv
|
@ -1,10 +0,0 @@
|
||||||
5.1,3.5
|
|
||||||
4.9,3.0
|
|
||||||
4.7,3.2
|
|
||||||
4.6,3.1
|
|
||||||
5.0,3.6
|
|
||||||
5.4,3.9
|
|
||||||
4.6,3.4
|
|
||||||
5.0,3.4
|
|
||||||
4.4,2.9
|
|
||||||
4.9,3.1
|
|
|
2
pom.xml
2
pom.xml
|
@ -9,7 +9,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.gcube.dataanalysis</groupId>
|
<groupId>org.gcube.dataanalysis</groupId>
|
||||||
<artifactId>ecological-engine</artifactId>
|
<artifactId>ecological-engine</artifactId>
|
||||||
<version>1.7.0-SNAPSHOT</version>
|
<version>1.7.1-SNAPSHOT</version>
|
||||||
<name>ecological-engine</name>
|
<name>ecological-engine</name>
|
||||||
<description>ecological-engine library</description>
|
<description>ecological-engine library</description>
|
||||||
<properties>
|
<properties>
|
||||||
|
|
|
@ -54,23 +54,21 @@ public class Transformations {
|
||||||
// gets all the columns from a matrix
|
// gets all the columns from a matrix
|
||||||
public static double[][] traspose(double[][] matrix) {
|
public static double[][] traspose(double[][] matrix) {
|
||||||
int m = matrix.length;
|
int m = matrix.length;
|
||||||
if (m>0){
|
if (m > 0) {
|
||||||
int n = matrix[0].length;
|
int n = matrix[0].length;
|
||||||
|
|
||||||
double columns[][]= new double[n][m];
|
double columns[][] = new double[n][m];
|
||||||
|
|
||||||
for (int i = 0; i<n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
for (int j=0;j<m;j++)
|
for (int j = 0; j < m; j++)
|
||||||
columns[i][j] = matrix[j][i];
|
columns[i][j] = matrix[j][i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return columns;
|
return columns;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// gets a column from a matrix
|
// gets a column from a matrix
|
||||||
public static double[] getColumn(int index, double[][] matrix) {
|
public static double[] getColumn(int index, double[][] matrix) {
|
||||||
int colulen = matrix.length;
|
int colulen = matrix.length;
|
||||||
|
@ -90,7 +88,6 @@ public class Transformations {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// merge matrixes: puts the rows of a matrix under another matrix
|
// merge matrixes: puts the rows of a matrix under another matrix
|
||||||
public static double[][] mergeMatrixes(double[][] matrix1, double[][] matrix2) {
|
public static double[][] mergeMatrixes(double[][] matrix1, double[][] matrix2) {
|
||||||
|
|
||||||
|
@ -113,130 +110,137 @@ public class Transformations {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String vector2String(double[] vector){
|
public static String vector2String(double[] vector) {
|
||||||
String out = "";
|
String out = "";
|
||||||
for (int i=0;i<vector.length;i++){
|
for (int i = 0; i < vector.length; i++) {
|
||||||
if (i>0)
|
if (i > 0)
|
||||||
out = out + ","+vector[i];
|
out = out + "," + vector[i];
|
||||||
else
|
else
|
||||||
out = ""+vector[i];
|
out = "" + vector[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object getObjectFromFile(String file) throws Exception {
|
||||||
public static Object getObjectFromFile(String file) throws Exception{
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
FileInputStream fis = new FileInputStream(file);
|
* FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); return ois.readObject();
|
||||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
*/
|
||||||
return ois.readObject();
|
|
||||||
*/
|
|
||||||
XStream xstream = new XStream();
|
XStream xstream = new XStream();
|
||||||
return xstream.fromXML(new FileInputStream(file));
|
return xstream.fromXML(new FileInputStream(file));
|
||||||
// return xstream.fromXML(FileTools.loadString(file, "UTF-8"));
|
// return xstream.fromXML(FileTools.loadString(file, "UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void dumpObjectToFile(String file, Object toWrite) throws Exception {
|
public static void dumpObjectToFile(String file, Object toWrite) throws Exception {
|
||||||
|
|
||||||
XStream xstream = new XStream();
|
XStream xstream = new XStream();
|
||||||
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
|
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
|
||||||
bw.write(xstream.toXML(toWrite));
|
bw.write(xstream.toXML(toWrite));
|
||||||
bw.close();
|
bw.close();
|
||||||
/*
|
/*
|
||||||
FileOutputStream fos = new FileOutputStream(file);
|
* FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream ois = new ObjectOutputStream(fos); ois.writeObject(toWrite);
|
||||||
ObjectOutputStream ois = new ObjectOutputStream(fos);
|
*/
|
||||||
ois.writeObject(toWrite);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void dumpConfig(String pathToFile, AlgorithmConfiguration config) throws Exception {
|
public static void dumpConfig(String pathToFile, AlgorithmConfiguration config) throws Exception {
|
||||||
Transformations.dumpObjectToFile(pathToFile, config);
|
Transformations.dumpObjectToFile(pathToFile, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AlgorithmConfiguration restoreConfig(String configFile) throws Exception{
|
public static AlgorithmConfiguration restoreConfig(String configFile) throws Exception {
|
||||||
FileInputStream fis = new FileInputStream(new File(configFile));
|
FileInputStream fis = new FileInputStream(new File(configFile));
|
||||||
AlgorithmConfiguration config = (AlgorithmConfiguration) new XStream().fromXML(fis);
|
AlgorithmConfiguration config = (AlgorithmConfiguration) new XStream().fromXML(fis);
|
||||||
fis.close();
|
fis.close();
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double indexString(String string) {
|
public static double indexString(String string) {
|
||||||
// string = Sha1.SHA1(string);
|
// string = Sha1.SHA1(string);
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
if ((string==null) ||(string.length()==0))
|
if ((string == null) || (string.length() == 0))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int m= string.length();
|
int m = string.length();
|
||||||
for (int i=0;i<m;i++){
|
for (int i = 0; i < m; i++) {
|
||||||
sb.append((int)string.charAt(i));
|
sb.append((int) string.charAt(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
double d = Double.MAX_VALUE;
|
double d = Double.MAX_VALUE;
|
||||||
try{
|
try {
|
||||||
d = Double.valueOf(sb.toString());
|
d = Double.valueOf(sb.toString());
|
||||||
}catch(Exception e){
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d>Integer.MAX_VALUE)
|
if (d > Integer.MAX_VALUE)
|
||||||
return (indexString(string.substring(0,3)));
|
return (indexString(string.substring(0, 3)));
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
public static void main(String[] args) throws Exception{
|
String in = "h,t,,,,k,";
|
||||||
|
in = "Salmo lucidus (Richardson 1836)\",\"Salmo lucidus\",\"Richardson 1836\",\"SIMPLE\",0.6547619047619048,\"ASFIS\",\"APL\",\"Plantae aquaticae\",\"\",\"\",\"PLANTAE AQUATICAE MISCELLANEA\",\"Aquatic plants nei\",\"Plantes aquatiques nca\",\"Plantas acuáticas nep\"";
|
||||||
List<String> ll = Transformations.parseCVSString("h,t,,,,k,", ",");
|
|
||||||
for (String l:ll){
|
List<String> ll = Transformations.parseCVSString(in, ",");
|
||||||
|
|
||||||
|
for (String l : ll) {
|
||||||
System.out.println(l);
|
System.out.println(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
String s = "un'estate al mare";
|
String s = "un'estate al mare";
|
||||||
System.out.println("index: "+indexString(s));
|
System.out.println("index: " + indexString(s));
|
||||||
System.out.println("sha1: "+Sha1.SHA1(s));
|
System.out.println("sha1: " + Sha1.SHA1(s));
|
||||||
String s1 = "un'estate al mari";
|
String s1 = "un'estate al mari";
|
||||||
System.out.println("index: "+indexString(s1));
|
System.out.println("index: " + indexString(s1));
|
||||||
System.out.println("sha1: "+Sha1.SHA1(s1));
|
System.out.println("sha1: " + Sha1.SHA1(s1));
|
||||||
String s2 = "ciao amico mio come stai oggi? fa caldo o sono io che mi scotto?";
|
String s2 = "ciao amico mio come stai oggi? fa caldo o sono io che mi scotto?";
|
||||||
System.out.println("index: "+indexString(s2));
|
System.out.println("index: " + indexString(s2));
|
||||||
System.out.println("sha1: "+Sha1.SHA1(s2));
|
System.out.println("sha1: " + Sha1.SHA1(s2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> parseCVSString(String row, String delimiter) throws Exception{
|
public static List<String> parseCVSString(String row, String delimiter) throws Exception {
|
||||||
|
|
||||||
List<String> elements = new ArrayList<String>();
|
List<String> elements = new ArrayList<String>();
|
||||||
String phrase=row;
|
String phrase = row;
|
||||||
int idxdelim = -1;
|
int idxdelim = -1;
|
||||||
boolean quot = false;
|
boolean quot = false;
|
||||||
phrase = phrase.trim();
|
phrase = phrase.trim();
|
||||||
while ( (idxdelim = phrase.indexOf(delimiter))>=0) {
|
while ((idxdelim = phrase.indexOf(delimiter)) >= 0) {
|
||||||
quot=phrase.startsWith("\"");
|
quot = phrase.startsWith("\"");
|
||||||
if (quot){
|
if (quot) {
|
||||||
phrase = phrase.substring(1);
|
phrase = phrase.substring(1);
|
||||||
RE regexp = new RE("[^\\\\]\"");
|
|
||||||
boolean matching = regexp.match(phrase);
|
|
||||||
String quoted = "";
|
String quoted = "";
|
||||||
if (matching){
|
if (phrase.startsWith("\""))
|
||||||
int i0 = regexp.getParenStart(0);
|
phrase = phrase.substring(1);
|
||||||
quoted= phrase.substring(0,i0+1).trim();
|
else{
|
||||||
phrase = phrase.substring(i0+2).trim();
|
RE regexp = new RE("[^\\\\]\"");
|
||||||
|
boolean matching = regexp.match(phrase);
|
||||||
|
|
||||||
|
if (matching) {
|
||||||
|
int i0 = regexp.getParenStart(0);
|
||||||
|
quoted = phrase.substring(0, i0 + 1).trim();
|
||||||
|
phrase = phrase.substring(i0 + 2).trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phrase.startsWith(delimiter))
|
if (phrase.startsWith(delimiter))
|
||||||
phrase=phrase.substring(1);
|
phrase = phrase.substring(1);
|
||||||
|
|
||||||
elements.add(quoted);
|
elements.add(quoted);
|
||||||
|
|
||||||
}
|
} else {
|
||||||
else{
|
elements.add(phrase.substring(0, idxdelim));
|
||||||
elements.add(phrase.substring(0,idxdelim));
|
phrase = phrase.substring(idxdelim + 1).trim();
|
||||||
phrase= phrase.substring(idxdelim+1).trim();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (phrase.startsWith("\""))
|
||||||
|
phrase = phrase.substring(1);
|
||||||
|
|
||||||
|
if (phrase.endsWith("\""))
|
||||||
|
phrase = phrase.substring(0, phrase.length() - 1);
|
||||||
|
|
||||||
elements.add(phrase);
|
elements.add(phrase);
|
||||||
|
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
#Generated by Maven
|
||||||
|
#Thu Oct 03 09:52:55 CEST 2013
|
||||||
|
version=1.7.1-SNAPSHOT
|
||||||
|
groupId=org.gcube.dataanalysis
|
||||||
|
artifactId=ecological-engine
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<ID></ID>
|
||||||
|
<Type>Library</Type>
|
||||||
|
<Profile>
|
||||||
|
<Description>Ecological Engine Library</Description>
|
||||||
|
<Class>EcologicalEngine</Class>
|
||||||
|
<Name>ecological-engine</Name>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<Packages>
|
||||||
|
<Software>
|
||||||
|
<Name>ecological-engine</Name>
|
||||||
|
<Version>1.7.1-SNAPSHOT</Version>
|
||||||
|
<MavenCoordinates>
|
||||||
|
<groupId>org.gcube.dataanalysis</groupId>
|
||||||
|
<artifactId>ecological-engine</artifactId>
|
||||||
|
<version>1.7.1-SNAPSHOT</version>
|
||||||
|
</MavenCoordinates>
|
||||||
|
<Files>
|
||||||
|
<File>ecological-engine-1.7.1-SNAPSHOT.jar</File>
|
||||||
|
</Files>
|
||||||
|
</Software>
|
||||||
|
</Packages>
|
||||||
|
</Profile>
|
||||||
|
</Resource>
|
Loading…
Reference in New Issue