def discoverObject(object):

    print(Mag+"Building concept to search KB for"+st,object.lemma_)

    features = languageTools.extractFeatures(object)
    facts = [["inst","?x",object.lemma_]]
    for f in features:
        type = kb.whatAmI(f.lemma_)
        facts.append([type,"?x",f.lemma_])
    print(Mag+"Looking for an ?x that matches:"+st)
    for f in facts:
        print("     ",f)
    bindings=kb.betterAsk(facts)
    if len(bindings) != 1:
        if object.text[-1] == "s":
            print(Mag+"Found reference in KB:")
            elements = [a for a in map(lambda x: x["?x"], bindings)]
            recentMemory.addElements(object.lemma_,elements)
            return elements
        else:
            print(Mag+"We have a problem with too many objects matching in the KB"+st)
    elif bindings == False:
        print(Mag+"We have a problem because nothing matches these facts in the KB"+st)
    else:
        print(Mag+"Found reference in KB:"+st,bindings[0]["?x"])
        recentMemory.addElements(object.lemma_,[bindings[0]["?x"]])
        return [bindings[0]["?x"]]
def discoverObject(object):

    print(Mag + "Building concept to search KB for" + st, object.lemma_)

    features = languageTools.extractFeatures(object)
    facts = [["inst", "?x", object.lemma_]]
    for f in features:
        type = kb.whatAmI(f.lemma_)
        facts.append([type, "?x", f.lemma_])
    print(Mag + "Looking for an ?x that matches:" + st)
    for f in facts:
        print("     ", f)
    bindings = kb.betterAsk(facts)
    if len(bindings) != 1:
        if object.text[-1] == "s":
            print(Mag + "Found reference in KB:")
            elements = [a for a in map(lambda x: x["?x"], bindings)]
            recentMemory.addElements(object.lemma_, elements)
            return elements
        else:
            print(
                Mag +
                "We have a problem with too many objects matching in the KB" +
                st)
    elif bindings == False:
        print(
            Mag +
            "We have a problem because nothing matches these facts in the KB" +
            st)
    else:
        print(Mag + "Found reference in KB:" + st, bindings[0]["?x"])
        recentMemory.addElements(object.lemma_, [bindings[0]["?x"]])
        return [bindings[0]["?x"]]
def discoverQuantifiedObjects(object):

    count_number = utilities.countNumber(False,object)
    reference = languageTools.trackReference(object)

    print(Cyn+"Building concept to search KB for"+st,object.lemma_, "'"+reference.lemma_+"'")

    features = languageTools.extractFeatures(object)
    facts = [["inst","?x",reference.lemma_]]
    for f in features:
        type = kb.whatAmI(f.lemma_)
        facts.append([type,"?x",f.lemma_])
    print(rd+"Looking for an ?x that matches:"+st)
    for f in facts:
        print("     ",f)
    bindings=kb.betterAsk(facts)
    if len(bindings) < count_number:
        print(Cyn+"We have a problem with too few objects matching in the KB"+st)
    elif bindings == False:
        print(Cyn+"We have a problem because nothing matches these facts in the KB"+st)
    else:
        print(Cyn+"Found references in KB:")
        print(Cyn+"Selecting a subset from",len(bindings),"elements")
        candidates = [a for a in map(lambda x: x["?x"], bindings)]
        print(longTermMemory)
        remaining = longTermMemory.getElementAndStatus(reference.lemma_, "remaining")
        if remaining:
            candidates = [a for a in filter(lambda x: x in remaining, candidates)]
            print(Cyn+"Filtering off of last statement")
        names = candidates[0:count_number]
        others = candidates[count_number:]
        print(Cyn+"Selected"+st, names)
        recentMemory.addElements(reference.lemma_, names)
        recentMemory.addElementAndStatus(reference.lemma_, others, "remaining")
        return names
def discoverQuantifiedObjects(object):

    count_number = utilities.countNumber(False,object)
    reference = languageTools.trackReference(object)

    print(Cyn+"Building concept to search KB for"+st,object.lemma_, "'"+reference.lemma_+"'")

    features = languageTools.extractFeatures(object)
    facts = [["inst","?x",reference.lemma_]]
    for f in features:
        type = kb.whatAmI(f.lemma_)
        facts.append([type,"?x",f.lemma_])
    print(rd+"Looking for an ?x that matches:"+st)
    for f in facts:
        print("     ",f)
    bindings=kb.betterAsk(facts)
    if len(bindings) < count_number:
        print(Cyn+"We have a problem with too few objects matching in the KB"+st)
    elif bindings == False:
        print(Cyn+"We have a problem because nothing matches these facts in the KB"+st)
    else:
        print(Cyn+"Found references in KB:")
        print(Cyn+"Selecting a subset from",len(bindings),"elements")
        candidates = [a for a in map(lambda x: x["?x"], bindings)]
        print(longTermMemory)
        remaining = longTermMemory.getElementAndStatus(reference.lemma_, "remaining")
        if remaining:
            candidates = [a for a in filter(lambda x: x in remaining, candidates)]
            print(Cyn+"Filtering off of last statement")
        names = candidates[0:count_number]
        others = candidates[count_number:]
        print(Cyn+"Selected"+st, names)
        recentMemory.addElements(reference.lemma_, names)
        recentMemory.addElementAndStatus(reference.lemma_, others, "remaining")
        return names
def resolveObjectFOPC(object):

    global recentMemory

    # First, we want to find out if the object is the name for anything we know about
    # To do this, we need to find out if it is a proper noun and if it exists in the KB.
    # If it does, then we are just going to return its name.

    print(lf + Grn + "Resolving object:" + st, object.lemma_)

    if object.pos_ == "PROPN":
        print(object.text, Grn + " is a proper name." + st)
        inst = kb.betterAsk(["inst", object.text, "?x"])
        recentMemory.addElements(inst, [object.text])
        print(Grn + "Returning" + st, object.text)
        return [object.text]

    # The object might also be a referent that quantifies something else. When we identify that we
    # then have to go find it.

    if object.pos_ == "ADJ":
        objects = discoverReferencedObjects(object)
        print(Grn + "Returning" + st, objects)
        return objects

    # Next we need to do is find out the determiner. If it is 'the', we are referring
    # an existing object and need to look it up.

    determiner = languageTools.extractDeterminer(object)
    if determiner != None:
        if determiner.lemma_ == "the":
            new_obj = discoverObject(object)
            print(Grn + "Returning" + st, new_obj)
            return new_obj

# The object might also be a referent that quantifies something else. When we identify that we
# then have to go find it.

    if object.pos_ == "NUM":
        new_objs = discoverQuantifiedObjects(object)
        print(Grn + "Returning" + st, new_objs)
        return new_objs

    # If it is neither an existing object in the KB nor the name of a thing, then it is something
    # we have to build and assert

    new_objs = buildNewObject(object)
    print(Grn + "Returning" + st, new_objs)
    return new_objs
def resolveObjectFOPC(object):

    global recentMemory

    # First, we want to find out if the object is the name for anything we know about
    # To do this, we need to find out if it is a proper noun and if it exists in the KB.
    # If it does, then we are just going to return its name.

    print(lf+Grn+"Resolving object:"+st, object.lemma_)

    if object.pos_ == "PROPN":
        print(object.text, Grn+" is a proper name."+st)
        inst = kb.betterAsk(["inst", object.text,"?x"])
        recentMemory.addElements(inst,[object.text])
        print(Grn+"Returning"+st, object.text)
        return [object.text]

    # The object might also be a referent that quantifies something else. When we identify that we
    # then have to go find it.

    if object.pos_ == "ADJ":
        objects = discoverReferencedObjects(object)
        print(Grn+"Returning"+st, objects)
        return objects

    # Next we need to do is find out the determiner. If it is 'the', we are referring
    # an existing object and need to look it up.

    determiner = languageTools.extractDeterminer(object)
    if determiner != None:
        if determiner.lemma_ == "the":
            new_obj = discoverObject(object)
            print(Grn+"Returning"+st, new_obj)
            return new_obj

# The object might also be a referent that quantifies something else. When we identify that we
    # then have to go find it.

    if object.pos_ == "NUM":
        new_objs = discoverQuantifiedObjects(object)
        print(Grn+"Returning"+st, new_objs)
        return new_objs

    # If it is neither an existing object in the KB nor the name of a thing, then it is something
    # we have to build and assert

    new_objs = buildNewObject(object)
    print(Grn+"Returning"+st, new_objs)
    return new_objs
 def test04_02(self):
     core.buildFOPC('The pyramids are on the metal table')
     self.assertTrue(kb.betterAsk([['on', 'Block11', 'Table1']]))
 def test03_02(self):
     core.buildFOPC('The boxes are big')
     self.assertTrue(kb.betterAsk([['size', 'Box1', 'big']]))
 def test02_02(self):
     core.buildFOPC('The green blocks are pyramids')
     self.assertTrue(kb.betterAsk([['inst', 'Block11', 'pyramid']]))
 def test01_03(self):
     core.buildFOPC('Three of the red blocks are cubes')
     self.assertTrue(kb.betterAsk([['inst', 'Block2', 'cube']]))
 def test04_02(self):
     core.buildFOPC('The pyramids are on the metal table')
     self.assertTrue(kb.betterAsk([['on', 'Block11', 'Table1']]))
 def test03_02(self):
     core.buildFOPC('The boxes are big')
     self.assertTrue(kb.betterAsk([['size', 'Box1', 'big']]))
 def test02_02(self):
     core.buildFOPC('The green blocks are pyramids')
     self.assertTrue(kb.betterAsk([['inst', 'Block11', 'pyramid']]))
 def test01_03(self):
     core.buildFOPC('Three of the red blocks are cubes')
     self.assertTrue(kb.betterAsk([['inst', 'Block2', 'cube']]))