Esempio n. 1
0
        /// <summary>
        /// Checks if the specified phonetic shape node matches this boundary context.
        /// </summary>
        /// <param name="node">The phonetic shape node.</param>
        /// <param name="dir">The direction.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="instantiatedVars">The instantiated variables.</param>
        /// <returns>All matches.</returns>
        internal override IList <Match> Match(PhoneticShapeNode node, Direction dir, ModeType mode,
                                              VariableValues instantiatedVars)
        {
            // only match boundaries in synthesis mode
            if (mode == ModeType.SYNTHESIS)
            {
                switch (node.Type)
                {
                case PhoneticShapeNode.NodeType.BOUNDARY:
                    Boundary bdry = node as Boundary;
                    // check if string representations match
                    if (m_bdryDef.StrRep != bdry.BoundaryDefinition.StrRep)
                    {
                        return(new List <Match>());
                    }
                    // move to next node
                    IList <Match> matches = MatchNext(node, dir, mode, instantiatedVars);
                    foreach (Match match in matches)
                    {
                        match.Add(node, m_partition);
                    }
                    return(matches);

                case PhoneticShapeNode.NodeType.MARGIN:
                    Margin margin = node as Margin;
                    if (dir == margin.MarginType)
                    {
                        // we are at the end of the phonetic shape, so it does not match
                        return(new List <Match>());
                    }
                    else
                    {
                        return(Match(node.GetNext(dir), dir, mode, instantiatedVars));
                    }
                }

                return(new List <Match>());
            }
            else
            {
                PhoneticPatternNode n = GetNext(dir);
                if (n == null)
                {
                    // this was the last node in the pattern, so we have a match
                    List <Match> matches = new List <Match>();
                    matches.Add(new Match(instantiatedVars));
                    return(matches);
                }
                else
                {
                    return(n.Match(node, dir, mode, instantiatedVars));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if the specified phonetic shape node matches this nested phonetic pattern.
        /// </summary>
        /// <param name="node">The phonetic shape node.</param>
        /// <param name="dir">The direction.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="instantiatedVars">The instantiated variables.</param>
        /// <returns>All matches.</returns>
        internal override IList <Match> Match(PhoneticShapeNode node, Direction dir, ModeType mode,
                                              VariableValues instantiatedVars)
        {
            Set <Match> candidates = new Set <Match>(new MatchComparer());
            Queue <KeyValuePair <Match, int> > queue = new Queue <KeyValuePair <Match, int> >();
            IList <Match> firstMatches;

            // collect first set of matches and add them to the queue
            m_pattern.IsMatch(node, dir, mode, instantiatedVars, out firstMatches);
            foreach (Match match in firstMatches)
            {
                if (match.EntireMatch.Count == 0)
                {
                    continue;
                }

                // only add it to the list of candidates if it long enough
                if (m_minOccur <= 1)
                {
                    if (!candidates.Contains(match))
                    {
                        candidates.Add(match);
                        queue.Enqueue(new KeyValuePair <Match, int>(match, 1));
                    }
                }
                else
                {
                    queue.Enqueue(new KeyValuePair <Match, int>(match, 1));
                }
            }

            while (queue.Count > 0)
            {
                KeyValuePair <Match, int> pair = queue.Dequeue();
                // if we hit upper limit then do not process this match any longer
                if (m_maxOccur > -1 && pair.Value >= m_maxOccur)
                {
                    continue;
                }

                IList <PhoneticShapeNode> nodes = pair.Key.EntireMatch;
                PhoneticShapeNode         n     = nodes[nodes.Count - 1].GetNext(dir);

                IList <Match> curMatches;
                m_pattern.IsMatch(n, dir, mode, pair.Key.VariableValues, out curMatches);
                foreach (Match match in curMatches)
                {
                    if (match.EntireMatch.Count == 0)
                    {
                        continue;
                    }

                    for (PhoneticShapeNode curNode = nodes[nodes.Count - 1]; curNode != nodes[0].GetPrev(dir); curNode = curNode.GetPrev(dir))
                    {
                        match.Add(curNode);
                    }

                    // only add to the list of candidates if it is long enough
                    if (m_minOccur <= pair.Value + 1)
                    {
                        if (!candidates.Contains(match))
                        {
                            candidates.Add(match);
                            queue.Enqueue(new KeyValuePair <Match, int>(match, pair.Value + 1));
                        }
                    }
                    else
                    {
                        queue.Enqueue(new KeyValuePair <Match, int>(match, pair.Value + 1));
                    }
                }
            }

            // iterate thru list of candidates and see if they match the rest of the pattern
            List <Match> matches = new List <Match>();

            foreach (Match candidate in candidates)
            {
                IList <PhoneticShapeNode> nodes      = candidate.EntireMatch;
                IList <Match>             curMatches = MatchNext(nodes[nodes.Count - 1], dir, mode, candidate.VariableValues);
                foreach (Match match in curMatches)
                {
                    for (PhoneticShapeNode curNode = nodes[nodes.Count - 1]; curNode != nodes[0].GetPrev(dir); curNode = curNode.GetPrev(dir))
                    {
                        match.Add(curNode, m_partition);
                    }
                    matches.Add(match);
                }
            }

            // finally, if this pattern can occur 0 times, then collect any 0 length matches
            if (m_minOccur == 0)
            {
                PhoneticPatternNode n = GetNext(dir);
                if (n == null)
                {
                    matches.Add(new Match(instantiatedVars));
                }
                else
                {
                    matches.AddRange(n.Match(node, dir, mode, instantiatedVars));
                }
            }
            matches.Sort();

            return(matches);
        }