Exemplo n.º 1
0
 /// <summary>
 /// Parses a single text run (plain text), replacing regex matches with Chinese text ranges.
 /// </summary>
 /// <param name="strRun">The text to parse.</param>
 /// <param name="re">The regex to use.</param>
 /// <param name="mtr">An object matching the regex, used to translate into correct Chinese run.</param>
 /// <returns>The new runs that shall replace the one whose text we parsed.</returns>
 private List<TextRun> parseRun(string strRun, Regex re, MatchTranslator mtr)
 {
     List<TextRun> res = new List<TextRun>();
     // Find regex's matches in inpout
     var matches = re.Matches(strRun);
     int start = 0;
     foreach (Match m in matches)
     {
         // Anything before match's start and the last character we looked at?
         // If yes, it becomes a Latin run
         string textBefore = strRun.Substring(start, m.Index - start).Trim();
         if (textBefore != string.Empty) res.Add(new TextRunLatin(textBefore));
         // Translate match into a Chinese run
         res.Add(mtr.Translate(m.Groups, normalizePinyin));
         // Move finger
         start = m.Index + m.Length;
     }
     // Trailing text at end? That's a final Latin range.
     if (start < strRun.Length)
     {
         string textAfter = strRun.Substring(start, strRun.Length - start).Trim();
         if (textAfter != string.Empty) res.Add(new TextRunLatin(textAfter));
     }
     // Donee
     return res;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a single text run (plain text), replacing regex matches with Chinese text ranges.
        /// </summary>
        /// <param name="strRun">The text to parse.</param>
        /// <param name="re">The regex to use.</param>
        /// <param name="mtr">An object matching the regex, used to translate into correct Chinese run.</param>
        /// <returns>The new runs that shall replace the one whose text we parsed.</returns>
        private static List <TextRun> parseRun(string strRun, Regex re, MatchTranslator mtr)
        {
            List <TextRun> res = new List <TextRun>();
            // Find regex's matches in inpout
            var matches = re.Matches(strRun);
            int start   = 0;

            foreach (Match m in matches)
            {
                // Anything before match's start and the last character we looked at?
                // If yes, it becomes a Latin run
                string textBefore = strRun.Substring(start, m.Index - start).Trim();
                if (textBefore != string.Empty)
                {
                    res.Add(new TextRunLatin(textBefore));
                }
                // Translate match into a Chinese run
                res.Add(mtr.Translate(m.Groups, normalizePinyin));
                // Move finger
                start = m.Index + m.Length;
            }
            // Trailing text at end? That's a final Latin range.
            if (start < strRun.Length)
            {
                string textAfter = strRun.Substring(start, strRun.Length - start).Trim();
                if (textAfter != string.Empty)
                {
                    res.Add(new TextRunLatin(textAfter));
                }
            }
            // Donee
            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Split a list of runs into possibly more runs, by running a specific Chinese recognizer.
        /// </summary>
        private static List <TextRun> splitRuns(List <TextRun> runs, Regex re, MatchTranslator mtr)
        {
            List <TextRun> res = new List <TextRun>();

            foreach (TextRun run in runs)
            {
                if (run is TextRunZho)
                {
                    res.Add(run);
                }
                else
                {
                    res.AddRange(parseRun(run.GetPlainText(), re, mtr));
                }
            }
            return(res);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Split a list of runs into possibly more runs, by running a specific Chinese recognizer.
 /// </summary>
 private List<TextRun> splitRuns(List<TextRun> runs, Regex re, MatchTranslator mtr)
 {
     List<TextRun> res = new List<TextRun>();
     foreach (TextRun run in runs)
     {
         if (run is TextRunZho) res.Add(run);
         else res.AddRange(parseRun(run.GetPlainText(), re, mtr));
     }
     return res;
 }