Exemplo n.º 1
0
        public string Convert(string input, TSChineseConverterDirection direction, TSChineseDictionary customDictionary = null)
        {
            // Pre-conversion using custom dictionary.
            if (customDictionary != null)
            {
                input = customDictionary.Convert(input);
            }

            // Conversion using Microsoft Word.
            string result = MSWordConvertChinese(input, direction);

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 轉換整個檔案。
        /// </summary>
        /// <param name="inFileName">來源檔名。目前僅支援 UTF-8 編碼。</param>
        /// <param name="outFileName">目的檔名。目前僅支援 UTF-8 編碼。</param>
        /// <param name="direction">轉換方向。</param>
        /// <param name="customDictionary">自訂詞彙轉換表。注意:在呼叫 WORD 轉換之前會先使用此表進行字串替換。詳細用法參見 TSChineseDictionary 類別的說明。</param>
        public void Convert(string inFileName, string outFileName, TSChineseConverterDirection direction,
                            TSChineseDictionary customDictionary = null)
        {
            if (!File.Exists(inFileName))
            {
                throw new FileNotFoundException("指定的來源檔案不存在: " + inFileName);
            }

            using (var dstFileWriter = new StreamWriter(outFileName))
            {
                using (var srcFileReader = new StreamReader(inFileName))
                {
                    string line = srcFileReader.ReadLine();
                    while (line != null)
                    {
                        string convertedLine = this.Convert(line, direction, customDictionary);
                        dstFileWriter.WriteLine(convertedLine);
                        line = srcFileReader.ReadLine();
                    }
                    srcFileReader.Close();
                    dstFileWriter.Close();
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("ChineseConverter v0.2 by Huan-Lin Tsai (2014)");

            if (args.Length < 3)
            {
                ShowUsage();
                return;
            }

            string srcFileName = args[0];
            string dstFileName = args[1];

            string direction     = args[2];
            string dirLabel      = null;
            var    convDirection = TSChineseConverterDirection.SimplifiedToTraditional;

            if ("t2s".Equals(direction, StringComparison.InvariantCultureIgnoreCase))
            {
                convDirection = TSChineseConverterDirection.TraditionalToSimplified;
                dirLabel      = "繁->簡";
            }
            else if ("s2t".Equals(direction, StringComparison.InvariantCultureIgnoreCase))
            {
                convDirection = TSChineseConverterDirection.SimplifiedToTraditional;
                dirLabel      = "簡->繁";
            }
            else
            {
                Console.WriteLine("Invalid conversion direction: " + direction);
                Console.WriteLine("Use 't2s' or 's2t'.");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("輸入檔案: {0}", srcFileName);
            Console.WriteLine("輸出檔案: {0}", dstFileName);
            Console.WriteLine("轉換操作: {0}", dirLabel);

            TSChineseDictionary dict = null;

            if (args.Length >= 4)
            {
                dict = new TSChineseDictionary();
                int index = 3;
                while (index < args.Length)
                {
                    string dictFileName = args[index];
                    if (!File.Exists(dictFileName))
                    {
                        Console.WriteLine("檔案不存在: " + dictFileName);
                        return;
                    }
                    dict.Load(dictFileName);
                    Console.WriteLine("使用字典: {0}", dictFileName);

                    if (dict.HasError)
                    {
                        Console.WriteLine("發現錯誤的定義,請依下方記錄訊息來修正字典檔:");
                        Console.WriteLine(dict.Logs);
                        Console.WriteLine();
                    }
                    dict.ClearLogs();

                    index++;
                }
            }

            // 執行轉換
            Console.WriteLine("\r\n正在轉換.....");

            var converter = new TSChineseConverter();

            try
            {
                converter.Convert(srcFileName, dstFileName, convDirection, dict);
                Console.WriteLine();
                Console.WriteLine("轉換完畢!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("轉換時發生錯誤: ");
                Console.WriteLine(ex.Message);
            }
            finally
            {
                converter.Dispose();
            }
        }
Exemplo n.º 4
0
        public string ToSimplifiedChinese(string input, TSChineseDictionary customDictionary = null)
        {
            string result = this.Convert(input, TSChineseConverterDirection.TraditionalToSimplified, customDictionary);

            return(result);
        }
Exemplo n.º 5
0
 public void ToTraditionalChinese(string inFileName, string outFileName, TSChineseDictionary customDictionary = null)
 {
     this.Convert(inFileName, outFileName, TSChineseConverterDirection.SimplifiedToTraditional, customDictionary);
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // Setup DI
            var serviceProvider = new ServiceCollection() // Microsoft.Extensions.DependencyInjection
                                  .AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true))
                                  .BuildServiceProvider();

            // Create logger
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.File("d:/0/1.log", Serilog.Events.LogEventLevel.Information)
                         .CreateLogger();

            Log.Information("MergePhrase 應用程式開始執行。");

            Console.WriteLine("ChineseConverter v0.3 by Huan-Lin Tsai (2022)");

            if (args.Length < 3)
            {
                ShowUsage();
                return;
            }

            string srcFileName = args[0];
            string dstFileName = args[1];

            string direction     = args[2];
            string dirLabel      = null;
            var    convDirection = TSChineseConverterDirection.SimplifiedToTraditional;

            if ("t2s".Equals(direction, StringComparison.InvariantCultureIgnoreCase))
            {
                convDirection = TSChineseConverterDirection.TraditionalToSimplified;
                dirLabel      = "繁->簡";
            }
            else if ("s2t".Equals(direction, StringComparison.InvariantCultureIgnoreCase))
            {
                convDirection = TSChineseConverterDirection.SimplifiedToTraditional;
                dirLabel      = "簡->繁";
            }
            else
            {
                Console.WriteLine("Invalid conversion direction: " + direction);
                Console.WriteLine("Use 't2s' or 's2t'.");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("輸入檔案: {0}", srcFileName);
            Console.WriteLine("輸出檔案: {0}", dstFileName);
            Console.WriteLine("轉換操作: {0}", dirLabel);

            TSChineseDictionary dict = null;

            if (args.Length >= 4)
            {
                dict = new TSChineseDictionary(Log.Logger);
                int index = 3;
                while (index < args.Length)
                {
                    string dictFileName = args[index];
                    if (!File.Exists(dictFileName))
                    {
                        Console.WriteLine("檔案不存在: " + dictFileName);
                        return;
                    }
                    dict.Load(dictFileName);
                    Console.WriteLine("使用字典: {0}", dictFileName);

                    if (dict.HasError)
                    {
                        Console.WriteLine("處理過程中發現錯誤或值得注意的事項,請閱讀 log 檔案以了解細節。");
                        Console.WriteLine();
                    }
                    dict.Reset();

                    index++;
                }
            }

            // 執行轉換
            Console.WriteLine("\r\n正在轉換.....");

            var converter = new TSChineseConverter();

            try
            {
                converter.Convert(srcFileName, dstFileName, convDirection, dict);
                Console.WriteLine();
                Console.WriteLine("轉換完畢!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("轉換時發生錯誤: ");
                Console.WriteLine(ex.Message);
            }
        }