public virtual void Annotate <_T0>(MatchedExpression matchedExpression, IList <_T0> nested)
     where _T0 : ICoreMap
 {
     if (resultNestedAnnotationField != null)
     {
         matchedExpression.annotation.Set(resultNestedAnnotationField, nested);
     }
     // NOTE: for now value must be extracted after nested annotation is in place...
     Annotate(matchedExpression);
 }
        private Pair <IList <ICoreMap>, IList <T> > ApplyCompositeRule <_T0>(SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> compositeExtractRule, IList <_T0> merged, IList <T> matchedExpressions, int limit)
            where _T0 : ICoreMap
        {
            // Apply higher order rules
            bool done = false;
            // Limit of number of times rules are applied just in case
            int maxIters = limit;
            int iters    = 0;

            while (!done)
            {
                IList <T> newExprs  = new List <T>();
                bool      extracted = compositeExtractRule.Extract(merged, newExprs);
                if (verbose && extracted)
                {
                    log.Info("applyCompositeRule() extracting with " + compositeExtractRule + " from " + merged + " gives " + newExprs);
                }
                if (extracted)
                {
                    AnnotateExpressions(merged, newExprs);
                    newExprs = MatchedExpression.RemoveNullValues(newExprs);
                    if (!newExprs.IsEmpty())
                    {
                        newExprs = MatchedExpression.RemoveNested(newExprs);
                        newExprs = MatchedExpression.RemoveOverlapping(newExprs);
                        merged   = MatchedExpression.ReplaceMerged(merged, newExprs);
                        // Favor newly matched expressions over older ones
                        Sharpen.Collections.AddAll(newExprs, matchedExpressions);
                        matchedExpressions = MatchedExpression.RemoveNested(newExprs);
                        matchedExpressions = MatchedExpression.RemoveOverlapping(matchedExpressions);
                    }
                    else
                    {
                        extracted = false;
                    }
                }
                done = !extracted;
                iters++;
                if (maxIters > 0 && iters >= maxIters)
                {
                    if (verbose)
                    {
                        log.Warn("Aborting application of composite rules: Maximum iteration " + maxIters + " reached");
                    }
                    break;
                }
            }
            return(new Pair <IList <ICoreMap>, IList <T> >(merged, matchedExpressions));
        }
 public MatchedExpression(MatchedExpression me)
 {
     // end static class SingleAnnotationExtractor
     this.annotation  = me.annotation;
     this.extractFunc = me.extractFunc;
     this.text        = me.text;
     this.value       = me.value;
     //this.attributes = me.attributes;
     this.priority     = me.priority;
     this.weight       = me.weight;
     this.order        = me.order;
     this.charOffsets  = me.charOffsets;
     this.tokenOffsets = me.tokenOffsets;
     this.chunkOffsets = me.tokenOffsets;
 }
            public virtual void Annotate(MatchedExpression matchedExpression)
            {
                IValue ev = null;

                if (expressionToValue != null)
                {
                    ev = expressionToValue.Apply(matchedExpression);
                }
                matchedExpression.value = (ev != null) ? ev : valueExtractor.Apply(matchedExpression.annotation);
                if (resultAnnotationField != null)
                {
                    if (resultAnnotationExtractor != null)
                    {
                        object result = resultAnnotationExtractor.Apply(matchedExpression);
                        SetAnnotations(matchedExpression.annotation, resultAnnotationField, result);
                    }
                    else
                    {
                        // TODO: Should default result be the matchedExpression, value, object???
                        //matchedExpression.annotation.set(resultAnnotationField, matchedExpression);
                        IValue v = matchedExpression.GetValue();
                        SetAnnotations(matchedExpression.annotation, resultAnnotationField, (v != null) ? v.Get() : null);
                    }
                }
                if (tokensResultAnnotationField != null)
                {
                    IList <ICoreMap> tokens = (IList <ICoreMap>)matchedExpression.annotation.Get(tokensAnnotationField);
                    if (resultAnnotationExtractor != null)
                    {
                        object result = resultAnnotationExtractor.Apply(matchedExpression);
                        foreach (ICoreMap cm in tokens)
                        {
                            SetAnnotations(cm, tokensResultAnnotationField, result);
                        }
                    }
                    else
                    {
                        // TODO: Should default result be the matchedExpression, value, object???
                        //matchedExpression.annotation.set(resultAnnotationField, matchedExpression);
                        IValue v = matchedExpression.GetValue();
                        foreach (ICoreMap cm in tokens)
                        {
                            SetAnnotations(cm, tokensResultAnnotationField, (v != null) ? v.Get() : null);
                        }
                    }
                }
            }
        public virtual IList <T> ExtractExpressions(ICoreMap annotation)
        {
            // Extract potential expressions
            IList <T>   matchedExpressions = new List <T>();
            IList <int> stageIds           = new List <int>(stages.Keys);

            stageIds.Sort();
            foreach (int stageId in stageIds)
            {
                CoreMapExpressionExtractor.Stage <T>          stage            = stages[stageId];
                SequenceMatchRules.IExtractRule <ICoreMap, T> basicExtractRule = stage.basicExtractRule;
                if (stage.clearMatched)
                {
                    matchedExpressions.Clear();
                }
                if (basicExtractRule != null)
                {
                    basicExtractRule.Extract(annotation, matchedExpressions);
                    if (verbose && matchedExpressions != null)
                    {
                        log.Info("extractExpressions() extracting with " + basicExtractRule + " from " + annotation + " gives " + matchedExpressions);
                    }
                    AnnotateExpressions(annotation, matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveNullValues(matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveNested(matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveOverlapping(matchedExpressions);
                }
                IList <ICoreMap> merged = MatchedExpression.ReplaceMergedUsingTokenOffsets(annotation.Get(tokensAnnotationKey), matchedExpressions);
                SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> compositeExtractRule = stage.compositeExtractRule;
                if (compositeExtractRule != null)
                {
                    Pair <IList <ICoreMap>, IList <T> > p = ApplyCompositeRule(compositeExtractRule, merged, matchedExpressions, stage.limitIters);
                    merged             = p.First();
                    matchedExpressions = p.Second();
                }
                matchedExpressions = FilterInvalidExpressions(stage.filterRule, matchedExpressions);
            }
            matchedExpressions.Sort(MatchedExpression.ExprTokenOffsetsNestedFirstComparator);
            if (!keepTags)
            {
                CleanupTags(annotation);
            }
            return(matchedExpressions);
        }