建立java工程,导入需要的jar包
准备好测试的数据:D:\text.txt
1,101,5
1,102,3
1,103,2.5
2,101,2
2,102,2.5
2,103,5
2,104,2
3,101,2.5
3,104,4
3,105,4.5
3,107,5
4,101,5
4,103,3
4,104,4.5
4,106,4
5,101,4
5,102,3
5,103,2
5,104,4
5,105,3.5
5,106,4
java代码
import java.io.*;
import java.util.*;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.neighborhood.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;
public class TestMahout {
// private TestMahout(){};
public static void main(String args[]) throws Exception {
TestMahout testMahout = new TestMahout();
System.out.println("The baseUserCF Result:");
testMahout.baseUserCF();
System.out.println("The baseItemCF Result:");
testMahout.baseItemCF();
System.out.println("The baseSlopOne Result:");
testMahout.baseSlopOne();
}
//基于用户相似度的协同过滤推荐实现
public void baseUserCF(){
// 1,构建模型
DataModel dataModel;
try {
dataModel = new FileDataModel(new File("d://test.txt"));
// 2,计算相似度
UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(dataModel);
// 3,查找k紧邻
UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(2,
userSimilarity, dataModel);
// 4,构造推荐引擎
Recommender recommender = new GenericUserBasedRecommender(dataModel,
userNeighborhood, userSimilarity);
// 为用户i推荐两个Item
for (int i = 1; i < 6; i++) {
System.out.println("recommand for user:" + i);
List
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TasteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//基于内容相似度的协同过滤推荐实现
public void baseItemCF(){
DataModel model;
try {
model = new FileDataModel(new File("d://test.txt"));
ItemSimilarity itemsimilarity =new PearsonCorrelationSimilarity(model);
Recommender recommender= new GenericItemBasedRecommender(model,itemsimilarity);
List
for(RecommendedItem recommendation :recommendations){
System.out.println(recommendation);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TasteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//基于SlopOne的推荐实现
public void baseSlopOne(){
DataModel model;
try {
model = new FileDataModel(new File("d://test.txt"));
Recommender recommender= new SlopeOneRecommender(model);
List
for(RecommendedItem recommendation :recommendations){
System.out.println(recommendation);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Io Error");
e.printStackTrace();
} catch (TasteException e) {
// TODO Auto-generated catch block
System.out.println("Taste Error");
e.printStackTrace();
}
}
}
运行结果:
The baseUserCF Result:----------------------------------
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Creating FileDataModel for file d:\test.txt
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:09 org.slf4j.impl.JCLLoggerAdapter info
信息: Processed 5 users
recommand for user:1
RecommendedItem[item:104, value:4.257081]
RecommendedItem[item:106, value:4.0]
recommand for user:2
recommand for user:3
RecommendedItem[item:106, value:4.0]
RecommendedItem[item:103, value:2.5905366]
recommand for user:4
RecommendedItem[item:102, value:3.0]
recommand for user:5
The baseItemCF Result:----------------------------------
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Creating FileDataModel for file d:\test.txt
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Processed 5 users
RecommendedItem[item:104, value:5.0]
The baseSlopOne Result:---------------------------------
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Creating FileDataModel for file d:\test.txt
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Reading file info...
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Read lines: 21
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Processed 5 users
2013-2-26 19:18:10 org.slf4j.impl.JCLLoggerAdapter info
信息: Building average diffs...
RecommendedItem[item:105, value:5.75]