private static void ParseTextFile(string fname, TSChineseDictionary dict) { using (var reader = new StreamReader(fname, Encoding.UTF8)) { int lineCount = 0; string s = reader.ReadLine(); while (s != null) { if (!s.StartsWith(';')) { string[] words = s.Split('=', ','); if (words.Length >= 2) { dict.Add(words[0], words[1]); } } lineCount++; if (lineCount % 100 == 0) { Console.Write("."); } s = reader.ReadLine(); } } }
public void ConvertToSimplifiedChineseWithNestedMapping() { // 測試巢狀定義。所謂的巢狀定義指的是:一筆對應的來源字串包含於另一筆對應的目標字串。 var myDict = new TSChineseDictionary(Log.Logger); myDict.Load("NestedMappingTest.dict"); Assert.AreEqual(myDict.HasError, true); // 應該要能偵測到字典檔裡面有巢狀定義的情形。 string input = "從應用程式組態檔中讀取欲使用的類別名稱"; string expectedResult = "从应用程序配置文件中读取欲使用的类名"; var converter = new TSChineseConverter(); string result = converter.ToSimplifiedChinese(input, myDict); Assert.AreEqual(result, expectedResult); }
public void ConvertToSimplifiedChinese() { var myDict = new TSChineseDictionary(Log.Logger); myDict.Add("擴充方法=擴展方法;extension method") .Add("預設=默認=default") .Add("建構函式=構造函數") .Add("類別名稱=類名") .Add("類別=類;class"); string input = "Convert() 是一個擴充方法,它所擴充的類別名稱是 Foo。Foo 類別有提供預設建構函式。"; string expectedResult = "Convert() 是一个扩展方法,它所扩充的类名是 Foo。Foo 类有提供默认构造函数。"; var converter = new TSChineseConverter(); string result = converter.ToSimplifiedChinese(input, myDict); Assert.AreEqual(result, expectedResult); }
private static void RunOptionsAndReturnExitCode(Options opts) { // 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(opts.LogFileName, Serilog.Events.LogEventLevel.Information) .CreateLogger(); Log.Information("MergePhrase 應用程式開始執行。"); var dict = new TSChineseDictionary(Log.Logger); if (File.Exists(opts.OutputFileName)) { dict.Load(opts.OutputFileName); } dict.OverwriteExistingPhrase = opts.OverwriteExistingPhrase; foreach (var fname in opts.InputFileNames) { if (!File.Exists(fname)) { Console.WriteLine($"檔案不存在:{fname}"); continue; } Console.WriteLine($"正在處理 {fname} ..."); if (fname.EndsWith(".json")) { ParseTongWenJsonFile(fname, dict); } else { ParseTextFile(fname, dict); } } dict.Save(opts.OutputFileName); Log.Information("MergePhrase 應用程式執行完畢。"); }
private static void ParseTongWenJsonFile(string fname, TSChineseDictionary dict) { var content = File.ReadAllText(fname, Encoding.UTF8); var tongWenTable = JsonConvert.DeserializeObject <TongWenPhraseTable>(content); int lastProgress = -1; dict.Add(tongWenTable.Map, (sender, args) => { if (lastProgress != args.ProgressPercentage) { lastProgress = args.ProgressPercentage; if (lastProgress % 10 == 0) { Console.WriteLine($"{lastProgress}%"); } else { Console.Write("."); } } }); }