class LookUpTableBrainTest(unittest.TestCase): def setUp(self): from neural import LookUpTableBrain self._brain = LookUpTableBrain() def test_learn_exception(self): self._brain.learn(None) def test_learn(self): dataset = {(0,0): (1,1)} self._brain.learn(dataset) def test_think_exception(self): from neural import LookUpTableBrainException with self.assertRaises(LookUpTableBrainException): self._brain.think(0) def test_think_exception(self): from neural import LookUpTableBrainException with self.assertRaises(LookUpTableBrainException): self._brain.think((0,0)) def test_think(self): from neural import LookUpTableBrainException dataset = {(0,0): (1,1),(0,1): (1,2)} self._brain.learn(dataset) output_data = self._brain.think((0,0)) self.assertEqual(output_data[0],1) self.assertEqual(output_data[1],1) output_data = self._brain.think((0,1)) self.assertEqual(output_data[0],1) self.assertEqual(output_data[1],2)
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()