<commit_before>mol.modules.map.search = function(mol) {

    mol.map.search = {};

    mol.map.search.SearchEngine = mol.mvp.Engine.extend(
        {
            /**
             * @param bus mol.bus.Bus
             */
            init: function(proxy, bus) {
                this.proxy = proxy;
                this.bus = bus;
                this.sql = '' +
                    'SELECT ' +
                    'provider as source, scientificname as name, type as type ' +
                    'FROM scientificnames WHERE scientificname = \'{0}\'';
            },

            /**
             * Starts the SearchEngine. Note that the container parameter is
             * ignored.
             */
            start: function() {
                this.display = new mol.map.search.SearchDisplay();
                this.display.toggle(true);
                this.initAutocomplete();
                this.addEventHandlers();
                this.fireEvents();
            },
            /*
             * Initialize autocomplate functionality
             */
            initAutocomplete: function() {
                this.populateAutocomplete(null, null);

                // http://stackoverflow.com/questions/2435964/jqueryui-how-can-i-custom-format-the-autocomplete-plug-in-results
                $.ui.autocomplete.prototype._renderItem = function (ul, item) {
                    var val = item.label.split(':'),
                        name = val[0],
                        kind = val[1],
                        eng = '<a>{0}</a>'.format(name),
                        sci = '<a><i>{0}</i></a>'.format(name);

                    item.label = kind === 'sci' ? sci : eng;
                    item.value = name;

                    item.label = item.label.replace(
                        new RegExp("(?![^&;]+;)(?!<[^<>]*)(" +
                                   $.ui.autocomplete.escapeRegex(this.term) +
                                   ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
                    return $("<li></li>")
                        .data("item.autocomplete", item)
                        .append("<a>" + item.label + "</a>")
                        .appendTo(ul);
                };
            },

            /*
             * Populate autocomplete results list
             */
            populateAutocomplete : function(action, response) {
                $(this.display.searchBox).autocomplete(
                    {
                        minLength: 3,
                        delay: 0,
                        source: function(request, response) {
                            $.getJSON(
                                'api/autocomplete',
                                {
                                    key: 'acn-{0}'.format(request.term)
                                },
                                function(names) {
                                    response(names);
                                }
                            );
                        },
                        select: function(event, ui) {
                            $(this).autocomplete("close");
                        }<commit_msg>SQL base autocomplete, because I am impatient and it works.<commit_after>mol.modules.map.search = function(mol) {

    mol.map.search = {};

    mol.map.search.SearchEngine = mol.mvp.Engine.extend(
        {
            /**
             * @param bus mol.bus.Bus
             */
            init: function(proxy, bus) {
                this.proxy = proxy;
                this.bus = bus;
                this.sql = '' +
                    'SELECT ' +
                    'provider as source, scientificname as name, type as type ' +
                    'FROM scientificnames WHERE scientificname = \'{0}\'';
            },

            /**
             * Starts the SearchEngine. Note that the container parameter is
             * ignored.
             */
            start: function() {
                this.display = new mol.map.search.SearchDisplay();
                this.display.toggle(true);
                this.initAutocomplete();
                this.addEventHandlers();
                this.fireEvents();
            },
            /*
             * Initialize autocomplate functionality
             */
            initAutocomplete: function() {
                this.populateAutocomplete(null, null);

                // http://stackoverflow.com/questions/2435964/jqueryui-how-can-i-custom-format-the-autocomplete-plug-in-results
                $.ui.autocomplete.prototype._renderItem = function (ul, item) {
                    var val = item.label.split(':'),
                        name = val[0],
                        kind = val[1],
                        eng = '<a>{0}</a>'.format(name),
                        sci = '<a><i>{0}</i></a>'.format(name);

                    item.label = kind === 'sci' ? sci : eng;
                    item.value = name;

                    item.label = item.label.replace(
                        new RegExp("(?![^&;]+;)(?!<[^<>]*)(" +
                                   $.ui.autocomplete.escapeRegex(this.term) +
                                   ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
                    return $("<li></li>")
                        .data("item.autocomplete", item)
                        .append("<a>" + item.label + "</a>")
                        .appendTo(ul);
                };
            },

            /*
             * Populate autocomplete results list
             */
            populateAutocomplete : function(action, response) {
                $(this.display.searchBox).autocomplete(
                    {
                        minLength: 3,
                        delay: 0,
                        source: function(request, response) {
                            $.getJSON(
                                //'api/autocomplete',
                                "http://mol.cartodb.com/api/v2/sql?q=SELECT scientificname, vernacularName from scientificnames where " +
                                "scientificname ~* '\\m" + request.term + "' OR vernacularName ~* '\\m" + request.term + "' LIMIT 100",
                                {
                                    key: 'acn-{0}'.format(request.term)
                                },
                                function(json) {
                                    var names = [];
                                    _.each (
                                        json.rows,
                                        function(row) {
                                            if(row.scientificname != '') {
                                                names.push(row.scientificname+':scientific');
                                            }
                                            if(row.vernacularname != '') {
                                                names.push(row.vernacularname+':english');
                                            }
                                        }
                                    );
                                    response(names);
                                }
                            );
                        },
                        select: function(event, ui) {
                            $(this).autocomplete("close");
                        }
{'fraction_non_alphanumeric': 0.12345846320639653, 'fraction_numerical': 0.004878709852283507, 'mean_word_length': 0.8589420654911839, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3066145', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1593, 'n_words': 273}