<span style="font-size:18px;">/*** * @author YangXin * @date 2016/2/18 * @info 一个基于性别的物品相似性度量 * 例如:对于两个男性或者两个女性档案非常相似,并设置它们的相似度为1.0。 * 假定男性和女性的档案之间的相似度为 -1.0.最后一对档案中的一个或两个未知性别,则设为 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 stub this.men = men; this.women = women; } @Override public double itemSimilarity(long profileID1, long profileID2) throws TasteException { // TODO Auto-generated method stub Boolean profile1IsMan = isMan(profileID1); if(profile1IsMan == null) return 0.0; Boolean profile2IsMan = isMan(profileID2); if(profile2IsMan == null) return 0.0; return profile1IsMan == profile2IsMan ? 1.0 : 1.0; } @Override public double[] itemSimilarities(long itemID1, long[] itemID2s) throws TasteException { // TODO Auto-generated method stub double[] result = new double[itemID2s.length]; for(int i = 0; i < itemID2s.length; i++){ result[i] = itemSimilarity(itemID1, itemID2s[i]); } return result; } /** * 此方法内什么都不做 */ @Override public void refresh(Collection<Refreshable> arg0) { // TODO Auto-generated method stub } @Override public long[] allSimilarItemIDs(long arg0) throws TasteException { // TODO Auto-generated method stub return null; } private Boolean isMan(long profileID){ if(men.contains(profileID)){ return Boolean.TRUE; } if(women.contains(profileID)){ return Boolean.FALSE; } return null; } } </span>