0
votes

I am trying to implement a mahout based recommender system. I am not able to display the results on a jsf page.

@ManagedBean(name="similarvaluerecommender") @ViewScoped

public class SimilarValueRecommender implements 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;

 }

}

I would like to display the results of this page on a jsf page in the form of a table. This is the view

<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

1 Answers

0
votes

Do the following things:

  • Declare a List<RecommendedItem> recommendations attribute in your managed bean.
  • Define getter and setter methods for this attribute. Do not include any business logic in any of these methods.
  • Load the data in a @PostConstruct method and store it into recommendations attribute.
  • Display the data from recommendations attribute in the view.

In code:

@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...
}

In your view:

<h:dataTable value="#{similarValueRecommender.recommendations}" var="recommendation">
    <h:column>
        #{recommendation.text}
    </h:column>
</h:dataTable>