Skip to content

Commit

Permalink
Make it work with latest versions of gate-interaction, weka-wrapper, …
Browse files Browse the repository at this point in the history
…and sklearn-wrapper.
  • Loading branch information
johann-petrak committed Jul 11, 2016
1 parent 069108b commit 0aea2b1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion creole.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- creole.xml for Learning_Framework -->
<CREOLE-DIRECTORY
ID="gate.LearningFramework"
VERSION="3.4-gamma"
VERSION="3.4"
DESCRIPTION="Learning Framework"
HELPURL="https://github.com/GateNLP/gateplugin-LearningFramework/wiki"
>
Expand Down
Binary file modified lib-static/interaction-1.0-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -137,19 +138,22 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {

@Override
protected void loadModel(File directory, String parms) {
ArrayList<String> finalCommand = new ArrayList<String>();
// Instead of loading a model, this establishes a connection with the
// external sklearn process.

File commandFile = findWrapperCommand(directory, true);
String modelFileName = new File(directory,"sklmodel").getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+modelFileName;
finalCommand.add(commandFile.getAbsolutePath());
finalCommand.add(wrapperhome);
finalCommand.add(modelFileName);
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
finalCommand.add(0,shellcmd);
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
Expand All @@ -168,13 +172,15 @@ protected void saveModel(File directory) {

@Override
public void trainModel(File dataDirectory, String instanceType, String parms) {
ArrayList<String> finalCommand = new ArrayList<String>();
// invoke the sklearn wrapper for training
// NOTE: for this the first word in parms must be the full sklearn class name, the rest are parms
if(parms == null || parms.isEmpty()) {
if(parms == null || parms.trim().isEmpty()) {
throw new GateRuntimeException("Cannot train using SklearnWrapper, algorithmParameter must contain fulle SciKit Learn algorithm class name as first word");
}
String sklearnClass = null;
String sklearnParms = "";
parms = parms.trim();
int spaceIdx = parms.indexOf(" ");
if(spaceIdx<0) {
sklearnClass = parms;
Expand All @@ -192,16 +198,27 @@ public void trainModel(File dataDirectory, String instanceType, String parms) {
Exporter.EXPORTER_MATRIXMARKET2_CLASS, dataDirectory, instanceType, parms);
String dataFileName = dataDirectory.getAbsolutePath()+File.separator;
String modelFileName = new File(dataDirectory, "sklmodel").getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+dataFileName+" "+modelFileName+" "+sklearnClass+" "+sklearnParms;
finalCommand.add(commandFile.getAbsolutePath());
finalCommand.add(wrapperhome);
finalCommand.add(dataFileName);
finalCommand.add(modelFileName);
finalCommand.add(sklearnClass);
if(!sklearnParms.isEmpty()) {
String[] tmp = sklearnParms.split("\\s+",-1);
finalCommand.addAll(Arrays.asList(tmp));
}
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
finalCommand.add(0,shellcmd);
}
System.err.println("Running: ");
for(int i=0; i<finalCommand.size();i++) {
System.err.println(i+": >"+finalCommand.get(i)+"<");
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalSklearnWrapperModel";

Expand Down
38 changes: 30 additions & 8 deletions src/gate/plugin/learningframework/engines/EngineWekaExternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class EngineWekaExternal extends Engine {
private String shellcmd = null;
private String shellparms = null;
private String wrapperhome = null;
private boolean linuxLike = true;
private boolean windowsLike = false;

/**
* Try to find the script running the Weka-Wrapper command.
Expand Down Expand Up @@ -110,8 +113,8 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {
// we use the simple heuristic that if the file separator is "/"
// we assume we can use the bash script, if it is "\" we use the windows
// script and otherwise we give up
boolean linuxLike = System.getProperty("file.separator").equals("/");
boolean windowsLike = System.getProperty("file.separator").equals("\\");
linuxLike = System.getProperty("file.separator").equals("/");
windowsLike = System.getProperty("file.separator").equals("\\");
if(linuxLike) {
if(apply)
commandFile = new File(new File(wrapperHome,"bin"),"wekaWrapperApply.sh");
Expand All @@ -137,6 +140,7 @@ private File findWrapperCommand(File dataDirectory, boolean apply) {

@Override
protected void loadModel(File directory, String parms) {
ArrayList<String> finalCommand = new ArrayList<String>();
// Instead of loading a model, this establishes a connection with the
// external weka process. For this, we expect an additional file in the
// directory, weka.yaml, which describes how to run the weka wrapper
Expand All @@ -149,14 +153,18 @@ protected void loadModel(File directory, String parms) {
if(!new File(header).exists()) {
throw new GateRuntimeException("File not found: "+header);
}
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+modelFileName+" "+header;
finalCommand.add(commandFile.getAbsolutePath());
finalCommand.add(wrapperhome);
finalCommand.add(modelFileName);
finalCommand.add(header);

// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
finalCommand.add(0,shellcmd);
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
Expand All @@ -175,13 +183,15 @@ protected void saveModel(File directory) {

@Override
public void trainModel(File dataDirectory, String instanceType, String parms) {
ArrayList<String> finalCommand = new ArrayList<String>();
// TODO: invoke the weka wrapper
// NOTE: for this the first word in parms must be the full weka class name, the rest are parms
if(parms == null || parms.isEmpty()) {
if(parms == null || parms.trim().isEmpty()) {
throw new GateRuntimeException("Cannot train using WekaWrapper, algorithmParameter must contain Weka algorithm class as first word");
}
String wekaClass = null;
String wekaParms = "";
parms = parms.trim();
int spaceIdx = parms.indexOf(" ");
if(spaceIdx<0) {
wekaClass = parms;
Expand All @@ -199,16 +209,28 @@ public void trainModel(File dataDirectory, String instanceType, String parms) {
Exporter.EXPORTER_ARFF_CLASS, dataDirectory, instanceType, parms);
String dataFileName = new File(dataDirectory,Globals.dataBasename+".arff").getAbsolutePath();
String modelFileName = new File(dataDirectory, FILENAME_MODEL).getAbsolutePath();
String finalCommand = commandFile.getAbsolutePath()+" "+wrapperhome+" "+dataFileName+" "+modelFileName+" "+wekaClass+" "+wekaParms;

finalCommand.add(commandFile.getAbsolutePath());
finalCommand.add(wrapperhome);
finalCommand.add(dataFileName);
finalCommand.add(modelFileName);
finalCommand.add(wekaClass);
if(!wekaParms.isEmpty()) {
String[] tmp = wekaParms.split("\\s+",-1);
finalCommand.addAll(Arrays.asList(tmp));
}
// if we have a shell command prepend that, and if we have shell parms too, include them
if(shellcmd != null) {
String tmp = shellcmd;
if(shellparms != null) {
shellcmd += " " + shellparms;
}
finalCommand = shellcmd + " " + finalCommand;
finalCommand.add(0,shellcmd);
}
System.err.println("Running: ");
for(int i=0; i<finalCommand.size();i++) {
System.err.println(i+": >"+finalCommand.get(i)+"<");
}
System.err.println("Running: "+finalCommand);
// Create a fake Model jsut to make LF_Apply... happy which checks if this is null
model = "ExternalWekaWrapperModel";

Expand Down

0 comments on commit 0aea2b1

Please sign in to comment.