//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 = recItemsTable.length;
//we get the two dimensional table of results - in each row there are two columns: one with resource URI, second with recommendation item
Value[][] items = RecommendationUtils.performTwodimensionalTableQuery(Repository.JEROMEDL_REPOSITORY.getLocalRepository(),
QueryLanguage.SERQL, RecommendationQueries.GENERAL_BOOK_ITEM_TABLE_QUERY.bookItemTableQuery(this.name.getUri(), allBooks.toArray()));
Map<URI, HashSet<Value>> foundBooksItems = new HashMap<URI, HashSet<Value>>();
for(int i=0; i<items.length; i++) {
if(foundBooksItems.containsKey(items[i][0])) {
HashSet<Value> tmp = foundBooksItems.remove(items[i][0]);
tmp.add(items[i][1]);
foundBooksItems.put((URI)items[i][0], tmp);
} else {
HashSet<Value> tmp = new HashSet<Value>();
tmp.add(items[i][1]);
foundBooksItems.put((URI)items[i][0], tmp);
}
}
for(URI aBook : foundBooksItems.keySet()) {
HashSet<Value> 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(recItemsTable, itemsSet.toArray());
//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));
}
}
//-----TEST------