コード例 #1
0
def test_recommender_extra_data(test_ctx):
    # Test that the recommender uses locale data from the "extra"
    # section if available.
    def validate_recommendations(data, expected_locale):
        # Make sure the structure of the recommendations is correct and that we
        # recommended the the right addon.
        data = sorted(data, key=lambda x: x[1], reverse=True)
        assert isinstance(data, list)
        assert len(data) == len(FAKE_LOCALE_DATA[expected_locale])

        # Make sure that the reported addons are the one from the fake data.
        for (addon_id, weight), (expected_id, expected_weight) in zip(
                data, FAKE_LOCALE_DATA[expected_locale]):
            assert addon_id == expected_id
            assert weight == expected_weight

    ctx = install_mock_data(test_ctx)
    r = LocaleRecommender(ctx)
    recommendations = r.recommend({}, 10, extra_data={"locale": "en"})
    validate_recommendations(recommendations, "en")

    # Make sure that we favour client data over the extra data.
    recommendations = r.recommend({"locale": "en"},
                                  10,
                                  extra_data={"locale": "te-ST"})
    validate_recommendations(recommendations, "en")
コード例 #2
0
def test_can_recommend_no_model(mock_s3_json_downloader):
    r = LocaleRecommender()

    # We should never be able to recommend if something went
    # wrong with the model.
    assert not r.can_recommend({})
    assert not r.can_recommend({"locale": []})
    assert not r.can_recommend({"locale": "it"})
コード例 #3
0
ファイル: notebook.py プロジェクト: crankycoder/taar_notebook
    def __init__(self, TOP_ADDONS_BY_LOCALE_FILE_PATH):
        LocaleRecommender.__init__(self)

        with open(TOP_ADDONS_BY_LOCALE_FILE_PATH) as data_file:
            top_addons_by_locale = json.load(data_file)

        self.top_addons_by_locale = defaultdict(lambda: defaultdict(int),
                                                top_addons_by_locale)
コード例 #4
0
def test_can_recommend_no_model(test_ctx):
    ctx = install_mock_data(test_ctx)
    r = LocaleRecommender(ctx)

    # We should never be able to recommend if something went
    # wrong with the model.
    assert not r.can_recommend({})
    assert not r.can_recommend({"locale": []})
    assert not r.can_recommend({"locale": "it"})
コード例 #5
0
def test_can_recommend(mock_s3_json_downloader):
    r = LocaleRecommender()

    # Test that we can't recommend if we have not enough client info.
    assert not r.can_recommend({})
    assert not r.can_recommend({"locale": []})

    # Check that we can recommend if the user has at least an addon.
    assert r.can_recommend({"locale": "en"})
コード例 #6
0
def test_can_recommend_no_model():
    ctx = create_test_ctx()
    r = LocaleRecommender(ctx)

    # We should never be able to recommend if something went
    # wrong with the model.
    assert not r.can_recommend({})
    assert not r.can_recommend({"locale": []})
    assert not r.can_recommend({"locale": "it"})
コード例 #7
0
def test_can_recommend(test_ctx):
    ctx = install_mock_data(test_ctx)
    r = LocaleRecommender(ctx)

    # Test that we can't recommend if we have not enough client info.
    assert not r.can_recommend({})
    assert not r.can_recommend({"locale": []})

    # Check that we can recommend if the user has at least an addon.
    assert r.can_recommend({"locale": "en"})
コード例 #8
0
def test_recommendations(mock_s3_json_downloader):
    # Test that the locale recommender returns the correct
    # locale dependent addons.
    r = LocaleRecommender()
    recommendations = r.recommend({"locale": "en"}, 10)

    # Make sure the structure of the recommendations is correct and that we
    # recommended the the right addon.
    assert isinstance(recommendations, list)
    assert len(recommendations) == len(FAKE_LOCALE_DATA["en"])

    # Make sure that the reported addons are the one from the fake data.
    for addon_id in recommendations:
        assert addon_id in FAKE_LOCALE_DATA["en"]
コード例 #9
0
def test_recommender_str():
    """Tests that the string representation of the recommender is correct
    """
    # TODO: this test is brittle and should be removed once it is safe
    # to do so
    ctx = create_test_ctx()
    r = LocaleRecommender(ctx)
    assert str(r) == "LocaleRecommender"
コード例 #10
0
def test_recommendations(test_ctx):
    """Test that the locale recommender returns the correct
    locale dependent addons.

    The JSON output for this recommender should be a list of 2-tuples
    of (GUID, weight).
    """
    ctx = install_mock_data(test_ctx)
    r = LocaleRecommender(ctx)
    recommendations = r.recommend({"locale": "en"}, 10)

    # Make sure the structure of the recommendations is correct and that we
    # recommended the the right addon.
    assert isinstance(recommendations, list)
    assert len(recommendations) == len(FAKE_LOCALE_DATA["en"])

    # Make sure that the reported addons are the one from the fake data.
    for (addon_id, weight) in recommendations:
        assert 1 == weight
        assert addon_id in FAKE_LOCALE_DATA["en"]
コード例 #11
0
ファイル: run_package_test.py プロジェクト: eu9ene/taar
def load_recommenders():
    from taar.recommenders import LocaleRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import CollaborativeRecommender
    from taar.context import package_context

    ctx = package_context()

    lr = LocaleRecommender(ctx)
    sr = SimilarityRecommender(ctx)
    cr = CollaborativeRecommender(ctx)
    return {LOCALE: lr, COLLABORATIVE: cr, SIMILARITY: sr}
コード例 #12
0
def load_recommenders(ctx):
    from taar.recommenders import LocaleRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import CollaborativeRecommender

    ctx = default_context()

    reload_configuration()

    lr = LocaleRecommender(ctx)
    sr = SimilarityRecommender(ctx)
    cr = CollaborativeRecommender(ctx)
    return {LOCALE: lr, COLLABORATIVE: cr, SIMILARITY: sr}
コード例 #13
0
def test_recommender_extra_data():
    # Test that the recommender uses locale data from the "extra"
    # section if available.
    def validate_recommendations(data, expected_locale):
        # Make sure the structure of the recommendations is correct and that we
        # recommended the the right addon.
        assert isinstance(data, list)
        assert len(data) == len(FAKE_LOCALE_DATA[expected_locale])

        # Make sure that the reported addons are the one from the fake data.
        for (addon_id, weight) in data:
            assert addon_id in FAKE_LOCALE_DATA[expected_locale]
            assert 1 == weight

    ctx = create_test_ctx()
    r = LocaleRecommender(ctx)
    recommendations = r.recommend({}, 10, extra_data={"locale": "en"})
    validate_recommendations(recommendations, "en")

    # Make sure that we favour client data over the extra data.
    recommendations = r.recommend({"locale": "en"}, 10, extra_data={"locale": "te-ST"})
    validate_recommendations(recommendations, "en")
コード例 #14
0
ファイル: context.py プロジェクト: mlopatka/taar
def default_context():
    ctx = _default_context()
    from taar.recommenders import CollaborativeRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import LocaleRecommender

    # Note that the EnsembleRecommender is *not* in this map as it
    # needs to ensure that the recommender_map key is installed in the
    # context
    ctx['recommender_factory_map'] = {
        'collaborative': lambda: CollaborativeRecommender(ctx.child()),
        'similarity': lambda: SimilarityRecommender(ctx.child()),
        'locale': lambda: LocaleRecommender(ctx.child())
    }

    return ctx
コード例 #15
0
def default_context():
    ctx = Context()
    from taar.recommenders import LegacyRecommender
    from taar.recommenders import CollaborativeRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import LocaleRecommender
    from taar.cache import Clock
    from taar.cache import JSONCache

    # Note that the EnsembleRecommender is *not* in this map as it
    # needs to ensure that the recommender_map key is installed in the
    # context
    ctx['recommender_factory_map'] = {
        'legacy': lambda: LegacyRecommender(ctx.child()),
        'collaborative': lambda: CollaborativeRecommender(ctx.child()),
        'similarity': lambda: SimilarityRecommender(ctx.child()),
        'locale': lambda: LocaleRecommender(ctx.child())
    }

    ctx['utils'] = utils
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx
コード例 #16
0
ファイル: context.py プロジェクト: eu9ene/taar
def app_context():
    """
    Prepare context for TAAR web servie
    """
    from taar.settings import AppSettings, DefaultCacheSettings, RedisCacheSettings
    from taar.recommenders.cache import TAARCache
    from taar.recommenders.redis_cache import TAARCacheRedis
    from taar.logs.moz_logging import Logging

    ctx = Context()

    logger = Logging(ctx)
    logger.set_log_level(AppSettings.PYTHON_LOG_LEVEL)
    ctx[IMozLogging] = logger

    if AppSettings.DISABLE_REDIS:
        ctx['cache_settings'] = DefaultCacheSettings
        ctx[ITAARCache] = TAARCache.get_instance(ctx)
    else:
        ctx['cache_settings'] = RedisCacheSettings
        ctx[ITAARCache] = TAARCacheRedis.get_instance(ctx)

    from taar.recommenders import CollaborativeRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import LocaleRecommender

    # Note that the EnsembleRecommender is *not* in this map as it
    # needs to ensure that the recommender_map key is installed in the
    # context
    ctx["recommender_factory_map"] = {
        "collaborative": lambda: CollaborativeRecommender(ctx.child()),
        "similarity": lambda: SimilarityRecommender(ctx.child()),
        "locale": lambda: LocaleRecommender(ctx.child()),
    }

    return ctx
コード例 #17
0
ファイル: notebook.py プロジェクト: crankycoder/taar_notebook
    num_mask = get_num_masked(addons)

    masked, unmasked = random_partition(addons, num_mask)

    client['installed_addons'] = unmasked
    client['masked_addons'] = masked

    return client


training_masked = map(mask_addons, training)

recommenders = {
    "collaborative": CollaborativeRecommender(),
    "similarity": SimilarityRecommender(),
    "locale": LocaleRecommender("./top_addons_by_locale.json"),
    "legacy": LegacyRecommender()
}

def compute_features(client_data):
    recommendations = []
    matrix = []

    for _, recommender in recommenders.items():
        recommendations.append(recommender.get_weighted_recommendations(client_data))

    for addon in whitelist:
        matrix.append([features[addon] for features in recommendations])

    return client_data, np.array(matrix)
コード例 #18
0
def test_recommender_str(mock_s3_json_downloader):
    # Tests that the string representation of the recommender is correct
    r = LocaleRecommender()
    assert str(r) == "LocaleRecommender"