基于iciba的英汉翻译助手(离线版,基于文本)
在之前写过一个 TranslateHelper类用于在线翻译英文单词。
我之前写过一个 单词翻译大师用于将文件中出现的所有单词存储在另一个文件中。存储的格式如下:
word: explain
如:
apple: 苹果;苹果树;苹果公司
banana: 香蕉;芭蕉属植物;喜剧演员
我们假设这里获取单词的原文件为D:|test_english.txt
我们假设这里获取单词的原文件为D:|test_english.txt
存储单词的文件为D:\word_lib.txt
这次写的TranslateHelper2类就是在此基础上编写的一个英汉词典的离线版本。
在此之前我写了一个WordFinder类用于获取D:\word_lib.txt下的特定单词及其解释(没有的话返回null)。
WordFinder.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class WordFinder {
public static String find(String word) throws Exception {
String filename = new String("D:\\word_lib.txt");
BufferedReader reader = new BufferedReader( new FileReader(filename));
String line = "";
while((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ":");
String key = st.nextToken();
if(key.equals(word)) {
return st.nextToken();
}
}
return null;
}
public static void main(String[] args) throws Exception {
String ans = find("apple");
System.out.println(ans);
}
}
下面是TranslateHelper2类,其词库是基于文件D:\word_lib.txt的,如下:
import java.io.FileReader;
import java.util.StringTokenizer;
public class WordFinder {
public static String find(String word) throws Exception {
String filename = new String("D:\\word_lib.txt");
BufferedReader reader = new BufferedReader( new FileReader(filename));
String line = "";
while((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ":");
String key = st.nextToken();
if(key.equals(word)) {
return st.nextToken();
}
}
return null;
}
public static void main(String[] args) throws Exception {
String ans = find("apple");
System.out.println(ans);
}
}
新增了一个按键可在线更新词库,即D:\word_lib.txt里面的内容(在现实点按键可更新)。
TranslateHelper2.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TranslateHelper2 extends JFrame {
private static final int Width = 500;
private static final int Height = 220;
private static JFrame frame = null;
private static FlowLayout flowLayout = null;
private static JLabel label = null;
private static JTextField wordText = null;
private static JTextField explainText = null;
private static JButton button = null;
private static JButton new_button = null;
public TranslateHelper2() {
frame = new JFrame("Translate Helper");
flowLayout = new FlowLayout(FlowLayout.CENTER);
flowLayout.setHgap(20);
flowLayout.setVgap(30);
frame.setLayout(flowLayout);
label = new JLabel("单词:");
wordText = new JTextField(10);
explainText = new JTextField(40);
button = new JButton("提交");
new_button = new JButton("在线时点击可更新");
frame.add(label);
frame.add(wordText);
frame.add(button);
frame.add(explainText);
frame.add(new_button);
button.addActionListener( new ButtonAction());
new_button.addActionListener( new ButtonAction());
frame.setVisible( true);
frame.setSize(Width, Height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object s = evt.getSource();
// System.out.println("hello");
if(s == button) {
String word = wordText.getText();
try {
String _word = word;
String _explain = WordFinder.find(word);
wordText.setText(_word);
explainText.setText(_explain);
} catch (Exception e) {
e.printStackTrace();
}
} else if(s == new_button) {
try {
TranslateMaster.translateAllLocal("D:\\test_english.txt", "D:\\word_lib.txt");
} catch (Exception e) {
return;
}
}
}
}
public static void main(String[] args) {
new TranslateHelper2();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TranslateHelper2 extends JFrame {
private static final int Width = 500;
private static final int Height = 220;
private static JFrame frame = null;
private static FlowLayout flowLayout = null;
private static JLabel label = null;
private static JTextField wordText = null;
private static JTextField explainText = null;
private static JButton button = null;
private static JButton new_button = null;
public TranslateHelper2() {
frame = new JFrame("Translate Helper");
flowLayout = new FlowLayout(FlowLayout.CENTER);
flowLayout.setHgap(20);
flowLayout.setVgap(30);
frame.setLayout(flowLayout);
label = new JLabel("单词:");
wordText = new JTextField(10);
explainText = new JTextField(40);
button = new JButton("提交");
new_button = new JButton("在线时点击可更新");
frame.add(label);
frame.add(wordText);
frame.add(button);
frame.add(explainText);
frame.add(new_button);
button.addActionListener( new ButtonAction());
new_button.addActionListener( new ButtonAction());
frame.setVisible( true);
frame.setSize(Width, Height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object s = evt.getSource();
// System.out.println("hello");
if(s == button) {
String word = wordText.getText();
try {
String _word = word;
String _explain = WordFinder.find(word);
wordText.setText(_word);
explainText.setText(_explain);
} catch (Exception e) {
e.printStackTrace();
}
} else if(s == new_button) {
try {
TranslateMaster.translateAllLocal("D:\\test_english.txt", "D:\\word_lib.txt");
} catch (Exception e) {
return;
}
}
}
}
public static void main(String[] args) {
new TranslateHelper2();
}
}