我正在尝试实施基于mahout的推荐系统。我无法在jsf页面上显示结果。
@ManagedBean(名称= “similarvaluerecommender”) @ViewScoped
公共类SimilarValueRecommender实现Serializable { private list recommendedItems;
@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>
答案 0 :(得分:0)
做以下事情:
List<RecommendedItem> recommendations
属性。@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>