<commit_before>
var WTF = (function() {

    'use strict';

    /*
      ------------------------------------------------------------

        Constants & variables

      ------------------------------------------------------------
    */

    var RE_QUOTE = /\"([^\"]+)\"/gi;
    var RE_JSON = /\.json$/i;
    var RE_KEY = /[a-z0-9_-]{32,}/i;
    var DOCS_PATH = 'https://docs.google.com/spreadsheet/pub?key={key}&output=csv';

    var templates;
    var responses;
    var headings;
    var corpus;
    var regex;
    var dom;
    var ref = ""; //the reference parameter for the character

    /*
      ------------------------------------------------------------

        Called once initialisation as finished

      ------------------------------------------------------------
    */

    function start() {
        
        // Copy out templates then remove from corpus

        templates = corpus.template;
        responses = corpus.response;
        headings = corpus.heading;

        delete corpus.template;
        delete corpus.response;
        delete corpus.heading;

        // Enable UI and generate first idea

        initUI();
        buildRexExp();
        generate();
        
        // A rough calculation of the number of possible results
        var sum = 0;
        $.each(corpus, function (index, value) {
            sum += value.length;
        }); 
        $("#count").text(sum);
    }

    /*
      ------------------------------------------------------------

        Converts CSV to a regular corpus object
        @see sample.json

      ------------------------------------------------------------
    */

    function parseCSV( csv ) {

        var corpus = {};

        var i, j, k, n, m, cols, keys = {}, data = {}, rows = csv.split( '\n' );

        for ( i = 0, n = rows.length; i < n; i++, j = i - 1 ) {

            cols = rows[ i ].replace( RE_QUOTE, escape ).split( ',' );

            for ( k = 0, m = cols.length; k < m; k++ ) {

                if ( i === 0 ) {

                    data[ keys[ k ] = cols[ k ].toLowerCase() ] = [];

                } else if ( cols[ k ] ) {

                    data[ keys[ k ] ][ j ] = unescape( cols[ k ] ).replace( /^\"|\"$/g, '' );
                }
            }
        }

        return data;
    }
    
     /*
      ------------------------------------------------------------

        Read URL parameter

      ------------------------------------------------------------
    */
    
    function getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return sParameterName[1];
            }
        }
        return "";
    } 

    /*
      ------------------------------------------------------------

        Binds event handlers to control the interface

      ------------------------------------------------------------
    */

    function initUI() {

        $( '.loading' ).remove();

        dom = {
            generate: $( '#generate' ),
            output: $( '#output' )
        };

        dom.generate.click( function() {
            generate();
            return false;
        });
    }

    /*
      ------------------------------------------------------------

        Builds a regular expression for all types in the corpus

      ------------------------------------------------------------
    */

    function buildRexExp() {

        var types = [];

        for ( var type in corpus )

            types.push( type );

        var content = '@(type)'.replace( 'type', types.join( '|' ) );

        regex = new RegExp( content, 'gi' );
    }

    /*
      ------------------------------------------------------------

        Generates ideas based on the corpus

      ------------------------------------------------------------
    */

    function generate() {
        
        var param = getUrlParameter('id');
        var temp;
        
        var type, text, part, iter = 0, // Safety mechanism
            idea = randomItem( templates, false, true ),
            item = regex.exec( idea ),
            copy = cloneCorpus();
        
        //if parameter was used, parse the idea template
        if(param.length > 0){
            //before parsing, set ref to param
            ref = param;
            //parse
            temp = parseInt(param.substring(0,2), 16);
            param = param.substring(2);
            idea = templates[temp];
        }
        
        while ( item && ++iter < 1000 ) {

            type = item[ 0 ];
            text = item[ 1 ];

            //console.log( text, copy, copy[ text ] );
            if(param.length > 0){
                temp = parseInt(param.substring(0,2), 16);
                param = param.substring(2);
                part = copy[ text ][temp];
            }else{<commit_msg>Reflink will be reset on new generated sentence<commit_after>
var WTF = (function() {

    'use strict';

    /*
      ------------------------------------------------------------

        Constants & variables

      ------------------------------------------------------------
    */

    var RE_QUOTE = /\"([^\"]+)\"/gi;
    var RE_JSON = /\.json$/i;
    var RE_KEY = /[a-z0-9_-]{32,}/i;
    var DOCS_PATH = 'https://docs.google.com/spreadsheet/pub?key={key}&output=csv';

    var templates;
    var responses;
    var headings;
    var corpus;
    var regex;
    var dom;
    var ref = ""; //the reference parameter for the character

    /*
      ------------------------------------------------------------

        Called once initialisation as finished

      ------------------------------------------------------------
    */

    function start() {
        
        // Copy out templates then remove from corpus

        templates = corpus.template;
        responses = corpus.response;
        headings = corpus.heading;

        delete corpus.template;
        delete corpus.response;
        delete corpus.heading;

        // Enable UI and generate first idea

        initUI();
        buildRexExp();
        generate();
        
        // A rough calculation of the number of possible results
        var sum = 0;
        $.each(corpus, function (index, value) {
            sum += value.length;
        }); 
        $("#count").text(sum);
    }

    /*
      ------------------------------------------------------------

        Converts CSV to a regular corpus object
        @see sample.json

      ------------------------------------------------------------
    */

    function parseCSV( csv ) {

        var corpus = {};

        var i, j, k, n, m, cols, keys = {}, data = {}, rows = csv.split( '\n' );

        for ( i = 0, n = rows.length; i < n; i++, j = i - 1 ) {

            cols = rows[ i ].replace( RE_QUOTE, escape ).split( ',' );

            for ( k = 0, m = cols.length; k < m; k++ ) {

                if ( i === 0 ) {

                    data[ keys[ k ] = cols[ k ].toLowerCase() ] = [];

                } else if ( cols[ k ] ) {

                    data[ keys[ k ] ][ j ] = unescape( cols[ k ] ).replace( /^\"|\"$/g, '' );
                }
            }
        }

        return data;
    }
    
     /*
      ------------------------------------------------------------

        Read URL parameter

      ------------------------------------------------------------
    */
    
    function getUrlParameter(sParam) {
        var sPageURL = window.location.search.substring(1);
        var sURLVariables = sPageURL.split('&');
        for (var i = 0; i < sURLVariables.length; i++) 
        {
            var sParameterName = sURLVariables[i].split('=');
            if (sParameterName[0] == sParam) 
            {
                return sParameterName[1];
            }
        }
        return "";
    } 

    /*
      ------------------------------------------------------------

        Binds event handlers to control the interface

      ------------------------------------------------------------
    */

    function initUI() {

        $( '.loading' ).remove();

        dom = {
            generate: $( '#generate' ),
            output: $( '#output' )
        };

        dom.generate.click( function() {
            generate();
            return false;
        });
    }

    /*
      ------------------------------------------------------------

        Builds a regular expression for all types in the corpus

      ------------------------------------------------------------
    */

    function buildRexExp() {

        var types = [];

        for ( var type in corpus )

            types.push( type );

        var content = '@(type)'.replace( 'type', types.join( '|' ) );

        regex = new RegExp( content, 'gi' );
    }

    /*
      ------------------------------------------------------------

        Generates ideas based on the corpus

      ------------------------------------------------------------
    */

    function generate() {
        
        //reset reference
        ref = "";
        
        var param = getUrlParameter('id');
        var temp;
        
        var type, text, part, iter = 0, // Safety mechanism
            idea = randomItem( templates, false, true ),
            item = regex.exec( idea ),
            copy = cloneCorpus();
        
        //if parameter was used, parse the idea template
        if(param.length > 0){
            //before parsing, set ref to param
            ref = param;
            //parse
            temp = parseInt(param.substring(0,2), 16);
            param = param.substring(2);
            idea = templates[temp];
        }
        
        while ( item && ++iter < 1000 ) {

            type = item[ 0 ];
            text = item[ 1 ];

            //console.log( text, copy, copy[ text ] );
            if(param.length > 0){
                temp = parseInt(param.substring(0,2), 16);
                param = param.substring(2);
                part = copy[ text ][temp];
            }else{
{'fraction_non_alphanumeric': 0.2745945945945946, 'fraction_numerical': 0.00628992628992629, 'mean_word_length': 0.888641425389755, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1623998', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2396, 'n_words': 675}