Ejemplo n.º 1
0
    def respond(self, input, sessionID=_globalSessionID):
        """Return the Kernel's response to the input string."""
        if len(input) == 0:
            return ""

        #ensure that input is a unicode string
        try:
            input = input.decode(self._textEncoding, 'replace')
        except UnicodeError:
            pass
        except AttributeError:
            pass

        # prevent other threads from stomping all over us.
        self._respondLock.acquire()

        # Add the session, if it doesn't already exist
        self._addSession(sessionID)

        # split the input into discrete sentences
        sentences = Utils.sentences(input)
        finalResponse = ""

        for s in sentences:
            # Add the input to the history list before fetching the
            # response, so that <input/> tags work properly.
            inputHistory = self.getPredicate(self._inputHistory, sessionID)
            inputHistory.append(s)

            while len(inputHistory) > self._maxHistorySize:
                inputHistory.pop(0)
            self.setPredicate(self._inputHistory, inputHistory, sessionID)

            # Fetch the response
            response = self._respond(s, sessionID)

            # add the data from this exchange to the history lists
            outputHistory = self.getPredicate(self._outputHistory, sessionID)
            outputHistory.append(response)
            while len(outputHistory) > self._maxHistorySize:
                outputHistory.pop(0)
            self.setPredicate(self._outputHistory, outputHistory, sessionID)

            # append this response to the final response.
            finalResponse += (response + "  ")
        finalResponse = finalResponse.strip()
        finalResponse = mergeChineseSpace(
            unicode(finalResponse, self._textEncoding
                    ) if type(finalResponse) == str else finalResponse)
        assert (len(self.getPredicate(self._inputStack, sessionID)) == 0)

        # release the lock and return
        self._respondLock.release()
        try:
            return finalResponse.encode(self._textEncoding)
        except UnicodeError:
            return finalResponse
Ejemplo n.º 2
0
Archivo: Kernel.py Proyecto: ictar/XIXI
    def respond(self, input, sessionID=_globalSessionID):
        """Return the Kernel's response to the input string."""
        if len(input) == 0:
            return ""

        # ensure that input is a unicode string
        try:
            input = input.decode(self._textEncoding, "replace")
        except UnicodeError:
            pass
        except AttributeError:
            pass

        # prevent other threads from stomping all over us.
        self._respondLock.acquire()

        # Add the session, if it doesn't already exist
        self._addSession(sessionID)

        # split the input into discrete sentences
        sentences = Utils.sentences(input)
        finalResponse = ""

        for s in sentences:
            # Add the input to the history list before fetching the
            # response, so that <input/> tags work properly.
            inputHistory = self.getPredicate(self._inputHistory, sessionID)
            inputHistory.append(s)

            while len(inputHistory) > self._maxHistorySize:
                inputHistory.pop(0)
            self.setPredicate(self._inputHistory, inputHistory, sessionID)

            # Fetch the response
            response = self._respond(s, sessionID)

            # add the data from this exchange to the history lists
            outputHistory = self.getPredicate(self._outputHistory, sessionID)
            outputHistory.append(response)
            while len(outputHistory) > self._maxHistorySize:
                outputHistory.pop(0)
            self.setPredicate(self._outputHistory, outputHistory, sessionID)

            # append this response to the final response.
            finalResponse += response + "  "
        finalResponse = finalResponse.strip()
        finalResponse = mergeChineseSpace(
            unicode(finalResponse, self._textEncoding) if type(finalResponse) == str else finalResponse
        )
        assert len(self.getPredicate(self._inputStack, sessionID)) == 0

        # release the lock and return
        self._respondLock.release()
        try:
            return finalResponse.encode(self._textEncoding)
        except UnicodeError:
            return finalResponse