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

#
# Copyright 2015, 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/>.
#
#
# ai kernal, central hub for all the other components to hook into
#
# natural language -> [ tokenizer ] -> tokens -> [ seq2seq model ] -> Python/SWI-Prolog -> response
#

from __future__ import print_function

import os
import sys
import logging
import traceback
import imp
import time
import random
import codecs
import datetime
import pytz
import json

import numpy as np

from six                    import viewitems
from tzlocal                import get_localzone # $ pip install tzlocal
from copy                   import deepcopy, copy
from six                    import text_type
from scipy.spatial.distance import cosine
from xsbprolog              import xsb_hl_init, xsb_hl_command, xsb_hl_query, xsb_close, xsb_command_string, xsb_query_string, xsb_make_vars, xsb_var_string, xsb_next

from nltools                import misc
from nltools.tokenizer      import tokenize
from zamiaai.data_engine    import DataEngine
from zamiaai                import model

USER_PREFIX        = u'user'
DEFAULT_USER       = USER_PREFIX + u'Default'
TEST_USER          = USER_PREFIX + u'Test'
TEST_TIME          = datetime.datetime(2016,12,6,13,28,6,tzinfo=get_localzone()).isoformat()
TEST_MODULE        = '__test__'
MAX_NER_RESULTS    = 5

def avg_feature_vector(words, model, num_features, index2word_set):
    #function to average all words vectors in a given paragraph
    featureVec = np.zeros((num_features,), dtype="float32")
    nwords = 0

    for word in words:
        if word in index2word_set:
            nwords = nwords+1
            featureVec = np.add(featureVec, model[word])

    if nwords > 0:
        featureVec = np.divide(featureVec, nwords)
    return featureVec

class AIContext(object):

    def __init__(self, user, session, lang):
        self.dlg_log      = []
        self.staged_resps = []
        self.high_score   = 0.0
        self.inp          = u''
        self.user         = user
        self.ner_dict     = {} # DB cache
        self.session      = session
        self.lang         = lang

    def set_inp(self, inp):
        self.inp = inp

    def resp(self, resp, score=0.0, actions=[], mems=[]):
        if score > self.high_score:
            self.high_score   = score
            self.staged_resps = []
        self.staged_resps.append( (resp, score, actions, mems) )

    def get_resps(self):
        return self.staged_resps

    def commit_resp(self, i):
        self.dlg_log.append( { 'inp': self.inp, 
                               'out': self.staged_resps[i][0] })

        mems = self.staged_resps[i][3]
        if mems:
            for m in mems:
                logging.debug ('executing mem %s' % repr(m))
                xsb_command_string(m)
<commit_msg>add current_dt to context<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#
# Copyright 2015, 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/>.
#
#
# ai kernal, central hub for all the other components to hook into
#
# natural language -> [ tokenizer ] -> tokens -> [ seq2seq model ] -> Python/SWI-Prolog -> response
#

from __future__ import print_function

import os
import sys
import logging
import traceback
import imp
import time
import random
import codecs
import datetime
import pytz
import json

import numpy as np

from six                    import viewitems
from tzlocal                import get_localzone # $ pip install tzlocal
from copy                   import deepcopy, copy
from six                    import text_type
from scipy.spatial.distance import cosine
from xsbprolog              import xsb_hl_init, xsb_hl_command, xsb_hl_query, xsb_close, xsb_command_string, xsb_query_string, xsb_make_vars, xsb_var_string, xsb_next

from nltools                import misc
from nltools.tokenizer      import tokenize
from zamiaai.data_engine    import DataEngine
from zamiaai                import model

USER_PREFIX        = u'user'
DEFAULT_USER       = USER_PREFIX + u'Default'
TEST_USER          = USER_PREFIX + u'Test'
TEST_TIME          = datetime.datetime(2016,12,6,13,28,6,tzinfo=get_localzone()).isoformat()
TEST_MODULE        = '__test__'
MAX_NER_RESULTS    = 5

def avg_feature_vector(words, model, num_features, index2word_set):
    #function to average all words vectors in a given paragraph
    featureVec = np.zeros((num_features,), dtype="float32")
    nwords = 0

    for word in words:
        if word in index2word_set:
            nwords = nwords+1
            featureVec = np.add(featureVec, model[word])

    if nwords > 0:
        featureVec = np.divide(featureVec, nwords)
    return featureVec

class AIContext(object):

    def __init__(self, user, session, lang):
        self.dlg_log      = []
        self.staged_resps = []
        self.high_score   = 0.0
        self.inp          = u''
        self.user         = user
        self.ner_dict     = {} # DB cache
        self.session      = session
        self.lang         = lang
        self.current_dt   = datetime.datetime.now()

    def set_inp(self, inp):
        self.inp = inp

    def resp(self, resp, score=0.0, actions=[], mems=[]):
        if score > self.high_score:
            self.high_score   = score
            self.staged_resps = []
        self.staged_resps.append( (resp, score, actions, mems) )

    def get_resps(self):
        return self.staged_resps

    def commit_resp(self, i):
        self.dlg_log.append( { 'inp': self.inp, 
                               'out': self.staged_resps[i][0] })

        mems = self.staged_resps[i][3]
        if mems:
            for m in mems:
                logging.debug ('executing mem %s' % repr(m))
                xsb_command_string(m)

{'fraction_non_alphanumeric': 0.08216833095577745, 'fraction_numerical': 0.011697574893009986, 'mean_word_length': 2.4742319127849357, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '313348', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2074, 'n_words': 681}