必威体育Betway必威体育官网
当前位置:首页 > IT技术

英文文本校对小程序

时间:2019-07-16 18:41:05来源:IT技术作者:seo实验室小编阅读:69次「手机版」
 

校对英文

一、场景

给定一段英文,标记出错误的英文单词。

二、示例

三、程序

package Nlp;
import org.apache.http.HttpEntity;
import org.apache.http.Httpresponse;
import org.apache.http.client.httpclient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检查单词是否正确,错误给出提示
 *
 * @author ruyi.yury
 * @date 2018/6/15 0015 10:16
 */
public class Checkword {
    /**
     *
     * @param word
     * @return
     */
    public static boolean checkWords(String word) {
        boolean isRight = true;
        HttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet("http://fanyi.youdao.com/openapi.do?keyfrom=<keyform>&key=<key>&type=data&doctype=xml&version=1.1&q="
                + word);//把<keyfrom>替换为自己的,把<key>替换为自己的
        HttpResponse response = null;
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity, "utf-8");
            Document doc = DocumentHelper.parseText(str);
            Element root = doc.getRootElement();
            Element query = root.element("query");
            Element paragraph = root.element("translation").element("paragraph");
            if (!isChinese(paragraph.getText())){
                isRight = false;
            }
        } catch (IOException e) {
            e.printstacktrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return isRight;
    }

    /**
     * 读取文本
     * @param content
     * @return
     */
    public static String readText(String content) {
        String[] text = content.split(" ");
        System.out.println(text.length);
        System.out.println(text.toString());
        for (int i = 0; i < text.length; i++) {
            if(!checkWords(text[i].substring(0,text[i].length()-1))){
                    text[i] = "("+text[i]+")-->不正确";
            }
        }
        stringbuilder checkdText = new StringBuilder();
        for (int i = 0; i < text.length; i++) {
            checkdText.APPend(text[i]+" ");
        }
        System.out.println(checkdText.toString());
        return checkdText.toString();
    }

    /**
     * 判断是否为中文
     * @param word
     * @return
     */
    public static boolean isChinese(String word){
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern pattern = pattern.compile(regEx);
        Matcher matcher = pattern.matcher(word);
        boolean flag = false;
        if(matcher.find()){
            flag = true;
        }
        return flag;
    }
    public static void main(String[] args) {
        readText("this eeee your fatestds!");
    }
}

相关阅读

将英文版的Ubuntu 18.04系统改为中文

很多朋友安装Ubuntu后是英文的,看不懂要怎么办?其实Ubuntu是可以设置成中文的,按照以下方法一步步设置就可以了。首先,更新镜像配置,请

奥巴马在复旦大学的演讲稿(中英文对照)

[转载] https://www.201980.com/yanjiang/yingyu/9727_3.html[视频] http://www.iqiyi.com/w_19rsjdiw15.html2009年11月中旬美国

星期一到星期日的英文_缩写_读音_巧记方法

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,

护士英文面试常见问题

Why did you choose nursing?Why do you want to work for us?What courses did you like best/least? Why?What qualities do yo

英雄联盟英文台词

雷恩加尔: Tonight,we hunt.          今晚,猎个痛快。 迦娜: The tempest is at your command.      风之化身听候您的

分享到:

栏目导航

推荐阅读

热门阅读