Implementing "Baidu Translation Interface API Fast Translation" with Java Multithreading

I don't know why, but I suddenly started wanting to write a blog. Maybe I wanted to find a place to write something. If we can't write about sensational literature and art, we will write a technical post. Inappropriate, but also hope that comrades give advice, be grateful.

API preparation: first go to Baidu to apply for a Baidu translation API . In other words, Baidu translation is still possible. The free translation of 200W characters per month is basically enough for non-commercial business. Thank you Baidu. Baidu not only provides APIs, but also demos of various programming languages. We use the java language version here. You can try to play by yourself.

What I want to do is to use Baidu Translator to help me translate about a dozen English documents, ranging in length from a dozen KB to more than 100 KB, and the total is around 600KB.

After testing, the single thread ran for about an hour, and the multi-thread ran for about 12 minutes (the longest document left in the end, the longboard effect!! It's not because it will shorten the time a lot!!!).

In the past, I felt that multi-threading and parallel computing were old-fashioned. In fact, after implementing it, I found that it was too simple to implement. In fact, it is to encapsulate the code that needs the same repeated execution into the run() method. A very basic point of multithreading is that the execution of sub-threads does not affect the execution of the main thread. It is almost enough to understand this.

//extends Thread is to inherit the multi-threaded class
public class MultiTranslate extends Thread{
	private static final String APP_ID = "你的API_ID";
	private static final String SECURITY_KEY = "API密匙";
	//By defining class attributes to pass variables, the run() method of the Thread class cannot take parameters
	String filename="";
	public MultiTranslate(String filename) {
		super();
		this.filename = filename;
	}
	
	@Override
	public void run() {
		String ThreadName=Thread.currentThread().getName();
		//call Baidu translation API
		TransApi api = new TransApi(APP_ID, SECURITY_KEY);
		//Get the path of the document to be translated, the document is the source file of the project under eclipse, and the filename is the file name
		String url = newMain.class.getClassLoader().getResource(filename).getPath();
		try {
			String newFile = URLDecoder.decode(url, "UTF-8");
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(newFile), "utf-8"));
			String line = null;
			//After translating and outputting to a new document, I prefixed the new document name with "ZH"
			FileOutputStream out = new FileOutputStream(new File("ZH"+filename),true);
			//Regular matching, Baidu translates and returns a json form, including a lot of content, I only take the translation result
			String questionRegex = "\"dst\":\"(.*)\"";
			Pattern pattern = Pattern.compile(questionRegex);
			while ((line = reader.readLine()) != null) {
				String[] words=line.split(" ");
				StringBuilder NewLine=new StringBuilder();
				for(int i=0;i<words.length;i++){
					//Call the interface for translation
					String get = new String(api.getTransResult(words[i], "auto", "zh"));
					Matches matches = pattern.matcher (get);
					while (matcher.find()) {
						String getword = matcher.group(1);
						//API returns Unicode encoding, import org.apache.commons.lang.StringEscapeUtils,
						//Call the StringEscapeUtils.unescapeJava() method to solve the encoding problem
						String newgetword = StringEscapeUtils.unescapeJava(getword);
						NewLine.append(newgetword+" ");
					}
				}
				String newline=NewLine.toString().trim()+"\n";
				System.out.println(filename+" "+ThreadName+" newline="+newline);
				out.write(newline.getBytes("utf-8"));
			}
			out.close();
			reader.close();
		} catch (Exception e) {
			throw new RuntimeException("Document load failed!");
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
		String path="xxx";
		ArrayList<String> filenameList=new ArrayList<String>();
		filenameList=getAllFile(path);
		for (int i = 0; i < filenameList.size(); i++) {
			//Generate a thread for each document, and the for loop generates multiple threads to perform translation tasks respectively
			Thread myThread=new MultiTranslate(filenameList.get(i));
			//Thread will execute the run() method
			myThread.start();
			//If the join() method is added, it will become a single-threaded effect, and the main thread will wait for the child thread to finish executing before continuing.
			//myThread.join();
		}
	}
	/**
	 * Read all files in a folder, including files in subfolders
	 *
	 * @param filepath
	 * path to the folder
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static ArrayList<String> getAllFile(String filepath)
			throws FileNotFoundException, IOException {
		File file = new File(filepath);
		ArrayList<String> pathList=new ArrayList<String>();
		File[] filelist = null;
		try {
			filelist = file.listFiles();
			for (File onefile : filelist) {
				File readfile = new File(onefile.getPath());
				if (readfile.isDirectory()) {
					getAllFile(onefile.getPath());
				} else {
					String filename=getDocName(readfile.getPath());
					pathList.add(filename);
				}
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException("getAllFile()   Exception:"
					+ e.getMessage());
		}
		return pathList;
	}
	
	// get the document title
	public static String getDocName(String path) {
		String[] temp = path.split("\\\\");
		if (temp.length >= 1) {
			String strtemp = temp[temp.length - 1];
			return strtemp.trim();
		}
		return "";
	}
}


Please indicate the source of the original text , thank you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389452&siteId=291194637