<commit_before>from libcpp cimport bool
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref

# basic wrapping for std::string conversion
cdef extern from "<string>" namespace "std":
    cdef cppclass string:
        string()
        string(char *)
        char * c_str()

# Basic OpenCog types
ctypedef int UUID
ctypedef int Type

# Handle
cdef extern from "opencog/atomspace/Handle.h" namespace "opencog":
    cdef cppclass cHandle "opencog::Handle":
        cHandle()
        cHandle(UUID)
        UUID value()
        bint operator==(cHandle h)
        cHandle UNDEFINED


cdef class Handle:
    cdef cHandle *h
    def __cinit__(self, h):
        self.h=new cHandle(h)
    def __dealloc__(self):
        del self.h
    def value(self):
        return self.h.value()
    def __richcmp__(Handle h1, Handle h2, int op):
        if op == 2: # ==
            return h1.h.value() == h2.h.value()
        elif op == 3: # !=
            return h1.h.value() != h2.h.value()
        elif op == 4: # >
            return h1.h.value() > h2.h.value()
        elif op == 0: # <
            return h1.h.value() < h2.h.value()
        elif op == 1: # <=
            return h1.h.value() <= h2.h.value()
        elif op == 5: # >=
            return h1.h.value() >= h2.h.value()
    def __str__(self):
        return "<UUID:" + str(self.h.value()) + ">"
    def is_undefined(self):
        if deref(self.h) == self.h.UNDEFINED: return True
        return False

# TruthValue
ctypedef int count_t
ctypedef float confidence_t
ctypedef float strength_t

cdef extern from "opencog/atomspace/TruthValue.h" namespace "opencog":
    cdef cppclass cTruthValue "opencog::TruthValue":
        strength_t getMean()
        confidence_t getConfidence()
        count_t getCount()
        cTruthValue DEFAULT_TV()

cdef extern from "opencog/atomspace/SimpleTruthValue.h" namespace "opencog":
    cdef cppclass cSimpleTruthValue "opencog::SimpleTruthValue":
        cSimpleTruthValue(float, float)
        strength_t getMean()
        confidence_t getConfidence()
        count_t getCount()
        cTruthValue DEFAULT_TV()

cdef class TruthValue:
    cdef cTruthValue *cobj
    def __cinit__(self, float strength, float count):
        self.cobj = <cTruthValue*> new cSimpleTruthValue(strength,count)
    def __dealloc__(self):
        del self.cobj
    def mean(self):
        return deref(self.cobj).getMean()
    def confidence(self):
        return deref(self.cobj).getConfidence()
    def count(self):
        return deref(self.cobj).getCount()

# HandleSeq
#ctypedef vector[cHandle] HandleSeq

# TimeServer
cdef extern from "opencog/atomspace/TimeServer.h" namespace "opencog":
    cdef cppclass cTimeServer "opencog::TimeServer":
        TimeServer()

cdef class TimeServer:
    cdef cTimeServer *timeserver

    def __cinit__(self):
        #self.timeserver = &timeserver
        pass

    def __dealloc__(self):
        # Don't do anything because the AtomSpace takes care of cleaning up
        pass
# AtomSpace

cdef extern from "opencog/atomspace/AtomSpace.h" namespace "opencog":
    cdef cppclass cAtomSpace "opencog::AtomSpace":
        AtomSpace()

        cHandle addNode(Type t, string s)
        cHandle addNode(Type t, string s, cTruthValue tvn)

        cHandle addPrefixedNode(Type t, string s)
        cHandle addPrefixedNode(Type t, string s, cTruthValue tvn)

        cHandle addLink(Type t, vector[cHandle])
        cHandle addLink(Type t, vector[cHandle], cTruthValue tvn)

        int getSize()

        cTimeServer getTimeServer()
        void print_list "print" ()

cdef class AtomSpace:
    cdef cAtomSpace *atomspace
    cdef cTimeServer *timeserver

    # TODO how do we do a copy constructor that shares the AtomSpaceAsync?
    def __cinit__(self):
        self.atomspace = new cAtomSpace()

    def __dealloc__(self):
        del self.atomspace

    def add_node(self,Type t,n,TruthValue tv=None):
        # convert to string
        py_byte_string = n.encode('UTF-8')
        # create temporary cpp string
        cdef string *name = new string(py_byte_string)
        cdef cHandle result
        if tv is None:
            # get handle
            result = self.atomspace.addNode(t,deref(name))
        else:
            result = self.atomspace.addNode(t,deref(name),deref(tv.cobj))
        # delete temporary string
        del name
        if result == result.UNDEFINED: return None
        return Handle(result.value());

    def add_link(self,Type t,outgoing):
        # create temporary cpp vector
        cdef vector[cHandle] o_vect
        for h in outgoing:
            o_vect.push_back(deref((<Handle>h).h))
        # get handle
        cdef cHandle result = self.atomspace.addLink(t,o_vect)
        # delete temporary vector
        if result == result.UNDEFINED: return None
        return Handle(result.value());

    def size(self):
        return self.atomspace.getSize()

    #def get_time_server(self):
        #timeserver = &self.atomspace.getTimeServer()
        #return TimeServer(timeserver)

    def print_list(self):
        self.atomspace.print_list()
<commit_msg>add truthvalue parameter to add_link<commit_after>from libcpp cimport bool
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref

# basic wrapping for std::string conversion
cdef extern from "<string>" namespace "std":
    cdef cppclass string:
        string()
        string(char *)
        char * c_str()

# Basic OpenCog types
ctypedef int UUID
ctypedef int Type

# Handle
cdef extern from "opencog/atomspace/Handle.h" namespace "opencog":
    cdef cppclass cHandle "opencog::Handle":
        cHandle()
        cHandle(UUID)
        UUID value()
        bint operator==(cHandle h)
        cHandle UNDEFINED


cdef class Handle:
    cdef cHandle *h
    def __cinit__(self, h):
        self.h=new cHandle(h)
    def __dealloc__(self):
        del self.h
    def value(self):
        return self.h.value()
    def __richcmp__(Handle h1, Handle h2, int op):
        if op == 2: # ==
            return h1.h.value() == h2.h.value()
        elif op == 3: # !=
            return h1.h.value() != h2.h.value()
        elif op == 4: # >
            return h1.h.value() > h2.h.value()
        elif op == 0: # <
            return h1.h.value() < h2.h.value()
        elif op == 1: # <=
            return h1.h.value() <= h2.h.value()
        elif op == 5: # >=
            return h1.h.value() >= h2.h.value()
    def __str__(self):
        return "<UUID:" + str(self.h.value()) + ">"
    def is_undefined(self):
        if deref(self.h) == self.h.UNDEFINED: return True
        return False

# TruthValue
ctypedef int count_t
ctypedef float confidence_t
ctypedef float strength_t

cdef extern from "opencog/atomspace/TruthValue.h" namespace "opencog":
    cdef cppclass cTruthValue "opencog::TruthValue":
        strength_t getMean()
        confidence_t getConfidence()
        count_t getCount()
        cTruthValue DEFAULT_TV()

cdef extern from "opencog/atomspace/SimpleTruthValue.h" namespace "opencog":
    cdef cppclass cSimpleTruthValue "opencog::SimpleTruthValue":
        cSimpleTruthValue(float, float)
        strength_t getMean()
        confidence_t getConfidence()
        count_t getCount()
        cTruthValue DEFAULT_TV()

cdef class TruthValue:
    cdef cTruthValue *cobj
    def __cinit__(self, float strength, float count):
        self.cobj = <cTruthValue*> new cSimpleTruthValue(strength,count)
    def __dealloc__(self):
        del self.cobj
    def mean(self):
        return deref(self.cobj).getMean()
    def confidence(self):
        return deref(self.cobj).getConfidence()
    def count(self):
        return deref(self.cobj).getCount()

# HandleSeq
#ctypedef vector[cHandle] HandleSeq

# TimeServer
cdef extern from "opencog/atomspace/TimeServer.h" namespace "opencog":
    cdef cppclass cTimeServer "opencog::TimeServer":
        TimeServer()

cdef class TimeServer:
    cdef cTimeServer *timeserver

    def __cinit__(self):
        #self.timeserver = &timeserver
        pass

    def __dealloc__(self):
        # Don't do anything because the AtomSpace takes care of cleaning up
        pass
# AtomSpace

cdef extern from "opencog/atomspace/AtomSpace.h" namespace "opencog":
    cdef cppclass cAtomSpace "opencog::AtomSpace":
        AtomSpace()

        cHandle addNode(Type t, string s)
        cHandle addNode(Type t, string s, cTruthValue tvn)

        cHandle addPrefixedNode(Type t, string s)
        cHandle addPrefixedNode(Type t, string s, cTruthValue tvn)

        cHandle addLink(Type t, vector[cHandle])
        cHandle addLink(Type t, vector[cHandle], cTruthValue tvn)

        int getSize()

        cTimeServer getTimeServer()
        void print_list "print" ()

cdef class AtomSpace:
    cdef cAtomSpace *atomspace
    cdef cTimeServer *timeserver

    # TODO how do we do a copy constructor that shares the AtomSpaceAsync?
    def __cinit__(self):
        self.atomspace = new cAtomSpace()

    def __dealloc__(self):
        del self.atomspace

    def add_node(self,Type t,n,TruthValue tv=None):
        # convert to string
        py_byte_string = n.encode('UTF-8')
        # create temporary cpp string
        cdef string *name = new string(py_byte_string)
        cdef cHandle result
        if tv is None:
            # get handle
            result = self.atomspace.addNode(t,deref(name))
        else:
            result = self.atomspace.addNode(t,deref(name),deref(tv.cobj))
        # delete temporary string
        del name
        if result == result.UNDEFINED: return None
        return Handle(result.value());

    def add_link(self,Type t,outgoing,TruthValue tv=None):
        # create temporary cpp vector
        cdef vector[cHandle] o_vect
        for h in outgoing:
            o_vect.push_back(deref((<Handle>h).h))
        cdef cHandle result
        if tv is None:
            # get handle
            result = self.atomspace.addLink(t,o_vect)
        else:
            result = self.atomspace.addLink(t,o_vect,deref(tv.cobj))
        if result == result.UNDEFINED: return None
        return Handle(result.value());

    def size(self):
        return self.atomspace.getSize()

    #def get_time_server(self):
        #timeserver = &self.atomspace.getTimeServer()
        #return TimeServer(timeserver)

    def print_list(self):
        self.atomspace.print_list()

{'fraction_non_alphanumeric': 0.0986050986050986, 'fraction_numerical': 0.00404040404040404, 'mean_word_length': 2.555403556771546, 'pattern_counts': {'":': 22, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '942229', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3291, 'n_words': 868}