コード例 #1
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (!(check is IParsedPhrase))
            {
                fail.Fail("Cannot match a " + check.GetType(), succ);
                return(true);
            }

            IParsedPhrase phrase = (IParsedPhrase)check;

            if (phrase.Part != "=P")
            {
                fail.Fail("Can only match a paragraph", succ);
                return(true);
            }

            context.Map["$sentences.check"] = check;
            TwoTuple <List <IContent>, IContinuation> parts = SplitArguments(context, succ);
            List <IContent> chunk = parts.one;

            // Match against each element
            int sentenceStart = 0;

            foreach (IParsedPhrase constituent in phrase.Branches)
            {
                Context first = new Context(context, chunk);
                first.Map["$sentences.index"] = sentenceStart + 1;

                Matcher.MatchAgainst(salience, first, constituent, new List <IParsedPhrase>(), parts.two, fail);
            }

            return(true);
        }
コード例 #2
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context context = (Context) value;
            if (principleSource == null || assertionSource == null) {
                fail.Fail("ConceptNet sources missing", succ);
                return true;
            }

            Notion concept;
            if (!principleSource.TryGetValue(StarUtilities.ContentsCode(context, tagger, parser), out concept))
            {
                fail.Fail("Could not find produced in ConceptNet", succ);
                return true;
            }

            List<Assertion> assertions;
            if (!assertionSource.TryGetValue(new KeyValuePair<Notion, string>(concept, relation), out assertions))
                assertions = new List<Assertion>();

            List<IContent> contents = new List<IContent>();

            foreach (Assertion assertion in assertions)
            {
                contents.Add(new Word(assertion.Sentence));
                contents.Add(new Word(" ."));
            }

            succ.Continue(new Context(context, contents), fail);
            return true;
        }
コード例 #3
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = (Concept)context.Lookup("$knowConcept");

            if (context.Contents.Count == 1)
            {
                if (context.Contents[0] is Variable)
                {
                    Variable left = (Variable)context.Contents[0];
                    if (left.Match(context, concept))
                    {
                        succ.Continue(new Context(context, new List <IContent>()), fail);
                    }
                    else
                    {
                        fail.Fail("Left doesn't match context", succ);
                    }
                    return(true);
                }
                else if (context.Contents[0].Name == "*" || context.Contents[0].Name == "_")
                {
                    List <IContent> words = new List <IContent>();
                    words.Add(new Word(concept.Name));
                    context.Map.Add(StarUtilities.NextStarName(context, context.Contents[0].Name), words);

                    succ.Continue(new Context(context, new List <IContent>()), fail);
                    return(true);
                }
            }

            fail.Fail("Know given multiple values", succ);
            return(true);
        }
コード例 #4
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            // XXX: How does childctx get used???
            // Add optional punctuation to end
            Context childctx = new Context(context, context.Contents);

            childctx.Contents.Add(new Special("%opt"));
            childctx.Contents.Add(new Special("%punct"));

            if (check is IParsedPhrase)
            {
                IParsedPhrase phrase = (IParsedPhrase)check;
                if (phrase.Part == "=P")
                {
                    // Match against each element
                    foreach (IParsedPhrase constituent in phrase.Branches)
                    {
                        Matcher.MatchAgainst(salience, context, constituent, new List <IParsedPhrase>(), succ, fail);
                    }

                    return(true);
                }
                else if (phrase.Part == "FRAG" || phrase.Part == "S" || phrase.Part == "SBARQ")
                {
                    // Do a match using my contents
                    Matcher.MatchAgainst(salience, context, (IParsedPhrase)check, new List <IParsedPhrase>(), succ, fail);

                    return(true);
                }
            }

            fail.Fail("Not a sentence.", succ);
            return(true);
        }
コード例 #5
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context context = (Context)value;

            double result = 0;

            foreach (IContent content in context.Contents)
            {
                double term = 0;
                if (content is Word && double.TryParse(content.Name, out term))
                {
                    result += term;
                }
                else
                {
                    fail.Fail("Argument isn't number", succ);
                    return(true);
                }
            }

            List <IContent> cntres = new List <IContent>();

            cntres.Add(new Word(result.ToString()));
            succ.Continue(new Context(context, cntres), fail);

            return(true);
        }
コード例 #6
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (!(check is IParsedPhrase))
            {
                fail.Fail("Cannot match a " + check.GetType(), succ);
                return(true);
            }

            // Set up main check
            IParsedPhrase full = (IParsedPhrase)check;

            GroupPhrase sofar = context.LookupDefaulted <GroupPhrase>("$active$" + name, null);

            if (sofar != null)
            {
                full = sofar.AddBranch(full);
            }

            bool?isMatch = IsMatch(full);

            if (!isMatch.HasValue)
            {
                List <IContent> contents = new List <IContent>();
                contents.Add(new Value(this));
                Context tryagain = new Context(context, contents);
                tryagain.Map["$active$" + name] = new GroupPhrase(full);
                // Continue with same context
                succ.Continue(tryagain, fail);
            }
            else
            {
                if (isMatch.Value)
                {
                    Propogate(context, full, 1.0);
                    context.Map[StarUtilities.NextStarName(context, name)] = full.Text;

                    succ.Continue(context.ChildRange(1), fail);
                }
                else
                {
                    fail.Fail("Does not match " + full.Text, succ);
                }
            }

            return(true);
        }
コード例 #7
0
 public int Fail(IFailure fail, double salience, string reason, IContinuation skip)
 {
     // Clone it!
     fail = (IFailure)fail.Clone();
     if (fail is IAgent)
         ((IAgent)fail).Initialize(this, salience);
     if (salience > 0)
         return fail.Fail(reason, skip);
     return 1;
 }
コード例 #8
0
        public override bool Produce(Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = CompletePartials(context);

            if (concept == null)
            {
                fail.Fail("Nothing to complete partials", succ);
                return(true);
            }

            succ.Continue(new Context(context, new List <IContent>()), fail);
            return(true);
        }
コード例 #9
0
 public bool Fail(IFailure fail, double salience, string reason, IContinuation skip)
 {
     // Clone it!
     fail = (IFailure)fail.Clone();
     if (fail is IAgent)
     {
         ((IAgent)fail).Initialize(this, salience);
     }
     if (salience > 0)
     {
         return(fail.Fail(reason, skip));
     }
     return(true);
 }
コード例 #10
0
        public bool ConstructSentence(Context context, IContinuation succ, IFailure fail, params object[] args)
        {
            // Need to produce all my contents!
            IParsedPhrase phrase = StarUtilities.ProducedPhrase(context, tagger, parser);

            if (phrase == null)
            {
                // oops, we failed to produce
                fail.Fail("Context could not be produced", succ);
                return(true);
            }

            if (!(phrase.Part == "=P"))
            {
                if (phrase.Part == "FRAG" || phrase.Part == "S" || phrase.Part == "SBARQ")
                {
                    if (final != null)
                    {
                        GroupPhrase   groupPhrase = new GroupPhrase(phrase);
                        IParsedPhrase last        = groupPhrase.GetBranch(groupPhrase.Count - 1);
                        if (!(last.Part == "." || last.Part == "!" || last.Part == "?"))
                        {
                            List <IParsedPhrase> branches = new List <IParsedPhrase>();
                            branches.AddRange(phrase.Branches);
                            branches.Add((IParsedPhrase)final.Clone());
                            phrase = new GroupPhrase(phrase.Part, branches);
                        }
                    }
                }
                else
                {
                    List <IParsedPhrase> branches = new List <IParsedPhrase>();
                    branches.Add(phrase);
                    if (final != null)
                    {
                        branches.Add((IParsedPhrase)final.Clone());
                    }
                    phrase = new GroupPhrase("FRAG", branches);
                }
            }

            List <IContent> contents = new List <IContent>();

            contents.Add(new Word(phrase.Text));
            Context child = new Context(context, contents);

            succ.Continue(child, fail);

            return(true);
        }
コード例 #11
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context context = (Context)value;

            if (principleSource == null || assertionSource == null)
            {
                fail.Fail("ConceptNet sources missing", succ);
                return(true);
            }

            Notion concept;

            if (!principleSource.TryGetValue(StarUtilities.ContentsCode(context, tagger, parser), out concept))
            {
                fail.Fail("Could not find produced in ConceptNet", succ);
                return(true);
            }

            List <Assertion> assertions;

            if (!assertionSource.TryGetValue(new KeyValuePair <Notion, string>(concept, relation), out assertions))
            {
                assertions = new List <Assertion>();
            }

            List <IContent> contents = new List <IContent>();

            foreach (Assertion assertion in assertions)
            {
                contents.Add(new Word(assertion.Sentence));
                contents.Add(new Word(" ."));
            }

            succ.Continue(new Context(context, contents), fail);
            return(true);
        }
コード例 #12
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (!(check is IParsedPhrase)) {
                fail.Fail("Cannot match a " + check.GetType(), succ);
                return true;
            }

            // Set up main check
            IParsedPhrase full = (IParsedPhrase) check;

            GroupPhrase sofar = context.LookupDefaulted<GroupPhrase>("$active$" + name, null);
            if (sofar != null)
                full = sofar.AddBranch(full);

            bool? isMatch = IsMatch(full);

            if (!isMatch.HasValue) {
                List<IContent> contents = new List<IContent>();
                contents.Add(new Value(this));
                Context tryagain = new Context(context, contents);
                tryagain.Map["$active$" + name] = new GroupPhrase(full);
                // Continue with same context
                succ.Continue(tryagain, fail);
            } else {
                if (isMatch.Value) {
                    Propogate(context, full, 1.0);
                    context.Map[StarUtilities.NextStarName(context, name)] = full.Text;

                    succ.Continue(context.ChildRange(1), fail);
                } else {
                    fail.Fail("Does not match " + full.Text, succ);
                }
            }

            return true;
        }
コード例 #13
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (check is IParsedPhrase && ((IParsedPhrase)check).Part == "PRN")
            {
                // Set up propogate-on-clear
                ContinueToCallAgent cont = CallAgentWrapper.MakeContinuation(PropogateOnClear, succ, 100.0, 4, 10, "%paren", check);

                // Do a match using my contents
                Matcher.MatchAgainst(salience, context, (IParsedPhrase)check, new List <IParsedPhrase>(), cont, fail);
                return(true);
            }
            else
            {
                fail.Fail("check is not parenthetical", succ);
                return(true);
            }
        }
コード例 #14
0
        /*public void Generate(Coderack coderack, IContinuation succ, IFailure fail, double weight)
         * {
         *  PatternTemplateSource checker = new PatternTemplateSource(this, coderack, weight, succ);
         *
         *  ContinuationAppender appender = new ContinuationAppender(pattern, checker);
         *
         *  Evaluator eval = new Evaluator(maxSalience * weight, ArgumentMode.ManyArguments, appender.AsIndex(0), appender.AsIndex(1), true);
         *
         *  eval.Continue(pattern, fail);
         * }*/

        #region IContinuation Members

        public bool Continue(object value, IFailure fail)
        {
            Context context = (Context)value;

            // Did we successfully match everything?  If so, evaluate the template
            if (Matcher.IsRemainderOptional(context.Contents))
            {
                Evaluator eval  = new Evaluator(salience, ArgumentMode.ManyArguments, succ, new NopCallable(), true);
                Context   child = new Context(context, template.Contents);
                child.Map["$production"] = true;
                return(eval.Continue(child, fail));
            }
            else
            {
                // we didn't match everything
                fail.Fail("Context is not empty", succ);
                return(true);
            }
        }
コード例 #15
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context  context = (Context)value;
            IContent content = context.Contents[0];
            double   term    = 0;

            if (content is Word && double.TryParse(content.Name, out term))
            {
                term = -term;
                Context result = new Context(context, context.Contents);
                result.Contents[0] = new Word(term.ToString());
                succ.Continue(result, fail);
            }
            else
            {
                fail.Fail("Argument isn't number", succ);
            }

            return(true);
        }
コード例 #16
0
        public override bool Produce(Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = CompletePartials(context);

            if (concept == null)
            {
                fail.Fail("Nothing to complete partials", succ);
                return(true);
            }

            IParsedPhrase phrase = ((ClauseVariable)context.Lookup("%clause")).GetPropogated(context, "%clause");

            if (phrase != null)
            {
                EventVariable.AddEventClause(context, phrase);
                EventVariable.AddEventConcept(context, concept);
            }

            succ.Continue(new Context(context, new List <IContent>()), fail);
            return(true);
        }
コード例 #17
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context context = (Context) value;

            double result = 0;

            foreach (IContent content in context.Contents)
            {
                double term = 0;
                if (content is Word && double.TryParse(content.Name, out term))
                    result += term;
                else
                {
                    fail.Fail("Argument isn't number", succ);
                    return true;
                }
            }

            List<IContent> cntres = new List<IContent>();
            cntres.Add(new Word(result.ToString()));
            succ.Continue(new Context(context, cntres), fail);

            return true;
        }
コード例 #18
0
 public bool Continue(object value, IFailure fail)
 {
     Context context = (Context) value;
     // Did we successfully match everything?  If so, evaluate the template
     if (Matcher.IsRemainderOptional(context.Contents))
     {
         Evaluator eval = new Evaluator(salience, ArgumentMode.ManyArguments, succ, new NopCallable(), true);
         Context child = new Context(context, template.Contents);
         child.Map["$production"] = true;
         return eval.Continue(child, fail);
     }
     else
     {
         // we didn't match everything
         fail.Fail("Context is not empty", succ);
         return true;
     }
 }
コード例 #19
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (check is IParsedPhrase && ((IParsedPhrase) check).Part == "PRN")
            {
                // Set up propogate-on-clear
                ContinueToCallAgent cont = CallAgentWrapper.MakeContinuation(PropogateOnClear, succ, 100.0, 4, 10, "%paren", check);

                // Do a match using my contents
                Matcher.MatchAgainst(salience, context, (IParsedPhrase) check, new List<IParsedPhrase>(), cont, fail);
                return true;
            }
            else
            {
                fail.Fail("check is not parenthetical", succ);
                return true;
            }
        }
コード例 #20
0
 // default is failure to rescue
 public virtual bool CallRescue(Coderack coderack, IParsedPhrase input, PatternTemplateSource patternTemplateSource, string reason, IContinuation skip, IContinuation succ, IFailure fail)
 {
     return fail.Fail(reason, skip);
 }
コード例 #21
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            if (!(check is IParsedPhrase)) {
                fail.Fail("Cannot match a " + check.GetType(), succ);
                return true;
            }

            IParsedPhrase phrase = (IParsedPhrase) check;
            if (phrase.Part != "=P") {
                fail.Fail("Can only match a paragraph", succ);
                return true;
            }

            context.Map["$sentences.check"] = check;
            TwoTuple<List<IContent>, IContinuation> parts = SplitArguments(context, succ);
            List<IContent> chunk = parts.one;

            // Match against each element
            int sentenceStart = 0;
            foreach (IParsedPhrase constituent in phrase.Branches) {
                Context first = new Context(context, chunk);
                first.Map["$sentences.index"] = sentenceStart + 1;

                Matcher.MatchAgainst(salience, first, constituent, new List<IParsedPhrase>(), parts.two, fail);
            }

            return true;
        }
コード例 #22
0
        public override bool Call(object value, IContinuation succ, IFailure fail)
        {
            Context context = (Context) value;
            IContent content = context.Contents[0];
            double term = 0;
            if (content is Word && double.TryParse(content.Name, out term))
            {
                term = -term;
                Context result = new Context(context, context.Contents);
                result.Contents[0] = new Word(term.ToString());
                succ.Continue(result, fail);
            }
            else
                fail.Fail("Argument isn't number", succ);

            return true;
        }
コード例 #23
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            // XXX: How does childctx get used???
            // Add optional punctuation to end
            Context childctx = new Context(context, context.Contents);
            childctx.Contents.Add(new Special("%opt"));
            childctx.Contents.Add(new Special("%punct"));

            if (check is IParsedPhrase) {
                IParsedPhrase phrase = (IParsedPhrase) check;
                if (phrase.Part == "=P")
                {
                    // Match against each element
                    foreach (IParsedPhrase constituent in phrase.Branches)
                        Matcher.MatchAgainst(salience, context, constituent, new List<IParsedPhrase>(), succ, fail);

                    return true;
                }
                else if (phrase.Part == "FRAG" || phrase.Part == "S" || phrase.Part == "SBARQ")
                {
                    // Do a match using my contents
                    Matcher.MatchAgainst(salience, context, (IParsedPhrase) check, new List<IParsedPhrase>(), succ, fail);

                    return true;
                }
            }

            fail.Fail("Not a sentence.", succ);
            return true;
        }
コード例 #24
0
        public override bool Produce(Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = CompletePartials(context);
            if (concept == null)
            {
                fail.Fail("Nothing to complete partials", succ);
                return true;
            }

            succ.Continue(new Context(context, new List<IContent>()), fail);
            return true;
        }
コード例 #25
0
        public override bool Match(object check, Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = (Concept) context.Lookup("$knowConcept");
            if (context.Contents.Count == 1)
            {
                if (context.Contents[0] is Variable)
                {
                    Variable left = (Variable)context.Contents[0];
                    if (left.Match(context, concept))
                        succ.Continue(new Context(context, new List<IContent>()), fail);
                    else
                        fail.Fail("Left doesn't match context", succ);
                    return true;
                }
                else if (context.Contents[0].Name == "*" || context.Contents[0].Name == "_")
                {
                    List<IContent> words = new List<IContent>();
                    words.Add(new Word(concept.Name));
                    context.Map.Add(StarUtilities.NextStarName(context, context.Contents[0].Name), words);

                    succ.Continue(new Context(context, new List<IContent>()), fail);
                    return true;
                }
            }

            fail.Fail("Know given multiple values", succ);
            return true;
        }
コード例 #26
0
        public override bool Produce(Context context, IContinuation succ, IFailure fail)
        {
            Concept concept = CompletePartials(context);
            if (concept == null)
            {
                fail.Fail("Nothing to complete partials", succ);
                return true;
            }

            IParsedPhrase phrase = ((ClauseVariable)context.Lookup("%clause")).GetPropogated(context, "%clause");
            if (phrase != null)
            {
                EventVariable.AddEventClause(context, phrase);
                EventVariable.AddEventConcept(context, concept);
            }

            succ.Continue(new Context(context, new List<IContent>()), fail);
            return true;
        }
コード例 #27
0
        public bool ConstructSentence(Context context, IContinuation succ, IFailure fail, params object[] args)
        {
            // Need to produce all my contents!
            IParsedPhrase phrase = StarUtilities.ProducedPhrase(context, tagger, parser);
            if (phrase == null)
            {
                // oops, we failed to produce
                fail.Fail("Context could not be produced", succ);
                return true;
            }

            if (!(phrase.Part == "=P"))
            {
                if (phrase.Part == "FRAG" || phrase.Part == "S" || phrase.Part == "SBARQ")
                {
                    if (final != null)
                    {
                        GroupPhrase groupPhrase = new GroupPhrase(phrase);
                        IParsedPhrase last = groupPhrase.GetBranch(groupPhrase.Count - 1);
                        if (!(last.Part == "." || last.Part == "!" || last.Part == "?")) {
                            List<IParsedPhrase> branches = new List<IParsedPhrase>();
                            branches.AddRange(phrase.Branches);
                            branches.Add((IParsedPhrase)final.Clone());
                            phrase = new GroupPhrase(phrase.Part, branches);
                        }
                    }
                }
                else
                {
                    List<IParsedPhrase> branches = new List<IParsedPhrase>();
                    branches.Add(phrase);
                    if (final != null)
                        branches.Add((IParsedPhrase)final.Clone());
                    phrase = new GroupPhrase("FRAG", branches);
                }
            }

            List<IContent> contents = new List<IContent>();
            contents.Add(new Word(phrase.Text));
            Context child = new Context(context, contents);
            succ.Continue(child, fail);

            return true;
        }
コード例 #28
0
 // default is failure to rescue
 public virtual bool CallRescue(Coderack coderack, IParsedPhrase input, PatternTemplateSource patternTemplateSource, string reason, IContinuation skip, IContinuation succ, IFailure fail)
 {
     return(fail.Fail(reason, skip));
 }
コード例 #29
0
 public bool Evaluate()
 {
     return(fail.Fail(reason, skip));
 }