<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#
# Copyright 2016, 2017 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from copy                 import deepcopy, copy
from nltools.tokenizer    import tokenize
from zamiaprolog.logic    import NumberLiteral, StringLiteral, ListLiteral, Literal, Variable, Predicate, Clause, SourceLocation
from zamiaprolog.builtins import do_gensym, do_assertz, do_retract
from zamiaprolog.errors   import PrologRuntimeError

#
# very basic utilities
#

def builtin_say (g, pe):

    """ say (+C, +Str) """

    pe._trace ('CALLED BUILTIN say', g)

    pred = g.terms[g.inx]
    args = pred.args
    if len(args) != 2:
        raise PrologRuntimeError('say: 2 args (+C, +Str) expected.', g.location)

    arg_C     = pe.prolog_eval         (args[0], g.env, g.location)
    arg_Str   = pe.prolog_get_string   (args[1], g.env, g.location)

    # figure out language from context

    solutions = pe.search_predicate('lang', [arg_C, '_1'], env=g.env)

    if len(solutions)<1:
        import pdb; pdb.set_trace()
        pe.set_trace(True)
        solutions = pe.search_predicate('lang', [arg_C, '_1'], env=g.env)
        raise PrologRuntimeError('say: internal error: failed to determine language.', g.location)

    lang = solutions[0]['_1'].name

    parts = []
    for p1 in arg_Str.split('{'):
        for p2 in p1.split('}'):
            parts.append(p2)

    res = {}
    cnt = 0
    for part in parts:

        if cnt % 2 == 1:

            subparts = part.split(',')

            if len(subparts)!=2:
                self.report_error ('variable string "%s" not recognized .' % repr(part))

            var_s = subparts[0]
            fmt_s = subparts[1]

            var_v = pe.prolog_eval (Variable(var_s), g.env, g.location)
            
            if isinstance(var_v, StringLiteral):
                v = var_v.s
            else:
                v = unicode(var_v)

            if fmt_s == 'd':
                v = unicode(int(v))
            elif fmt_s == 'f':
                v = unicode(float(v))

            res = do_assertz (g.env, Clause ( Predicate('c_say', [arg_C, StringLiteral(v)]) , location=g.location), res=res)

        else:

            for t in tokenize(part, lang=lang, keep_punctuation=True):
                res = do_assertz (g.env, Clause ( Predicate('c_say', [arg_C, StringLiteral(t)]) , location=g.location), res=res)
        cnt += 1


    return [res]

<commit_msg>round decimals<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#
# Copyright 2016, 2017 <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from copy                 import deepcopy, copy
from nltools.tokenizer    import tokenize
from zamiaprolog.logic    import NumberLiteral, StringLiteral, ListLiteral, Literal, Variable, Predicate, Clause, SourceLocation
from zamiaprolog.builtins import do_gensym, do_assertz, do_retract
from zamiaprolog.errors   import PrologRuntimeError

#
# very basic utilities
#

def builtin_say (g, pe):

    """ say (+C, +Str) """

    pe._trace ('CALLED BUILTIN say', g)

    pred = g.terms[g.inx]
    args = pred.args
    if len(args) != 2:
        raise PrologRuntimeError('say: 2 args (+C, +Str) expected.', g.location)

    arg_C     = pe.prolog_eval         (args[0], g.env, g.location)
    arg_Str   = pe.prolog_get_string   (args[1], g.env, g.location)

    # figure out language from context

    solutions = pe.search_predicate('lang', [arg_C, '_1'], env=g.env)

    if len(solutions)<1:
        import pdb; pdb.set_trace()
        pe.set_trace(True)
        solutions = pe.search_predicate('lang', [arg_C, '_1'], env=g.env)
        raise PrologRuntimeError('say: internal error: failed to determine language.', g.location)

    lang = solutions[0]['_1'].name

    parts = []
    for p1 in arg_Str.split('{'):
        for p2 in p1.split('}'):
            parts.append(p2)

    res = {}
    cnt = 0
    for part in parts:

        if cnt % 2 == 1:

            subparts = part.split(',')

            if len(subparts)!=2:
                self.report_error ('variable string "%s" not recognized .' % repr(part))

            var_s = subparts[0]
            fmt_s = subparts[1]

            var_v = pe.prolog_eval (Variable(var_s), g.env, g.location)
            
            if isinstance(var_v, StringLiteral):
                v = var_v.s
            else:
                v = unicode(var_v)

            if fmt_s == 'd':
                v = unicode(int(round(float(v))))
            elif fmt_s == 'f':
                v = unicode(float(v))

            res = do_assertz (g.env, Clause ( Predicate('c_say', [arg_C, StringLiteral(v)]) , location=g.location), res=res)

        else:

            for t in tokenize(part, lang=lang, keep_punctuation=True):
                res = do_assertz (g.env, Clause ( Predicate('c_say', [arg_C, StringLiteral(t)]) , location=g.location), res=res)
        cnt += 1


    return [res]


{'fraction_non_alphanumeric': 0.11625263841532717, 'fraction_numerical': 0.00974184120798831, 'mean_word_length': 2.492063492063492, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '76479', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1883, 'n_words': 601}