Example #1
0
    def parse_taxonomy_for_recommender(self, recommenderEl):
        taxonomyEl = recommenderEl.find('taxonomy')
        taxonomy = Taxonomy(taxonomyEl.get('name'), self.parse_taxons(taxonomyEl))
        if taxonomyEl.get('inherit'):
            taxonomy.inherit(self.taxonomies[taxonomyEl.get('inherit')])

        return taxonomy
Example #2
0
class TaxonomyTestCase(unittest.TestCase):
    def setUp(self):
        self.taxonomy = Taxonomy('test', {
            'key': 'value',
            'key2': 'value2',
            'key3': 'value3',
        })

    def test_get_name(self):
        self.assertEqual(self.taxonomy.get_name(), 'test')

    def test_get_taxons(self):
        taxons = self.taxonomy.get_taxons()
        self.assertEqual(taxons, {
            'key': 'value',
            'key2': 'value2',
            'key3': 'value3',
        })
        self.assertEqual(len(taxons), 3)

    def test_get(self):
        self.assertEqual(self.taxonomy.get('key'), 'value')
        self.assertEqual(self.taxonomy.get('key2'), 'value2')
        self.assertIsNone(self.taxonomy.get('key4'))

    def test_inherit(self):
        parentTaxonomy = Taxonomy('parentTest', {'key5': 'value5'})
        self.assertEqual(parentTaxonomy.get('key5'), 'value5')
        self.assertIsNone(self.taxonomy.get('key4'))
        self.assertIsNone(parentTaxonomy.get('key4'))

        parentParentTaxonomy = Taxonomy('parentTest', {'key4': 'value4'})
        parentTaxonomy.inherit(parentParentTaxonomy)
        self.assertEqual(parentTaxonomy.get('key4'), 'value4')

        self.taxonomy.inherit(parentTaxonomy)
        self.assertEqual(self.taxonomy.get('key4'), 'value4')
        self.assertEqual(self.taxonomy.get('key5'), 'value5')

    def test_translate(self):
        translated = self.taxonomy.translate({'value': 'asd', 'value3': 'qwe'})
        self.assertEqual(translated, {
            'key': 'asd',
            'key2': 'value2',
            'key3': 'qwe'
        })
Example #3
0
 def setUp(self):
     taxonomy = Taxonomy('base', {'key': 'value', 'key2': 'value2'})
     component1 = Engine('recommender1', taxonomy,
                         {'base_url': 'http://localhost'})
     component2 = Engine('recommender2', taxonomy,
                         {'base_url': 'http://localhost2'})
     components = {'component1': component1, 'component2': component2}
     settings = {'test': 'value'}
     self.engine = HybridEngine('hybrid', taxonomy, components, settings)
Example #4
0
    def setUp(self):
        taxonomy = Taxonomy('base', {'key': 'value', 'key2': 'value2'})
        self.engine = Engine('recommender1', taxonomy, {
            'base_url': 'http://localhost',
            'key': 'value'
        })

        self.engine.requests = MagicMock()
        self.engine.requests.request = MagicMock()
        self.engine.requests.get = MagicMock()
Example #5
0
class TaxonomyTestCase(unittest.TestCase):
    def setUp(self):
        self.taxonomy = Taxonomy('test', {
            'key': 'value',
            'key2': 'value2',
            'key3': 'value3',
        })

    def test_get_name(self):
        self.assertEqual(self.taxonomy.get_name(), 'test')

    def test_get_taxons(self):
        taxons = self.taxonomy.get_taxons()
        self.assertEqual(taxons, {
            'key': 'value',
            'key2': 'value2',
            'key3': 'value3',
        })
        self.assertEqual(len(taxons), 3)

    def test_get(self):
        self.assertEqual(self.taxonomy.get('key'), 'value')
        self.assertEqual(self.taxonomy.get('key2'), 'value2')
        self.assertIsNone(self.taxonomy.get('key4'))

    def test_inherit(self):
        parentTaxonomy = Taxonomy('parentTest', {'key5': 'value5'})
        self.assertEqual(parentTaxonomy.get('key5'), 'value5')
        self.assertIsNone(self.taxonomy.get('key4'))
        self.assertIsNone(parentTaxonomy.get('key4'))

        parentParentTaxonomy = Taxonomy('parentTest', {'key4': 'value4'})
        parentTaxonomy.inherit(parentParentTaxonomy)
        self.assertEqual(parentTaxonomy.get('key4'), 'value4')

        self.taxonomy.inherit(parentTaxonomy)
        self.assertEqual(self.taxonomy.get('key4'), 'value4')
        self.assertEqual(self.taxonomy.get('key5'), 'value5')

    def test_translate(self):
        translated = self.taxonomy.translate({'value': 'asd', 'value3': 'qwe'})
        self.assertEqual(translated, {'key': 'asd', 'key2': 'value2', 'key3': 'qwe'})
Example #6
0
    def test_inherit(self):
        parentTaxonomy = Taxonomy('parentTest', {'key5': 'value5'})
        self.assertEqual(parentTaxonomy.get('key5'), 'value5')
        self.assertIsNone(self.taxonomy.get('key4'))
        self.assertIsNone(parentTaxonomy.get('key4'))

        parentParentTaxonomy = Taxonomy('parentTest', {'key4': 'value4'})
        parentTaxonomy.inherit(parentParentTaxonomy)
        self.assertEqual(parentTaxonomy.get('key4'), 'value4')

        self.taxonomy.inherit(parentTaxonomy)
        self.assertEqual(self.taxonomy.get('key4'), 'value4')
        self.assertEqual(self.taxonomy.get('key5'), 'value5')
Example #7
0
    def _setUp(self, engineClass, name):
        taxonomy = Taxonomy('base', {
            'key': 'value',
            'key2': 'value2',
            'item_id': 'sku'
        })
        self.engine = engineClass(name, taxonomy, {
            'base_url': 'http://localhost/',
            'key': 'value'
        })

        self.engine.requests = MagicMock()
        self.engine.requests.request = MagicMock()
        self.engine.requests.post = MagicMock()
Example #8
0
    def setUp(self):
        taxonomy = Taxonomy('base', {'key': 'value', 'key2': 'value2'})
        component1 = Engine('recommender1', taxonomy,
                            {'base_url': 'http://localhost'})
        component1.requests = MagicMock()
        component1.requests.get = MagicMock()

        component2 = Engine('recommender2', taxonomy,
                            {'base_url': 'http://localhost2'})
        component2.requests = MagicMock()
        component2.requests.get = MagicMock()

        components = {'component1': component1, 'component2': component2}
        settings = {'weight': {'component1': 0.25, 'component2': 0.75}}
        self.engine = HybridEngine('hybrid', taxonomy, components, settings)
Example #9
0
    def parse_taxonomy(self, name, taxonomies):
        taxonomyEl = taxonomies[name]

        # check if taxonomy is already processed
        if name in self.taxonomies:
            return self.taxonomies[name]

        # create taxonomy object
        self.taxonomies[name] = Taxonomy(name, self.parse_taxons(taxonomyEl))

        # check inheritance
        if taxonomyEl.get('inherit'):
            parent_taxonomy = self.parse_taxonomy(taxonomyEl.get('inherit'), taxonomies)
            self.taxonomies[name].inherit(parent_taxonomy)

        return self.taxonomies[name]
Example #10
0
    def test_inherit(self):
        parentTaxonomy = Taxonomy('parentTest', {'key5': 'value5'})
        self.assertEqual(parentTaxonomy.get('key5'), 'value5')
        self.assertIsNone(self.taxonomy.get('key4'))
        self.assertIsNone(parentTaxonomy.get('key4'))

        parentParentTaxonomy = Taxonomy('parentTest', {'key4': 'value4'})
        parentTaxonomy.inherit(parentParentTaxonomy)
        self.assertEqual(parentTaxonomy.get('key4'), 'value4')

        self.taxonomy.inherit(parentTaxonomy)
        self.assertEqual(self.taxonomy.get('key4'), 'value4')
        self.assertEqual(self.taxonomy.get('key5'), 'value5')
Example #11
0
    def test_recommend(self):
        result1 = {'item1': 6, 'item2': 2, 'item3': 1}
        result2 = {'item1': 2, 'item2': 4, 'item3': 3}
        component1 = component2 = MagicMock()
        component1.json = MagicMock(return_value=result1)
        component2.json = MagicMock(return_value=result2)
        self.engine.components[
            'component1'].requests.get.return_value = component1
        self.engine.components[
            'component2'].requests.get.return_value = component2

        self.assertEqual(self.engine.recommend({'key': 'value'}),
                         ['item2', 'item3', 'item1'])

        old_taxonomy = self.engine.taxonomy
        self.engine.taxonomy = Taxonomy('base', {'key': 'value', 'limit': '2'})
        self.assertEqual(self.engine.recommend({'key': 'value'}),
                         ['item2', 'item3'])
        self.engine.taxonomy = old_taxonomy
Example #12
0
 def setUp(self):
     self.taxonomy = Taxonomy('test', {
         'key': 'value',
         'key2': 'value2',
         'key3': 'value3',
     })
Example #13
0
 def setUp(self):
     self.taxonomy = Taxonomy('test', {
         'key': 'value',
         'key2': 'value2',
         'key3': 'value3',
     })