Menu

Collaborative Filtering

Victor Anthony Arrascue Ayala

This pages describes how to create and evaluate collaborative filtering queries. For more information on how to create a recommender repository see [Repository].

import org.openrdf.repository.Repository;
import org.openrdf.repository.sail.SailRecommenderRepository;
import org.openrdf.sail.memory.RecommenderMemoryStore;
...

try {
    //CREATION OF RECOMMENDER REPOSITORY myRepository
    [...]
    myRepository.initialize();

    //ADDING TRIPLES TO YOUR SYSTEM
    //Use file "AliceBobMoviesExample.ttl" provided in the download section.
    [...]

    //A writer serializes the results obtained from the evaluation
    FileOutputStream recResults= new FileOutputStream([A VALID PATH]);

    SPARQLResultsTSVWriter sparqlWriter = 
        new SPARQLResultsTSVWriter(recResults);

    String queryString =
        "PREFIX resparql: <http://example.org/resparql#>\n"

            + "PREFIX movies: <http://example.org/movies#>\n"
            + "RECOMMEND ?user ?movie ?user.REC ?movie.REC ?SIMscore ?RATING USING CF\n" 
            + "WHERE { \n"               
                + "?user movies:hasRated ?personalRating .\n"
                + "?personalRating movies:ratedMovie ?movie .\n"
                + "?personalRating movies:hasRating ?uRating \n"
            + "}\n"
            + "BASED ON {\n"
                + "?user rdf:type resparql:User. \n"
                + "?movie rdf:type resparql:Item.  \n"
                + "?uRating rdf:type resparql:UserRating. \n"
                + "?user movies:hasRated ?personalRating .\n"
                + "?personalRating movies:ratedMovie ?movie .\n"
                + "?user movies:hasRated ?personalRating .\n"
                + "?personalRating movies:hasRating ?uRating .\n"
            + "}"
            + "ORDER BY ASC(?user) DESC(?RATING)\n";


    TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.RESPARQL,
            queryString);            
    tupleQuery.evaluate(sparqlWriter);

    [...]

} catch (RepositoryException e) {
    e.printStackTrace();
} catch (OpenRDFException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
    con.close();
}

Related

Wiki: Repository

MongoDB Logo MongoDB