Skip to content

Instantly share code, notes, and snippets.

@firecentaur
Created March 24, 2015 06:59
Show Gist options
  • Save firecentaur/9a0f1db1252f53e29898 to your computer and use it in GitHub Desktop.
Save firecentaur/9a0f1db1252f53e29898 to your computer and use it in GitHub Desktop.
This http server runs on port 8080 and conjugates verbs by analysing the get request, just visit http://localhost:8080/?verb=have
package SimpleFrameworkExampleHttpServer;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import org.simpleframework.http.Query;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerServer;
import org.simpleframework.transport.Server;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import simplenlg.framework.*;
import simplenlg.lexicon.*;
import simplenlg.realiser.english.*;
import simplenlg.phrasespec.*;
import simplenlg.features.*;
public class sHello implements Container {
public void handle(Request request, Response response) {
try {
PrintStream body = response.getPrintStream();
long time = System.currentTimeMillis();
response.set("Content-Type", "text/plain");
response.set("Server", "HelloWorld/1.0 (Simple 4.0)");
response.setDate("Date", time);
response.setDate("Last-Modified", time);
Query query = request.getQuery();
String word = query.get("verb");
Lexicon lexicon = Lexicon.getDefaultLexicon();
NLGFactory nlgFactory = new NLGFactory(lexicon);
Realiser realiser = new Realiser(lexicon);
SPhraseSpec p = nlgFactory.createClause();
p.setVerb(word);
p.setFeature(Feature.TENSE, Tense.PAST);
String output = realiser.realiseSentence(p); // Realiser created earlier.
System.out.println(output);
p.setFeature(Feature.TENSE, Tense.PRESENT);
output = realiser.realiseSentence(p); // Realiser created earlier.
System.out.println(output);
p.setFeature(Feature.TENSE, Tense.FUTURE);
output = realiser.realiseSentence(p); // Realiser created earlier.
System.out.println(output);
p.setFeature(Feature.TENSE, Tense.PRESENT);
p.setFeature(Feature.NEGATED, true);
output = realiser.realiseSentence(p); // Realiser created earlier.
System.out.println(output);
body.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] list) throws Exception {
Container container = new sHello();
Server server = new ContainerServer(container,1);
Connection connection = new SocketConnection(server);
SocketAddress address = new InetSocketAddress(8080);
connection.connect(address);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment