Création d'interface de recommandation et de 4 implémentations
Showing
5 changed files
with
292 additions
and
0 deletions
1 | +package org.legrog.recommendation.process; | ||
2 | + | ||
3 | +import org.grouplens.lenskit.GlobalItemScorer; | ||
4 | +import org.grouplens.lenskit.ItemRecommender; | ||
5 | +import org.grouplens.lenskit.ItemScorer; | ||
6 | +import org.grouplens.lenskit.RecommenderBuildException; | ||
7 | +import org.grouplens.lenskit.baseline.BaselineScorer; | ||
8 | +import org.grouplens.lenskit.core.LenskitConfiguration; | ||
9 | +import org.grouplens.lenskit.core.LenskitRecommender; | ||
10 | +import org.grouplens.lenskit.data.dao.EventDAO; | ||
11 | +import org.grouplens.lenskit.data.history.LikeCountUserHistorySummarizer; | ||
12 | +import org.grouplens.lenskit.data.history.UserHistorySummarizer; | ||
13 | +import org.grouplens.lenskit.data.pref.PreferenceDomain; | ||
14 | +import org.grouplens.lenskit.data.text.DelimitedColumnEventFormat; | ||
15 | +import org.grouplens.lenskit.data.text.Fields; | ||
16 | +import org.grouplens.lenskit.data.text.LikeEventType; | ||
17 | +import org.grouplens.lenskit.data.text.TextEventDAO; | ||
18 | +import org.grouplens.lenskit.knn.item.ItemItemGlobalScorer; | ||
19 | +import org.grouplens.lenskit.knn.item.ItemItemScorer; | ||
20 | +import org.grouplens.lenskit.knn.item.ItemSimilarity; | ||
21 | +import org.grouplens.lenskit.knn.item.ItemVectorSimilarity; | ||
22 | +import org.grouplens.lenskit.scored.ScoredId; | ||
23 | +import org.grouplens.lenskit.vectors.similarity.CosineVectorSimilarity; | ||
24 | +import org.grouplens.lenskit.vectors.similarity.VectorSimilarity; | ||
25 | +import org.slf4j.Logger; | ||
26 | +import org.slf4j.LoggerFactory; | ||
27 | + | ||
28 | +import java.io.File; | ||
29 | +import java.util.List; | ||
30 | + | ||
31 | +public class CosineRecommender implements Recommender { | ||
32 | + | ||
33 | + Logger logger = LoggerFactory.getLogger(getClass()); | ||
34 | + | ||
35 | + LenskitConfiguration config; | ||
36 | + DelimitedColumnEventFormat delimitedColumnEventFormat; | ||
37 | + | ||
38 | + ItemRecommender irec; | ||
39 | + | ||
40 | + @Override | ||
41 | + public void Recommender(String filePath) { | ||
42 | + config = new LenskitConfiguration(); | ||
43 | + | ||
44 | + config.bind(ItemScorer.class).to(ItemItemScorer.class); | ||
45 | + config.bind(GlobalItemScorer.class).to(ItemItemGlobalScorer.class); | ||
46 | + config.bind(BaselineScorer.class, ItemScorer.class).to(ItemItemScorer.class); | ||
47 | + config.bind(PreferenceDomain.class).to(new PreferenceDomain(0, 1)); | ||
48 | + config.bind(ItemSimilarity.class).to(ItemVectorSimilarity.class); | ||
49 | + config.bind(VectorSimilarity.class).to(CosineVectorSimilarity.class); | ||
50 | + config.bind(UserHistorySummarizer.class).to(LikeCountUserHistorySummarizer.class); | ||
51 | + | ||
52 | + delimitedColumnEventFormat = new DelimitedColumnEventFormat(new LikeEventType()); | ||
53 | + delimitedColumnEventFormat.setDelimiter("\t"); | ||
54 | + delimitedColumnEventFormat.setHeaderLines(1); | ||
55 | + delimitedColumnEventFormat.setFields(Fields.item(), Fields.user()); | ||
56 | + | ||
57 | + TextEventDAO textEventDAO = new TextEventDAO(new File(filePath), delimitedColumnEventFormat); | ||
58 | + config.bind(EventDAO.class).to(textEventDAO); | ||
59 | + | ||
60 | + LenskitRecommender rec; | ||
61 | + | ||
62 | + try { | ||
63 | + rec = LenskitRecommender.build(config); | ||
64 | + irec = rec.getItemRecommender(); | ||
65 | + } catch (RecommenderBuildException e) { | ||
66 | + logger.error("Recommender RecommenderBuildException {}", e.getStackTrace()); | ||
67 | + } | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public List<ScoredId> recommend(long user, int n) { | ||
72 | + return irec.recommend(user, n); | ||
73 | + } | ||
74 | +} |
1 | +package org.legrog.recommendation.process; | ||
2 | + | ||
3 | +import org.grouplens.lenskit.GlobalItemScorer; | ||
4 | +import org.grouplens.lenskit.ItemRecommender; | ||
5 | +import org.grouplens.lenskit.ItemScorer; | ||
6 | +import org.grouplens.lenskit.RecommenderBuildException; | ||
7 | +import org.grouplens.lenskit.baseline.BaselineScorer; | ||
8 | +import org.grouplens.lenskit.core.LenskitConfiguration; | ||
9 | +import org.grouplens.lenskit.core.LenskitRecommender; | ||
10 | +import org.grouplens.lenskit.data.dao.EventDAO; | ||
11 | +import org.grouplens.lenskit.data.history.RatingVectorUserHistorySummarizer; | ||
12 | +import org.grouplens.lenskit.data.history.UserHistorySummarizer; | ||
13 | +import org.grouplens.lenskit.data.pref.PreferenceDomain; | ||
14 | +import org.grouplens.lenskit.data.text.DelimitedColumnEventFormat; | ||
15 | +import org.grouplens.lenskit.data.text.Fields; | ||
16 | +import org.grouplens.lenskit.data.text.RatingEventType; | ||
17 | +import org.grouplens.lenskit.data.text.TextEventDAO; | ||
18 | +import org.grouplens.lenskit.knn.item.ItemItemGlobalScorer; | ||
19 | +import org.grouplens.lenskit.mf.funksvd.FunkSVDItemScorer; | ||
20 | +import org.grouplens.lenskit.scored.ScoredId; | ||
21 | +import org.slf4j.Logger; | ||
22 | +import org.slf4j.LoggerFactory; | ||
23 | + | ||
24 | +import java.io.File; | ||
25 | +import java.util.List; | ||
26 | + | ||
27 | +public class FunkSVDRecommender implements Recommender { | ||
28 | + | ||
29 | + Logger logger = LoggerFactory.getLogger(getClass()); | ||
30 | + | ||
31 | + LenskitConfiguration config; | ||
32 | + DelimitedColumnEventFormat delimitedColumnEventFormat; | ||
33 | + | ||
34 | + ItemRecommender irec; | ||
35 | + | ||
36 | + @Override | ||
37 | + public void Recommender(String filePath) { | ||
38 | + config = new LenskitConfiguration(); | ||
39 | + | ||
40 | + config.bind(ItemScorer.class).to(FunkSVDItemScorer.class); | ||
41 | + config.bind(GlobalItemScorer.class).to(ItemItemGlobalScorer.class); | ||
42 | + config.bind(BaselineScorer.class, ItemScorer.class).to(FunkSVDItemScorer.class); | ||
43 | + config.bind(PreferenceDomain.class).to(new PreferenceDomain(1.0, 5.0, 1.0)); | ||
44 | + config.bind(UserHistorySummarizer.class).to(RatingVectorUserHistorySummarizer.class); | ||
45 | + | ||
46 | + delimitedColumnEventFormat = new DelimitedColumnEventFormat(new RatingEventType()); | ||
47 | + delimitedColumnEventFormat.setDelimiter("\t"); | ||
48 | + delimitedColumnEventFormat.setHeaderLines(1); | ||
49 | + delimitedColumnEventFormat.setFields(Fields.item(), Fields.user(), Fields.rating()); | ||
50 | + | ||
51 | + TextEventDAO textEventDAO = new TextEventDAO(new File(filePath), delimitedColumnEventFormat); | ||
52 | + config.bind(EventDAO.class).to(textEventDAO); | ||
53 | + | ||
54 | + LenskitRecommender rec; | ||
55 | + | ||
56 | + try { | ||
57 | + rec = LenskitRecommender.build(config); | ||
58 | + irec = rec.getItemRecommender(); | ||
59 | + } catch (RecommenderBuildException e) { | ||
60 | + logger.error("Recommender RecommenderBuildException {}", e.getStackTrace()); | ||
61 | + } | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public List<ScoredId> recommend(long user, int n) { | ||
66 | + return irec.recommend(user, n); | ||
67 | + } | ||
68 | +} |
1 | +package org.legrog.recommendation.process; | ||
2 | + | ||
3 | +import org.grouplens.lenskit.GlobalItemScorer; | ||
4 | +import org.grouplens.lenskit.ItemRecommender; | ||
5 | +import org.grouplens.lenskit.ItemScorer; | ||
6 | +import org.grouplens.lenskit.RecommenderBuildException; | ||
7 | +import org.grouplens.lenskit.baseline.BaselineScorer; | ||
8 | +import org.grouplens.lenskit.core.LenskitConfiguration; | ||
9 | +import org.grouplens.lenskit.core.LenskitRecommender; | ||
10 | +import org.grouplens.lenskit.data.dao.EventDAO; | ||
11 | +import org.grouplens.lenskit.data.history.LikeCountUserHistorySummarizer; | ||
12 | +import org.grouplens.lenskit.data.history.UserHistorySummarizer; | ||
13 | +import org.grouplens.lenskit.data.pref.PreferenceDomain; | ||
14 | +import org.grouplens.lenskit.data.text.DelimitedColumnEventFormat; | ||
15 | +import org.grouplens.lenskit.data.text.Fields; | ||
16 | +import org.grouplens.lenskit.data.text.LikeEventType; | ||
17 | +import org.grouplens.lenskit.data.text.TextEventDAO; | ||
18 | +import org.grouplens.lenskit.knn.item.ItemItemGlobalScorer; | ||
19 | +import org.grouplens.lenskit.knn.item.ItemItemScorer; | ||
20 | +import org.grouplens.lenskit.knn.item.ItemSimilarity; | ||
21 | +import org.grouplens.lenskit.knn.item.ItemVectorSimilarity; | ||
22 | +import org.grouplens.lenskit.scored.ScoredId; | ||
23 | +import org.grouplens.lenskit.vectors.similarity.PearsonCorrelation; | ||
24 | +import org.grouplens.lenskit.vectors.similarity.VectorSimilarity; | ||
25 | +import org.slf4j.Logger; | ||
26 | +import org.slf4j.LoggerFactory; | ||
27 | + | ||
28 | +import java.io.File; | ||
29 | +import java.util.List; | ||
30 | + | ||
31 | +public class PearsonRecommender implements Recommender { | ||
32 | + | ||
33 | + Logger logger = LoggerFactory.getLogger(getClass()); | ||
34 | + | ||
35 | + LenskitConfiguration config; | ||
36 | + DelimitedColumnEventFormat delimitedColumnEventFormat; | ||
37 | + | ||
38 | + ItemRecommender irec; | ||
39 | + | ||
40 | + @Override | ||
41 | + public void Recommender(String filePath) { | ||
42 | + config = new LenskitConfiguration(); | ||
43 | + | ||
44 | + config.bind(ItemScorer.class).to(ItemItemScorer.class); | ||
45 | + config.bind(GlobalItemScorer.class).to(ItemItemGlobalScorer.class); | ||
46 | + config.bind(BaselineScorer.class, ItemScorer.class).to(ItemItemScorer.class); | ||
47 | + config.bind(PreferenceDomain.class).to(new PreferenceDomain(0, 1)); | ||
48 | + config.bind(ItemSimilarity.class).to(ItemVectorSimilarity.class); | ||
49 | + config.bind(VectorSimilarity.class).to(PearsonCorrelation.class); | ||
50 | + config.bind(UserHistorySummarizer.class).to(LikeCountUserHistorySummarizer.class); | ||
51 | + | ||
52 | + delimitedColumnEventFormat = new DelimitedColumnEventFormat(new LikeEventType()); | ||
53 | + delimitedColumnEventFormat.setDelimiter("\t"); | ||
54 | + delimitedColumnEventFormat.setHeaderLines(1); | ||
55 | + delimitedColumnEventFormat.setFields(Fields.item(), Fields.user()); | ||
56 | + | ||
57 | + TextEventDAO textEventDAO = new TextEventDAO(new File(filePath), delimitedColumnEventFormat); | ||
58 | + config.bind(EventDAO.class).to(textEventDAO); | ||
59 | + | ||
60 | + LenskitRecommender rec; | ||
61 | + | ||
62 | + try { | ||
63 | + rec = LenskitRecommender.build(config); | ||
64 | + irec = rec.getItemRecommender(); | ||
65 | + } catch (RecommenderBuildException e) { | ||
66 | + logger.error("Recommender RecommenderBuildException {}", e.getStackTrace()); | ||
67 | + } | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public List<ScoredId> recommend(long user, int n) { | ||
72 | + return irec.recommend(user, n); | ||
73 | + } | ||
74 | +} |
1 | +package org.legrog.recommendation.process; | ||
2 | + | ||
3 | +import org.grouplens.lenskit.GlobalItemScorer; | ||
4 | +import org.grouplens.lenskit.ItemRecommender; | ||
5 | +import org.grouplens.lenskit.ItemScorer; | ||
6 | +import org.grouplens.lenskit.RecommenderBuildException; | ||
7 | +import org.grouplens.lenskit.baseline.BaselineScorer; | ||
8 | +import org.grouplens.lenskit.core.LenskitConfiguration; | ||
9 | +import org.grouplens.lenskit.core.LenskitRecommender; | ||
10 | +import org.grouplens.lenskit.data.dao.EventDAO; | ||
11 | +import org.grouplens.lenskit.data.history.RatingVectorUserHistorySummarizer; | ||
12 | +import org.grouplens.lenskit.data.history.UserHistorySummarizer; | ||
13 | +import org.grouplens.lenskit.data.pref.PreferenceDomain; | ||
14 | +import org.grouplens.lenskit.data.text.*; | ||
15 | +import org.grouplens.lenskit.knn.item.ItemItemGlobalScorer; | ||
16 | +import org.grouplens.lenskit.scored.ScoredId; | ||
17 | +import org.grouplens.lenskit.slopeone.SlopeOneItemScorer; | ||
18 | +import org.slf4j.Logger; | ||
19 | +import org.slf4j.LoggerFactory; | ||
20 | + | ||
21 | +import java.io.File; | ||
22 | +import java.util.List; | ||
23 | + | ||
24 | +public class SlopeOneRecommender implements Recommender { | ||
25 | + | ||
26 | + Logger logger = LoggerFactory.getLogger(getClass()); | ||
27 | + | ||
28 | + LenskitConfiguration config; | ||
29 | + DelimitedColumnEventFormat delimitedColumnEventFormat; | ||
30 | + | ||
31 | + ItemRecommender irec; | ||
32 | + | ||
33 | + @Override | ||
34 | + public void Recommender(String filePath) { | ||
35 | + config = new LenskitConfiguration(); | ||
36 | + | ||
37 | + config.bind(ItemScorer.class).to(SlopeOneItemScorer.class); | ||
38 | + config.bind(GlobalItemScorer.class).to(ItemItemGlobalScorer.class); | ||
39 | + config.bind(BaselineScorer.class, ItemScorer.class).to(SlopeOneItemScorer.class); | ||
40 | + config.bind(PreferenceDomain.class).to(new PreferenceDomain(1.0, 5.0, 1.0)); | ||
41 | + config.bind(UserHistorySummarizer.class).to(RatingVectorUserHistorySummarizer.class); | ||
42 | + | ||
43 | + delimitedColumnEventFormat = new DelimitedColumnEventFormat(new RatingEventType()); | ||
44 | + delimitedColumnEventFormat.setDelimiter("\t"); | ||
45 | + delimitedColumnEventFormat.setHeaderLines(1); | ||
46 | + delimitedColumnEventFormat.setFields(Fields.item(), Fields.user(), Fields.rating()); | ||
47 | + | ||
48 | + TextEventDAO textEventDAO = new TextEventDAO(new File(filePath), delimitedColumnEventFormat); | ||
49 | + config.bind(EventDAO.class).to(textEventDAO); | ||
50 | + | ||
51 | + LenskitRecommender rec; | ||
52 | + | ||
53 | + try { | ||
54 | + rec = LenskitRecommender.build(config); | ||
55 | + irec = rec.getItemRecommender(); | ||
56 | + } catch (RecommenderBuildException e) { | ||
57 | + logger.error("Recommender RecommenderBuildException {}", e.getStackTrace()); | ||
58 | + } | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + public List<ScoredId> recommend(long user, int n) { | ||
63 | + return irec.recommend(user, n); | ||
64 | + } | ||
65 | +} |
-
Please register or login to post a comment