Example #1
0
def atom_from_tree(tree, a):
    if tree.is_variable():
        return a.add(types.VariableNode, name='$'+str(tree.op))
    elif tree.is_leaf():
        # Node (simply the handle)        
        if isinstance (tree.op, Atom):
            return tree.op
        # Empty Link
        else:
            return a.add(get_type(tree.op), out = [])
    else:
        out = [atom_from_tree(x, a) for x in tree.args]
        return a.add(get_type(tree.op), out=out)
Example #2
0
def atom_from_tree(tree, a):
    if tree.is_variable():
        return a.add(types.VariableNode, name='$' + str(tree.op))
    elif tree.is_leaf():
        # Node (simply the handle)
        if isinstance(tree.op, Atom):
            return tree.op
        # Empty Link
        else:
            return a.add(get_type(tree.op), out=[])
    else:
        out = [atom_from_tree(x, a) for x in tree.args]
        return a.add(get_type(tree.op), out=out)
Example #3
0
 def get_type(self):
     if self.is_variable():
         return types.Atom
     elif isinstance(self.op, Atom):
         return self.op.t
     else:
         return get_type(self.op)
Example #4
0
 def get_type(self):
     if self.is_variable():
         return types.Atom
     elif isinstance(self.op, Atom):
         return self.op.t
     else:
         return get_type(self.op)
Example #5
0
def write_atoms(atomspace, cn_assertion, template_no, link_type=''):
    # Assertion is a list. 0.5 confidence is used(for links)because that is
    # the weight given to most of the assertions on ConceptNet
    TV = TruthValue(1, .5)

    cn_argument1=cn_assertion[1][6:]
    cn_argument2=cn_assertion[2][6:]
    cn_arg1_stv=set_TV(cn_argument1)
    cn_arg2_stv=set_TV(cn_argument2)

    cn1 = atomspace.add_node(types.ConceptNode, cn_argument1, tv=cn_arg1_stv)
    cn2 = atomspace.add_node(types.ConceptNode, cn_argument2, tv=cn_arg2_stv)

    if template_no == 1:
        link = atomspace.add_link(get_type(link_type), [cn1, cn2], tv=TV)

        return repr(link)
    elif template_no == 2:
        cn_relation=cn_assertion[0][3:]
        cn_rel_stv=set_TV(cn_assertion[0][3:])
        pn = atomspace.add_node(types.PredicateNode, cn_relation, tv=cn_rel_stv)

        listlink = atomspace.add_link(types.ListLink, [cn1, cn2])
        evallink = atomspace.add_link(types.EvaluationLink, [pn, listlink], tv=TV)
        return repr(evallink)
Example #6
0
    def link_connect_impl(self, decided_atoms, new_blended_atom, config_base):
        check_type_str = BlendConfig().get_str(self.a, "connect-check-type",
                                               config_base)
        strength_diff_threshold = BlendConfig().get_str(
            self.a, "connect-strength-diff-limit", config_base)
        confidence_above_threshold = BlendConfig().get_str(
            self.a, "connect-confidence-above-limit", config_base)
        viable_atoms_count_threshold = BlendConfig().get_str(
            self.a, "connect-viable-atoms-count-limit", config_base)

        self.check_type = get_type(check_type_str)

        if check_type_str != get_type_name(self.check_type):
            raise UserWarning("Can't resolve type" + str(check_type_str))

        try:
            self.strength_diff_limit = \
                float(strength_diff_threshold)
            self.confidence_above_limit = \
                float(confidence_above_threshold)
        except (TypeError, ValueError):
            raise UserWarning(
                "Can't parse threshold value:: "
                "{strength: {0}, confidence: {1}, count: {2}}".format(
                    str(strength_diff_threshold),
                    str(confidence_above_threshold),
                    str(viable_atoms_count_threshold)))

        try:
            self.viable_atoms_count_threshold = \
                int(viable_atoms_count_threshold)
        except ValueError:
            self.viable_atoms_count_threshold = None

        self.__connect_links_simple(decided_atoms, new_blended_atom)
Example #7
0
def is_sequential_and(atom):
    """Return True iff atom is a sequential and.

    Also for now we use AltSequentialAndLink.

    """

    return is_a(atom.type, get_type("AltSequentialAndLink"))
Example #8
0
def is_sequential_and(atom: Atom) -> bool:
    """Return True iff atom is a sequential and.

    Also for now we use BackSequentialAndLink.

    """

    return is_a(atom.type, get_type("BackSequentialAndLink"))
Example #9
0
    def __make_atoms(self, assertion, is_eval_link, link_type=''):
        # /c/en/computer -> computer
        node_1_name = assertion[1][6:]
        node_2_name = assertion[2][6:]

        node_1_stv = self.make_opencog_tv(node_1_name)
        node_2_stv = self.make_opencog_tv(node_2_name)

        link_confidence = float(assertion[4])
        if link_confidence > 0.0:
            # For true statements we give a strength of 0.5 to 1.0, ramping up
            # quickly towards 1.0 as the cn_confidence grows.
            confidence_value = 1.0 - math.pow(0.2, link_confidence)/2.0
        else:
            # For any false statement we currently just set a strength of 0.0,
            # basically ignoring the ConceptNet confidence value other than
            # to note that it was false.
            # TODO: This could probably be improved, but not sure how.
            confidence_value = 0.0

        # For the STV we use the computed confidence from ConceptNet as the
        # 'strength'. Currently just using a 'confidence' of ~0.9 indicating
        # that whatever ConceptNet thinks the truth value is, we agree strongly
        # with this analysis. Maybe something better could be done, hard to say
        # for sure though. The 10000 gets converted to the confidence of ~0.9
        # by the constructor.
        link_tv = TruthValue(confidence_value, 10000)

        node_1 = self.a.add_node(types.ConceptNode, node_1_name, tv=node_1_stv)
        node_2 = self.a.add_node(types.ConceptNode, node_2_name, tv=node_2_stv)

        if is_eval_link:
            relation = assertion[0][3:]
            predicate_node = self.a.add_node(
                types.PredicateNode,
                relation,
                tv=link_tv
            )
            list_link = self.a.add_link(types.ListLink, [node_1, node_2])
            eval_link = self.a.add_link(
                types.EvaluationLink,
                [predicate_node, list_link],
                tv=link_tv
            )
            return eval_link
        else:
            link = self.a.add_link(
                get_type(link_type),
                [node_1, node_2],
                tv=link_tv
            )
            return link
Example #10
0
def load_instance2type(filename):
    """
    Load a filename generated by sumo-instance-types.py that
    associates each SUMO instance and its atom type and construct a
    dictionary that maps each instance to its atom type.
    """
    i2t = defaultdict(lambda: types.ConceptNode)
    with open(filename, 'rt') as i2tfile:
        for line in i2tfile:
            tokens = line.split()
            instance = tokens[0]
            atom_type = get_type(tokens[1])
            i2t[instance] = atom_type
    return i2t
Example #11
0
def _atom_from_dict(space, d):
    (type_name,) = (d["type"],)
    (name,) = (d["name"],)
    (out,) = (d["outgoing"],)
    (tv,) = (_tv_from_dict(d["truthvalue"]),)

    out = [_atom_from_dict(space, o) for o in out]

    if out == []:
        out = None
    if type_name.endswith("Link"):
        name = None

    # print get_type(type_name), name, out, tv
    atom = space.add(get_type(type_name), name=name, out=out, tv=tv)
    return atom
Example #12
0
def _atom_from_dict(space, d):
    (type_name, ) = d['type'],
    (name, ) = d['name'],
    (out, ) = d['outgoing'],
    (tv, ) = _tv_from_dict(d['truthvalue']),

    out = [_atom_from_dict(space, o) for o in out]

    if out == []:
        out = None
    if type_name.endswith('Link'):
        name = None

    #print get_type(type_name), name, out, tv
    atom = space.add(get_type(type_name), name=name, out=out, tv=tv)
    return atom
Example #13
0
def _atom_from_dict(space, d):
    (type_name,) = d['type'],
    (name,) = d['name'],
    (out,) = d['outgoing'],
    (tv,) = _tv_from_dict(d['truthvalue']),
    
    out = [_atom_from_dict(space, o) for o in out]
    
    if out == []:
        out = None
    if type_name.endswith('Link'):
        name = None
    
    #print get_type(type_name), name, out, tv
    atom =space.add(get_type(type_name), name=name,
              out = out, tv = tv)
    return atom
Example #14
0
    def __make_atoms(self, assertion, is_eval_link, link_type=''):
        # /c/en/computer -> computer
        node_1_name = assertion[1][6:]
        node_2_name = assertion[2][6:]

        node_1_stv = self.make_opencog_tv(node_1_name)
        node_2_stv = self.make_opencog_tv(node_2_name)

        link_confidence = float(assertion[4])
        if link_confidence > 0.0:
            # For true statements we give a strength of 0.5 to 1.0, ramping up
            # quickly towards 1.0 as the cn_confidence grows.
            confidence_value = 1.0 - math.pow(0.2, link_confidence) / 2.0
        else:
            # For any false statement we currently just set a strength of 0.0,
            # basically ignoring the ConceptNet confidence value other than
            # to note that it was false.
            # TODO: This could probably be improved, but not sure how.
            confidence_value = 0.0

        # For the STV we use the computed confidence from ConceptNet as the
        # 'strength'. Currently just using a 'confidence' of ~0.9 indicating
        # that whatever ConceptNet thinks the truth value is, we agree strongly
        # with this analysis. Maybe something better could be done, hard to say
        # for sure though. The 10000 gets converted to the confidence of ~0.9
        # by the constructor.
        link_tv = TruthValue(confidence_value, 10000)

        node_1 = self.a.add_node(types.ConceptNode, node_1_name, tv=node_1_stv)
        node_2 = self.a.add_node(types.ConceptNode, node_2_name, tv=node_2_stv)

        if is_eval_link:
            relation = assertion[0][3:]
            predicate_node = self.a.add_node(types.PredicateNode,
                                             relation,
                                             tv=link_tv)
            list_link = self.a.add_link(types.ListLink, [node_1, node_2])
            eval_link = self.a.add_link(types.EvaluationLink,
                                        [predicate_node, list_link],
                                        tv=link_tv)
            return eval_link
        else:
            link = self.a.add_link(get_type(link_type), [node_1, node_2],
                                   tv=link_tv)
            return link
Example #15
0
def write_atoms(atomspace, cn_assertion, template_no, link_type=''):

    cn_argument1 = cn_assertion[1][6:]
    cn_argument2 = cn_assertion[2][6:]
    cn_arg1_stv = set_TV(cn_argument1)
    cn_arg2_stv = set_TV(cn_argument2)
    cn_confidence = float(cn_assertion[4])
    if (cn_confidence > 0.0):
        # For true statements we give a strength of 0.5 to 1.0, ramping up
        # quickly towards 1.0 as the cn_confidence grows.
        confidence_value = 1.0 - math.pow(0.2, cn_confidence) / 2.0
    else:
        # For any false statement we currently just set a strength of 0.0, basically
        # ignoring the conceptnet confidence value other than to note that it was false.
        #TODO: This could probably be improved, but not sure how.
        confidence_value = 0.0

    # For the STV we use the computed confidence from conceptnet as the 'strength'.
    # Currently just using a 'confidence' of ~0.9 indicating that whatever
    # conceptnet thinks the truth value is, we agree strongly with this
    # analysis.  Maybe something better could be done, hard to say for sure
    # though.  The 10000 gets converted to the confidence of ~0.9 by the constructor.
    TV = TruthValue(confidence_value, 10000)

    cn1 = atomspace.add_node(types.ConceptNode, cn_argument1, tv=cn_arg1_stv)
    cn2 = atomspace.add_node(types.ConceptNode, cn_argument2, tv=cn_arg2_stv)

    if template_no == 1:
        link = atomspace.add_link(get_type(link_type), [cn1, cn2], tv=TV)

        return repr(link)
    elif template_no == 2:
        cn_relation = cn_assertion[0][3:]
        pn = atomspace.add_node(types.PredicateNode, cn_relation, tv=TV)

        listlink = atomspace.add_link(types.ListLink, [cn1, cn2])
        evallink = atomspace.add_link(types.EvaluationLink, [pn, listlink],
                                      tv=TV)
        return repr(evallink)
Example #16
0
def fetch_cogscms(atomspace: AtomSpace) -> set[Atom]:
    """Fetch all cognitive schematics from an given atomspace."""

    pit = get_type("BackPredictiveImplicationScopeLink")
    return set(atomspace.get_atoms_by_type(pit))
Example #17
0
def is_S(atom: Atom) -> bool:
    """Return True iff the atom is S ..."""

    return atom.type == get_type("SLink")
Example #18
0
def is_Z(atom: Atom) -> bool:
    """Return True iff the atom is Z."""

    return atom.type == get_type("ZLink")
Example #19
0
def is_predictive_implication_scope(atom: Atom) -> bool:
    """Return True iff the atom is a predictive implication scope link."""

    return is_a(atom.type, get_type("BackPredictiveImplicationScopeLink"))
Example #20
0
#! /usr/bin/env python
#
# is_a.py
#
"""
A simple example of how to use the nameserver.
"""

from opencog.atomspace import AtomSpace
from opencog.atomspace import get_type, is_a
from opencog.atomspace import types
from opencog.type_constructors import *

a = AtomSpace()

# Tell the type constructors which atomspace to use.
set_default_atomspace(a)

# Is a set unordered
set_is_unordered = is_a(get_type("SetLink"), get_type("UnorderedLink"))
print("Is a set unordered?", set_is_unordered)

# Is A a concept or a predicate?
A = ConceptNode("A")
print("Is A a concept?", is_a(A.type, get_type("ConceptNode")))
print("Is A a predicate?", is_a(A.type, get_type("PredicateNode")))
Example #21
0
 def test_get_type(self):
     self.assertEqual(get_type("ConceptNode"), types.ConceptNode)
     self.assertEqual(get_type(""), types.NO_TYPE)
     self.assertRaises(TypeError, get_type, 1)
Example #22
0
def is_predictive_implication(atom):
    """Return True iff the atom is a predictive implication link."""

    return is_a(atom.type, get_type("PredictiveImplicationLink"))
Example #23
0
 def test_get_type(self):
     self.assertEqual(get_type("ConceptNode"), types.ConceptNode)
     self.assertEqual(get_type(""), types.NO_TYPE)
     self.assertRaises(TypeError, get_type, 1)