<commit_before>// MARK: - This is internal tool, we are free to use the latest javascript, thus no need to do jshint, which is for legacy frontend stuff
/* jshint ignore:start */
// MARK: - This has to pass through the gulp testing, so no var or for of loops, or any other modern features. 
var splitter = '-|-';
var startTime = new Date();
var localStorageKey = 'translation';
if (window.opener && window.opener.userName) {
    window.userName = window.opener.userName;
} else {
    window.userName = '';
}

var delegate = new Delegate(document.body);

// MARK: - Links in translated text
delegate.on('click', '.info-original a[href], .info-translation a[href], .info-original strong, .info-translation strong', function(event){
    try {
        var textArea = this.closest(".info-container").querySelector('textarea');
        var selectionStart = textArea.selectionStart;
        var selectionEnd = textArea.selectionEnd;
        var text = textArea.value;
        if (selectionEnd > selectionStart) {
            var textBefore = text.substring(0, selectionStart);
            var textSelected = text.substring(selectionStart, selectionEnd);
            var textAfter = text.substring(selectionEnd, text.length);
            var t = this.tagName.toLowerCase();
            var newText;
            if (t === 'a' && this.getAttribute('href')) {
                newText = textBefore + '<a href="' + this.getAttribute('href') + '" targe="_blank">' + textSelected + '</a>' + textAfter;
            } else {
                newText = textBefore + '<' + t + '>' + textSelected + '</' + t + '>' + textAfter;
            }
            textArea.value = newText;
        } else {
            alert('请选中右边文本框的相应的文本内容来添加链接！');
        }
    } catch(ignore){
        alert('请选中右边文本框的相应的文本内容来添加链接！');
    }
    event.stopImmediatePropagation();
    return false;
});

delegate.on('click', '.info-translation', function(event){
    confirmTranslation(this);
});

delegate.on('click', '.translation-suggestion', function(event){
    var textArea = this.closest(".info-container").querySelector('textarea');
    var selectionStart = textArea.selectionStart;
    var selectionEnd = textArea.selectionEnd;
    var text = textArea.value;
    if (selectionStart <= selectionEnd && selectionStart >= 0) {
        var textBefore = text.substring(0, selectionStart);
        var newText = this.getAttribute('data-translation');
        var textAfter = text.substring(selectionEnd, text.length);
        var newText = textBefore + newText + textAfter;
        textArea.value = newText;
    } else {
        alert('请选中右边文本框的相应的文本内容来快捷填写！');
    }
});


// MARK: - Equivalent to php's str_word_count, which is used by the FTC's CMS workflow statistics
function str_word_count(str) {
    if (typeof str !== 'string') {return 0;}
    var words = str.replace(/([(\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '')
        .replace(/(^\s*)|(\s*$)/gi,"")
        .replace(/[0-9]/gi,"")
        .replace(/[ ]{2,}/gi," ")
        .replace(/\n /,"\n");
    return words.split(' ').length;
}

function convertTextToArray(t) {
    if (typeof t !== 'string') {return [];}
    var newText = t.trim().replace(/^[\n\r\s]+/, '').replace(/[\n\r\s]+$/, '');
    newText = newText.replace(/[\r\n]+/g, splitter);
    if (newText.split(splitter).length > 1) {return newText.split(splitter);}
    var englishInfoDiv = document.createElement('DIV');
    englishInfoDiv.innerHTML = newText;
    var htmlTexts = [];
    for (var i=0; i<englishInfoDiv.children.length; i++) {
        htmlTexts.push(englishInfoDiv.children[i].outerHTML);
    }
    return htmlTexts;
}

function addTranslation(ele) {
    if (ele.className.indexOf(' selected')>=0) {return;}
    var text = ele.innerHTML;
    ele.className += ' selected';
    ele.title = '';
    var finalTranslationEle = document.getElementById('final-translation');
    var prefix = (finalTranslationEle.value === '') ? '' : '\n\n';
    finalTranslationEle.value += prefix + text;
    var ea = ele.getAttribute('data-translation-index');
    gtag('event', ea, {'event_label': window.userName, 'event_category': 'Translation Helper', 'non_interaction': false});
}

function tidyHTML(html) {
    var d = document.createElement('div');
    d.innerHTML = html;
    return d.innerHTML;
}

function confirmTranslation(ele) {
    if (ele.className.indexOf(' selected')>=0) {return;}
    var text = ele.innerHTML;
    ele.classList.add('selected');
    ele.title = '';
    var finalTranslationEle = ele.parentNode.parentNode.querySelector('textarea');
    var prefix = (finalTranslationEle.value === '') ? '' : '\n\n';
    var translatedText = prefix + text;
    finalTranslationEle.value += translatedText;
    var infoContainers = document.querySelectorAll('.info-container');
    var originalText = ele.parentNode.parentNode.querySelector('.info-original');
    // MARK: - You only need to translate once for the same phrase
    for (var i=0; i < infoContainers.length; i++) {
        var containerEle = infoContainers[i];
        var textEle = containerEle.querySelector('textarea');
        var originalEle = containerEle.querySelector('.info-original');
        if (originalText.innerHTML !== originalEle.innerHTML || textEle.value !== '') {continue;}
        textEle.value = translatedText;
        var infoTranslations = containerEle.querySelectorAll('.info-translation');
        for (var j=0; j<infoTranslations.length; j++) {
            var translationEle = infoTranslations[j];
            if (translationEle.innerHTML !== text) {continue;}
            translationEle.classList.add('selected');
        }
    }
    var ea = ele.getAttribute('data-translation-index');
    gtag('event', ea, {'event_label': window.userName, 'event_category': 'Translation Helper', 'non_interaction': false});
}



function start() {
    var englishText = document.getElementById('english-text');
    var translationInfoEle = document.getElementById('translation-info');
    var translationInfoString = translationInfoEle.value;
    var storyBodyEle = document.getElementById('story-body-container');
    if (translationInfoString !== '') {
        var translationInfo = JSON.parse(translationInfoString);
        var englishEle = document.createElement('DIV');
        englishEle.innerHTML = englishText.value;
        var k = '';
        if (window.opener && window.opener.titles) {
            for (var j=0; j<window.opener.titles.length; j++) {
                var info = window.opener.titles[j];
                var infoHTML = '<div class="info-original">' + info.original + '</div>';
                var id = info.id;
                var translations = info.translations.split(splitter);
                for (var m=0; m<translations.length; m++) {
                    infoHTML += '<div onclick="confirmTranslation(this)" data-translation-index="' + m + '"  class="info-translation" title="click to confirm this translation to the right">' + translations[m] + '</div>';
                }
                infoHTML = '<div class="info-container"><div>' + infoHTML + '</div><div><textarea data-info-id="' + id + '" placeholder="点选右边的翻译版本，您也可以继续编辑"></textarea></div></div><hr>';
                k += infoHTML;
            }
        }
        var existingTranslationDict = {};
        if (window.opener && window.opener.checkUpdate === true) {
            var cbodyEle = window.opener.document.getElementById('cbody');
            var ebodyEle = window.opener.document.getElementById('ebody');
            if (cbodyEle && ebodyEle) {
                var cbody = cbodyEle.value;
                var ebody = ebodyEle.value;
                var chineseParagraphs = cbody.split('\n');
                var englishParagraphs = ebody.split('\n');
                // console.log(`englishParagraphs: ${englishParagraphs.length}, chineseParagraphs: ${chineseParagraphs.length}`);
                if (englishParagraphs.length > 0 && chineseParagraphs.length > 0) {
                    for (var l=0; l<englishParagraphs.length; l++) {
                        var englishParagraph = englishParagraphs[l];
                        if (englishParagraph === '' || l >= chineseParagraphs.length) {continue;}
                        var chineseParagraph = chineseParagraphs[l];
                        existingTranslationDict[englishParagraph] = chineseParagraph;
                    }
                }
            }
        }
        console.log(JSON.stringify(existingTranslationDict));

        for (var i=0; i<translationInfo.length; i++) {
            var info = translationInfo[i];<commit_msg>translation helper<commit_after>// MARK: - This is internal tool, we are free to use the latest javascript, thus no need to do jshint, which is for legacy frontend stuff
/* jshint ignore:start */
// MARK: - This has to pass through the gulp testing, so no var or for of loops, or any other modern features. 
var splitter = '-|-';
var startTime = new Date();
var localStorageKey = 'translation';
if (window.opener && window.opener.userName) {
    window.userName = window.opener.userName;
} else {
    window.userName = '';
}

var delegate = new Delegate(document.body);

// MARK: - Links in translated text
delegate.on('click', '.info-original a[href], .info-translation a[href], .info-original strong, .info-translation strong', function(event){
    try {
        var textArea = this.closest(".info-container").querySelector('textarea');
        var selectionStart = textArea.selectionStart;
        var selectionEnd = textArea.selectionEnd;
        var text = textArea.value;
        if (selectionEnd > selectionStart) {
            var textBefore = text.substring(0, selectionStart);
            var textSelected = text.substring(selectionStart, selectionEnd);
            var textAfter = text.substring(selectionEnd, text.length);
            var t = this.tagName.toLowerCase();
            var newText;
            if (t === 'a' && this.getAttribute('href')) {
                newText = textBefore + '<a href="' + this.getAttribute('href') + '" targe="_blank">' + textSelected + '</a>' + textAfter;
            } else {
                newText = textBefore + '<' + t + '>' + textSelected + '</' + t + '>' + textAfter;
            }
            textArea.value = newText;
        } else {
            alert('请选中右边文本框的相应的文本内容来添加链接！');
        }
    } catch(ignore){
        alert('请选中右边文本框的相应的文本内容来添加链接！');
    }
    event.stopImmediatePropagation();
    return false;
});

delegate.on('click', '.info-translation', function(event){
    confirmTranslation(this);
});

delegate.on('click', '.translation-suggestion', function(event){
    var textArea = this.closest(".info-container").querySelector('textarea');
    var selectionStart = textArea.selectionStart;
    var selectionEnd = textArea.selectionEnd;
    var text = textArea.value;
    if (selectionStart <= selectionEnd && selectionStart >= 0) {
        var textBefore = text.substring(0, selectionStart);
        var newText = this.getAttribute('data-translation');
        var textAfter = text.substring(selectionEnd, text.length);
        var newText = textBefore + newText + textAfter;
        textArea.value = newText;
    } else {
        alert('请选中右边文本框的相应的文本内容来快捷填写！');
    }
});


// MARK: - Equivalent to php's str_word_count, which is used by the FTC's CMS workflow statistics
function str_word_count(str) {
    if (typeof str !== 'string') {return 0;}
    var words = str.replace(/([(\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '')
        .replace(/(^\s*)|(\s*$)/gi,"")
        .replace(/[0-9]/gi,"")
        .replace(/[ ]{2,}/gi," ")
        .replace(/\n /,"\n");
    return words.split(' ').length;
}

function convertTextToArray(t) {
    if (typeof t !== 'string') {return [];}
    var newText = t.trim().replace(/^[\n\r\s]+/, '').replace(/[\n\r\s]+$/, '');
    newText = newText.replace(/[\r\n]+/g, splitter);
    if (newText.split(splitter).length > 1) {return newText.split(splitter);}
    var englishInfoDiv = document.createElement('DIV');
    englishInfoDiv.innerHTML = newText;
    var htmlTexts = [];
    for (var i=0; i<englishInfoDiv.children.length; i++) {
        htmlTexts.push(englishInfoDiv.children[i].outerHTML);
    }
    return htmlTexts;
}

function addTranslation(ele) {
    if (ele.className.indexOf(' selected')>=0) {return;}
    var text = ele.innerHTML;
    ele.className += ' selected';
    ele.title = '';
    var finalTranslationEle = document.getElementById('final-translation');
    var prefix = (finalTranslationEle.value === '') ? '' : '\n\n';
    finalTranslationEle.value += prefix + text;
    var ea = ele.getAttribute('data-translation-index');
    gtag('event', ea, {'event_label': window.userName, 'event_category': 'Translation Helper', 'non_interaction': false});
}

function tidyHTML(html) {
    var d = document.createElement('div');
    d.innerHTML = html;
    return d.innerHTML;
}

function confirmTranslation(ele) {
    if (ele.className.indexOf(' selected')>=0) {return;}
    var text = ele.innerHTML;
    ele.classList.add('selected');
    ele.title = '';
    var finalTranslationEle = ele.parentNode.parentNode.querySelector('textarea');
    var prefix = (finalTranslationEle.value === '') ? '' : '\n\n';
    var translatedText = prefix + text;
    finalTranslationEle.value += translatedText;
    var infoContainers = document.querySelectorAll('.info-container');
    var originalText = ele.parentNode.parentNode.querySelector('.info-original');
    // MARK: - You only need to translate once for the same phrase
    for (var i=0; i < infoContainers.length; i++) {
        var containerEle = infoContainers[i];
        var textEle = containerEle.querySelector('textarea');
        var originalEle = containerEle.querySelector('.info-original');
        if (originalText.innerHTML !== originalEle.innerHTML || textEle.value !== '') {continue;}
        textEle.value = translatedText;
        var infoTranslations = containerEle.querySelectorAll('.info-translation');
        for (var j=0; j<infoTranslations.length; j++) {
            var translationEle = infoTranslations[j];
            if (translationEle.innerHTML !== text) {continue;}
            translationEle.classList.add('selected');
        }
    }
    var ea = ele.getAttribute('data-translation-index');
    gtag('event', ea, {'event_label': window.userName, 'event_category': 'Translation Helper', 'non_interaction': false});
}



function start() {
    var englishText = document.getElementById('english-text');
    var translationInfoEle = document.getElementById('translation-info');
    var translationInfoString = translationInfoEle.value;
    var storyBodyEle = document.getElementById('story-body-container');
    if (translationInfoString !== '') {
        var translationInfo = JSON.parse(translationInfoString);
        var englishEle = document.createElement('DIV');
        englishEle.innerHTML = englishText.value;
        var k = '';
        if (window.opener && window.opener.titles) {
            for (var j=0; j<window.opener.titles.length; j++) {
                var info = window.opener.titles[j];
                var infoHTML = '<div class="info-original">' + info.original + '</div>';
                var id = info.id;
                var translations = info.translations.split(splitter);
                for (var m=0; m<translations.length; m++) {
                    infoHTML += '<div onclick="confirmTranslation(this)" data-translation-index="' + m + '"  class="info-translation" title="click to confirm this translation to the right">' + translations[m] + '</div>';
                }
                infoHTML = '<div class="info-container"><div>' + infoHTML + '</div><div><textarea data-info-id="' + id + '" placeholder="点选左边的翻译版本，您也可以继续编辑"></textarea></div></div><hr>';
                k += infoHTML;
            }
        }
        var existingTranslationDict = {};
        if (window.opener && window.opener.checkUpdate === true) {
            var cbodyEle = window.opener.document.getElementById('cbody');
            var ebodyEle = window.opener.document.getElementById('ebody');
            if (cbodyEle && ebodyEle) {
                var cbody = cbodyEle.value;
                var ebody = ebodyEle.value;
                var chineseParagraphs = cbody.split('\n');
                var englishParagraphs = ebody.split('\n');
                // console.log(`englishParagraphs: ${englishParagraphs.length}, chineseParagraphs: ${chineseParagraphs.length}`);
                if (englishParagraphs.length > 0 && chineseParagraphs.length > 0) {
                    for (var l=0; l<englishParagraphs.length; l++) {
                        var englishParagraph = englishParagraphs[l];
                        if (englishParagraph === '' || l >= chineseParagraphs.length) {continue;}
                        var chineseParagraph = chineseParagraphs[l];
                        existingTranslationDict[englishParagraph] = chineseParagraph;
                    }
                }
            }
        }
        console.log(JSON.stringify(existingTranslationDict));

        for (var i=0; i<translationInfo.length; i++) {
            var info = translationInfo[i];
{'fraction_non_alphanumeric': 0.14973449261831126, 'fraction_numerical': 0.0022174242866312656, 'mean_word_length': 2.7815533980582523, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, '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': '5195408', 'n_tokens_mistral': 5050, 'n_tokens_neox': 4961, 'n_words': 1070}