| Dim dictionary_path, TSCharacters, TSPhrases, STCharacters, STPhrases |
| dictionary_path = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) |
| TSCharacters = ReadText(dictionary_path + "TSCharacters.txt") |
| TSPhrases = ReadText(dictionary_path + "TSPhrases.txt") |
| STCharacters = ReadText(dictionary_path + "STCharacters.txt") |
| STPhrases = ReadText(dictionary_path + "STPhrases.txt") |
| |
| |
| Call Test() |
| Function Test() |
| Dim str1, str2 |
| str1 = "中华人民共和国是工人阶级领导的、以工农联盟为基础的人民****的社会主义国家。" |
| str1 = str1 & vbCrLf & "干活 干杯 西太后 后天" |
| str1 = str1 & vbCrLf & "划过来" |
| str2 = "中華人民共和國是工人階級領導的、以工農聯盟爲基礎的人民**專政的社會主義国家。" |
| str2 = str2 & vbCrLf & "幹活 乾杯 西太后 後天" |
| str2 = str2 & vbCrLf & "划過來" |
| |
| WScript.Echo Now() & vbTab & " TC2SC() " |
| WScript.Echo Now() & VbCrLf & str1 & vbCrLf & " ==> " |
| WScript.Echo Now() & VbCrLf & SC2TC(str1) & vbCrLf & vbCrLf |
| |
| WScript.Echo Now() & vbTab & " TC2SC() " |
| WScript.Echo Now() & VbCrLf & str2 & vbCrLf & " ==> " |
| WScript.Echo Now() & VbCrLf & TC2SC(str2) |
| End Function |
| |
| |
| |
| Function isInitDic() |
| isInit = Not (STCharacters="" Or STPhrases="" Or TSCharacters="" Or TSPhrases="") |
| If Not isInit Then WScript.Echo("正在载入词库,请稍等……") |
| isInitDic = isInit |
| End Function |
| |
| |
| Function SC2TC(str) |
| If (isInitDic()) Then |
| SC2TC = TCSCConverter(STPhrases & vbLf & STCharacters, str) |
| Else |
| SC2TC = "" |
| End If |
| End Function |
| |
| |
| Function TC2SC(str) |
| If (isInitDic()) Then |
| TC2SC = TCSCConverter(TSPhrases & vbLf & TSCharacters, str) |
| Else |
| TC2SC = "" |
| End If |
| End Function |
| |
| |
| Function TCSCConverter(strDictionaryOpenCC, strSrc) |
| |
| arr_Phrases = Split(strDictionaryOpenCC, vbLf) |
| Call OpenCC_Dic_Sort(arr_Phrases) |
| |
| |
| Dim i, arr_find() |
| ReDim Preserve arr_find(0) |
| For i = 0 To UBound(arr_Phrases) |
| If (InStr(arr_Phrases(i), vbTab) > 1) And (Len(arr_Phrases(i)) >=3) Then |
| Dim str_SrcPhrases, str_DesPhrases |
| str_SrcPhrases = Split(arr_Phrases(i), vbTab)(0) |
| str_DesPhrases = Split(arr_Phrases(i), vbTab)(1) |
| If (InStr(strSrc, str_SrcPhrases) > 0) And (str_SrcPhrases <> "" ) Then |
| ReDim Preserve arr_find(UBound(arr_find)+1) |
| If (InStr(str_DesPhrases, " ") > 1) And (Len(str_DesPhrases) >= 3) Then |
| arr_find(UBound(arr_find)) = "<[?" & UBound(arr_find) & "?]>" & vbTab & Split(str_DesPhrases," ")(0) |
| Else |
| arr_find(UBound(arr_find)) = "<[?" & UBound(arr_find) & "?]>" & vbTab & str_DesPhrases |
| End If |
| strSrc = Replace(strSrc, str_SrcPhrases, "<[?" & UBound(arr_find) & "?]>") |
| End If |
| End If |
| Next |
| |
| |
| For i = 0 To UBound(arr_find) |
| If (InStr(arr_find(i), vbTab) > 1) And (Len(arr_find(i)) >=8) Then |
| |
| |
| If (InStr(strSrc, Split(arr_find(i), vbTab)(0)) > 0) Then |
| If Split(arr_find(i), vbTab)(1) <> "" Then |
| strSrc = Replace(strSrc, Split(arr_find(i), vbTab)(0), Split(arr_find(i), vbTab)(1)) |
| End If |
| End If |
| |
| End If |
| Next |
| |
| TCSCConverter = strSrc |
| End Function |
| |
| |
| Function OpenCC_Dic_Sort(ByRef arr) |
| Dim arrTable() |
| ReDim Preserve arrTable(UBound(arr),1) |
| Dim intRow |
| For intRow = 0 To UBound(arr) |
| arrTable(intRow,0) = Len(arr(intRow)) |
| arrTable(intRow,1) = arr(intRow) |
| Next |
| Call arrSort(arrTable, 0) |
| For intRow = 0 To UBound(arr) |
| arr(intRow) = arrTable(intRow,1) |
| Next |
| End Function |
| |
| |
| |
| Function arrSort(ByRef arr, ByVal intSortField) |
| Const adOpenStatic = 3 |
| Const adUseClient = 3 |
| Const adDouble = 5 |
| Const adVarChar = 200 |
| Const adLongVarWChar = 203 |
| Set rs = CreateObject("ADODB.Recordset") |
| rs.CursorLocation = adUseClient |
| |
| Dim intRow, intCol |
| |
| For intCol = 0 To UBound(arr, 2) |
| If intCol = intSortField Then |
| rs.Fields.append intCol, adDouble |
| Else |
| rs.Fields.append intCol, adLongVarWChar, 1024 |
| End If |
| Next |
| rs.CursorType = adOpenStatic |
| rs.Open |
| |
| |
| For intRow = 0 To UBound(arr, 1) |
| rs.AddNew |
| For intCol = 0 To UBound(arr, 2) |
| If intCol = intSortField Then |
| rs(intCol) = CDBl(arr(intRow, intCol)) |
| Else |
| rs(intCol) = arr(intRow, intCol) |
| End If |
| Next |
| rs.Update |
| Next |
| |
| |
| rs.Sort = rs(intSortField).Name & " DESC" |
| rs.MoveFirst |
| |
| For intRow = 0 To UBound(arr, 1) |
| For intCol = 0 To UBound(arr, 2) |
| arr(intRow, intCol) = rs(intCol) |
| Next |
| rs.MoveNext |
| Next |
| End Function |
| |
| |
| |
| Function ReadText(FileName) |
| ReadText = Pfile(FileName, "utf-8", "ForReading", "") |
| End Function |
| Function SaveText(FileName, TextString) |
| SaveText = Pfile(FileName, "utf-8", "ForWriting", TextString) |
| End Function |
| Function SaveWSH(FileName, TextString) |
| SaveWSH = Pfile(FileName, "Unicode", "ForWriting", TextString) |
| End Function |
| Function LogText(FileName, TextString) |
| LogText = Pfile(FileName, "utf-8", "ForAppending", TextString) |
| End Function |
| Function Pfile(FileName, FileCode, strType, TextString) |
| Set fso = CreateObject("Scripting.FileSystemObject") |
| Set objStream = CreateObject("ADODB.Stream") |
| objStream.Type = 2 |
| objStream.Mode = 3 |
| objStream.Charset = FileCode |
| If (fso.FileExists(FileName)) Then |
| objStream.Open() |
| objStream.LoadFromFile FileName |
| if (strType = "ForReading") Then TextString = objStream.ReadText() |
| if (strType = "ForAppending") Then TextString = TextString & objStream.ReadText() |
| objStream.Close() |
| End If |
| If (strType = "ForWriting") Or (strType ="ForAppending") Then |
| objStream.Open() |
| objStream.WriteText TextString |
| objStream.SaveToFile FileName, 2 |
| objStream.Close() |
| End If |
| Set objStream = Nothing |
| If (strType = "ForReading") Then Pfile = TextString |
| If (strType = "ForWriting") Or (strType = "ForAppending") Then Pfile = True |
| End FunctionCOPY |