I want to use the Mahout as the recommender system.
In my project, there are contents, tags,reactions. User share a content after tagged and other users can read the content and give reactions.
I want to recommend contents when any user read a content.
In that case, I will not have the preference value if I use just reader information that's why I want to use reactions of content and tags of content with default mahout item similarity.
I could not be sure that using the reaction and tag information is the correct way in the recommender system. Is it a recommender problem or not.
If it is a correct way that i explained above, I'm thinking use a composite object as the ItemSimilarity which wraps a Mahout similarity implementation (e.g. TanimotoCoefficientSimilarity) then add the similarity calculation result with tag and reaction similarity results.
Model :
user_id viewed_content_id
--------- -----------------
1 102
1 1032
2 105
content_id reaction_id reaction_count
---------- ----------- -------------
102 5 10000
105 3 500
206 5 2000
content_id tag_id
---------- ------
1 3
1 4
1 3
2 3
2 1
3 3
3 3
(Any content will have about 5 reaction option and about 5 tag.)
Item similarity class :
public class ContentSimilarity implements ItemSimilarity {
private ItemSimilarity similarity;
private FastByIDMap<ContentItem> map = new FastByIDMap<>();
private ContentSimilarity() {
}
/**
*
* @param dataModel
* @param similarity
* ---> let assume that TanimotoCoefficientSimilarity
* @return
* @throws TasteException
*/
public static ContentSimilarity createWith(DataModel dataModel, ItemSimilarity similarity) throws TasteException {
ContentSimilarity customSimilarity = new ContentSimilarity();
customSimilarity.setSimilarity(similarity);
return customSimilarity;
}
@Override
public void refresh(Collection<Refreshable> alreadyRefreshed) {
similarity.refresh(alreadyRefreshed);
}
@Override
public double itemSimilarity(long itemID1, long itemID2) throws TasteException {
double similarityResult = similarity.itemSimilarity(itemID1, itemID2) + customSimilarity(itemID1, itemID2);
return similarityResult;
}
int threshold = 10;
public double customSimilarity(long itemID1, long itemID2) throws TasteException {
ContentItem item1 = map.get(itemID1);
ContentItem item2 = map.get(itemID2);
double score = 0.0;
try {
// tag similarity
int tagIntersection = item1.getTagsAsFastIDSet().intersectionSize(item2.getTagsAsFastIDSet());
// reactionSimilarity
FastByIDMap<Long> item1Map = item1.getReactionsAsFastByIDMap();
FastByIDMap<Long> item2Map = item2.getReactionsAsFastByIDMap();
LongPrimitiveIterator item1Itr = item1Map.keySetIterator();
int reactionScore = 0;
while (item1Itr.hasNext()) {
long item1Key = item1Itr.next();
long item1Val = item1Map.get(item1Key);
Long item2ValO = item2Map.get(item1Key);
if (item1Val > threshold && item2ValO > threshold) {
reactionScore += item2ValO == null ? 0 : 1;
}
}
score = tagIntersection + reactionScore; // max score is 10
} catch (IOException e) {
e.printStackTrace();
}
return score / 10;
}
@Override
public double[] itemSimilarities(long itemID1, long[] itemID2s) throws TasteException {
return similarity.itemSimilarities(itemID1, itemID2s);
}
@Override
public long[] allSimilarItemIDs(long itemID) throws TasteException {
return similarity.allSimilarItemIDs(itemID);
}
public ItemSimilarity getSimilarity() {
return similarity;
}
public void setSimilarity(ItemSimilarity similarity) {
this.similarity = similarity;
}
}