예제 #1
0
        public static void SolveFromFile(string inputFile, string outputFile, string modelName)
        {
            var model  = ModelLoader.LoadByName(modelName);
            var method = new NGramInputMethod(model);

            using (var outputWriter = File.CreateText(outputFile))
            {
                var lineCount = 0;
                foreach (string input in File.ReadLines(inputFile))
                {
                    method.Clear();
                    var pinyins = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    SolveAndWritePinyin(pinyins, method, outputWriter);
                    ++lineCount;
                    if (lineCount % 10 == 0)
                    {
                        Console.Error.WriteLine($"Converted {lineCount} lines");
                    }
                }
                Console.Error.WriteLine($"Done converting pinyin, {lineCount} lines in total.");
            }
        }
예제 #2
0
 private static void SolveAndWritePinyin(string[] pinyins, NGramInputMethod method, TextWriter writer)
 {
     try
     {
         foreach (string pinyin in pinyins)
         {
             method.Input(pinyin);
         }
     }
     catch (InvalidOperationException e)
     {
         e.ToString(); // make compiler happy
         Console.Error.WriteLine("在翻译\"" + string.Join(" ", pinyins) + "\"时发生错误,未能获取完整结果");
         var bestAnswer = method.NowBestAnswer;
         if (bestAnswer != null)
         {
             writer.WriteLine(bestAnswer + new string('?', pinyins.Length - bestAnswer.Length));
         }
         return;
     }
     writer.WriteLine(method.Results.First());
 }
예제 #3
0
        public static void InteractiveSolve(string modelName)
        {
            NGramBase model  = ModelLoader.LoadByName(modelName);
            var       method = new NGramInputMethod(model)
            {
                PrintDistributeSize = PersistentConfiguration.CandidatesEachStep,
            };

            Console.WriteLine($"Using {method.Name}");

            while (true)
            {
                Console.Write("Pinyin > ");
                var raw = Console.ReadLine();
                if (raw == null)
                {
                    break;
                }
                var input = raw.Trim();
                method.Clear();
                var pinyins = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                SolveAndWritePinyin(pinyins, method, Console.Out);
            }
        }