Ejemplo n.º 1
0
class InnerVoiceTest(unittest.TestCase):
    def setUp(self):
        from neural import InnerVoice

        self.innervoice = InnerVoice()

    def test_start(self):
        import jarvis
        from database import InnerVoiceDataBase

        he = jarvis.Jarvis()
        he.respond = lambda request: []

        self.innervoice.innerVoices(InnerVoiceDataBase())
        self.innervoice.jarvis(he)

        self.innervoice.start()
        self.innervoice.stop()

    def test_innervoice(self):
        self.assertTrue(hasattr(self.innervoice, "_innervoice"))

    def test_isTimeToSpeak(self):
        import datetime

        delta = datetime.timedelta(seconds=5)
        self.innervoice.ALLOW_TIME = (
            (datetime.datetime.now() - delta).time(),
            (datetime.datetime.now() + delta).time(),
        )
        self.assertTrue(self.innervoice._isAllowedToSpeak())

        self.innervoice.ALLOW_TIME = (
            (datetime.datetime.now() - delta).time(),
            (datetime.datetime.now() - delta).time(),
        )
        self.assertFalse(self.innervoice._isAllowedToSpeak())
Ejemplo n.º 2
0
class Jarvis(object):
    """
        Main Jarivs class

        Attributes:
        _brain          - Brain object
        _body           - Body object
        _word_db        - Words database
        _traning_db     - Training database
        _stop_event     - Stop event
    """
    def __init__(self):
        from neural import LookUpTableBrain
        from body import Body
        from voice import Voice
        from neural import InnerVoice
        import threading

        self._brain = LookUpTableBrain()
        self._body = Body()
        self._voice = Voice()
        self._inner_voices = InnerVoice()
        self._inner_voices.jarvis(self)

        self._word_db = None
        self._traning_db = None

        self._stop_event = threading.Event()

    def respond(self,request):
        """
            This method responds to request

            Input:
            request     - Request (string)

            Returns:
            answer
        """
        from database import DataBaseException
        from database import WordParser
        from body import BodyException
        from neural import BrainException

        if self._word_db == None: raise JarvisException("Don't have dictionary.")

        try:
            request_coded = tuple(self._word_db.multipleWordId(WordParser(request).wordsList()))
        except DataBaseException as error: raise JarvisException("Don't understand: " + request + " . Error: " + str(error))

        try:
            thought_coded = self._brain.think(request_coded)
        except BrainException as error: raise JarvisException("Cannot replay to this request: " + request + " . Error: " + str(error))

        try:
            thought_tuple = self._word_db.multipleIdWord(thought_coded)
        except DataBaseException as error: raise JarvisException("Cannot replay to this request: " + request + " . Error: " + str(error))

        try:
            answer_tuple = self._body.do(thought_tuple)
        except BodyException as error: raise JarvisException("Cannot do this request: " + request + " . Error: " + str(error))

        answer = " ".join([word.getValue() for word in answer_tuple])

        self._voice.speak(answer)

        return answer

    def createWordsDataBase(self,builder):
        """
            This method sets words database

            Input:
            builder         - Word database builder

            Returns:
            Nothing
        """
        self._word_db = builder.generateDataBase()

    def createTrainingDataBase(self,builder):
        """
            This method builds traning database

            Input:
            builder         - Traning database builder

            Returns:
            Nothing
        """
        self._traning_db = builder.generateDataBase()

    def createInnerVoiceDatabase(self,builder):
        """
            This method builds internal voice database

            Input:
            builder     - Database builder

            Returns:
            Nothing
        """
        self._inner_voices.innerVoices(builder.generateDataBase())

    def createAbilitiesDataBase(self,builder):
        """
            This method builds abilities database

            Input:
            builder     - Abilities builder

            Returns:
            Nothing
        """
        builder.jarvis(self)
        self._body.abilitiesDataBase(builder.generateDataBase())

    def train(self):
        """
            Trains Jarvis brain

            Input:
            Nothing

            Returns:
            Nothing
        """
        from trainer import Trainer

        if self._word_db == None: raise JarvisException("Don't have dictionary.")
        if self._traning_db == None: raise JarvisException("Don't have traning database.")

        trainer = Trainer(self._word_db,self._traning_db)
        trainer.train(self._brain)

    def start(self):
        """
            This method starts Jarvis internals

            Input:
            Nothing

            Returns:
            Nothing
        """
        from neural import InnerVoiceException

        try: self._inner_voices.start()
        except InnerVoiceException as error: raise JarvisException(str(error))

        self._stop_event.wait()

    def stop(self):
        """
            This method stops Jarvis internals

            Input:
            Nothing

            Returns:
            Nothing
        """
        self._inner_voices.stop()
        self._stop_event.set()