Commit d6498897 authored by Elias Englmeier's avatar Elias Englmeier
Browse files

initial commit

parents
# Maven
target/
dependency-reduced-pom.xml
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
# IntelliJ IDEA
.idea/
*.iml
*.ipr
*.iws
# Eclipse
.settings/
.classpath
.project
nb-configuration.xml
atlassian-ide-plugin.xml
logs
\ No newline at end of file
# classificationservice
How to start the classificationservice application
---
1. Run `mvn clean install` to build your application
1. Start application with `java -jar target/classification-1.0-SNAPSHOT.jar server config.yml`
1. To check that your application is running enter url `http://localhost:8080`
Health Check
---
To see your applications health enter url `http://localhost:8081/healthcheck`
logging:
level: INFO
loggers:
org.tmms.classification.: DEBUG
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.0.0</maven>
</prerequisites>
<groupId>org.tmms.classification</groupId>
<artifactId>classification</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>classificationservice</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<dropwizard.version>1.3.0</dropwizard.version>
<mainClass>org.tmms.classification.classificationserviceApplication</mainClass>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-bom</artifactId>
<version>${dropwizard.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-assets</artifactId>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.1</version>
<classifier>models</classifier> <!-- will get the dependent model jars -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass>
</transformer>
</transformers>
<!-- exclude signed Manifests -->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
</plugin>
</plugins>
</reporting>
</project>
package org.tmms.classification.api;
import org.hibernate.validator.constraints.Length;
import com.fasterxml.jackson.annotation.JsonProperty;
public class NamedEntityRecognitionResult {
private long id;
@Length(max = 3)
private String content;
public NamedEntityRecognitionResult(){
}
public NamedEntityRecognitionResult(long id, String value) {
this.id = id;
this.content = value;
}
@JsonProperty
public long getId() {
return id;
}
@JsonProperty
public String getContent() {
return content;
}
}
package org.tmms.classification.api;
import org.hibernate.validator.constraints.Length;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Saying {
private long id;
@Length(max = 3)
private String content;
public Saying() {
// Jackson deserialization
}
public Saying(long id, String content) {
this.id = id;
this.content = content;
}
@JsonProperty
public long getId() {
return id;
}
@JsonProperty
public String getContent() {
return content;
}
}
\ No newline at end of file
package org.tmms.classification;
import org.tmms.classification.health.TemplateHealthCheck;
import org.tmms.classification.resources.HelloWorldResource;
import org.tmms.classification.resources.NamedEntityRecognitionResource;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class classificationserviceApplication extends Application<classificationserviceConfiguration> {
public static void main(final String[] args) throws Exception {
new classificationserviceApplication().run(args);
}
@Override
public String getName() {
return "classificationservice";
}
@Override
public void initialize(final Bootstrap<classificationserviceConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle ("/assets/pretrainedClassifiers/german.conll.germeval2014.hgc_175m_600.crf.ser"));
}
@Override
public void run(final classificationserviceConfiguration configuration, final Environment environment) {
final HelloWorldResource helloWorldResource = new HelloWorldResource(configuration.getTemplate(),
configuration.getDefaultName());
environment.jersey().register(helloWorldResource);
final NamedEntityRecognitionResource NERResource = new NamedEntityRecognitionResource(configuration.getTemplate(), configuration.getDefaultEmptyResponse());
environment.jersey().register(NERResource);
final TemplateHealthCheck healthCheck = new TemplateHealthCheck(configuration.getTemplate());
environment.healthChecks().register("template", healthCheck);
environment.jersey().register(helloWorldResource);
}
}
package org.tmms.classification;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
public class classificationserviceConfiguration extends Configuration {
@NotEmpty
private String template;
@NotEmpty
private String defaultName = "Stranger";
@NotEmpty
private String defaultEmptyResponse = "No Data Received!";
@JsonProperty
public String getDefaultEmptyResponse() {
return defaultEmptyResponse;
}
@JsonProperty
public void setDefaultEmptyResponse(String defaultEmptyResponse) {
this.defaultEmptyResponse = defaultEmptyResponse;
}
@JsonProperty
public String getTemplate() {
return template;
}
@JsonProperty
public void setTemplate(String template) {
this.template = template;
}
@JsonProperty
public String getDefaultName() {
return defaultName;
}
@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}
}
package org.tmms.classification.core;
import java.io.Serializable;
import edu.stanford.nlp.ie.AbstractSequenceClassifier;
import edu.stanford.nlp.ie.crf.CRFClassifier;
public class NamedEntityRecognizer implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4963688218460681322L;
AbstractSequenceClassifier classifier;
public NamedEntityRecognizer(String pathToClassifier){
classifier = CRFClassifier.getClassifierNoExceptions(pathToClassifier);
}
public String classifyString(String s){
return classifier.classifyToString(s);
}
}
package org.tmms.classification.health;
import com.codahale.metrics.health.HealthCheck;
public class TemplateHealthCheck extends HealthCheck {
private final String template;
public TemplateHealthCheck(String template) {
this.template = template;
}
@Override
protected Result check() throws Exception {
final String saying = String.format(template, "TEST");
if (!saying.contains("TEST")) {
return Result.unhealthy("template doesn't include a name");
}
return Result.healthy();
}
}
package org.tmms.classification.resources;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.tmms.classification.api.Saying;
import com.codahale.metrics.annotation.Timed;
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public HelloWorldResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.orElse(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}
package org.tmms.classification.resources;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.tmms.classification.api.NamedEntityRecognitionResult;
import org.tmms.classification.core.NamedEntityRecognizer;
import com.codahale.metrics.annotation.Timed;
import edu.stanford.nlp.simple.Sentence;
@Path("/ner")
@Produces(MediaType.APPLICATION_JSON)
public class NamedEntityRecognitionResource {
private final String template;
private final String defaultResponse;
private final AtomicLong counter;
private NamedEntityRecognizer classifier;
public NamedEntityRecognitionResource(String template, String defaultResponse) {
this.template = template;
this.counter = new AtomicLong();
this.defaultResponse = defaultResponse;
}
@GET
@Path("/init")
@Timed
public boolean initClassifier(){
try{
classifier = new NamedEntityRecognizer("http://localhost:8080/assets/pretrainedClassifiers/german.conll.germeval2014.hgc_175m_600.crf.ser");
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
@GET
@Timed
public NamedEntityRecognitionResult analyseNER(@QueryParam("data") String data) {
Sentence sent = new Sentence(data);
List<String> nerTags = sent.nerTags();
//String result = classifier.classifyString(data);
final String value = String.format(template, nerTags.toString());
return new NamedEntityRecognitionResult(counter.incrementAndGet(), value);
}
}
================================================================================
classificationservice
================================================================================
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