用WTS API和JFrame做的机器翻译的Application

来源:互联网 发布:linux ftp下载文件 编辑:程序博客网 时间:2024/06/07 22:22

所用到的WTS API为:
类 com.ibm.lt.LTengine中的

1、GetService(String,String) 
       获取服务句柄以开始一个事务。此 GetService(hostname, language); 函数将联系 WebSphere Translation Server,并为翻译函数提供句柄,以使用 WTS 服务。第一个参数是服务器的主机名,第二个参数是语言方向或 HTTP 协议 Accept-language qValue。例如,联系本地机器上的服务器并访问英语到德语的翻译。

类 com.ibm.lt.LTinterface中的

2、jltBeginTranslation(String)
       开始机器翻译请求。参数:option,选项参数以巴科斯诺尔范式(BNF)给出。
       这个方法是最关键的方法了。里面有以下的选项:
       *format(text 或 html 或 otext)
       *charset(此选项只能设置为 unicode。缺省支持是 ISO8859-1
       *subject(多种设置主题:字典)
       *path(字典/数据位置)
       *metacontent (没用到。0或1创建或关闭HTML META 头。缺省值是打开。)
       *lexstorage(file 或 memory,缺省为file,一般来说缺省就可以了)
       *lexicons(要装入的用户字典)

3、jltTranslate(Object,String)
       用于翻译已分段数据的扩展语言翻译使用的函数。参数:Object为ltBeginTranslation 请求的句柄,String输入数据,传递空则表示数据的结尾。

4、jltEndTranslation(String)
       此函数结束机器翻译请求,并释放此翻译会话的所有资源分配。参数为标识 ltBeginTranslation 请求的句柄。

程序运行效果如下:

相关部分代码如下:

1、JFrame实现Translation GUI

/*
 * @author Liu Bing
 * Created on 21.11.2006
 * Modified on 22.11.2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/

package translate;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;


public class Translation_GUI extends JFrame implements ActionListener {
    String sa 
=null// the variable fot the Subject Area
    String dr ="ende";  // the variable for the direction of translation.Default is from English to Germany.
    
    JPanel pao 
= new JPanel();
    JPanel pau 
= new JPanel();
    JTextArea textAreao 
= new JTextArea();
    JTextArea textArea 
= new JTextArea();
    JButton b1 
= new JButton("Translate");
    
    JScrollPane jScrollPane2;
    
    JComboBox cbb 
= new JComboBox();
    JComboBox direc 
= new JComboBox();
    
    
public Translation_GUI(String titel) {
        
super(titel);
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);         
        
        getContentPane().setLayout(
new BorderLayout());
        getContentPane().add(
"South",pau);
        getContentPane().add(
"Center",pao);        
        
        
//Title
        JLabel label = new JLabel();
        label.setText(
"<html>Übersetzen Prüfen</html>");
        label.setFont(
new Font("Serif", Font.PLAIN, 18)); 
        pao.add(label);        
        
        
//input area
        textAreao = new JTextArea();
        textAreao.setColumns(
42);
        textAreao.setRows(
10);
        textAreao.setEditable(
true);
        textAreao.append(
"Here input your text.");
        pao.add(textAreao);
        JScrollPane jScrollPane1 
= new JScrollPane(textAreao);
        pao.add(jScrollPane1);

        
//Select the direction and the subject area of translations
        JLabel labe2 = new JLabel("Auswählen:       ",SwingConstants.RIGHT);
        pao.add(labe2);
        
        
//Directions
        JLabel labe3 = new JLabel("Richtung ",SwingConstants.RIGHT);
        labe3.setFont(
new Font("Arial", Font.PLAIN, 12)); 
        pao.add(labe3);
        direc 
= new JComboBox();
        direc.setBackground(Color.WHITE);
        direc.addItem(
"ende");
        direc.addItem(
"deen");
        direc.addItemListener(
new ItemListener(){

            
public void itemStateChanged(ItemEvent e)
            
{
                dr 
= e.getItem().toString();
            }

            }
);
        pao.add(direc);
        
        
//Subject Areas
        JLabel labe4 = new JLabel("                Bereich ",SwingConstants.RIGHT);
        labe4.setFont(
new Font("Arial", Font.PLAIN, 12)); 
        pao.add(labe4);
        cbb 
= new JComboBox();
        cbb.setBackground(Color.WHITE);
        cbb.addItem(
"");
        cbb.addItem(
"chat");
        cbb.addItem(
"economy");
        cbb.addItemListener(
new ItemListener(){

            
public void itemStateChanged(ItemEvent e)
            
{
                sa 
= e.getItem().toString();
            }

            }
);
        pao.add(cbb);
        
        
        
//output the results
        textArea = new JTextArea();
        textArea.setColumns(
42);
        textArea.setRows(
10);
        textArea.setEditable(
false);        
        pao.add(textArea);
        JScrollPane jScrollPane2 
= new JScrollPane(textArea);
        jScrollPane2.setBorder(
new BevelBorder(BevelBorder.LOWERED));  
        pao.add(jScrollPane2);
        
        
//add the Button
        b1 = new JButton("Translate");
        b1.addActionListener(
this);
        pau.add(b1);

        
//Set the properties for this JFrame
        setSize(505,450);
        setVisible(
true);
        setResizable(
false);
        }


    
public static void main(String[] args) {
        
        Translation_GUI thui 
= new Translation_GUI("Test");
        
    }


    
//listen the button-actions
    public void actionPerformed(ActionEvent e) {
        Object quelle 
= e.getSource();
//        pao.setBackground(Color.PINK);        
        
        String ziele 
= null;
        MainUebersetzen mu 
= new MainUebersetzen();
        
        textArea.setText(mu.Translate(textAreao.getText(),ziele,sa,dr));
            
    }



}

2、翻译程序(通过WTS API使用RMI协议)

package translate;
/*
 * @author Liu Bing
 * Created on 21.11.2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 
*/


import com.ibm.lt.LTengine;
import com.ibm.lt.LTinterface;

public class MainUebersetzen {
    
public MainUebersetzen(){
        }

    
    
public String Translate(String s_lang,String t_lang,String subjectarea, String direction){
        
        LTinterface service
=null;
        Object handle
=null;
        String jltBToptions 
= null;
        
if (subjectarea == "" || subjectarea == null)
            jltBToptions
="*format=html";
            
        
else
            jltBToptions
="*format=html *subject="+subjectarea;
        
        System.out.println(jltBToptions);
        System.out.println(direction);

        
try 
        service 
= (LTinterface) LTengine.GetService("127.0.0.1",direction);
        }
catch (Throwable t)
        t.printStackTrace(); 
        System.out.println(
"no service available"); 
        System.exit(
0); 
        }
 
        
try
        handle
=service.jltBeginTranslation(jltBToptions); 
        t_lang
=service.jltTranslate(handle,s_lang);
        System.out.println(t_lang); 
        service.jltEndTranslation(handle);
        }
catch (Exception e){
        e.printStackTrace(); 
        System.exit(
0); 
        }
 
        
return t_lang;
        
    }

    
    
public static void main(String[] args) {
        String tag 
= null;
        String subjectarea 
= null;
        MainUebersetzen mu 
= new MainUebersetzen();
        mu.Translate(
"i",tag,subjectarea,"ende");
        System.out.println(tag);
        
    }


}

原创粉丝点击