<commit_before>;
; python.scm -- mixing Python code with Scheme
;
; The built-in python interpreter can be invoked from scheme, as shown
; below.  It should not be confused with another concept: calling python
; functions from ExecutionOutputLinks, which is demoed in `execute.scm`.


; Start by loading the python module:
(use-modules (opencog) (opencog python))

; The below should print "hello! 4"
(python-eval "print ('hello! ' + str(2+2))")

; Use exec(open()) to load python files:
(python-eval "exec(open('my_py_func.py'))")

; -------------------------------------------------------------------
; It is possible to communicate an AtomSpace from guile to python.
; There are two ways to do this: one way is to tell python what the
; atomspace is; the other is to have python ask what it is. The first
; example shows how to "tell" python: it calls a python function,
; supplying some atomspace as an argument.
;
; This requires several steps. First, define a python function that
; takes an atomspace as an argument.  Note that standard python
; whitespace indentation is used to define a multi-line block of python
; code.
(python-eval "
from opencog.atomspace import AtomSpace, TruthValue
from opencog.atomspace import types

def foo(atspace):
    TV = TruthValue(0.42, 0.69)
    atspace.add_node(types.ConceptNode, 'Apple', TV)
")

; The (cog-atomspace) function returns the current atomspace being used
; by scheme.  We pass it to the newly-defined python function "foo":
(python-call-with-as "foo" (cog-atomspace))

; Verify that a ConceptNode called "Apple" was created, and that it has
; the correct truth value, as set in the python code:
(cog-node 'ConceptNode "Apple")

; -------------------------------------------------------------------<commit_msg>Use open(...).read() for exec method<commit_after>;
; python.scm -- mixing Python code with Scheme
;
; The built-in python interpreter can be invoked from scheme, as shown
; below.  It should not be confused with another concept: calling python
; functions from ExecutionOutputLinks, which is demoed in `execute.scm`.


; Start by loading the python module:
(use-modules (opencog) (opencog python))

; The below should print "hello! 4"
(python-eval "print ('hello! ' + str(2+2))")

; Use exec(open()) to load python files:
(python-eval "exec(open('my_py_func.py').read())")

; -------------------------------------------------------------------
; It is possible to communicate an AtomSpace from guile to python.
; There are two ways to do this: one way is to tell python what the
; atomspace is; the other is to have python ask what it is. The first
; example shows how to "tell" python: it calls a python function,
; supplying some atomspace as an argument.
;
; This requires several steps. First, define a python function that
; takes an atomspace as an argument.  Note that standard python
; whitespace indentation is used to define a multi-line block of python
; code.
(python-eval "
from opencog.atomspace import AtomSpace, TruthValue
from opencog.atomspace import types

def foo(atspace):
    TV = TruthValue(0.42, 0.69)
    atspace.add_node(types.ConceptNode, 'Apple', TV)
")

; The (cog-atomspace) function returns the current atomspace being used
; by scheme.  We pass it to the newly-defined python function "foo":
(python-call-with-as "foo" (cog-atomspace))

; Verify that a ConceptNode called "Apple" was created, and that it has
; the correct truth value, as set in the python code:
(cog-node 'ConceptNode "Apple")

; -------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.16236679753224903, 'fraction_numerical': 0.005047672462142457, 'mean_word_length': 3.025959367945824, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '217368', 'n_tokens_mistral': 1004, 'n_tokens_neox': 979, 'n_words': 444}