Commit 47f38b77 authored by Markus Krug's avatar Markus Krug
Browse files

initial upload of the engine

parents
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.uniwue.az.sentiment</groupId>
<artifactId>sentimentAnalysis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.uima</groupId>
<artifactId>uimaj-core</artifactId>
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.uima</groupId>
<artifactId>uimafit-core</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.uniwue.mk.nappi</groupId>
<artifactId>
de.uniwue.mk.nappi.serviceprovider
</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- or whatever version you use -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package sentimentAnalysis.engine;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.impl.TypeSystem2Xml;
import org.apache.uima.cas.impl.XmiCasDeserializer;
import org.apache.uima.cas.impl.XmiCasSerializer;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.xml.sax.SAXException;
public class KallimachosSentimentAnalysisEngine extends JCasAnnotator_ImplBase {
private File xmiFile;
private File typeSystemFile;
/**
* The location of the model.
*/
public static final String PARAM_SCRIPT_LOCATION = "PARAM_SCRIPT_LOCATION";
@ConfigurationParameter(name = PARAM_SCRIPT_LOCATION, mandatory = true)
private String scriptLocationParam;
private String scriptLocation;
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
// set the temporary file for the features
try {
xmiFile = File.createTempFile("tempXMIPyCAS", ".xmi");
typeSystemFile = File.createTempFile("TypeSystem", "CorefTypeSystem.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmiFile.deleteOnExit();
typeSystemFile.deleteOnExit();
scriptLocation = (String) context.getConfigParameterValue(PARAM_SCRIPT_LOCATION);
super.initialize(context);
}
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
CAS cas = jCas.getCas();
// serialite TS
serializeTS(cas);
// serializie CAS
serializeXMI(cas);
// process the script
try {
String command = "python " + scriptLocation + " " + xmiFile.getAbsolutePath() + " "
+ typeSystemFile.getAbsolutePath() + " " + xmiFile.getAbsolutePath();
Process exec = Runtime.getRuntime().exec(command);
// output both stdout and stderr data from proc to stdout of this
// process
StreamGobbler errorGobbler = new StreamGobbler(exec.getErrorStream());
StreamGobbler outputGobbler = new StreamGobbler(exec.getInputStream());
errorGobbler.start();
outputGobbler.start();
exec.waitFor();
exec.destroy();
System.out.println(command);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
XmiCasDeserializer.deserialize(new FileInputStream(xmiFile), cas);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmiFile.delete();
typeSystemFile.delete();
}
private void serializeXMI(CAS cas) {
try {
FileOutputStream aStream = new FileOutputStream(xmiFile);
XmiCasSerializer.serialize(cas, aStream);
aStream.flush();
aStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void serializeTS(CAS cas) {
try {
TypeSystem typeSystem = cas.getTypeSystem();
FileOutputStream aOutputStream = new FileOutputStream(typeSystemFile);
TypeSystem2Xml.typeSystem2Xml(typeSystem, aOutputStream);
aOutputStream.flush();
aOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
super.collectionProcessComplete();
xmiFile.delete();
typeSystemFile.delete();
}
}
package sentimentAnalysis.engine;
import de.uniwue.mk.nappi.serviceprovider.aeservice.IAnalysisEngineService;
public class SentimentDetectionAnalysisEngineService implements IAnalysisEngineService {
public Class<KallimachosSentimentAnalysisEngine> getAnalysisEngine() {
return KallimachosSentimentAnalysisEngine.class;
}
}
package sentimentAnalysis.engine;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
class StreamGobbler extends Thread {
InputStream is;
// reads everything from is until empty.
StreamGobbler(InputStream is) {
this.is = is;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
\ No newline at end of file
sentimentAnalysis.engine.SentimentDetectionAnalysisEngineService
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment