英文文本校对小程序

一、场景

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

二、示例

三、程序

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!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值