Example #1
0
        public virtual void TestCompose()
        {
            IFunction <int, int> plusOne  = null;
            IFunction <int, int> doubler  = null;
            IFunction <int, int> composed = Functions.Compose(plusOne, doubler);

            NUnit.Framework.Assert.AreEqual(composed.Apply(1), 3);
            NUnit.Framework.Assert.AreEqual(composed.Apply(2), 5);
        }
        public void TestFunction(IFunction function, JToken args, JToken expected)
        {
            var actual = args != null?function.Apply(args) : function.Apply();

            (actual == null).Should().Be(expected == null, "actual and expected should both be present or not");
            if (actual != null)
            {
                actual.Should().BeEquivalentTo(expected);
            }
        }
Example #3
0
        public object Apply(IContext context, IList <object> arguments, IDictionary <string, object> namedArguments)
        {
            var dynobj = new DynamicObject(this);

            if (this.HasMethod(ConstructorName))
            {
                IFunction      constructor = this.GetMethod(ConstructorName);
                IList <object> args        = new List <object>()
                {
                    dynobj
                };

                if (arguments != null && arguments.Count > 0)
                {
                    foreach (var arg in arguments)
                    {
                        args.Add(arg);
                    }
                }

                constructor.Apply(context, args, namedArguments);
            }

            return(dynobj);
        }
Example #4
0
        private void FinishSentence(ICoreMap sentence, IList <Tree> trees)
        {
            if (treeMap != null)
            {
                IList <Tree> mappedTrees = Generics.NewLinkedList();
                foreach (Tree tree in trees)
                {
                    Tree mappedTree = treeMap.Apply(tree);
                    mappedTrees.Add(mappedTree);
                }
                trees = mappedTrees;
            }
            ParserAnnotatorUtils.FillInParseAnnotations(Verbose, BuildGraphs, gsf, sentence, trees, extraDependencies);
            if (saveBinaryTrees)
            {
                TreeBinarizer binarizer = TreeBinarizer.SimpleTreeBinarizer(parser.GetTLPParams().HeadFinder(), parser.TreebankLanguagePack());
                Tree          binarized = binarizer.TransformTree(trees[0]);
                Edu.Stanford.Nlp.Trees.Trees.ConvertToCoreLabels(binarized);
                sentence.Set(typeof(TreeCoreAnnotations.BinarizedTreeAnnotation), binarized);
            }
            // for some reason in some corner cases nodes aren't having sentenceIndex set
            // do a pass and make sure all nodes have sentenceIndex set
            SemanticGraph sg = sentence.Get(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation));

            if (sg != null)
            {
                foreach (IndexedWord iw in sg.VertexSet())
                {
                    if (iw.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)) == null && sentence.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)) != null)
                    {
                        iw.SetSentIndex(sentence.Get(typeof(CoreAnnotations.SentenceIndexAnnotation)));
                    }
                }
            }
        }
        /// <summary>This method just tests the functionality of the included transformers.</summary>
        public static void Main(string[] args)
        {
            //TreeFactory tf = new LabeledScoredTreeFactory();
            Tree stringyTree = null;

            try
            {
                stringyTree = (new PennTreeReader(new StringReader("(S (VP (VBZ Try) (NP (DT this))) (. .))"), new LabeledScoredTreeFactory(new StringLabelFactory()))).ReadTree();
            }
            catch (IOException)
            {
            }
            // do nothing
            System.Console.Out.WriteLine(stringyTree);
            IFunction <Tree, Tree> a = GetLabeledTreeToCategoryWordTagTreeFunction();
            Tree adaptyTree          = a.Apply(stringyTree);

            System.Console.Out.WriteLine(adaptyTree);
            adaptyTree.PercolateHeads(new CollinsHeadFinder());
            System.Console.Out.WriteLine(adaptyTree);
            IFunction <Tree, Tree> b = GetLabeledTreeToStringLabeledTreeFunction();
            Tree stringLabelTree     = b.Apply(adaptyTree);

            System.Console.Out.WriteLine(stringLabelTree);
        }
Example #6
0
        public float[] Multiply(float[] vector, float[,] matrix, IFunction activationFunction)
        {
            activationFunction = activationFunction ?? new Linear();

            var xLength = matrix.GetLength(0);
            var yLength = matrix.GetLength(1);

            if (vector.Length != xLength)
            {
                throw new ArgumentOutOfRangeException(nameof(vector), $"Vector length {vector.Length} doesn't correspond to matrix size [{xLength},{yLength}]");
            }

            var result = new float[yLength];

            Parallel.For(0, yLength, y =>
            {
                for (int x = 0; x < xLength; x++)
                {
                    result[y] += vector[x] * matrix[x, y];
                }

                result[y] = activationFunction.Apply(result[y]);
            });

            return(result);
        }
        public object Invoke(string name, IContext context, IList <object> arguments, IDictionary <string, object> namedArguments)
        {
            var       value  = this.GetValue(name);
            IFunction method = value as IFunction;

            if (method == null)
            {
                throw new TypeError(string.Format("'{0}' object is not callable", Types.GetTypeName(value)));
            }

            IList <object> args = new List <object>()
            {
                this
            };

            if (arguments != null && arguments.Count > 0)
            {
                foreach (var arg in arguments)
                {
                    args.Add(arg);
                }
            }

            return(method.Apply(context, args, namedArguments));
        }
Example #8
0
        /// <summary>
        /// Only works on English, as it is hard coded for using the
        /// Morphology class, which is English-only
        /// </summary>
        public virtual IList <CoreLabel> Lemmatize <_T0>(IList <_T0> tokens)
            where _T0 : IHasWord
        {
            IList <TaggedWord> tagged;

            if (GetOp().testOptions.preTag)
            {
                IFunction <IList <IHasWord>, IList <TaggedWord> > tagger = LoadTagger();
                tagged = tagger.Apply(tokens);
            }
            else
            {
                Tree tree = Parse(tokens);
                tagged = tree.TaggedYield();
            }
            Morphology        morpha = new Morphology();
            IList <CoreLabel> lemmas = Generics.NewArrayList();

            foreach (TaggedWord token in tagged)
            {
                CoreLabel label = new CoreLabel();
                label.SetWord(token.Word());
                label.SetTag(token.Tag());
                morpha.Stem(label);
                lemmas.Add(label);
            }
            return(lemmas);
        }
Example #9
0
        /// <summary>
        /// helper method to find the longest entity mention that is coreferent to an entity mention
        /// after coref has been run...match an entity mention to a coref mention, go through all of
        /// the coref mentions and find the one with the longest matching entity mention, return
        /// that entity mention
        /// </summary>
        /// <param name="em">the entity mention of interest</param>
        /// <param name="ann">the annotation, after coreference has been run</param>
        /// <returns/>
        public virtual Optional <ICoreMap> FindBestCoreferentEntityMention(ICoreMap em, Annotation ann)
        {
            // helper lambda
            IFunction <Optional <ICoreMap>, int> lengthOfOptionalEntityMention = null;
            // initialize return value as empty Optional
            Optional <ICoreMap> bestCoreferentEntityMention = Optional.Empty();
            // look for matching coref mention
            int                entityMentionIndex        = em.Get(typeof(CoreAnnotations.EntityMentionIndexAnnotation));
            Optional <int>     matchingCorefMentionIndex = Optional.OfNullable(ann.Get(typeof(CoreAnnotations.EntityMentionToCorefMentionMappingAnnotation))[entityMentionIndex]);
            Optional <Mention> matchingCorefMention      = matchingCorefMentionIndex.IsPresent() ? Optional.Of(ann.Get(typeof(CorefCoreAnnotations.CorefMentionsAnnotation))[matchingCorefMentionIndex.Get()]) : Optional.Empty();

            // if there is a matching coref mention, look at all of the coref mentions in its coref chain
            if (matchingCorefMention.IsPresent())
            {
                Optional <CorefChain>           matchingCorefChain          = Optional.OfNullable(ann.Get(typeof(CorefCoreAnnotations.CorefChainAnnotation))[matchingCorefMention.Get().corefClusterID]);
                IList <CorefChain.CorefMention> corefMentionsInTextualOrder = matchingCorefChain.IsPresent() ? matchingCorefChain.Get().GetMentionsInTextualOrder() : new List <CorefChain.CorefMention>();
                foreach (CorefChain.CorefMention cm in corefMentionsInTextualOrder)
                {
                    Optional <int>      candidateCoreferentEntityMentionIndex = Optional.OfNullable(ann.Get(typeof(CoreAnnotations.CorefMentionToEntityMentionMappingAnnotation))[cm.mentionID]);
                    Optional <ICoreMap> candidateCoreferentEntityMention      = candidateCoreferentEntityMentionIndex.IsPresent() ? Optional.OfNullable(ann.Get(typeof(CoreAnnotations.MentionsAnnotation))[candidateCoreferentEntityMentionIndex.Get()]) : Optional.Empty(
                        );
                    if (lengthOfOptionalEntityMention.Apply(candidateCoreferentEntityMention) > lengthOfOptionalEntityMention.Apply(bestCoreferentEntityMention))
                    {
                        bestCoreferentEntityMention = candidateCoreferentEntityMention;
                    }
                }
            }
            return(bestCoreferentEntityMention);
        }
        public static IList <T> MergeListWithSortedMatched <T, _T1, _T2>(IList <_T1> list, IList <_T2> matched, IFunction <IList <T>, T> aggregator)
            where _T1 : T
            where _T2 : IHasInterval <int>
        {
            IList <T> merged = new List <T>(list.Count);
            // Approximate size
            int last = 0;

            foreach (IHasInterval <int> m in matched)
            {
                Interval <int> interval = m.GetInterval();
                int            start    = interval.GetBegin();
                int            end      = interval.GetEnd();
                if (start >= last)
                {
                    Sharpen.Collections.AddAll(merged, list.SubList(last, start));
                    T t = aggregator.Apply(list.SubList(start, end));
                    merged.Add(t);
                    last = end;
                }
            }
            // Add rest of elements
            if (last < list.Count)
            {
                Sharpen.Collections.AddAll(merged, list.SubList(last, list.Count));
            }
            return(merged);
        }
        public static IList <T> MergeListWithSortedMatchedPreAggregated <T, _T1, _T2>(IList <_T1> list, IList <_T2> matched, IFunction <T, Interval <int> > toIntervalFunc)
            where _T1 : T
            where _T2 : T
        {
            IList <T> merged = new List <T>(list.Count);
            // Approximate size
            int last = 0;

            foreach (T m in matched)
            {
                Interval <int> interval = toIntervalFunc.Apply(m);
                int            start    = interval.GetBegin();
                int            end      = interval.GetEnd();
                if (start >= last)
                {
                    Sharpen.Collections.AddAll(merged, list.SubList(last, start));
                    merged.Add(m);
                    last = end;
                }
            }
            // Add rest of elements
            if (last < list.Count)
            {
                Sharpen.Collections.AddAll(merged, list.SubList(last, list.Count));
            }
            return(merged);
        }
Example #12
0
        public object Evaluate(Context context, bool withvars = false)
        {
            object modulevalue = this.moduleexpression.Evaluate(context, withvars);
            object namevalue   = this.nameexpression.Evaluate(context, withvars);

            string modulename = ((Atom)modulevalue).Name;
            string name       = string.Format("{0}/{1}", ((Atom)namevalue).Name, this.argumentexpressions.Count);

            IList <object> arguments = new List <object>();

            foreach (var argexpr in this.argumentexpressions)
            {
                arguments.Add(argexpr.Evaluate(context, withvars));
            }

            Module module = context.GetValue(modulename) as Module;

            if (module == null || !module.ExportNames.Contains(name) || !(module.Context.GetValue(name) is IFunction))
            {
                throw new Exception(string.Format("undefined function {0}:{1}", modulename, name));
            }

            IFunction func = (IFunction)module.Context.GetValue(name);

            return(func.Apply(context, arguments));
        }
Example #13
0
 /// <summary>
 /// Adds all of the keys in <code>from</code> to <code>to</code>,
 /// applying <code>function</code> to the values to transform them
 /// from <code>V2</code> to <code>V1</code>.
 /// </summary>
 public static void AddAll <K, V1, V2>(IDictionary <K, V1> to, IDictionary <K, V2> from, IFunction <V2, V1> function)
 {
     foreach (KeyValuePair <K, V2> entry in from)
     {
         to[entry.Key] = function.Apply(entry.Value);
     }
 }
            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);
                        }
                    }
                }
            }
 /// <summary>
 /// Adds all of the entries in the <code>other</code> map, performing
 /// <code>function</code> on them to transform the values
 /// </summary>
 public virtual void AddAll <V2, _T1>(Edu.Stanford.Nlp.Util.TwoDimensionalMap <_T1> other, IFunction <V2, V> function)
     where _T1 : K1
 {
     foreach (TwoDimensionalMap.Entry <K1, K2, V2> entry in other)
     {
         Put(entry.GetFirstKey(), entry.GetSecondKey(), function.Apply(entry.GetValue()));
     }
 }
Example #16
0
        /// <summary>
        /// Returns a lexicalized Tree whose Labels are CategoryWordTag
        /// instances, all corresponds to the input tree.
        /// </summary>
        public static Tree Lexicalize(Tree t, IHeadFinder hf)
        {
            IFunction <Tree, Tree> a = TreeFunctions.GetLabeledTreeToCategoryWordTagTreeFunction();
            Tree t1 = a.Apply(t);

            t1.PercolateHeads(hf);
            return(t1);
        }
        public JToken Evaluate(JToken inputOptional, WalkedPath walkedPath, JObject context)
        {
            JToken valueOptional = null;

            try
            {
                // "key": "@0", "key": literal
                if (_function == null)
                {
                    valueOptional = _functionArgs[0].EvaluateArg(walkedPath, context);
                }
                // "key": "=abs(@(1,&0))"
                // this is most usual case, a single argument is passed and we need to evaluate and
                // pass the value, if present, to the spec function
                else if (_functionArgs.Length == 1)
                {
                    var evaluatedArgValue = _functionArgs[0].EvaluateArg(walkedPath, context);
                    valueOptional = evaluatedArgValue != null?_function.Apply(evaluatedArgValue) : _function.Apply();
                }
                // "key": "=abs(@(1,&0),-1,-3)"
                // this is more complicated case! if args is an array, after evaluation we cannot pass a missing value wrapped in
                // object[] into function. In such case null will be passed however, in json null is also a valid value, so it is
                // upto the implementer to interpret the value. Ideally we can almost always pass a list straight from input.
                else if (_functionArgs.Length > 1)
                {
                    var evaluatedArgs = EvaluateArgsValue(_functionArgs, context, walkedPath);
                    valueOptional = _function.Apply(evaluatedArgs);
                }
                //
                // FYI this is where the "magic" happens that allows functions that take a single method
                //  default to the current "match" rather than an explicit "reference".
                // Note, this does not work for functions that take more than a single input.
                //
                // "key": "=abs"
                else
                {
                    // pass current value as arg if present
                    valueOptional = inputOptional != null?_function.Apply(inputOptional) : _function.Apply();
                }
            }
            catch (Exception)
            {
            }

            return(valueOptional);
        }
Example #18
0
 public virtual Tree TransformTree(Tree tree)
 {
     foreach (Tree leaf in tree.GetLeaves())
     {
         ILabel label = leaf.Label();
         label.SetValue(transform.Apply(label.Value()));
     }
     return(tree);
 }
Example #19
0
        public float[] ApplyFunction(float[] vector, IFunction activationFunction)
        {
            activationFunction = activationFunction ?? new Linear();

            var result = new float[vector.Length];

            Parallel.For(0, vector.Length, i => { result[i] = activationFunction.Apply(vector[i]); });
            return(result);
        }
 // when we're inside a block to be transformed, we ignore
 // elements that don't end the block.
 public virtual void ProcessText(string text)
 {
     if (text.Length > 0)
     {
         text = function.Apply(text).ToString();
         outWriter.Print(XMLUtils.EscapeXML(text));
         outWriter.Print('\n');
     }
 }
Example #21
0
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        public virtual ICollection <string> TagSet(IFunction <string, string> basicCategoryFunction)
        {
            ICollection <string> tagSet = new HashSet <string>();

            foreach (string tag in tagIndex.ObjectsList())
            {
                tagSet.Add(basicCategoryFunction.Apply(tag));
            }
            return(tagSet);
        }
        /// <summary>
        /// Makes it possible to uniquify a collection of objects which are normally
        /// non-hashable.
        /// </summary>
        /// <remarks>
        /// Makes it possible to uniquify a collection of objects which are normally
        /// non-hashable. Alternatively, it lets you define an alternate hash function
        /// for them for limited-use hashing.
        /// </remarks>
        public static ICollection <ObjType> UniqueNonhashableObjects <ObjType, Hashable>(ICollection <ObjType> objects, IFunction <ObjType, Hashable> customHasher)
        {
            IDictionary <Hashable, ObjType> hashesToObjects = Generics.NewHashMap();

            foreach (ObjType @object in objects)
            {
                hashesToObjects[customHasher.Apply(@object)] = @object;
            }
            return(hashesToObjects.Values);
        }
Example #23
0
        private float[] SigmaOut(float[] ideal, float[] input, IFunction activationFunction)
        {
            activationFunction = activationFunction ?? new Linear();

            float[] sigmaOut = new float[input.Length];

            Parallel.For(0, sigmaOut.Length, i => { sigmaOut[i] = (ideal[i] - activationFunction.Apply(input[i])) * activationFunction.Derevative(input[i]); });

            return(sigmaOut);
        }
Example #24
0
        /// <summary>
        /// Will parse the text in <code>sentence</code> as if it represented
        /// a single sentence by first processing it with a tokenizer.
        /// </summary>
        public virtual Tree Parse(string sentence)
        {
            IList <IHasWord> tokens = Tokenize(sentence);

            if (GetOp().testOptions.preTag)
            {
                IFunction <IList <IHasWord>, IList <TaggedWord> > tagger = LoadTagger();
                tokens = tagger.Apply(tokens);
            }
            return(Parse(tokens));
        }
Example #25
0
        public virtual IFunction Evaluate(IList <IFunction> parameters)
        {
            IFunction result = this;

            foreach (IFunction parameter in parameters)
            {
                result = result.Apply(parameter);
            }

            return(result);
        }
        public static IList <T> MergeList <T, M, _T2>(IList <_T2> list, ICollection <M> matched, IFunction <M, Interval <int> > toIntervalFunc, IFunction <IList <T>, T> aggregator)
            where _T2 : T
        {
            IList <Interval <int> > matchedIntervals = new List <Interval <int> >(matched.Count);

            foreach (M m in matched)
            {
                matchedIntervals.Add(toIntervalFunc.Apply(m));
            }
            return(MergeList(list, matchedIntervals, aggregator));
        }
        /// <summary>Transforms the keyset of collection according to the given Function and returns a list.</summary>
        public static IList <T2> TransformAsList <T1, T2, _T2>(ICollection <_T2> original, IFunction <T1, T2> f)
            where _T2 : T1
        {
            IList <T2> transformed = new List <T2>();

            foreach (T1 t in original)
            {
                transformed.Add(f.Apply(t));
            }
            return(transformed);
        }
        /// <summary>Transforms the keyset of collection according to the given Function and returns a set of the keys.</summary>
        public static ICollection <T2> TransformAsSet <T1, T2, _T2>(ICollection <_T2> original, IFunction <T1, T2> f)
            where _T2 : T1
        {
            ICollection <T2> transformed = Generics.NewHashSet();

            foreach (T1 t in original)
            {
                transformed.Add(f.Apply(t));
            }
            return(transformed);
        }
Example #29
0
        public object Evaluate(ValueEnvironment environment)
        {
            IFunction function = (IFunction)Machine.Evaluate(this.first, environment);

            if (function == null)
            {
                throw new InvalidOperationException(string.Format("Unknown form '{0}'", this.first.ToString()));
            }

            return(function.Apply(this.Next, environment));
        }
 public static IFunction <TIn, TOut> SelectMany <TIn, TMid1, TMid2, TOut>(
     this IFunction <TIn, TMid1> func,
     Func <TMid1, IFunction <TIn, TMid2> > proj1,
     Func <TMid1, TMid2, TOut> proj2)
 {
     return(Create(x => {
         var mid1 = func.Apply(x);
         var mid2 = proj1(mid1).Apply(x);
         return proj2(mid1, mid2);
     }));
 }