Example #1
0
            /// <summary>
            /// Applies the given phoneme expression to all phonemes in this phoneme builder.
            /// <para/>
            /// This will lengthen phonemes that have compatible language sets to the expression, and drop those that are
            /// incompatible.
            /// </summary>
            /// <param name="phonemeExpr">The expression to apply.</param>
            /// <param name="maxPhonemes">The maximum number of phonemes to build up.</param>
            public void Apply(IPhonemeExpr phonemeExpr, int maxPhonemes)
            {
                // LUCENENET NOTE: LinkedHashSet cares about insertion order - in .NET, we can just use List<T> for that
                IList <Phoneme> newPhonemes = new List <Phoneme>(maxPhonemes);

                //EXPR_continue:
                foreach (Phoneme left in this.phonemes)
                {
                    foreach (Phoneme right in phonemeExpr.Phonemes)
                    {
                        LanguageSet languages = left.Languages.RestrictTo(right.Languages);
                        if (!languages.IsEmpty)
                        {
                            Phoneme join = new Phoneme(left, right, languages);
                            if (newPhonemes.Count < maxPhonemes)
                            {
                                newPhonemes.Add(join);
                                if (newPhonemes.Count >= maxPhonemes)
                                {
                                    goto EXPR_break;
                                }
                            }
                        }
                    }
                }
                EXPR_break : { }

                this.phonemes.Clear();
                // LUCENENET: We need to filter out any duplicates, since we converted from LinkedHashSet
                // to List.
                this.phonemes.AddRange(newPhonemes.Where(x => !phonemes.Any(y => y.Equals(x))));
            }
Example #2
0
 /// <summary>
 /// Creates a new rule.
 /// </summary>
 /// <param name="pattern">The pattern.</param>
 /// <param name="lContext">The left context.</param>
 /// <param name="rContext">The right context.</param>
 /// <param name="phoneme">The resulting phoneme.</param>
 public Rule(string pattern, string lContext, string rContext, IPhonemeExpr phoneme)
 {
     this.pattern  = pattern;
     this.lContext = GetPattern(lContext + "$");
     this.rContext = GetPattern("^" + rContext);
     this.phoneme  = phoneme;
 }
Example #3
0
            /// <summary>
            /// Applies the given phoneme expression to all phonemes in this phoneme builder.
            /// <para/>
            /// This will lengthen phonemes that have compatible language sets to the expression, and drop those that are
            /// incompatible.
            /// </summary>
            /// <param name="phonemeExpr">The expression to apply.</param>
            /// <param name="maxPhonemes">The maximum number of phonemes to build up.</param>
            public void Apply(IPhonemeExpr phonemeExpr, int maxPhonemes)
            {
                ISet <Phoneme> newPhonemes = new JCG.LinkedHashSet <Phoneme>(maxPhonemes);

                //EXPR_continue:
                foreach (Phoneme left in this.phonemes)
                {
                    foreach (Phoneme right in phonemeExpr.Phonemes)
                    {
                        LanguageSet languages = left.Languages.RestrictTo(right.Languages);
                        if (!languages.IsEmpty)
                        {
                            Phoneme join = new Phoneme(left, right, languages);
                            if (newPhonemes.Count < maxPhonemes)
                            {
                                newPhonemes.Add(join);
                                if (newPhonemes.Count >= maxPhonemes)
                                {
                                    goto EXPR_break;
                                }
                            }
                        }
                    }
                }
                EXPR_break : { }

                this.phonemes.Clear();
                this.phonemes.UnionWith(newPhonemes);
            }
Example #4
0
        private static IDictionary <string, IList <Rule> > ParseRules(TextReader reader, string location)
        {
            IDictionary <string, IList <Rule> > lines = new JCG.Dictionary <string, IList <Rule> >();
            int currentLine = 0;

            bool   inMultilineComment = false;
            string rawLine;

            try
            {
                while ((rawLine = reader.ReadLine()) != null)
                {
                    currentLine++;
                    string line = rawLine;

                    if (inMultilineComment)
                    {
                        if (line.EndsWith(ResourceConstants.EXT_CMT_END, StringComparison.Ordinal))
                        {
                            inMultilineComment = false;
                        }
                    }
                    else
                    {
                        if (line.StartsWith(ResourceConstants.EXT_CMT_START, StringComparison.Ordinal))
                        {
                            inMultilineComment = true;
                        }
                        else
                        {
                            // discard comments
                            int cmtI = line.IndexOf(ResourceConstants.CMT, StringComparison.Ordinal);
                            if (cmtI >= 0)
                            {
                                line = line.Substring(0, cmtI);
                            }

                            // trim leading-trailing whitespace
                            line = line.Trim();

                            if (line.Length == 0)
                            {
                                continue; // empty lines can be safely skipped
                            }

                            if (line.StartsWith(HASH_INCLUDE, StringComparison.Ordinal))
                            {
                                // include statement
                                string incl = line.Substring(HASH_INCLUDE.Length).Trim();
                                if (incl.Contains(" "))
                                {
                                    throw new ArgumentException("Malformed import statement '" + rawLine + "' in " +
                                                                location);
                                }
                                else
                                {
                                    lines.PutAll(ParseRules(CreateScanner(incl), location + "->" + incl));
                                }
                            }
                            else
                            {
                                // rule
                                string[] parts = WHITESPACE.Split(line).TrimEnd();
                                if (parts.Length != 4)
                                {
                                    throw new ArgumentException("Malformed rule statement split into " + parts.Length +
                                                                " parts: " + rawLine + " in " + location);
                                }
                                else
                                {
                                    try
                                    {
                                        string       pat   = StripQuotes(parts[0]);
                                        string       lCon  = StripQuotes(parts[1]);
                                        string       rCon  = StripQuotes(parts[2]);
                                        IPhonemeExpr ph    = ParsePhonemeExpr(StripQuotes(parts[3]));
                                        int          cLine = currentLine;
                                        Rule         r     = new RuleAnonymousClass(pat, lCon, rCon, ph, cLine, location);

                                        string patternKey = r.pattern.Substring(0, 1 - 0);
                                        if (!lines.TryGetValue(patternKey, out IList <Rule> rules) || rules == null)
                                        {
                                            rules             = new List <Rule>();
                                            lines[patternKey] = rules;
                                        }
                                        rules.Add(r);
                                    }
                                    catch (Exception e) when(e.IsIllegalArgumentException())
                                    {
                                        throw new InvalidOperationException("Problem parsing line '" + currentLine + "' in " +
                                                                            location, e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                reader.Dispose();
            }

            return(lines);
        }
Example #5
0
 public RuleAnonymousClass(string pat, string lCon, string rCon, IPhonemeExpr ph, int cLine, string location)
     : base(pat, lCon, rCon, ph)
 {
     this.myLine = cLine;
     this.loc    = location;
 }