您的位置:首页 > 其它

.Net平台上繁簡互轉

2013-11-20 10:37 746 查看
就目前已知在.Net平台上繁簡互轉的作法大約有5種

1. Microsoft.VisualBasic.dll

.Net平台內建可以直接參考使用,效率三級。

2. Microsoft Visual Studio International Pack 1.0 SR1

微軟官方所出的官方套件,需要另外安裝,目前不支援 VS 2008 以上的版本,官方網站下載後無法在VS 2008 以上的版本安裝,需要另外在網路上尋找 ChineseConverter.dll 加入參考,效率二級。

3. Microsoft.Office.Interop.Word.dll(Office 2010 Ver.14.0.4762.1000)

系統上若裝了 Office 就有,唯一提供繁簡詞意互轉的套件,但是轉換效能就不那麼漂亮,效率五級。

4. OS Kernel LCMapString

什麼都不用裝,直接使用系統內核kernel32.dll 提供的LCMapString 來進行轉換,效率一級。

5.http://opencc.byvoid.com/ 開源的,可以避免 著作等繁體轉換錯誤。

測試結果數據如下︰

VisualBasic Convert︰15.9458 ms

Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter︰3.5011 ms

Microsoft.Office.Interop.Word︰10082.5081 ms

Kernel32 LCMapString︰1.5212 ms

相關的程式碼︰

01
///
<summary>
02
///
使用系統 kernel32.dll 進行轉換
03
///
</summary>
04
private
const
int
LocaleSystemDefault
= 0x0800;
05
private
const
int
LcmapSimplifiedChinese
= 0x02000000;
06
private
const
int
LcmapTraditionalChinese
= 0x04000000;
07
08
[DllImport(
"kernel32"
,
CharSet = CharSet.Auto,SetLastError =
true
)]
09
private
static
extern
int
LCMapString(
int
locale,
int
dwMapFlags,
string
lpSrcStr,
int
cchSrc,
10
  
[Out]
string
lpDestStr,
int
cchDest);
11
12
public
static
string
ToSimplified(
string
argSource)
13
{
14
var
t =
new
String(
'
'
,
argSource.Length);
15
LCMapString(LocaleSystemDefault,
LcmapSimplifiedChinese,argSource,argSource.Length,t,argSource.Length);
16
return
t;
17
}
18
19
public
static
string
ToTraditional(
string
argSource)
20
{
21
var
t =
new
String(
'
'
,
argSource.Length);
22
LCMapString(LocaleSystemDefault,
LcmapTraditionalChinese,argSource,argSource.Length,t,argSource.Length);
23
return
t;
24
}
25
26
///
<summary>
27
///
使用 Office Word (Microsoft.Office.Interop.Word) 進行轉換
28
///
</summary>
29
public
static
string
ConvertUsingWord(
string
argSource,
bool
argIsCht)
30
{
31
var
doc =
new
Document();
32
doc.Content.Text
= argSource;
33
doc.Content.TCSCConverter(
34
argIsCht
35
?
WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC
36
:
WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC,
true
,
true
);
37
var
ret = doc.Content.Text;
38
object
saveChanges
=
false
;
39
object
originalFormat
= Missing.Value;
40
object
routeDocument
= Missing.Value;
41
doc.Close(
ref
saveChanges,
ref
originalFormat,
ref
routeDocument);
42
return
ret;
43
}
測式碼︰

view
source

print?

01
var
i = 1000;
02
var
sw =
new
System.Diagnostics.Stopwatch();
03
sw.Reset();
04
sw.Start();
05
while
(--i
> 0)
06
{
07
Strings.StrConv(
"她來聽我 的演唱會 在十七歲的初戀 第一次約會,繁轉簡<br
/>"
,
VbStrConv.SimplifiedChinese,2052);
08
Strings.StrConv(
"她来听我 的演唱会 在十七岁的初恋 第一次约会,簡轉繁<br
/>"
,
VbStrConv.TraditionalChinese,2052);
09
}
10
11
sw.Stop();
12
Response.Write(
string
.Format(
"<br
/>VisualBasic Convert︰{0}<br />"
,
13
 
sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)));
14
i
= 1000;
15
sw.Reset();
16
sw.Start();
17
while
(--i
> 0)
18
{
19
ChineseConverter.Convert(
"她來聽我 的演唱會 在十七歲的初戀 第一次約會,繁轉簡<br
/>"
,
20
 
ChineseConversionDirection.TraditionalToSimplified);
21
ChineseConverter.Convert(
"她来听我 的演唱会 在十七岁的初恋 第一次约会,簡轉繁<br
/>"
,
22
 
ChineseConversionDirection.SimplifiedToTraditional);
23
}
24
sw.Stop();
25
Response.Write(
26
string
.Format(
27
"<br
/>Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter︰{0}<br />"
,
28
sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)));
29
30
i
= 100;
31
sw.Reset();
32
sw.Start();
33
while
(--i
> 0)
34
{
35
36
ConvertUsingWord(
"她來聽我 的演唱會 在十七歲的初戀 第一次約會,繁轉簡<br
/>"
,
true
);
37
ConvertUsingWord(
"她来听我 的演唱会 在十七岁的初恋 第一次约会,簡轉繁<br
/>"
,
false
);
38
}
39
sw.Stop();
40
Response.Write(
string
.Format(
"<br
/>Microsoft.Office.Interop.Word︰{0}<br />"
,
41
 
sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)));
42
i
= 1000;
43
sw.Reset();
44
sw.Start();
45
while
(--i
> 0)
46
{
47
48
ToSimplified(
"她來聽我 的演唱會 在十七歲的初戀 第一次約會,繁轉簡<br
/>"
);
49
ToTraditional(
"她来听我 的演唱会 在十七岁的初恋 第一次约会,簡轉繁<br
/>"
);
50
}
51
sw.Stop();
52
Response.Write(
string
.Format(
"<br
/>kernel32 LCMapString︰{0}<br />"
,
53
 
sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: