public Datum(Concept left, Relations.Relation relation, Concept right, double score) { this.left = left; this.relation = relation; this.right = right; this.score = score; }
public Datum(Datum parent) { left = parent.left; relation = parent.relation; right = parent.right; score = parent.score; }
public virtual IParsedPhrase ConceptToPhrase(Context context, Concept concept, POSTagger tagger, GrammarParser parser) { if (concept.IsUnknown) return null; return concept.ToPhrase(tagger, parser); }
public Memory() { concepts = new Dictionary<string,List<Concept>>(); data = new Dictionary<Concept,List<Datum>>(); specialCount = 0; time = SpecialConcept("time", Concept.Kind.Entity); now = SpecialConcept("now", Concept.Kind.Entity); past = SpecialConcept("past", Concept.Kind.Entity); here = SpecialConcept("here", Concept.Kind.Entity); self = SpecialConcept("I", Concept.Kind.Entity); future = SpecialConcept("future", Concept.Kind.Entity); you = SpecialConcept("you", Concept.Kind.Entity); they = SpecialConcept("they", Concept.Kind.Entity); Know(now, Relations.Relation.IsA, time, 1.0); Know(past, Relations.Relation.IsA, time, 1.0); Know(past, Relations.Relation.Precedes, now, 1.0); Know(now, Relations.Relation.Precedes, future, 1.0); Concept dasein = SpecialConcept("[*]", Concept.Kind.Entity); // the phenomological awareness Know(dasein, Relations.Relation.InLocation, here, 1.0); Know(dasein, Relations.Relation.AtTime, now, 1.0); Know(dasein, Relations.Relation.Subject, self, 1.0); }
public virtual bool Match(Context env, Concept check) { if (IsMatch(check)) { Propogate(env, check, 1.0); return true; } return false; }
public void AddConcept(string name, Concept concept) { List<Concept> current; if (!concepts.TryGetValue(name, out current)) { current = new List<Concept>(); concepts.Add(name, current); } current.Add(concept); }
public static Verbs.Convert GetTense(Concept context, Concept within, Memory memory) { if (context.Precedes(within, memory)) // past < past, past < now return Verbs.Convert.ext_Ved; /*if (context.DerivesFrom(memory.now, memory, dbquery) && within.DerivesFrom(memory.now, memory, dbquery)) // now == now return Verbs.Convert.ext_Vs;*/ /*if (within.Precedes(context, memory, dbquery)) // past > past, now < past return Verbs.Convert.ext_V; // will...*/ return Verbs.Convert.ext_Vs; }
public virtual bool Match(Context env, Concept check, GetValue propfunc, params object[] args) { // Apply propfunc now, and save for later object value = propfunc(check, args); bool matched = false; if (value is Concept) matched = IsMatch((Concept) value); else if (value is IParsedPhrase) matched = IsMatch((IParsedPhrase) value); if (matched) { object propogate = new ThreeTuple<GetValue, object, object[]>(propfunc, check, args); Propogate(env, propogate, 1.0); return true; } return false; }
public double IsSameUnder(Datum datum, Concept before, Concept after) { if ((left == datum.left || (left == before && datum.left == after)) && (right == datum.right || (right == before && datum.right == after)) && (relation == datum.relation)) return 1.0 - Math.Abs(score - datum.score) / (score + datum.score); return 0.0; }
public void ChangeRight(Concept right, Memory memory) { memory.GetData(right).Remove(this); this.right = right; memory.GetData(right).Add(this); }
public void ChangeLeft(Concept left, Memory memory) { memory.GetData(left).Remove(this); this.left = left; memory.GetData(left).Add(this); }
public override bool Evaluate() { // Look through my concepts for a possible match List <IContent> contents = context.Contents; if (contents.Count == 1) { if (contents[0] is Variable) { Variable check = (Variable)contents[0]; // If we already have a concept in mind, just check its data Concept left = context.LookupDefaulted <Concept>("$knowConcept", null); if (left != null) { List <Datum> data = memory.GetData(left); foreach (Datum datum in data) { if (datum.Left == left && kinds.Contains(datum.Relation) && check.Match(context, datum.Right)) { List <Datum> completes = context.LookupAndAdd <List <Datum> >("$knowCompletes", new List <Datum>()); completes.Add(datum); succ.Continue(new Context(context, new List <IContent>()), fail); return(true); } } // Can't match anything! fail.Fail("Initial variable didn't match anything", succ); return(true); } // Does anything here match? for (int ii = 0; ii < directchecks.Count; ii++) { bool matched = kinds.Contains(directchecks[ii].Relation); if (matched) { matched = check.Match(context, directchecks[ii].Right); if (propfunc != null) // always do propfuncs, but only affect matched if failed { matched = check.Match(context, directchecks[ii].Right, propfunc, propargs) || matched; } } if (matched) { Context child = new Context(context, new List <IContent>()); child.Map["$knowConcept"] = directchecks[ii].Left; List <Datum> completes = child.LookupAndAdd <List <Datum> >("$knowCompletes", new List <Datum>()); completes.Add(directchecks[ii]); // Make a clone of ourselves, if this fails succ.Continue(child, MakeContinuingFailure(ii)); return(true); } } } else if (contents[0].Name == "*" || contents[0].Name == "_") { // If we already have a concept in mind, just check its data Concept left = context.LookupDefaulted <Concept>("$knowConcept", null); if (left != null) { List <Datum> data = memory.GetData(left); foreach (Datum datum in data) { if (datum.Left == left && kinds.Contains(datum.Relation)) { List <IContent> words = new List <IContent>(); if (!datum.Right.IsUnknown) { words.Add(new Word(datum.Right.Name)); } context.Map.Add(StarUtilities.NextStarName(context, contents[0].Name), words); List <Datum> completes = context.LookupAndAdd <List <Datum> >("$knowCompletes", new List <Datum>()); completes.Add(datum); succ.Continue(new Context(context, new List <IContent>()), fail); return(true); } } // Can't match anything! fail.Fail("Could not find a matching datum", succ); return(true); } // Does anything here match? for (int ii = 0; ii < directchecks.Count; ii++) { if (kinds.Contains(directchecks[ii].Relation)) { Context child = new Context(context, new List <IContent>()); List <IContent> words = new List <IContent>(); if (!directchecks[ii].Right.IsUnknown) { words.Add(new Word(directchecks[ii].Right.Name)); } child.Map.Add(StarUtilities.NextStarName(child, contents[0].Name), words); child.Map["$knowConcept"] = directchecks[ii].Left; List <Datum> completes = child.LookupAndAdd <List <Datum> >("$knowCompletes", new List <Datum>()); completes.Add(directchecks[ii]); succ.Continue(child, MakeContinuingFailure(ii)); return(true); } } } // Nothing matched! Expand search if (parentchecks.Count > 0) { Concept parent = parentchecks.Dequeue(); directchecks = memory.GetData(parent); Continue(new Context(context, context.Contents, context.Weight * 0.95), fail); return(true); } } // Don't fail if it's just a * if (contents.Count == 1 && contents[0].Name == "*") { Context child = new Context(context, new List <IContent>()); List <IContent> empty = new List <IContent>(); child.Map.Add(StarUtilities.NextStarName(child, contents[0].Name), empty); succ.Continue(child, fail); return(true); } fail.Fail("No data matches", succ); return(true); }
public static IParsedPhrase ConjugateToPhrase(Memory memory, IrregularVerbVariable.ConjugateVerb conjugate, Verbs.Person person, Concept right) { if (right == memory.past) { return(new WordPhrase(conjugate(person, Verbs.Convert.ext_Ved), "VBD")); } else if (right == memory.now) { return(new WordPhrase(conjugate(person, Verbs.Convert.ext_Vs), "VBZ")); } else if (right == memory.future) { return(new WordPhrase("will " + conjugate(person, Verbs.Convert.ext_V), "VB")); } List <string> parts = StringUtilities.SplitWords(right.Name, true); bool usedverb = false; for (int ii = 0; ii < parts.Count; ii++) { if (parts[ii] == "en") { parts[ii] = conjugate(person, Verbs.Convert.ext_Ven); usedverb = true; } else if (parts[ii] == "ing") { parts[ii] = conjugate(person, Verbs.Convert.ext_Ving); usedverb = true; } } if (!usedverb) { parts.Add(conjugate(person, Verbs.Convert.ext_V)); } List <IParsedPhrase> branches = new List <IParsedPhrase>(); branches.Add(new WordPhrase(StringUtilities.JoinWords(parts), "VB")); return(new GroupPhrase("VP", branches)); }
public static Datum ConceptNetReverseTranslate(Memory memory, Verbs verbs, Concept right, Assertion assertion) { double score = 1.0 - 1.0 / (assertion.Score + 1); if (assertion.Type == Associator.AtLocation) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.InLocation, right, score)); } if (assertion.Type == Associator.CapableOf) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.CapableOf, right, score)); } if (assertion.Type == Associator.Causes) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.Cause, right, score)); } if (assertion.Type == Associator.CausesDesire) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.CausesDesire, right, score)); } if (assertion.Type == Associator.CreatedBy) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.CreatedBy, right, score)); } if (assertion.Type == Associator.DefinedAs) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, right.RawKind), Relation.Means, right, score)); } if (assertion.Type == Associator.Desires) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.Desires, right, score)); } if (assertion.Type == Associator.HasFirstSubevent) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasFirstSubevent, right, score)); } if (assertion.Type == Associator.HasLastSubevent) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasLastSubevent, right, score)); } if (assertion.Type == Associator.HasPainCharacter) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasPainCharacter, right, score)); } if (assertion.Type == Associator.HasPainIntensity) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasPainIntensity, right, score)); } if (assertion.Type == Associator.HasPrerequisite) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.Condition, right, score)); } if (assertion.Type == Associator.HasProperty) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.HasProperty, right, score)); } if (assertion.Type == Associator.HasSubevent) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasSubevent, right, score)); } if (assertion.Type == Associator.IsA) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, right.RawKind), Relation.IsA, right, score)); } if (assertion.Type == Associator.MadeOf) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.SubstanceMeronym, right, score)); } if (assertion.Type == Associator.MotivatedByGoal) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.MotivatedByGoal, right, score)); } if (assertion.Type == Associator.PartOf) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.PartMeronym, right, score)); } if (assertion.Type == Associator.ReceivesAction) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.ReceivesAction, right, score)); } if (assertion.Type == Associator.UsedFor) { return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.UsedFor, right, score)); } //throw new ArgumentException("Unknown assertion type: " + assertion.Type.ToString()); return(new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.Unknown, right, score)); }
public Datum Know(Concept left, Relations.Relation relation, IEnumerable<Concept> rights, double score) { Datum last = null; foreach (Concept right in rights) last = Know(new Datum(left, relation, right, score)); return last; }
// Load the concept if it isn't already public List<Datum> GetData(Concept concept) { List<Datum> matches = new List<Datum>(); data.TryGetValue(concept, out matches); return matches; }
public static string RepresentToHave(Memory memory, Concept concept, Concept within, Verbs verbs, Nouns nouns) { return Verbs.ComposeToHave(nouns.GetPerson(concept.Name), GetTense(concept, within, memory)); }
public UnknownConceptVariable(string name, Concept.Kind? kind) : base("name") { this.kind = kind; }
public virtual bool IsMatch(Concept concept) { return false; }
public override bool IsMatch(Concept concept) { return (!kind.HasValue || concept.RawKind == kind.Value); }
public string Represent(Memory memory, Concept within, Verbs verbs, Nouns nouns) { string[] sentence = new string[] { }; switch (relation) { case Relations.Relation.InLocation: sentence = new string[] { left.Name, RepresentToBe(memory, left, within, verbs, nouns), "in", right.Name, " ." }; break; case Relations.Relation.HasA: sentence = new string[] { left.Name, RepresentToHave(memory, left, within, verbs, nouns), right.Name, " ." }; break; case Relations.Relation.HasProperty: sentence = new string[] { left.Name, RepresentToBe(memory, left, within, verbs, nouns), right.Name, " ." }; break; case Relations.Relation.IsA: sentence = new string[] { left.Name, RepresentToBe(memory, left, within, verbs, nouns), right.Name, " ." }; break; } return StringUtilities.JoinWords(new List<string>(sentence)); }
public static string ConjugateToTense(Memory memory, string verb, Verbs.Person person, Concept right, Verbs verbs) { if (right == memory.past) { return(verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ved)); } if (right == memory.now) { return(verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Vs)); } if (right == memory.future) { return("will " + verbs.ComposePersonable(verb, person, Verbs.Convert.ext_V)); } List <string> parts = StringUtilities.SplitWords(right.Name, true); bool usedverb = false; for (int ii = 0; ii < parts.Count; ii++) { if (parts[ii] == "en") { parts[ii] = verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ven); usedverb = true; } else if (parts[ii] == "ing") { parts[ii] = verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ving); usedverb = true; } } if (!usedverb) { parts.Add(verbs.InputToBase(verb)); } return(StringUtilities.JoinWords(parts)); }
public Concept NewConcept(string name, Concept.Kind kind) { List<Concept> matches; if (!concepts.TryGetValue(name, out matches)) { matches = new List<Concept>(); concepts.Add(name, matches); } Concept concept = new Concept(name, kind); data.Add(concept, new List<Datum>()); matches.Add(concept); return concept; }
protected Concept SpecialConcept(string name, Concept.Kind kind) { Concept concept = NewConcept(name, kind); specialCount++; concept.Id = -specialCount; return concept; }
public static string RepresentToHave(Memory memory, Concept concept, Concept within, Verbs verbs, Nouns nouns) { return(Verbs.ComposeToHave(nouns.GetPerson(concept.Name), GetTense(concept, within, memory))); }
public Concept GetConcept(string name, int id, Concept.Kind kind) { if (name == null) Console.WriteLine("But how?"); List<Concept> matches; if (concepts.TryGetValue(name, out matches)) { foreach (Concept match in matches) if (match.Id == id) return match; } else { matches = new List<Concept>(); concepts.Add(name, matches); } Concept concept = new Concept(name, kind); concept.Id = id; data.Add(concept, new List<Datum>()); matches.Add(concept); return concept; }
public Datum Know(Concept left, Relations.Relation relation, Concept right, double score) { return(Know(new Datum(left, relation, right, score))); }
public Datum Know(Concept left, Relations.Relation relation, Concept right, double score) { return Know(new Datum(left, relation, right, score)); }
public Datum Know(Concept left, Datum partial) { return(Know(new Datum(left, partial.Relation, partial.Right, partial.Score))); }
public Datum Know(Concept left, Datum partial) { return Know(new Datum(left, partial.Relation, partial.Right, partial.Score)); }
public static Concept ConceptNetGetConcept(Memory memory, Verbs verbs, Notion notion, Concept.Kind kind) { if (kind == Concept.Kind.Event) return memory.NewConcept(verbs.InputToBase(notion.Canonical), kind); else return memory.NewConcept(notion.Canonical, kind); }
public Concept NewUnknown(Concept.Kind kind) { return NewConcept("", kind); }
public static Datum ConceptNetReverseTranslate(Memory memory, Verbs verbs, Concept right, Assertion assertion) { double score = 1.0 - 1.0 / (assertion.Score + 1); if (assertion.Type == Associator.AtLocation) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.InLocation, right, score); if (assertion.Type == Associator.CapableOf) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.CapableOf, right, score); if (assertion.Type == Associator.Causes) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.Cause, right, score); if (assertion.Type == Associator.CausesDesire) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.CausesDesire, right, score); if (assertion.Type == Associator.CreatedBy) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.CreatedBy, right, score); if (assertion.Type == Associator.DefinedAs) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, right.RawKind), Relation.Means, right, score); if (assertion.Type == Associator.Desires) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.Desires, right, score); if (assertion.Type == Associator.HasFirstSubevent) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasFirstSubevent, right, score); if (assertion.Type == Associator.HasLastSubevent) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasLastSubevent, right, score); if (assertion.Type == Associator.HasPainCharacter) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasPainCharacter, right, score); if (assertion.Type == Associator.HasPainIntensity) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasPainIntensity, right, score); if (assertion.Type == Associator.HasPrerequisite) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.Condition, right, score); if (assertion.Type == Associator.HasProperty) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.HasProperty, right, score); if (assertion.Type == Associator.HasSubevent) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Event), Relation.HasSubevent, right, score); if (assertion.Type == Associator.IsA) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, right.RawKind), Relation.IsA, right, score); if (assertion.Type == Associator.MadeOf) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.SubstanceMeronym, right, score); if (assertion.Type == Associator.MotivatedByGoal) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.MotivatedByGoal, right, score); if (assertion.Type == Associator.PartOf) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.PartMeronym, right, score); if (assertion.Type == Associator.ReceivesAction) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.ReceivesAction, right, score); if (assertion.Type == Associator.UsedFor) return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.UsedFor, right, score); //throw new ArgumentException("Unknown assertion type: " + assertion.Type.ToString()); return new Datum(ConceptNetGetConcept(memory, verbs, assertion.Left, Concept.Kind.Entity), Relation.Unknown, right, score); }
public static IParsedPhrase ConjugateToPhrase(Memory memory, IrregularVerbVariable.ConjugateVerb conjugate, Verbs.Person person, Concept right) { if (right == memory.past) return new WordPhrase(conjugate(person, Verbs.Convert.ext_Ved), "VBD"); else if (right == memory.now) return new WordPhrase(conjugate(person, Verbs.Convert.ext_Vs), "VBZ"); else if (right == memory.future) return new WordPhrase("will " + conjugate(person, Verbs.Convert.ext_V), "VB"); List<string> parts = StringUtilities.SplitWords(right.Name, true); bool usedverb = false; for (int ii = 0; ii < parts.Count; ii++) { if (parts[ii] == "en") { parts[ii] = conjugate(person, Verbs.Convert.ext_Ven); usedverb = true; } else if (parts[ii] == "ing") { parts[ii] = conjugate(person, Verbs.Convert.ext_Ving); usedverb = true; } } if (!usedverb) parts.Add(conjugate(person, Verbs.Convert.ext_V)); List<IParsedPhrase> branches = new List<IParsedPhrase>(); branches.Add(new WordPhrase(StringUtilities.JoinWords(parts), "VB")); return new GroupPhrase("VP", branches); }
public static string ConjugateToTense(Memory memory, string verb, Verbs.Person person, Concept right, Verbs verbs) { if (right == memory.past) return verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ved); if (right == memory.now) return verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Vs); if (right == memory.future) return "will " + verbs.ComposePersonable(verb, person, Verbs.Convert.ext_V); List<string> parts = StringUtilities.SplitWords(right.Name, true); bool usedverb = false; for (int ii = 0; ii < parts.Count; ii++) { if (parts[ii] == "en") { parts[ii] = verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ven); usedverb = true; } else if (parts[ii] == "ing") { parts[ii] = verbs.ComposePersonable(verb, person, Verbs.Convert.ext_Ving); usedverb = true; } } if (!usedverb) parts.Add(verbs.InputToBase(verb)); return StringUtilities.JoinWords(parts); }
public Concept BeforeContext(Concept when) { if (when == now || when == past) return past; Concept before = NewConcept("before " + when.Name, when.RawKind); Know(before, Relations.Relation.Precedes, when, 1.0); return before; }
public ProbableStrength SeemsSimilar(Concept other) { if (other.kind != kind) return ProbableStrength.Zero; if (other.name == name) return ProbableStrength.Full; // for now, could be... return ProbableStrength.Half; }