<commit_before>'use strict';

angular.module('cfsn.data', [])

    .factory('dataService', ['$http',
        function($http) {

            var rdfType = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>";
            var snClass = '<' + cfsnConfig.snClass + '>';
            var definition = '<' + cfsnConfig.predicates.definition + '>';
            var canonicalUnits = '<' + cfsnConfig.predicates.canonicalUnits + '>';

            function logQuery(query) {
                console.log("making query: " + query);
            }

            /*
             * cache.termDict: populated individually if first requests are individual terms,
             * and all at once for the full list request.
             * cache.termList: version for the grid widget.
             */
            var cache = {termDict: {}, termList: undefined};

            /**
             * Customized error handler for an http request.
             * @param cb         callback to report error.
             * @returns {Function}  handler
             */
            var httpErrorHandler = function(cb) {
                return function(data, status, headers, config) {
                    var reqMsg = config.method + " '" + config.url + "'";
                    var error = "An error occured with HTTP request: " +reqMsg;
                    //error += "<br/> query: " + _.escape(config.params.query).replace(/\n/, '<br/>');
                    error += "<br/>Status: " + status;
                    cb(error);
                };
            };

            function getTermList(fns) {

                if (cache.termList) {
                    //console.log("termList in cache");
                    fns.gotTermList(undefined, cache.termList);
                    return;
                }

                var query = 'select distinct ?name ?definition ?canonicalUnits where {\n' +
                    '  ?name ' +rdfType+ ' ' + snClass + '.\n' +
                    '  ?name ' +definition+ ' ?definition.\n' +
                    '  ?name ' +canonicalUnits+ ' ?canonicalUnits.\n' +
                    '}\n' +
                    'order by ?name';

                logQuery(query);

                $http.get(cfsnConfig.sparqlEndpoint, {params: {query: query}})
                    .success(function (data, status, headers, config) {
                        //console.log("getTermList: data= ", data);

                        //var names = data.names;
                        var rows = data.values;

                        cache.termDict = {};

                        cache.termList = _.map(rows, function (e) {
                            var name           = e[0];
                            var definition     = e[1];
                            var canonicalUnits = e[2];

                            var termName = vutil.getTermName(name);

                            cache.termDict[termName] = {
                                definition:     definition,
                                canonicalUnits: canonicalUnits
                            };

                            return {
                                name:           name,
                                definition:     definition,
                                canonicalUnits: canonicalUnits
                            };
                        });

                        fns.gotTermList(undefined, cache.termList);
                    })
                    .error(httpErrorHandler(fns.gotTermList));
            }

            function getTermDetails(termName, fns) {

                if (termName in cache.termDict) {
                    //console.log("term", termName, "in cache");
                    var termDetails = cache.termDict[termName];
                    fns.gotTermDetails(undefined, termDetails);
                    return;
                }

                var termUri = '<' + cfsnConfig.snPrefix + termName + '>';
                var query = 'select distinct ?definition ?canonicalUnits where {\n' +
                    '  ' +termUri+ ' ' +definition+ ' ?definition.\n' +
                    '  ' +termUri+ ' ' +canonicalUnits+ ' ?canonicalUnits.\n' +
                    '}';

                logQuery(query);

                $http.get(cfsnConfig.sparqlEndpoint, {params: {query: query}})
                    .success(function (data, status, headers, config) {
                        //console.log("getTermDetails: data= ", data);
                        //var names = data.names;
                        var rows = data.values;

                        if (rows.length == 1) {
                            var termDetails = {
                                definition:     rows[0][0],
                                canonicalUnits: rows[0][1]
                            };
                            cache.termDict[termName] = termDetails;
                            fns.gotTermDetails(undefined, termDetails);
                            return;
                        }
                        if (rows.length > 1) {
                            console.log("WARN: unexpected number of results: ", rows.length);
                        }
                        fns.gotTermDetails("WARN: unexpected number of results: ", rows.length);
                    })
                    .error(httpErrorHandler(fns.gotTermDetails));
            }

            function getNercTermUri(termName, fns) {

                var query = cfsnConfig.nerc.uriQueryTemplate.replace('{{stdname}}', termName);
                console.log("making query: " + query + "\nagainst: " +cfsnConfig.nerc.sparqlEndpoint);

                $http.get(cfsnConfig.nerc.sparqlEndpoint, {params: {query: query, output: 'json'}})
                    .success(function (data, status, headers, config) {
                        //console.log("getNercTermUri: data= ", data);
                        // TODO more appropriate check of the response
                        var uri = data.results.bindings[0].uri.value;
                        //console.log("getNercTermUri: uri= ", uri);
                        fns.gotNercTermUri(undefined, uri);
                    })
                    .error(httpErrorHandler(fns.gotNercTermUri));
            }

            return {
                cachedTermDict:   function() { return cache.termDict; },
                getTermList:      getTermList,
                getTermDetails:   getTermDetails,

                getNercTermUri:   getNercTermUri
            };
        }]);<commit_msg>cache nvs term<commit_after>'use strict';

angular.module('cfsn.data', [])

    .factory('dataService', ['$http',
        function($http) {

            var rdfType = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>";
            var snClass = '<' + cfsnConfig.snClass + '>';
            var definition = '<' + cfsnConfig.predicates.definition + '>';
            var canonicalUnits = '<' + cfsnConfig.predicates.canonicalUnits + '>';

            function logQuery(query) {
                console.log("making query: " + query);
            }

            /*
             * cache.termDict: populated individually if first requests are individual terms,
             * and all at once for the full list request.
             * cache.termList: version for the grid widget.
             */
            var cache = {
                termDict: {},
                termList: undefined,
                nercDict: {}
            };

            /**
             * Customized error handler for an http request.
             * @param cb         callback to report error.
             * @returns {Function}  handler
             */
            var httpErrorHandler = function(cb) {
                return function(data, status, headers, config) {
                    var reqMsg = config.method + " '" + config.url + "'";
                    var error = "An error occured with HTTP request: " +reqMsg;
                    //error += "<br/> query: " + _.escape(config.params.query).replace(/\n/, '<br/>');
                    error += "<br/>Status: " + status;
                    cb(error);
                };
            };

            function getTermList(fns) {

                if (cache.termList) {
                    //console.log("termList in cache");
                    fns.gotTermList(undefined, cache.termList);
                    return;
                }

                var query = 'select distinct ?name ?definition ?canonicalUnits where {\n' +
                    '  ?name ' +rdfType+ ' ' + snClass + '.\n' +
                    '  ?name ' +definition+ ' ?definition.\n' +
                    '  ?name ' +canonicalUnits+ ' ?canonicalUnits.\n' +
                    '}\n' +
                    'order by ?name';

                logQuery(query);

                $http.get(cfsnConfig.sparqlEndpoint, {params: {query: query}})
                    .success(function (data, status, headers, config) {
                        //console.log("getTermList: data= ", data);

                        //var names = data.names;
                        var rows = data.values;

                        cache.termDict = {};

                        cache.termList = _.map(rows, function (e) {
                            var name           = e[0];
                            var definition     = e[1];
                            var canonicalUnits = e[2];

                            var termName = vutil.getTermName(name);

                            cache.termDict[termName] = {
                                definition:     definition,
                                canonicalUnits: canonicalUnits
                            };

                            return {
                                name:           name,
                                definition:     definition,
                                canonicalUnits: canonicalUnits
                            };
                        });

                        fns.gotTermList(undefined, cache.termList);
                    })
                    .error(httpErrorHandler(fns.gotTermList));
            }

            function getTermDetails(termName, fns) {

                if (termName in cache.termDict) {
                    //console.log("term", termName, "in cache");
                    var termDetails = cache.termDict[termName];
                    fns.gotTermDetails(undefined, termDetails);
                    return;
                }

                var termUri = '<' + cfsnConfig.snPrefix + termName + '>';
                var query = 'select distinct ?definition ?canonicalUnits where {\n' +
                    '  ' +termUri+ ' ' +definition+ ' ?definition.\n' +
                    '  ' +termUri+ ' ' +canonicalUnits+ ' ?canonicalUnits.\n' +
                    '}';

                logQuery(query);

                $http.get(cfsnConfig.sparqlEndpoint, {params: {query: query}})
                    .success(function (data, status, headers, config) {
                        //console.log("getTermDetails: data= ", data);
                        //var names = data.names;
                        var rows = data.values;

                        if (rows.length == 1) {
                            var termDetails = {
                                definition:     rows[0][0],
                                canonicalUnits: rows[0][1]
                            };
                            cache.termDict[termName] = termDetails;
                            fns.gotTermDetails(undefined, termDetails);
                            return;
                        }
                        if (rows.length > 1) {
                            console.log("WARN: unexpected number of results: ", rows.length);
                        }
                        fns.gotTermDetails("WARN: unexpected number of results: ", rows.length);
                    })
                    .error(httpErrorHandler(fns.gotTermDetails));
            }

            function getNercTermUri(termName, fns) {

                if (termName in cache.nercDict) {
                    //console.log("getNercTermUri", termName, "in cache");
                    var uri = cache.nercDict[termName].uri;
                    fns.gotNercTermUri(undefined, uri);
                    return;
                }

                var query = cfsnConfig.nerc.uriQueryTemplate.replace('{{stdname}}', termName);
                console.log("making query: " + query + "\nagainst: " +cfsnConfig.nerc.sparqlEndpoint);

                $http.get(cfsnConfig.nerc.sparqlEndpoint, {params: {query: query, output: 'json'}})
                    .success(function (data, status, headers, config) {
                        //console.log("getNercTermUri: data= ", data);
                        // TODO more appropriate check of the response
                        var uri = data.results.bindings[0].uri.value;
                        //console.log("getNercTermUri: uri= ", uri);
                        cache.nercDict[termName] = {uri: uri};
                        fns.gotNercTermUri(undefined, uri);
                    })
                    .error(httpErrorHandler(fns.gotNercTermUri));
            }

            return {
                cachedTermDict:   function() { return cache.termDict; },
                getTermList:      getTermList,
                getTermDetails:   getTermDetails,

                getNercTermUri:   getNercTermUri
            };
        }]);
{'fraction_non_alphanumeric': 0.1093633517220814, 'fraction_numerical': 0.0028328611898017, 'mean_word_length': 1.1096084289982702, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4959690', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3136, 'n_words': 658}