<span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @date 2016/2/18 * @info a gender-based item similarity metric * For example: two males or two female profiles are very similar and set their similarity to 1.0. * The similarity between the male and female profiles is assumed to be-1.0. One or two unknown gender in the last pair of files is set to 0.0. */package Unitfive;import Java.util.collection;import Org.apache.mahout.cf.taste.common.refreshable;import Org.apache.mahout.cf.taste.common.tasteexception;import Org.apache.mahout.cf.taste.impl.common.fastidset;import Org.apache.mahout.cf.taste.similarity.itemsimilarity;public class Genderitemsimilarity implements ItemSimilarity { Private final Fastidset men;private final fastidset women;public genderitemsimilarity (fastidset men, Fastidset Women) {// TODO auto-generated Constructor stubthis.men = Men;this.women = women;} @Overridepublic Double itemsimilarity (long profileID1, long profileID2) throws Tasteexception {//TODO auto-generated met Hod Stubboolean Profile1isman = Isman (profileID1); if (Profile1isman = = null) return 0.0; Boolean Profile2isman = Isman (profileID2), if (Profile2isman = = null) retuRN 0.0;return Profile1isman = = Profile2isman? 1:1;} @Overridepublic double[] itemsimilarities (Long itemID1, long[] itemid2s) throws Tasteexception {//TODO auto-generated Method stubdouble[] result = new Double[itemid2s.length];for (int i = 0; i < itemid2s.length; i++) {Result[i] = Itemsimil Arity (ItemID1, itemid2s[i]);} return result;} /** * Do nothing inside this method */@Overridepublic void Refresh (collection<refreshable> arg0) {//TODO auto-generated method Stub}@o Verridepublic long[] Allsimilaritemids (Long arg0) throws Tasteexception {//TODO auto-generated method Stubreturn null;} Private Boolean Isman (long profileID) {if (Men.contains (profileID)) {return boolean.true;} if (Women.contains (profileID)) {return boolean.false;} return null;}} </span>
Mahout implementation of a gender-based item similarity measurement method genderitemsimilarity