servlet and jquery interfaces for moses server. Thanks to Alexander Ulanov

This commit is contained in:
Hieu Hoang 2012-08-12 13:18:58 +01:00
parent e99576aedc
commit a7b8c75b44
8 changed files with 21033 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0

View File

@ -0,0 +1,129 @@
package com.hpl.mt;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
/**
*
* @author ulanov
*/
public class Translate extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
System.out.println("before" + request.getCharacterEncoding());
request.setCharacterEncoding("UTF-8");
System.out.println("after" + request.getCharacterEncoding());
PrintWriter out = response.getWriter();
try {
/*
* TODO output your page here. You may use following sample code.
*/
// Create an instance of XmlRpcClient
String textToTranslate = request.getParameter("text");
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:9008/RPC2"));
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// The XML-RPC data type used by mosesserver is <struct>. In Java, this data type can be represented using HashMap.
HashMap<String,String> mosesParams = new HashMap<String,String>();
mosesParams.put("text", textToTranslate);
mosesParams.put("align", "true");
mosesParams.put("report-all-factors", "true");
// The XmlRpcClient.execute method doesn't accept Hashmap (pParams). It's either Object[] or List.
Object[] params = new Object[] { null };
params[0] = mosesParams;
// Invoke the remote method "translate". The result is an Object, convert it to a HashMap.
HashMap result;
try {
result = (HashMap)client.execute("translate", params);
} catch (XmlRpcException ex) {
Logger.getLogger(Translate.class.getName()).log(Level.SEVERE, null, ex);
throw new IOException("XML-RPC failed");
}
// Print the returned results
String textTranslation = (String)result.get("text");
System.out.println("Input : "+textToTranslate);
System.out.println("Translation : "+textTranslation);
out.write(textTranslation);
if (result.get("align") != null){
Object[] aligns = (Object[])result.get("align");
System.out.println("Phrase alignments : [Source Start:Source End][Target Start]");
for ( Object element : aligns) {
HashMap align = (HashMap)element;
System.out.println("["+align.get("src-start")+":"+align.get("src-end")+"]["+align.get("tgt-start")+"]");
}
}
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Translation"/>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Translate</servlet-name>
<servlet-class>com.hpl.mt.Translate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Translate</servlet-name>
<url-pattern>/Translate</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

View File

@ -0,0 +1,22 @@
/*
Document : common
Created on : Jul 31, 2012, 11:53:29 AM
Author : ulanov
Description:
Purpose of the stylesheet follows.
*/
root {
display: block;
}
body {font-size:small; font-family: Verdana,Arial,sans-serif;height:auto; width: auto;}
span {font-size:medium;}
#north_tab {height: 10%; width: 100%; float: top;}
#south_tab {height: 80%; width: 100%; float: bottom;}
#input_text {height: 50%; width: 30%; margin-right: 10px; float: left;}
#output_text {height: 50%; width: 30%; margin-right: 10px; float: left;}
#translate {float: left; margin-right: 10px;}

View File

@ -0,0 +1,47 @@
<html lang="fr">
<head>
<style>
</style>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet"
type="text/css"/>
<script src="lib/jquery-1.6.4.js" type="text/javascript"></script>
<script src="lib/jquery-ui-1.8.16.custom.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/common.css" type="text/css"/>
<script>
$(document).ready(function () {
$( "input:submit").button();
$( "input:submit").click(function(){
$.ajax({
url: "Translate",
type: "POST",
context: document.body,
data: {text: $("#input_text").val()}
}).done(function(data) {
$("#output_text").val(data);
});
})
});
</script>
<title>Translate FR-EN</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="north_tab">
<h2>Translate FR-EN</h2>
</div>
<div id="south_tab">
<textarea id="input_text">
</textarea>
<input id="translate" type="submit" value="Translate">
<textarea id="output_text" readonly="readonly">
</textarea>
</div>
</body>
</html>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff