推荐系统

时间:2014-04-24 18:33:51

标签: jsf mahout

我正在尝试实施基于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>

1 个答案:

答案 0 :(得分:0)

做以下事情:

  • 在托管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>