//first we check how many recommendation items each book has
int n = foundBooksItems.get(aBook).size();
//then we simple count the inversion of n
float accuracy = (float)1/(float)n;
resultBooks.add(new Recommendation(aBook, accuracy));
}
}
//more difficult situation...
else {
int s = asvalue.length;
//we get the two dimensional table of results - in each row there are two columns: one with resource URI, second with recommendation item
Node[][] items = this.query.listRankedItems(allResults.toArray(new Node[0]));
Map<Node, HashSet<String>> foundBooksItems = new HashMap<Node, HashSet<String>>();
for(int i=0; i<items.length; i++) {
HashSet<String> tmp;
if(foundBooksItems.containsKey(items[i][0])) {
tmp = foundBooksItems.remove(items[i][0]);
} else {
tmp = new HashSet<String>();
}
if(items[i][1] instanceof Literal)
tmp.add(((Literal)items[i][1]).getValue());
else
tmp.add(items[i][1].toString());
foundBooksItems.put(items[i][0], tmp);
}
for(Node aBook : foundBooksItems.keySet()) {
HashSet<String> itemsSet = foundBooksItems.get(aBook);
//first we check how many recommendation items each book has
int n = itemsSet.size();
//then we compare both recommendation items sets and find the common part
int m = RecommendationUtils.commonPart(asvalue, this.postprocess(itemsSet.toArray(new String[0])));
//now we calculate the accuracy measure according to accuracy function (see documentation)
float accuracy = 1 - (1 - (float)m/(float)s) - ((float)m/(float)s * (float)Math.pow(((float)(n-m)/(float)n), 2.0));
resultBooks.add(new Recommendation(aBook, accuracy));
}
}
this.setResultsArray(resultBooks);