Package org.grouplens.lenskit

Examples of org.grouplens.lenskit.ItemRecommender


        public Optional<List<ScoredId>> load(RecommendRequest key) throws Exception {
            ItemDAO idao = recommender.get(ItemDAO.class);
            if (idao == null ) {
                throw new RuntimeException("cannot recommend without item DAO");
            }
            ItemRecommender irec = recommender.getItemRecommender();
            if (irec == null) {
                return Optional.absent();
            }
            LongSet candidates = key.candidates.select(LenskitTestUser.this);
            LongSet excludes = key.exclude.select(LenskitTestUser.this);
            return Optional.of(irec.recommend(getUserId(), key.listSize, candidates, excludes));
        }
View Full Code Here


    /**
     * Tests {@code recommend(long, SparseVector)}.
     */
    @Test
    public void testUserUserRecommender1() {
        ItemRecommender recommender = rec.getItemRecommender();
        assert recommender != null;
        List<ScoredId> recs = recommender.recommend(1);
        assertTrue(recs.isEmpty());

        recs = recommender.recommend(2);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(3);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L));

        recs = recommender.recommend(4);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(5);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(7L));

        recs = recommender.recommend(6);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));
    }
View Full Code Here

    /**
     * Tests {@code recommend(long, SparseVector, int)}.
     */
    @Test
    public void testUserUserRecommender2() {
        ItemRecommender recommender = rec.getItemRecommender();
        assert recommender != null;
        List<ScoredId> recs = recommender.recommend(1, -1);
        assertTrue(recs.isEmpty());

        recs = recommender.recommend(2, 2);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(2, -1);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(2, 1);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(2, 0);
        assertThat(recs, hasSize(0));

        recs = recommender.recommend(3, 1);
        assertEquals(1, recs.size());
        assertEquals(6, recs.get(0).getId());

        recs = recommender.recommend(3, 0);
        assertTrue(recs.isEmpty());

        recs = recommender.recommend(4, 1);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(5, -1);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(7L));

        recs = recommender.recommend(6, 2);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));

        recs = recommender.recommend(6, 1);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   anyOf(contains(6L), contains(7L)));

        recs = recommender.recommend(6, 0);
        assertThat(recs, hasSize(0));
    }
View Full Code Here

        List<Long> users = options.get("users");
        final int n = options.getInt("num_recs");

        LenskitRecommender rec = engine.createRecommender();
        ItemRecommender irec = rec.getItemRecommender();
        if (irec == null) {
            logger.error("recommender has no item recommender");
            throw new UnsupportedOperationException("no item recommender");
        }

        logger.info("recommending for {} users", users.size());
        Symbol pchan = getPrintChannel();
        Stopwatch timer = Stopwatch.createStarted();
        for (long user: users) {
            List<ScoredId> recs = irec.recommend(user, n);
            System.out.format("recommendations for user %d:\n", user);
            for (ScoredId item: recs) {
                System.out.format("  %d: %.3f", item.getId(), item.getScore());
                if (pchan != null && item.hasUnboxedChannel(pchan)) {
                    System.out.format(" (%f)", item.getUnboxedChannelValue(pchan));
View Full Code Here

    /**
     * Tests {@code recommend(long, SparseVector, Set)}.
     */
    @Test
    public void testUserUserRecommender3() {
        ItemRecommender recommender = rec.getItemRecommender();
        assert recommender != null;

        LongOpenHashSet candidates = new LongOpenHashSet();
        candidates.add(6);
        candidates.add(7);
        candidates.add(8);
        candidates.add(9);

        List<ScoredId> recs = recommender.recommend(1, candidates);
        assertTrue(recs.isEmpty());

        candidates.clear();
        candidates.add(9);
        recs = recommender.recommend(2, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        candidates.clear();
        candidates.add(6);
        candidates.add(7);
        candidates.add(8);
        recs = recommender.recommend(2, candidates);
        assertTrue(recs.isEmpty());

        candidates.add(9);
        recs = recommender.recommend(3, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L));

        recs = recommender.recommend(4, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(5, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(7L));

        candidates.remove(7);
        recs = recommender.recommend(5, candidates);
        assertTrue(recs.isEmpty());

        candidates.add(7);
        recs = recommender.recommend(6, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));

        candidates.remove(9);
        recs = recommender.recommend(6, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));

        candidates.remove(8);
        recs = recommender.recommend(6, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));

        candidates.remove(7);
        recs = recommender.recommend(6, candidates);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L));

        candidates.remove(6);
        recs = recommender.recommend(6, candidates);
        assertTrue(recs.isEmpty());
    }
View Full Code Here

    /**
     * Tests {@code recommend(long, SparseVector, int, Set, Set)}.
     */
    @Test
    public void testUserUserRecommender4() {
        ItemRecommender recommender = rec.getItemRecommender();
        assert recommender != null;

        LongOpenHashSet candidates = new LongOpenHashSet();
        candidates.add(9);
        List<ScoredId> recs = recommender.recommend(2, -1, candidates, null);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(2, 1, candidates, null);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(9L));

        recs = recommender.recommend(2, 0, candidates, null);
        assertTrue(recs.isEmpty());

        LongOpenHashSet exclude = new LongOpenHashSet();
        exclude.add(9);
        recs = recommender.recommend(2, -1, candidates, exclude);
        assertTrue(recs.isEmpty());

        // FIXME Add tests for default exclude set
        recs = recommender.recommend(5, -1, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L, 7L, 6L, 8L));

        recs = recommender.recommend(5, 5, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L, 7L, 6L, 8L));

        recs = recommender.recommend(5, 4, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L, 7L, 6L, 8L));

        recs = recommender.recommend(5, 3, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L, 7L, 6L));

        recs = recommender.recommend(5, 2, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L, 7L));

        recs = recommender.recommend(5, 1, null, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(9L));

        recs = recommender.recommend(5, 0, null, LongSets.EMPTY_SET);
        assertTrue(recs.isEmpty());

        candidates.clear();
        candidates.add(6);
        candidates.add(7);
        recs = recommender.recommend(6, -1, candidates, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   containsInAnyOrder(6L, 7L));

        candidates.remove(6);
        recs = recommender.recommend(6, -1, candidates, LongSets.EMPTY_SET);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(7L));

        candidates.remove(7);
        recs = recommender.recommend(6, -1, candidates, LongSets.EMPTY_SET);
        assertTrue(recs.isEmpty());

        candidates.add(6);
        candidates.add(7);
        exclude.add(6);
        recs = recommender.recommend(6, -1, candidates, exclude);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(7L));

        exclude.add(7);
        recs = recommender.recommend(6, -1, candidates, exclude);
        assertTrue(recs.isEmpty());

        exclude.remove(6);
        recs = recommender.recommend(6, -1, candidates, exclude);
        assertThat(Lists.transform(recs, ScoredIds.idFunction()),
                   contains(6L));
    }
View Full Code Here

TOP

Related Classes of org.grouplens.lenskit.ItemRecommender

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.