def test_update(self):
        """Test _FeatureExtractor update method."""

        hook = Hook(key='key', func=lambda: None)
        _prep = _FeatureExtractor().update(hook)

        self.assertTrue('key' in _prep.keys)
    def test_hook(self):
        """Test Hook initialization and key handling."""
        hook = Hook(key='key', func=lambda: 'test')

        self.assertEqual(hook.key, 'key')
        # w/o args
        self.assertEqual(hook(), 'test')
        # check invalid key
        with pytest.raises(ValueError):
            _ = Hook(key='key', func=lambda: None)

        # hook with args
        hook_args = Hook(key='key_', func=lambda x: x)

        self.assertEqual(Hook.get_current_keys(), {'key', 'key_'})
        self.assertEqual(hook_args.key, 'key_')
Exemple #3
0
    def test_init(self):
        hook = Hook(key='label', func=lambda x: x)
        attributes = ['project']

        label_prep = LabelPreprocessor(feed_attributes=attributes, hook=hook)

        self.assertIsInstance(label_prep, LabelPreprocessor)
        self.assertIsInstance(label_prep._hook, Hook)  # pylint: disable=protected-access
    def test_feed(self):
        """Test _FeatureExtractor feed method."""
        hook = Hook(key='key', func=lambda x: x)
        _prep = _FeatureExtractor().update(hook)

        # feed the extractor with skip=True
        result = _prep.feed({'x': 'test'}, skip_unfed_hooks=True)
        self.assertIsInstance(result, dict)

        key, value = list(*result.items())

        self.assertEqual(key, 'key')
        self.assertEqual(value, 'test')

        # feed and disable skip
        with self.assertRaises(TypeError):
            result = _prep.feed({'x': 'test'}, skip_unfed_hooks=False)
            key, value = list(*result.items())

            self.assertEqual(key, 'key')
            self.assertEqual(value, 'test')
    def test_fit_transform(self):
        """Test LabelPreprocessor `fit_transform` method."""
        hook = Hook(key='label', func=lambda p, d: 'label')
        attributes = ['project', 'description']

        test_data = [TEST_CVE]

        # prepare the data for label preprocessor
        feed_prep = NVDFeedPreprocessor(attributes, skip_duplicity=True)
        test_data = feed_prep.transform(X=test_data)

        label_prep = LabelPreprocessor(feed_attributes=attributes,
                                       output_attributes=['description'],
                                       hook=hook)

        result = label_prep.fit_transform(test_data)

        # check not empty
        self.assertTrue(any(result))
        # check that correct label is returned by the Hook
        label = result[0].label
        self.assertEqual(label, 'label')
        The cmp function must be of signature (a: int, b: int) -> bool

    :param limit: int, limit to compare to (the `b` argument in `cmp`)
    """
    word, _ = features[pos]  # type: str

    if cmp is None:
        cmp = operator.gt

    return cmp(len(word), limit)


# Feature Hooks
# -------------

has_uppercase_hook: Hook = Hook(key="has_uppercase", func=__has_uppercase)
"""Hook: Return whether the word in `tagged` contains uppercase letter."""

is_alnum_hook: Hook = Hook(key="is_alnum", func=__is_alnum)
"""Hook: Return whether the word contains only alphanumeric characters."""

ver_follows_hook: Hook = Hook(key="ver_follows", func=__ver_follows)
"""Hook: Return whether the given word is followed by a version string."""

ver_precedes_hook: Hook = Hook(key="ver_precedes", func=__ver_precedes)
"""Hook: Return whether the given word is preceded by a version string."""

ver_pos_hook: Hook = Hook(key="ver_pos", func=__ver_pos)
"""Hook: Return version position in the `tagged` w.r.t the given word."""

vendor_product_match_hook: Hook = Hook(key="vendor_product_match",