基于Java+Fiddler+百度翻译Api的词达人刷题

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档



前言

一个大二的学生写的,大佬勿喷,希望得到指点


提示:以下是本篇文章正文内容,下面案例可供参考


一、设计思路

由于词达人改版后将判断单词的True和false清除了

原先的answer已经被改成空,无法再通过这个标记判断了

但是通过json解析可以看出它的单词还是发了过来的如下图

 该content中存在的就是该单词,于是我们想,应该可以将该单词取出经过翻译再与几个选项中的中文相比较然后找出答案


二、具体步骤


1.将Fiddler请求的主题保存下来再Fiddler脚本插入以下语句

代码如下(示例):

    static function OnBeforeResponse(oSession: Session) {
   
        if(oSession.uriContains("https://gateway.vocabgo.com/Student/ClassTask/")||oSession.uriContains("https://gateway.vocabgo.com/Student/StudyTask")){
            oSession.utilDecodeResponse();
            oSession.SaveResponseBody("E:\\data\\word.txt");
        }
   
    }


2.将文本的目标单词和选项的中文提出来

代码如下(示例):

public   String getEn(String path) throws IOException {
    int index=0;
    int indexend=0;
    String s=null;
    String s2=null;
    File file=new File(path);
    BufferedReader bf=new BufferedReader(new FileReader(file));
   s= bf.readLine();
   index=s.indexOf("content");
   indexend=s.indexOf(" ",index+12);
   s2=s.substring(index+11,indexend);

   return s2;
}
//读取中文
public  ArrayList<String> getZh(String path) throws IOException {
       ArrayList<String>al = new ArrayList<>();
        int index=0;
        int indexend=0;
        String s=null;
        File file=new File(path);
        BufferedReader bf=new BufferedReader(new FileReader(file));
        String txt=null;
        while ((txt=bf.readLine())!=null)
        {

            if(txt.indexOf("adj ", indexend)!=-1)
            {

                while (txt.indexOf("adj ", indexend)!=-1) {
                    index = txt.indexOf("adj ", indexend);
                    indexend = txt.indexOf("\"", index);
                    al.add(txt.substring(index + 4, indexend));

                }

            }else if(txt.indexOf("noun ", indexend)!=-1)
            {
                while (txt.indexOf("noun ", indexend)!=-1) {
                    index = txt.indexOf("noun ", indexend);
                    indexend = txt.indexOf("\"", index);
                    al.add(txt.substring(index + 5, indexend));

                }


            }else  if(txt.indexOf("verb ", indexend)!=-1)
            {
                while (txt.indexOf("verb ", indexend)!=-1) {
                    index = txt.indexOf("verb ", indexend);
                    indexend = txt.indexOf("\"", index);
                    al.add(txt.substring(index + 5, indexend));

                }

            }

        }

        return al;
}

该处使用的url网络请求的数据。



3.将英文翻译成中文

public  String chaxun(String s) {
    String jg=null;
    String txt = null;
    String q=s;
    String AppId="20211024000981028";
   String ms="f93uan_Cna5uhEsYh_nA";
   String sjm="1435660288";
   String tep1=AppId+q+sjm+ms;
   //MD5加密
    String qm=getMD5String(tep1);


    try {
        URL  UL = new URL("https://api.fanyi.baidu.com/api/trans/vip/translate?q="+q+"&from=en&to=zh&appid="+AppId+"&salt="+sjm+"&sign="+qm);
        URLConnection urlConnection = UL.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        BufferedReader BF = new BufferedReader(new InputStreamReader(inputStream));


        String regex2="\"";

        String[] qfjg ;
        String[] qfjg2;
        while ((txt = BF.readLine()) != null) {
          qfjg =txt.split(regex2);
               jg=qfjg[qfjg.length-2];

        }




    } catch (Exception e) {
        e.printStackTrace();
    }


    return jg;
}

4.将该单词的中文字符串取每一个字与所读取的中文比较若相同则选取

public    void bjdaan(ArrayList<String> daan,String xuanxiang)
{    String s2=null;

    for (int i = 0; i <xuanxiang.length() ; i++) {
        int x=0;
        String s1=xuanxiang.substring(i,i+1);

        for (int y = 0; y < 4; y++, x++) {
            if (x < daan.size()) {
                s2 = daan.get(x);
                if (s2.contains(s1)) {
                    if (x == 1) {
                        System.out.println("机器预选:第二个");
                        i=100;
                        break;
                    } else if (x == 0) {
                        System.out.println("机器预选:第一个");
                        i=100;
                        break;

                    } else if (x == 2) {
                        System.out.println("机器预选:第三个");
                        i=100;
                        break;

                    } else if (x == 3) {
                        System.out.println("机器预选:第四个");
                        i=100;
                        break;
                    } else {
                        System.out.println("没翻译出来!");

                    }

                }

            }

        }

        }
    }

总结

在进行MD5加密时不要使用

return new BigInteger(1, md.digest()).toString(16);

可能会导致前置缺少零

这种方式笔者发现正确率只有百分之90几有好的想法希望大家多多留言

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值