我正在尝试实现一个基于mahout的推荐系统。我无法在jsf页面上显示结果。
@ManagedBean(name="similarAvaluerecommender")@ViewScoped
公共类SimilarValueRecommender实现Serializable{私人列表推荐项目;
@PostConstruct
public void init() {
this.recommendedItems = new ArrayList<>();
DataModel dm;
try {
dm = new FileDataModel(new File("Dataset/userdata.csv"));
//ItemSimilarity sim = new LogLikelihoodSimilarity(dm);
TanimotoCoefficientSimilarity sim = new TanimotoCoefficientSimilarity(dm);
GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dm, sim);
int x=1;
for(LongPrimitiveIterator items = dm.getItemIDs(); items.hasNext();) {
long itemId = items.nextLong();
List<RecommendedItem>recommendedItems1 = recommender.mostSimilarItems(itemId, 10);
this.recommendedItems.addAll(recommendedItems1);
x++;
}
} catch (TasteException ex) {
Logger.getLogger(SimilarValueRecommender.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(SimilarValueRecommender.class.getName()).log(Level.SEVERE, null, ex);
}
}
//getter and setter...
public List<RecommendedItem> getRecommendedItems(){
return recommendedItems;
}
public void setList(List<RecommendedItem> recommendedItems) {
this.recommendedItems=recommendedItems;
}
}
我想以表格的形式在jsf页面上显示此页面的结果。这是视图
<ui:composition template="WEB-INF/commonlayout.xhtml">
<ui:define name="content">
<h:form>
hi
<h:dataTable id="similarvaluestable" value="#{similarvaluerecommender.recommendedItems}" var="recommendedItem">
<h:column>
#{recommendedItem.itemID}
</h:column>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
执行以下操作:
- 在托管bean中声明一个
List<RecommendedItem> recommendations
属性 - 为此属性定义getter和setter方法。不要在这些方法中包含任何业务逻辑
- 在
@PostConstruct
方法中加载数据,并将其存储到recommendations
属性中 - 在视图中显示来自
recommendations
属性的数据
代码中:
@ManagedBean("similarValueRecommender")
@ViewScoped
public class SimilarValueRecommender implements Serializable {
List<RecommendedItem> recommendations;
@PostConstruct
public void init() {
this.recommendations = new ArrayList<RecommendedItem>();
//taken from your current code
//seems like this is the code to load recommendations
DataModel dm = new AlphaItemFileDataModel(new File("Dataset/userdata.csv"));
//ItemSimilarity sim = new LogLikelihoodSimilarity(dm);
TanimotoCoefficientSimilarity sim = new TanimotoCoefficientSimilarity(dm);
GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(dm, sim);
int x=1;
for(LongPrimitiveIterator items = dm.getItemIDs(); items.hasNext();) {
long itemId = items.nextLong();
List<RecommendedItem>recommendations = recommender.mostSimilarItems(itemId, 10);
/*
for(RecommendedItem recommendation : recommendations) {
similaritemID=recommendation.getItemID();
System.out.println(itemId + "," + recommendation.getItemID() + "," + recommendation.getValue());
}
*/
this.recommendations.addAll(recommendations);
x++;
//if(x>10) System.exit(1);
}
}
//getter and setter...
}
在您看来:
<h:dataTable value="#{similarValueRecommender.recommendations}" var="recommendation">
<h:column>
#{recommendation.text}
</h:column>
</h:dataTable>