コード例 #1
0
    def test_get_list_synapse_with_raise_missing_parameters(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        fake_geolocation_parameters = {
            "longitude": 66,
            "radius": 66,
        }
        signal1 = Signal(name="geolocation",
                         parameters=fake_geolocation_parameters)
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])

        synapses_list = [synapse1, synapse2]
        br = Brain(synapses=synapses_list)

        # Stubbing the Geolocation Signal with the brain
        geo = Geolocation()
        geo.brain = br

        with self.assertRaises(MissingParameter):
            geo.run()
コード例 #2
0
    def setUp(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")
        signal3 = Signal(name="order",
                         parameters="that is part of the third sentence")

        self.synapse1 = Synapse(name="Synapse1",
                                neurons=[neuron1, neuron2],
                                signals=[signal1])
        self.synapse2 = Synapse(name="Synapse2",
                                neurons=[neuron3, neuron4],
                                signals=[signal2])
        self.synapse3 = Synapse(name="Synapse3",
                                neurons=[neuron2, neuron4],
                                signals=[signal3])

        self.all_synapse_list = [self.synapse1, self.synapse2, self.synapse3]

        self.brain_test = Brain(synapses=self.all_synapse_list)
        self.settings_test = Settings(default_synapse="Synapse3")

        # clean the LiFO
        Singleton._instances = dict()
コード例 #3
0
    def test_load_brain(self):
        """
        Test the class return a valid brain object
        """

        neuron = Neuron(name='say', parameters={'message': ['test message']})
        neuron2 = Neuron(name='sleep', parameters={'seconds': 60})

        signal1 = Signal(name="order", parameters="test_order")
        signal2 = Signal(name="order", parameters="test_order_2")
        signal3 = Signal(name="order", parameters="test_order_3")
        signal4 = Signal(name="order", parameters="order_for_int")

        synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1])
        synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2])
        synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3])
        synapse4 = Synapse(name="testint",
                           neurons=[neuron2],
                           signals=[signal4])
        synapses = [synapse1, synapse2, synapse4, synapse3]

        brain = Brain()
        brain.synapses = synapses
        brain.brain_file = self.brain_to_test
        brain.brain_yaml = self.expected_result

        brain_loader = BrainLoader(file_path=self.brain_to_test)
        self.assertEqual(brain, brain_loader.brain)
コード例 #4
0
    def setUp(self):
        # Kill the singleton
        Singleton._instances = dict()

        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        self.synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
        self.synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
        self.synapse3 = Synapse(name="Synapse3", neurons=[neuron2, neuron4], signals=[signal3])

        self.all_synapse_list1 = [self.synapse1,
                                  self.synapse2,
                                  self.synapse3]

        self.all_synapse_list2 = [self.synapse2,
                                  self.synapse3]

        self.brain_test1 = Brain(synapses=self.all_synapse_list1)
        self.brain_test2 = Brain(synapses=self.all_synapse_list2)
        # this brain is the same as the first one
        self.brain_test3 = Brain(synapses=self.all_synapse_list1)

        self.settings_test = Settings(default_synapse="Synapse3")

        # clean the LiFO
        LIFOBuffer.lifo_list = list()
コード例 #5
0
    def test_check_synapes(self):
        synapse_1 = Synapse(name="test")
        synapse_2 = Synapse(name="test2")
        synapse_3 = Synapse(name="test")

        valid_synapse_list = [synapse_1, synapse_2]
        invalid_synapse_list = [synapse_1, synapse_3]

        self.assertTrue(ConfigurationChecker.check_synapes(valid_synapse_list))

        with self.assertRaises(MultipleSameSynapseName):
            ConfigurationChecker.check_synapes(invalid_synapse_list)
コード例 #6
0
    def test_get_matching_synapse(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")
        signal3 = Signal(name="order",
                         parameters="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)

        # TEST1: should return synapse1
        spoken_order = "this is the sentence"

        # Create the matched synapse
        matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
                                           matched_order=spoken_order,
                                           user_order=spoken_order)

        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(matched_synapse_1 in matched_synapses)

        # TEST2: should return synapse1 and 2
        spoken_order = "this is the second sentence"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 2)
        self.assertTrue(synapse1, synapse2 in matched_synapses)

        # TEST3: should empty
        spoken_order = "not a valid order"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)
コード例 #7
0
    def test_get_list_synapse_with_mqtt_subscriber(self):

        # test with one signal mqtt
        neuron = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "test",
                             "broker_ip": "192.168.0.1"
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron],
                           signals=[signal1])
        synapses = [synapse1]
        brain = Brain()
        brain.synapses = synapses

        expected_result = synapses

        mq = Mqtt_subscriber()
        mq.brain = brain

        generator = mq.get_list_synapse()

        self.assertEqual(expected_result, list(generator))

        # test with two synapse
        neuron = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="order", parameters="test_order")
        signal2 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "test",
                             "broker_ip": "192.168.0.1"
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron],
                           signals=[signal1])
        synapse2 = Synapse(name="synapse2",
                           neurons=[neuron],
                           signals=[signal1, signal2])

        synapses = [synapse1, synapse2]
        brain = Brain()
        brain.synapses = synapses

        expected_result = [synapse2]

        mq = Mqtt_subscriber()
        mq.brain = brain
        generator = mq.get_list_synapse()

        self.assertEqual(expected_result, list(generator))
コード例 #8
0
    def create_synapses(self):
        """
        curl -i -H "Content-Type: application/json" \
        --user admin:secret \
        -X POST \
        -d '{
          "name": "Say-hello",
          "signals": [
            {
              "order": "je suis nicolas"
            }
          ],
          "neurons": [
            {
              "say": {
                "message": "je sais"
              }
            }
          ]
        }' \
        http://127.0.0.1:5000/synapses
        :return:
        """
        if not request.get_json() or 'name' not in request.get_json():
            data = {
                "Error": "Wrong parameters, 'name' not set"
            }
            return jsonify(error=data), 400

        new_synapse = request.get_json()
        try:
            ConfigurationChecker().check_synape_dict(new_synapse)
        except (InvalidSynapeName, NoSynapeNeurons, NoSynapeSignals) as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400

        try:
            name = new_synapse["name"]
            neurons = BrainLoader.get_neurons(new_synapse["neurons"], self.settings)
            signals = BrainLoader.get_signals(new_synapse["signals"])
            new_synapse_instance = Synapse(name=name, neurons=neurons, signals=signals)
            self.brain.synapses.append(new_synapse_instance)
            # TODO save the brain in yaml
            return jsonify(new_synapse_instance.serialize()), 201
        except KalliopeModuleNotFoundError as e:
            data = {
                "error": "%s" % e
            }
            return jsonify(data), 400
コード例 #9
0
ファイル: test_models.py プロジェクト: igorstarki/kalliope
    def test_Synapse(self):
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order", parameters="this is the second sentence")

        synapse1 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])
        synapse2 = Synapse(name="Synapse2", neurons=[neuron3, neuron4], signals=[signal2])
        synapse3 = Synapse(name="Synapse1", neurons=[neuron1, neuron2], signals=[signal1])

        expected_result_serialize = {
            'signals': [
                {
                    'name': 'order',
                    'parameters': 'this is the sentence'
                }
            ],
            'neurons': [
                {
                    'name': 'neurone1',
                    'parameters': {
                         'var1': 'val1'
                     }
                },
                {
                    'name': 'neurone2',
                    'parameters':
                        {
                            'var2': 'val2'
                        }
                }
            ],
            'name': 'Synapse1',
            'enabled': True
        }

        self.assertDictEqual(expected_result_serialize, synapse1.serialize())

        self.assertTrue(synapse1.__eq__(synapse3))
        self.assertFalse(synapse1.__eq__(synapse2))
コード例 #10
0
    def test_get_brain(self):
        """
        Test the class return a valid brain object
        """

        neuron = Neuron(name='say', parameters={'message': ['test message']})

        signal1 = Order(sentence="test_order")
        signal2 = Order(sentence="test_order_2")
        signal3 = Order(sentence="test_order_3")

        synapse1 = Synapse(name="test", neurons=[neuron], signals=[signal1])
        synapse2 = Synapse(name="test2", neurons=[neuron], signals=[signal2])
        synapse3 = Synapse(name="test3", neurons=[neuron], signals=[signal3])
        synapses = [synapse1, synapse2, synapse3]

        brain = Brain()
        brain.synapses = synapses
        brain.brain_file = self.brain_to_test
        brain.brain_yaml = self.expected_result

        brain_loader = BrainLoader(file_path=self.brain_to_test)
        self.assertEqual(brain, brain_loader.brain)
コード例 #11
0
    def test_Synapse(self):
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])

        expected_result_serialize = {
            'signals': [{
                'name': 'order',
                'parameters': 'this is the sentence'
            }],
            'neurons': [{
                'name': 'neurone1',
                'parameters': {
                    'var1': 'val1'
                }
            }, {
                'name': 'neurone2',
                'parameters': {
                    'var2': 'val2'
                }
            }],
            'name':
            'Synapse1',
            'enabled':
            True
        }

        self.assertDictEqual(expected_result_serialize, synapse1.serialize())

        self.assertTrue(synapse1.__eq__(synapse3))
        self.assertFalse(synapse1.__eq__(synapse2))
コード例 #12
0
    def test_get_list_broker_to_instantiate(self):
        # ----------------
        # only one synapse
        # ----------------
        neuron = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic1",
                             "broker_ip": "192.168.0.1"
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron],
                           signals=[signal1])
        brain = Brain()
        brain.synapses = [synapse1]

        list_synapse_with_mqtt_subscriber = [synapse1]

        expected_broker = Broker()
        expected_broker.broker_ip = "192.168.0.1"
        expected_broker.topics = list()
        expected_topic = Topic()
        expected_topic.name = "topic1"
        # add the current synapse to the topic
        expected_topic.synapses = list()
        expected_topic.synapses.append(synapse1)
        expected_broker.topics.append(expected_topic)

        expected_retuned_list = [expected_broker]

        mq = Mqtt_subscriber(brain=brain)

        self.assertListEqual(
            expected_retuned_list,
            mq.get_list_broker_to_instantiate(
                list_synapse_with_mqtt_subscriber))

        # ----------------
        #  one synapse, two different broker
        # ----------------
        neuron = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic1",
                             "broker_ip": "192.168.0.1",
                             "is_json": False
                         })
        signal2 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic2",
                             "broker_ip": "172.16.0.1",
                             "is_json": False
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron],
                           signals=[signal1, signal2])
        brain = Brain()
        brain.synapses = [synapse1]

        list_synapse_with_mqtt_subscriber = [synapse1]

        expected_broker1 = Broker()
        expected_broker1.broker_ip = "192.168.0.1"
        expected_broker1.topics = list()
        expected_topic = Topic()
        expected_topic.name = "topic1"
        # add the current synapse to the topic
        expected_topic.synapses = list()
        expected_topic.synapses.append(synapse1)
        expected_broker1.topics.append(expected_topic)

        expected_broker2 = Broker()
        expected_broker2.broker_ip = "172.16.0.1"
        expected_broker2.topics = list()
        expected_topic = Topic()
        expected_topic.name = "topic2"
        # add the current synapse to the topic
        expected_topic.synapses = list()
        expected_topic.synapses.append(synapse1)
        expected_broker2.topics.append(expected_topic)

        expected_retuned_list = [expected_broker1, expected_broker2]

        mq = Mqtt_subscriber(brain=brain)

        self.assertEqual(
            expected_retuned_list,
            mq.get_list_broker_to_instantiate(
                list_synapse_with_mqtt_subscriber))

        # ----------------
        #  two synapse, same broker, different topics
        # ----------------
        # synapse 1
        neuron1 = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic1",
                             "broker_ip": "192.168.0.1"
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron1],
                           signals=[signal1])

        # synapse 2
        neuron2 = Neuron(name='say', parameters={'message': ['test message']})
        signal2 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic2",
                             "broker_ip": "192.168.0.1"
                         })
        synapse2 = Synapse(name="synapse2",
                           neurons=[neuron2],
                           signals=[signal2])

        brain = Brain()
        brain.synapses = [synapse1, synapse2]

        list_synapse_with_mqtt_subscriber = [synapse1, synapse2]

        expected_broker1 = Broker()
        expected_broker1.broker_ip = "192.168.0.1"
        expected_broker1.topics = list()
        expected_topic1 = Topic()
        expected_topic1.name = "topic1"
        expected_topic2 = Topic()
        expected_topic2.name = "topic2"
        # add the current synapse to the topic
        expected_topic1.synapses = [synapse1]
        expected_topic2.synapses = [synapse2]
        # add both topic to the broker
        expected_broker1.topics.append(expected_topic1)
        expected_broker1.topics.append(expected_topic2)

        expected_retuned_list = [expected_broker1]

        mq = Mqtt_subscriber(brain=brain)

        self.assertEqual(
            expected_retuned_list,
            mq.get_list_broker_to_instantiate(
                list_synapse_with_mqtt_subscriber))

        # ----------------
        #  two synapse, same broker, same topic
        # ----------------
        # synapse 1
        neuron1 = Neuron(name='say', parameters={'message': ['test message']})
        signal1 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic1",
                             "broker_ip": "192.168.0.1"
                         })
        synapse1 = Synapse(name="synapse1",
                           neurons=[neuron1],
                           signals=[signal1])

        # synapse 2
        neuron2 = Neuron(name='say', parameters={'message': ['test message']})
        signal2 = Signal(name="mqtt_subscriber",
                         parameters={
                             "topic": "topic1",
                             "broker_ip": "192.168.0.1"
                         })
        synapse2 = Synapse(name="synapse2",
                           neurons=[neuron2],
                           signals=[signal2])

        brain = Brain()
        brain.synapses = [synapse1, synapse2]

        list_synapse_with_mqtt_subscriber = [synapse1, synapse2]

        expected_broker1 = Broker()
        expected_broker1.broker_ip = "192.168.0.1"
        expected_broker1.topics = list()
        expected_topic1 = Topic()
        expected_topic1.name = "topic1"
        # add both synapses to the topic
        expected_topic1.synapses = [synapse1, synapse2]
        # add the topic to the broker
        expected_broker1.topics.append(expected_topic1)

        expected_retuned_list = [expected_broker1]

        mq = Mqtt_subscriber(brain=brain)

        self.assertEqual(
            expected_retuned_list,
            mq.get_list_broker_to_instantiate(
                list_synapse_with_mqtt_subscriber))
コード例 #13
0
    def test_get_matching_synapse(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")
        signal3 = Signal(name="order",
                         parameters="that is part of the third sentence")
        signal4 = Signal(name="order",
                         parameters={
                             "matching-type": "strict",
                             "text": "that is part of the fourth sentence"
                         })
        signal5 = Signal(name="order",
                         parameters={
                             "matching-type": "ordered-strict",
                             "text": "sentence 5 with specific order"
                         })
        signal6 = Signal(name="order",
                         parameters={
                             "matching-type": "normal",
                             "text": "matching type normal"
                         })
        signal7 = Signal(name="order",
                         parameters={
                             "matching-type": "non-existing",
                             "text": "matching type non existing"
                         })
        signal8 = Signal(name="order",
                         parameters={
                             "matching-type": "non-existing",
                             "non-existing-parameter": "will not match order"
                         })
        signal9 = Signal(
            name="order",
            parameters=
            "order that should be triggered because synapse is disabled")
        signal10 = Signal(name="order", parameters="i say this")
        signal11 = Signal(name="order", parameters="and then that")
        signal12 = Signal(name="order",
                          parameters={
                              "matching-type": "strict",
                              "text": "just 1 test",
                              "stt-correction": [{
                                  "input": "one",
                                  "output": "1"
                              }]
                          })

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])
        synapse4 = Synapse(name="Synapse4",
                           neurons=[neuron2, neuron4],
                           signals=[signal4])
        synapse5 = Synapse(name="Synapse5",
                           neurons=[neuron1, neuron2],
                           signals=[signal5])
        synapse6 = Synapse(name="Synapse6",
                           neurons=[neuron1, neuron2],
                           signals=[signal6])
        synapse7 = Synapse(name="Synapse7",
                           neurons=[neuron1, neuron2],
                           signals=[signal7])
        synapse8 = Synapse(name="Synapse8",
                           neurons=[neuron1, neuron2],
                           signals=[signal8])
        synapse9 = Synapse(name="Synapse9",
                           enabled=False,
                           neurons=[neuron1, neuron2],
                           signals=[signal9])
        synapse10 = Synapse(name="Synapse10",
                            neurons=[neuron1],
                            signals=[signal10, signal11])
        synapse11 = Synapse(name="Synapse11",
                            neurons=[neuron1],
                            signals=[signal12])

        all_synapse_list = [
            synapse1, synapse2, synapse3, synapse4, synapse5, synapse6,
            synapse7, synapse8, synapse9, synapse10, synapse11
        ]

        br = Brain(synapses=all_synapse_list)

        # TEST1: should return synapse1
        spoken_order = "this is the sentence"

        # Create the matched synapse
        expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)

        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_1 in matched_synapses)

        # with defined normal matching type
        spoken_order = "matching type normal"
        expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)

        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_5 in matched_synapses)

        # TEST2: should return synapse1 and 2
        spoken_order = "this is the second sentence"
        expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 2)
        self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2
                        in matched_synapses)

        # TEST3: should empty
        spoken_order = "not a valid order"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST4: with matching type strict
        spoken_order = "that is part of the fourth sentence"
        expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue(expected_matched_synapse_3 in matched_synapses)

        spoken_order = "that is part of the fourth sentence with more word"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST5: with matching type ordered strict
        spoken_order = "sentence 5 with specific order"
        expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_4 in matched_synapses)

        spoken_order = "order specific with 5 sentence"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST6: non supported type of matching. should fallback to normal
        spoken_order = "matching type non existing"
        expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue(expected_matched_synapse_5 in matched_synapses)

        # TEST7: should not match the disabled synapse
        spoken_order = "order that should be triggered because synapse is disabled"
        # we expect an empty list
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue(len(matched_synapses) == 0)

        # TEST8: a spoken order that match multiple time the same synapse should return the synapse only once
        spoken_order = "i say this and then that"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue("Synapse10" in matched_synapse.synapse.name
                        for matched_synapse in matched_synapses)
        self.assertTrue(len(matched_synapses) == 1)

        # TEST9: with STT correction
        spoken_order = "just one test"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue("Synapse11" in matched_synapse.synapse.name
                        for matched_synapse in matched_synapses)
        self.assertTrue(spoken_order in matched_synapse.user_order
                        for matched_synapse in matched_synapses)
        self.assertTrue(len(matched_synapses) == 1)
コード例 #14
0
    def test_get_matching_synapse(self):
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Signal(name="order", parameters="this is the sentence")
        signal2 = Signal(name="order",
                         parameters="this is the second sentence")
        signal3 = Signal(name="order",
                         parameters="that is part of the third sentence")
        signal4 = Signal(name="order",
                         parameters={
                             "matching-type": "strict",
                             "text": "that is part of the fourth sentence"
                         })
        signal5 = Signal(name="order",
                         parameters={
                             "matching-type": "ordered-strict",
                             "text": "sentence 5 with specific order"
                         })
        signal6 = Signal(name="order",
                         parameters={
                             "matching-type": "normal",
                             "text": "matching type normal"
                         })
        signal7 = Signal(name="order",
                         parameters={
                             "matching-type": "non-existing",
                             "text": "matching type non existing"
                         })
        signal8 = Signal(name="order",
                         parameters={
                             "matching-type": "non-existing",
                             "non-existing-parameter": "will not match order"
                         })

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])
        synapse4 = Synapse(name="Synapse4",
                           neurons=[neuron2, neuron4],
                           signals=[signal4])
        synapse5 = Synapse(name="Synapse5",
                           neurons=[neuron1, neuron2],
                           signals=[signal5])
        synapse6 = Synapse(name="Synapse6",
                           neurons=[neuron1, neuron2],
                           signals=[signal6])
        synapse7 = Synapse(name="Synapse6",
                           neurons=[neuron1, neuron2],
                           signals=[signal7])
        synapse8 = Synapse(name="Synapse6",
                           neurons=[neuron1, neuron2],
                           signals=[signal8])

        all_synapse_list = [
            synapse1, synapse2, synapse3, synapse4, synapse5, synapse6,
            synapse7, synapse8
        ]

        br = Brain(synapses=all_synapse_list)

        # TEST1: should return synapse1
        spoken_order = "this is the sentence"

        # Create the matched synapse
        expected_matched_synapse_1 = MatchedSynapse(matched_synapse=synapse1,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)

        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_1 in matched_synapses)

        # with defined normal matching type
        spoken_order = "matching type normal"
        expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse6,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)

        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_5 in matched_synapses)

        # TEST2: should return synapse1 and 2
        spoken_order = "this is the second sentence"
        expected_matched_synapse_2 = MatchedSynapse(matched_synapse=synapse1,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 2)
        self.assertTrue(expected_matched_synapse_1, expected_matched_synapse_2
                        in matched_synapses)

        # TEST3: should empty
        spoken_order = "not a valid order"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST4: with matching type strict
        spoken_order = "that is part of the fourth sentence"
        expected_matched_synapse_3 = MatchedSynapse(matched_synapse=synapse4,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue(expected_matched_synapse_3 in matched_synapses)

        spoken_order = "that is part of the fourth sentence with more word"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST5: with matching type ordered strict
        spoken_order = "sentence 5 with specific order"
        expected_matched_synapse_4 = MatchedSynapse(matched_synapse=synapse5,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertEqual(len(matched_synapses), 1)
        self.assertTrue(expected_matched_synapse_4 in matched_synapses)

        spoken_order = "order specific with 5 sentence"
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertFalse(matched_synapses)

        # TEST6: non supported type of matching. should fallback to normal
        spoken_order = "matching type non existing"
        expected_matched_synapse_5 = MatchedSynapse(matched_synapse=synapse7,
                                                    matched_order=spoken_order,
                                                    user_order=spoken_order)
        matched_synapses = OrderAnalyser.get_matching_synapse(
            order=spoken_order, brain=br)
        self.assertTrue(expected_matched_synapse_5 in matched_synapses)