Skip to content

Commit

Permalink
Add Filter CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachid Yahiaoui authored and Rachid Yahiaoui committed Jul 12, 2017
1 parent 8031b4e commit aaf703e
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 82 deletions.
130 changes: 78 additions & 52 deletions src/main/java/entypoint/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
package entypoint ;

import java.io.File ;
import java.util.List ;
import java.util.Arrays ;
import java.util.stream.Collectors ;
import org.inra.yedgen.processor.Processor ;

/**
Expand Down Expand Up @@ -36,46 +39,54 @@ public static void main (String[] args) throws Exception {
String classe = null ; int column = -1 ;
String prefixFile = null , connecFile = null , def_prefix = null ;

Integer matchColumn = null ; String _matchWord = null ;

boolean includingGraphVariables = false , verbose = false ;

int nbParams = 0 ;

for ( int i = 0 ; i < args.length ; i++ ) {

String token = args[i] ;
String token = args[i] ;

switch(token) {

case "-d" : directory = args[i+1] ; nbParams += 2 ;
break ;
case "-out" : outFile = args[i+1] ; nbParams += 2 ;
break ;
case "-ext" : ext = args[i+1] ; nbParams += 2 ;
break ;
case "-csv" : csv = args[i+1] ; nbParams += 2 ;
break ;
case "-prf" : prf = args[i+1] ; nbParams += 2 ;
break ;
case "-js" : js = args[i+1] ; nbParams += 2 ;
break ;
case "-class" : classe = args[i+1] ; nbParams += 2 ;
break ;
case "-column" : column = Integer.parseInt(args [ i+1 ]
.replaceAll(" +", "")) ;
nbParams += 2 ;
break ;
case "-ig" : includingGraphVariables = true ;
nbParams += 1 ;
break ;
case "-v" : verbose = true ;
nbParams += 1 ;
break ;
case "-def_prefix" : def_prefix = args[i+1] ; nbParams += 2 ;
break ;
case "-connecFile" : connecFile = args[i+1] ; nbParams += 2 ;
break ;
case "-prefixFile" : prefixFile = args[i+1] ; nbParams += 2 ;
break ;
case "-d" : directory = args[i+1] ; nbParams += 2 ;
break ;
case "-out" : outFile = args[i+1] ; nbParams += 2 ;
break ;
case "-ext" : ext = args[i+1] ; nbParams += 2 ;
break ;
case "-csv" : csv = args[i+1] ; nbParams += 2 ;
break ;
case "-prf" : prf = args[i+1] ; nbParams += 2 ;
break ;
case "-js" : js = args[i+1] ; nbParams += 2 ;
break ;
case "-class" : classe = args[i+1] ; nbParams += 2 ;
break ;
case "-column" : column = Integer.parseInt(args [ i+1 ]
.replaceAll(" +", "")) ;
nbParams += 2 ;
break ;
case "-ig" : includingGraphVariables = true ;
nbParams += 1 ;
break ;
case "-v" : verbose = true ;
nbParams += 1 ;
break ;
case "-def_prefix" : def_prefix = args[i+1] ; nbParams += 2 ;
break ;
case "-connecFile" : connecFile = args[i+1] ; nbParams += 2 ;
break ;
case "-prefixFile" : prefixFile = args[i+1] ; nbParams += 2 ;
break ;
case "-matchWord" : _matchWord = args[i+1] ; nbParams += 2 ;
break ;
case "-matchColumn" : matchColumn = Integer.parseInt(args [ i+1 ]
.replaceAll(" +", "")) ;
nbParams += 2 ;
break ;
}
}

Expand All @@ -88,35 +99,50 @@ public static void main (String[] args) throws Exception {
if( directory == null || directory.isEmpty() ||
outFile == null || outFile.isEmpty()) {

System.out.println (" directory or outFile is Empty " ) ;
System.out.println (" directory or outFile is Empty " ) ;
return ;
}

if(ext == null || ext.length() == 0 ) ext = ".graphml" ;
if(ext == null || ext.length() == 0 ) ext = ".graphml" ;

List<String> wordList = null ;

if( _matchWord != null && ! _matchWord.isEmpty() ) {

long startTime = System.currentTimeMillis() ;
wordList = Arrays.asList( _matchWord.trim()
.replaceAll(" +", " ")
.split(","))
.stream()
.map( word -> word.trim() )
.collect(Collectors.toList()) ;
}

long startTime = System.currentTimeMillis() ;

Processor processor = new Processor ( directory ,
ext ,
prf ,
js ,
connecFile ,
prefixFile ,
def_prefix ) ;
Processor processor = new Processor ( directory ,
ext ,
prf ,
js ,
connecFile ,
prefixFile ,
def_prefix ) ;

processor.process ( outFile ,
csv ,
includingGraphVariables ,
classe ,
column ,
verbose ) ;
processor.process ( outFile ,
csv ,
includingGraphVariables ,
classe ,
column ,
matchColumn ,
wordList ,
verbose ) ;

long executionTime = System.currentTimeMillis() - startTime ;
long executionTime = System.currentTimeMillis() - startTime ;

System.out.println(" Elapsed seconds : " +
executionTime / 1000 + " s" ) ;
System.out.println(" Elapsed seconds : " +
executionTime / 1000 + " s" ) ;

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

}

}
121 changes: 121 additions & 0 deletions src/main/java/org/inra/yedgen/csv/CsvFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

package org.inra.yedgen.csv ;

import java.util.List ;
import java.util.Arrays ;
import java.util.Objects ;
import java.nio.file.Files ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.stream.Stream ;
import java.util.logging.Level ;
import java.util.logging.Logger ;
import java.util.stream.Collectors ;
import org.inra.yedgen.processor.io.Writer ;

/**
*
* @author ryahiaoui
*/

public class CsvFilter {


public static void main (String[] args) throws Exception {

String csvFile = null ;
String outCsv = null ;
Integer matchColumn = null ;
String matchWord = null ;
String separator = null ;

for ( int i = 0 ; i < args.length ; i++ ) {

String token = args[i] ;

switch(token) {

case "-csv" : csvFile = args[i+1] ;
break ;
case "-outCsv" : outCsv = args[i+1] ;
break ;
case "-matchWord" : matchWord = args[i+1] ;
break ;
case "-matchColumn" : matchColumn = Integer.parseInt(args [ i+1 ]
.replaceAll(" +", "")) ;
break ;
case "-separator" : separator = args[i+1] ;
break ;
}
}

Objects.requireNonNull( csvFile ) ;
Objects.requireNonNull( outCsv ) ;

List<String> wordList = null ;

if( matchWord != null && ! matchWord.isEmpty() ) {

wordList = Arrays.asList( matchWord.trim()
.replaceAll(" +", " ")
.split(","))
.stream()
.map( word -> word.trim() )
.collect(Collectors.toList()) ;
}

/* Read File and Filter */

String _outCsv = outCsv ;
Integer _matchColumn = matchColumn ;
List<String> _wordList = wordList ;
String _separator = separator ;


System.out.println(" " ) ;
System.out.println(" ********************************** " ) ;
System.out.println(" - Input CSV File : " + _outCsv ) ;
System.out.println(" - scv_separator : " + _separator ) ;
System.out.println(" - matchColumn : " + _matchColumn ) ;
System.out.println(" - wordList : " + _wordList ) ;
System.out.println(" ********************************** " ) ;
System.out.println(" " ) ;


List<String> outLines = new ArrayList<>() ;

outLines.add( Files.lines(Paths.get(csvFile)).findFirst().get()) ;

try ( Stream<String> lines = Files.lines(Paths.get(csvFile)).skip(1)) {

lines.forEach ( line -> {

if( _matchColumn != null && _matchColumn > 0 &&
_wordList != null && ! _wordList.isEmpty() ) {

if( _wordList.contains( line.split(_separator)[ _matchColumn ]
.trim()
.replaceAll(" +", " "))) {
outLines.add(line ) ;
}
}
}) ;

} catch (Exception ex) {
Logger.getLogger(CsvFilter.class.getName()).log(Level.SEVERE, null, ex) ;
}

Writer.checkFile( _outCsv ) ;
Writer.writeTextFile( outLines , _outCsv) ;

if( outLines.size() == 1 ) {
System.out.println(" -> Empty CSV File Generated ") ;
System.out.println(" ") ;
}
else {
System.out.println(" -> CSV File Generated at : " + _outCsv ) ;
System.out.println(" ") ;
}
}

}
Loading

0 comments on commit aaf703e

Please sign in to comment.