示例#1
0
 def build_features(self):
     first_features = [
         Feature("my_first_feature",
                 self.compute_feature_1,
                 offset=offset) for offset in self.offsets
     ]
     second_features = [
         Feature("my_second_feature",
                 self.compute_feature_2,
                 offset=offset) for offset in self.offsets
     ]
     return first_features + second_features
示例#2
0
    def test_feature_should_work_with_cache(self):
        # Given
        def fn(tokens, token_index):
            value = tokens[token_index].value
            return "%s_%s" % (value, len(value))

        mocked_fn = MagicMock(side_effect=fn)

        cache = [{TOKEN_NAME: token} for token in
                 tokenize("hello beautiful world", LANGUAGE_EN)]
        feature = Feature("test_feature", mocked_fn, offset=0)
        feature.compute(2, cache)
        feature1 = Feature("test_feature", mocked_fn, offset=1)
        feature2 = Feature("test_feature", mocked_fn, offset=2)

        # When
        res1 = feature1.compute(1, cache)
        res1_bis = feature1.compute(0, cache)
        res2 = feature2.compute(0, cache)

        # Then
        self.assertEqual(res1, "world_5")
        self.assertEqual(res1_bis, "beautiful_9")
        self.assertEqual(res2, "world_5")
        self.assertEqual(mocked_fn.call_count, 2)
示例#3
0
    def test_feature_should_work_with_offset(self):
        # Given
        def fn(tokens, token_index):
            value = tokens[token_index].value
            return "%s_%s" % (value, len(value))

        cache = [{TOKEN_NAME: token} for token in
                 tokenize("hello beautiful world", LANGUAGE_EN)]
        feature = Feature("test_feature", fn, offset=1)

        # When
        res = feature.compute(1, cache)

        # Then
        self.assertEqual(res, "world_5")
示例#4
0
 def build_features(self):
     return [
         Feature(base_name=self.feature_name,
                 func=self.compute_feature,
                 offset=offset,
                 drop_out=self.drop_out) for offset in self.offsets
     ]
示例#5
0
    def test_feature_should_work_with_offset(self):
        # Given
        def fn(tokens, token_index):
            value = tokens[token_index].value
            return "%s_%s" % (value, len(value))

        cache = [{
            TOKEN_NAME: token
        } for token in tokenize("hello beautiful world", LANGUAGE_EN)]
        feature = Feature("test_feature", fn, offset=1)

        # When
        res = feature.compute(1, cache)

        # Then
        self.assertEqual(res, "world_5")
示例#6
0
 def build_features(self,
                    builtin_entity_parser=None,
                    custom_entity_parser=None):
     return [
         Feature(base_name=self.feature_name,
                 func=self.compute_feature,
                 offset=offset,
                 drop_out=self.drop_out) for offset in self.offsets
     ]
示例#7
0
    def build_features(self):
        features = []
        for name, collection in iteritems(self.collections):
            # We need to call this wrapper in order to properly capture
            # `collection`
            collection_match = self._build_collection_match_fn(collection)

            for offset in self.offsets:
                feature = Feature("entity_match_%s" % name, collection_match,
                                  offset, self.drop_out)
                features.append(feature)
        return features
示例#8
0
    def build_features(self):
        features = []
        for entity_name in self.entities:
            # We need to call this wrapper in order to properly capture
            # `entity_name`
            entity_match = self._build_entity_match_fn(entity_name)

            for offset in self.offsets:
                feature = Feature("entity_match_%s" % entity_name,
                                  entity_match, offset, self.drop_out)
                features.append(feature)
        return features
示例#9
0
    def build_features(self):
        features = []

        for builtin_entity in self.builtin_entities:
            # We need to call this wrapper in order to properly capture
            # `builtin_entity`
            builtin_entity_match = self._build_entity_match_fn(builtin_entity)
            for offset in self.offsets:
                feature_name = "builtin_entity_match_%s" % builtin_entity
                feature = Feature(feature_name, builtin_entity_match, offset,
                                  self.drop_out)
                features.append(feature)

        return features
示例#10
0
    def test_feature_should_work_with_cache(self):
        # Given
        def fn(tokens, token_index):
            value = tokens[token_index].value
            return "%s_%s" % (value, len(value))

        mocked_fn = MagicMock(side_effect=fn)

        cache = [{
            TOKEN_NAME: token
        } for token in tokenize("hello beautiful world", LANGUAGE_EN)]
        feature = Feature("test_feature", mocked_fn, offset=0)
        feature.compute(2, cache)
        feature1 = Feature("test_feature", mocked_fn, offset=1)
        feature2 = Feature("test_feature", mocked_fn, offset=2)

        # When
        res1 = feature1.compute(1, cache)
        res1_bis = feature1.compute(0, cache)
        res2 = feature2.compute(0, cache)

        # Then
        self.assertEqual(res1, "world_5")
        self.assertEqual(res1_bis, "beautiful_9")
        self.assertEqual(res2, "world_5")
        self.assertEqual(mocked_fn.call_count, 2)