前不久寫了一篇簡繁轉換的文章,其中有提到另一種簡繁轉換的作法為呼叫Word的簡繁轉換引擎來做轉換,透過Word跟上次那篇透過kernel32.dll的轉換方式不同,kernel32.dll目前只能做到逐字轉換,並沒有辦法做到辭意的轉換,也就是說他並不會幫我們把"電腦"翻譯成"計算機",但透過Word來轉則會進行詞的轉換。
前不久寫了一篇簡繁轉換的文章,其中有提到另一種簡繁轉換的作法為呼叫Word的簡繁轉換引擎來做轉換,透過Word跟上次那篇透過kernel32.dll的轉換方式不同,kernel32.dll目前只能做到逐字轉換,並沒有辦法做到辭意的轉換,也就是說他並不會幫我們把"電腦"翻譯成"計算機",但透過Word來轉則會進行詞的轉換。
透過kernel32.dll的好處是不用安裝額外的元件,但缺點事不能轉詞;透過Word的好處是可以轉詞,但需要在機器上安裝office。
今天簡單的玩了一下,其實還蠻簡單的,就這兩個function,我沒有寫註解,也沒有改裡面的內容,這段code我是從這個網址copy過來用的,裡頭的程式其實我還沒完全看懂,因為MSDN上講的也不太容易明瞭,但運行的結果是沒問題的。
http://www.dotblogs.com.tw/chhuang/archive/2008/03/18/1867.aspx
使用時須先參考Interop.Microsoft.Office.Interop.Word.dll才能用。
01
public string ToSimplified(
string strSource)
02 ![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockStart.gif)
...{
03
string strResult;
04
_Application objWord = new Microsoft.Office.Interop.Word.Application();
05
object t = Missing.Value;
06
object nt = Missing.Value;
07
object dt = Missing.Value;
08
object v = true;
09
Document objDocument = objWord.Documents.Add(ref t, ref nt, ref dt, ref v);
10
objWord.Selection.TypeText(strSource);
11
objWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
12
objWord.ActiveDocument.Select();
13
strResult = objWord.Selection.Text;
14
object sc = 0;
15
object of = Missing.Value;
16
object rd = Missing.Value;
17
objWord.Quit(ref sc, ref of, ref rd);
18
objDocument = null;
19
objWord = null;
20
return strResult;
21
} 22
public string ToTraditional(
string strSource)
23 ![](http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif)
...{
24
string strResult;
25
_Application objWord = new Microsoft.Office.Interop.Word.Application();
26
object t = Missing.Value;
27
object nt = Missing.Value;
28
object dt = Missing.Value;
29
object v = true;
30
Document objDocument = objWord.Documents.Add(ref t, ref nt, ref dt, ref v);
31
objWord.Selection.TypeText(strSource);
32
objWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
33
objWord.ActiveDocument.Select();
34
strResult = objWord.Selection.Text;
35
object sc = 0;
36
object of = Missing.Value;
37
object rd = Missing.Value;
38
objWord.Quit(ref sc, ref of, ref rd);
39
objDocument = null;
40
objWord = null;
41
return strResult;
42
} 這是運行的結果,果然可以將電腦轉成計算機,當然反過來轉也是沒問題的:
![簡繁 簡繁](https://dotblogsfile.blob.core.windows.net/user/jimmyyu/0904/CWord_12A68/%E7%B0%A1%E7%B9%81_thumb.jpg)
![](https://az787680.vo.msecnd.net/user/gipi/5073c299-25e4-46d9-97a5-8bb3ec338e27/1448671106_93373.png) |
游舒帆 (gipi)
探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。
|