<commit_before>define(['text!./analyzer.html', 'app', 'lodash', 'require', 'jquery', './analyzer-section', 'filters/lstring'],
        function(templateHtml, app, _, require, $) { 'use strict';

    var baseUrl = require.toUrl('');

    //====================================
    //
    //
    //====================================
    function mapReduce(key) {

        return function(map, v) {

            if(key) map[v[key]] = v;
            else    map[v]      = v;

            return  map;

        };
    }

    //==============================================
    //
    //
    //==============================================
    app.directive('nationalReportAnalyzer', ['$http', '$q', 'locale', '$filter', function($http, $q, locale, $filter) {
        return {
            restrict : 'E',
            replace : true,
            template : templateHtml,
            require : 'nationalReportAnalyzer',
            scope :  {
                selectedRegions: '=regions',
                selectedQuestions: '=questions',
                selectedReportType: '=reportType'
            },
            link: function ($scope, $element, attr, nrAnalyzer) {

                $scope.limit = 0;
                $scope.sumType = 'sum';
                $scope.sumTypes = [
                    { code: 'sum',           title: 'Count of responses'   },
                    { code: 'percentRow',    title: 'Percentage by row'    },
                    { code: 'percentColumn', title: 'Percentage by column' },
                    { code: 'percentGlobal', title: 'Percentage overall'   },
                ];

                //====================================
                //
                //
                //====================================
                $scope.$watch('selectedRegions',    load);
                $scope.$watch('selectedQuestions',  load);
                $scope.$watch('selectedReportType', load);

                //====================================
                //
                //
                //====================================
                function load() {

                    if(!$scope.selectedRegions)    return;
                    if(!$scope.selectedQuestions)  return;
                    if(!$scope.selectedReportType) return;

                    $scope.limit = 0;
                    $scope.infinitScrollVisible = true;

                    $q.all([loadRegions(), loadSections(), nrAnalyzer.loadReports()]).then(function(results) {

                        var regions  = results[0];
                        var sections = results[1];
                        var reports  = results[2];

                        var reportsCountriesMap = _(reports).pluck('government').sortBy().reduce(mapReduce(), {});

                        regions = _.map(regions, function(term){

                            return {
                                identifier: term.identifier,
                                title: term.title,
                                shortTitle: term.shortTitle,
                                htmlTitle : htmlTitle(term.shortTitle, term.title),
                                countriesMap: _.pick(reportsCountriesMap, [term.identifier].concat(term.expandedNarrowerTerms||term.narrowerTerms||[]))
                            };
                        });

                        $scope.regions = regions;
                        $scope.sections = sections;

                    }).then(function(){

                        nrAnalyzer.toggleSection($scope.sections[0].key, true);

                    });
                }

                //====================================
                //
                //
                //====================================
                function loadRegions() {

                    var lstring = $filter('lstring');

                    var countries = $http.get('/api/v2013/thesaurus/domains/countries/terms', { cache : true }).then(function(res) {
                        return res.data;
                    });
<commit_msg>Fix empty array error<commit_after>define(['text!./analyzer.html', 'app', 'lodash', 'require', 'jquery', './analyzer-section', 'filters/lstring'],
        function(templateHtml, app, _, require, $) { 'use strict';

    var baseUrl = require.toUrl('');

    //====================================
    //
    //
    //====================================
    function mapReduce(key) {

        return function(map, v) {

            if(key) map[v[key]] = v;
            else    map[v]      = v;

            return  map;

        };
    }

    //==============================================
    //
    //
    //==============================================
    app.directive('nationalReportAnalyzer', ['$http', '$q', 'locale', '$filter', function($http, $q, locale, $filter) {
        return {
            restrict : 'E',
            replace : true,
            template : templateHtml,
            require : 'nationalReportAnalyzer',
            scope :  {
                selectedRegions: '=regions',
                selectedQuestions: '=questions',
                selectedReportType: '=reportType'
            },
            link: function ($scope, $element, attr, nrAnalyzer) {

                $scope.limit = 0;
                $scope.sumType = 'sum';
                $scope.sumTypes = [
                    { code: 'sum',           title: 'Count of responses'   },
                    { code: 'percentRow',    title: 'Percentage by row'    },
                    { code: 'percentColumn', title: 'Percentage by column' },
                    { code: 'percentGlobal', title: 'Percentage overall'   },
                ];

                //====================================
                //
                //
                //====================================
                $scope.$watch('selectedRegions',    load);
                $scope.$watch('selectedQuestions',  load);
                $scope.$watch('selectedReportType', load);

                //====================================
                //
                //
                //====================================
                function load() {

                    if(!$scope.selectedRegions)    return;
                    if(!$scope.selectedQuestions)  return;
                    if(!$scope.selectedReportType) return;

                    $scope.limit = 0;
                    $scope.infinitScrollVisible = true;

                    $q.all([loadRegions(), loadSections(), nrAnalyzer.loadReports()]).then(function(results) {

                        var regions  = results[0];
                        var sections = results[1];
                        var reports  = results[2];

                        var reportsCountriesMap = _(reports).pluck('government').sortBy().reduce(mapReduce(), {});

                        regions = _.map(regions, function(term){

                            return {
                                identifier: term.identifier,
                                title: term.title,
                                shortTitle: term.shortTitle,
                                htmlTitle : htmlTitle(term.shortTitle, term.title),
                                countriesMap: _.pick(reportsCountriesMap, [term.identifier].concat(term.expandedNarrowerTerms||term.narrowerTerms||[]))
                            };
                        });

                        $scope.regions = regions;
                        $scope.sections = sections;

                    }).then(function(){

                        if($scope.sections && $scope.sections.length) {
                            nrAnalyzer.toggleSection($scope.sections[0].key, true);
                        }
                    });
                }

                //====================================
                //
                //
                //====================================
                function loadRegions() {

                    var lstring = $filter('lstring');

                    var countries = $http.get('/api/v2013/thesaurus/domains/countries/terms', { cache : true }).then(function(res) {
                        return res.data;
                    });

{'fraction_non_alphanumeric': 0.20616430044897463, 'fraction_numerical': 0.0024268899405411964, 'mean_word_length': 1.3184247538677918, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990265', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1775, 'n_words': 317}