极速翻译器-有道 重制版

划词翻译

依赖beautifultootip库:https://github.com/telppa/BeautifulToolTip

影响剪贴板

 

 1 #SingleInstance Force
 2 SetBatchLines, -1
 3 CoordMode, ToolTip, Screen
 4 
 5 #Persistent
 6 OnClipboardChange("ClipChanged")
 7 return
 8 #NoEnv
 9 SendMode Input
10 SetWorkingDir %A_ScriptDir%
11 Thread, Interrupt ,0
12 
13 ; text = Whether 60 or 16, there is in every human being’s heart the lure of wonders, the unfailing appetite for what’s next and the joy of the game of living. In the center of your heart and my heart, there is a wireless station so long as it receives messages of beauty, hope, courage and power from man and from the infinite, so long as you are young.When your aerials are down, and your spirit is covered with snows of cynicism and the ice of pessimism, then you’ve grown old, even at 20 but as long as your aerials are up, to catch waves of optimism, there’s hope you may die young at 80.
14 ; msgbox % youdao_translation(remove_punctuation(text))
15 ~LButton Up::
16 Send ^{c}
17 ClipChanged(){
18 text = % youdao_translation(remove_punctuation(Clipboard))
19 ;单行宽度
20 If (StrLen(text)>=70){
21     i := 1
22     Loop, % StrLen(text)/70+1{
23         text1 .= SubStr(text, i, 70)
24         text1 .= "`n"
25         i += 70
26     }
27 
28 }
29 
30     Else text1 := text
31 
32 
33 ret := btt(text1,,,,,{JustCalculateSize:1})
34 btt(text1,(1920-ret.w)/2,1015-ret.h,,"Style2")
35 ; ret := btt(text,,,,,{JustCalculateSize:1})
36 ; MsgBox, % "宽:" ret.w " 高:" ret.h
37 Return
38 }
39 
40 
41 
42 ;翻译主函数
43 youdao_translation(text){
44     Url=http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null
45     postdata=type=AUTO&i=%text%&doctype=json&xmlVersion=1.4&keyfrom=fanyi.web&ue=UTF-8&typoResult=true&flag=false
46     youdaoreText:= byteToStr(WinHttp(Url,"POST",postdata),"utf-8")
47     NeedleRegEx=O)tgt":"(.*?)"
48     FoundPos:=RegExMatch(youdaoreText,NeedleRegEx,OutMatch)
49     youdaoValue:=(ErrorLevel) ? :OutMatch.Value(1)
50     NeedleRegEx=O)entries":["","(.*?)"
51     FoundPos:=RegExMatch(youdaoreText,NeedleRegEx,OutMatch)
52     youdaoValue.=(! ErrorLevel and OutMatch.Value(1)="") ? :
53     Return youdaoValue
54 }
55 
56 ;发送接收数据
57 WinHttp(Httpurl,Httpmode="GET",Httppostdata=""){
58     StringUpper Httpmode,Httpmode
59     ;~ XMLHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
60     XMLHTTP := ComObjCreate("Microsoft.XMLHTTP")
61     XMLHTTP.open(Httpmode,Httpurl,false)
62     XMLHTTP.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0")
63     if Httpmode=POST
64     {
65         XMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
66         XMLHTTP.send(Httppostdata)
67     }else
68         XMLHTTP.send()
69     ;~ return XMLHTTP.responseText
70     return XMLHTTP.ResponseBody
71 }
72  
73 ;将原始数据流以指定的编码的形式读出
74 byteToStr(body, charset){
75     Stream := ComObjCreate("Adodb.Stream")
76     Stream.Type := 1
77     Stream.Mode := 3
78     Stream.Open()
79     Stream.Write(body)
80     Stream.Position := 0
81     Stream.Type := 2
82     Stream.Charset := charset
83     str := Stream.ReadText()
84     Stream.Close()
85     return str
86 }
87 
88 ;去除文本中的标点
89 remove_punctuation(text){
90     punctuation := [",", ".", "!", "`n", "?", """", ";"]
91     For k, v in punctuation{
92         text1 := StrReplace(text, v, " ")
93         text := text1
94     }
95     Return text
96 }

 

posted @ 2022-09-22 19:57  c/  阅读(175)  评论(0编辑  收藏  举报