Пример #1
0
        public IEnumerable <Word> Apply(Word input)
        {
            var inputSet = new HashSet <Word>(FreezableEqualityComparer <Word> .Default)
            {
                input
            };
            var tempSet = new HashSet <Word>(FreezableEqualityComparer <Word> .Default);
            var results = new HashSet <Word>(FreezableEqualityComparer <Word> .Default);

            for (int i = 0; i < _rules.Count && inputSet.Count > 0; i++)
            {
                if (!_morpher.RuleSelector(_strata[i]))
                {
                    continue;
                }

                HashSet <Word> outputSet = tempSet;
                outputSet.Clear();

                foreach (Word inData in inputSet)
                {
                    foreach (Word outData in _rules[i].Apply(inData))
                    {
                        outputSet.Add(outData);
                        results.Add(outData);
                    }
                }

                tempSet  = inputSet;
                inputSet = outputSet;
            }

            return(results);
        }
        public IEnumerable <Word> Apply(Word input)
        {
            if (!input.RealizationalFeatureStruct.IsUnifiable(input.SyntacticFeatureStruct))
            {
                return(Enumerable.Empty <Word>());
            }

            var  output             = new HashSet <Word>(FreezableEqualityComparer <Word> .Default);
            bool applicableTemplate = false;

            input = ChooseInflectionalStem(input);
            for (int i = 0; i < _templateRules.Count; i++)
            {
                if (_morpher.RuleSelector(_templates[i]) &&
                    input.SyntacticFeatureStruct.IsUnifiable(_templates[i].RequiredSyntacticFeatureStruct) &&
                    !input.RootAllomorph.Morpheme.IsPartial)
                {
                    applicableTemplate = true;
                    foreach (Word outWord in _templateRules[i].Apply(input))
                    {
                        Word word          = outWord;
                        bool templateFinal = outWord.IsPartial || _templates[i].IsFinal;
                        if (word.IsLastAppliedRuleFinal != templateFinal)
                        {
                            word = outWord.DeepClone();
                            word.IsLastAppliedRuleFinal = templateFinal;
                            word.Freeze();
                        }
                        output.Add(word);
                    }
                }
            }

            if (output.Count == 0)
            {
                if (!input.IsPartial && applicableTemplate)
                {
                    if (_morpher.TraceManager.IsTracing)
                    {
                        _morpher.TraceManager.ApplicableTemplatesNotApplied(_stratum, input);
                    }
                }
                else
                {
                    Word word = input;
                    if (!word.IsLastAppliedRuleFinal.HasValue || !word.IsLastAppliedRuleFinal.Value)
                    {
                        word = input.DeepClone();
                        word.IsLastAppliedRuleFinal = true;
                        word.Freeze();
                    }
                    output.Add(word);
                }
            }

            return(output);
        }
Пример #3
0
        public IEnumerable <Word> Apply(Word input)
        {
            if (!_morpher.RuleSelector(_stratum) || input.RootAllomorph.Morpheme.Stratum.Depth > _stratum.Depth)
            {
                return(input.ToEnumerable());
            }

            if (_morpher.TraceManager.IsTracing)
            {
                _morpher.TraceManager.BeginApplyStratum(_stratum, input);
            }

            var output = new HashSet <Word>(FreezableEqualityComparer <Word> .Default);

            foreach (Word mruleOutWord in ApplyMorphologicalRules(input).Concat(ApplyTemplates(input)))
            {
                if (!(mruleOutWord.IsLastAppliedRuleFinal ?? false))
                {
                    if (_morpher.TraceManager.IsTracing)
                    {
                        _morpher.TraceManager.NonFinalTemplateAppliedLast(_stratum, mruleOutWord);
                    }
                }
                else if (mruleOutWord.CurrentMorphologicalRule != null && mruleOutWord.CurrentMorphologicalRule.Stratum == _stratum)
                {
                    if (_morpher.TraceManager.IsTracing)
                    {
                        _morpher.TraceManager.ParseFailed(_morpher.Language, mruleOutWord, FailureReason.PartialParse, null, null);
                    }
                }
                else
                {
                    Word newWord = mruleOutWord.DeepClone();
                    _prulesRule.Apply(newWord);
                    newWord.IsLastAppliedRuleFinal = null;
                    newWord.Freeze();
                    if (_morpher.TraceManager.IsTracing)
                    {
                        _morpher.TraceManager.EndApplyStratum(_stratum, newWord);
                    }
                    output.Add(newWord);
                }
            }
            if (_morpher.TraceManager.IsTracing && output.Count == 0)
            {
                _morpher.TraceManager.EndApplyStratum(_stratum, input);
            }
            return(output);
        }
Пример #4
0
        public IEnumerable <Word> Apply(Word input)
        {
            if (!_morpher.RuleSelector(_template))
            {
                return(Enumerable.Empty <Word>());
            }

            FeatureStruct fs;

            if (!input.SyntacticFeatureStruct.Unify(_template.RequiredSyntacticFeatureStruct, out fs))
            {
                return(Enumerable.Empty <Word>());
            }

            if (_morpher.TraceManager.IsTracing)
            {
                _morpher.TraceManager.BeginUnapplyTemplate(_template, input);
            }

            Word inWord = input.DeepClone();

            inWord.Freeze();

            var output = new HashSet <Word>(FreezableEqualityComparer <Word> .Default);

#if SINGLE_THREADED
            ApplySlots(inWord, _rules.Count - 1, output);
#else
            ParallelApplySlots(inWord, output);
#endif

            foreach (Word outWord in output)
            {
                outWord.SyntacticFeatureStruct.Add(fs);
            }
            return(output);
        }