<commit_before>function format(oldFormat, newFormat, text) {
    if (oldFormat === 'standard-tab' && newFormat === 'wednesdays') {
        text = text.replace(/​/g, '')
        var lines = text.split('\n');
        var currentChordLine;
        var output = "";
        for (let i = 0; i < lines.length; i++) {
            var line = lines[i];
            if (isChordLine(line)) {
                currentChordLine = line;
            } else if (currentChordLine != null) {
                output += interleaveChords(currentChordLine, line) + '\n'
                currentChordLine = null;
            } else {
                output += line += '\n';
            }
        }
        return output;
    }
}

function isChordLine(line) {
    fragments = line.replace(/\s+/g, " ").trim().split(" ")
    for (let i = 0; i < fragments.length; i++) {
        if(!isAChord(fragments[i])) {
            return false;
        }
    }
    return true;
}

function isAChord(potentialChord) {
    return /[ABCDEFG][b#]?(m|dim|maj)?[79]?(sus[24])?/.test(potentialChord);
}

function interleaveChords(chords, lyrics) {
    var output = ""; 
    var alreadyUsedChordCharsCount = 0;
    for (let char = 0; char < lyrics.length; char++) {
        if (chords[char] && chords[char] != " " && alreadyUsedChordCharsCount === 0) {
            var chordString = "";
            for (let chordCharIndex = char; chordCharIndex < chords.length; chordCharIndex++) {
                if (chords[chordCharIndex] === " ") break;
                chordString += chords[chordCharIndex];
                alreadyUsedChordCharsCount++;
            }
            output += "(" + chordString + ")" + lyrics[char];
        } else {
            if (alreadyUsedChordCharsCount > 0) alreadyUsedChordCharsCount--;
            output += lyrics[char];
        }
    }
    return output;
}<commit_msg>Instrumentals have increased chances of being imported from guitar format. Refactors the particularly messy chord interleaving logic, at least a bit.<commit_after>function format(oldFormat, newFormat, text) {
    if (oldFormat === 'standard-tab' && newFormat === 'wednesdays') {
        text = text.replace(/​/g, '')
        var lines = text.split('\n');
        var currentChordLine;
        var output = "";
        for (let i = 0; i < lines.length; i++) {
            var line = lines[i];
            if (isChordLine(line)) {
                if (currentChordLine != null) {
                    output += formatAllFragmentsAsChords(currentChordLine) + '\n';
                }
                currentChordLine = line;
            } else if (currentChordLine != null) {
                output += interleaveChords(currentChordLine, line) + '\n'
                currentChordLine = null;
            } else {
                output += line += '\n';
            }
        }
        return output;
    }
}

function isChordLine(line) {
    fragments = line.replace(/\s+/g, ' ').trim().split(' ')
    for (let i = 0; i < fragments.length; i++) {
        if(!isAChord(fragments[i])) {
            return false;
        }
    }
    return true;
}

function isAChord(potentialChord) {
    return /[ABCDEFG][b#]?(m|dim|maj)?[79]?(sus[24])?/.test(potentialChord);
}

function formatAllFragmentsAsChords(line) {
    var words = line.split(" ");
    var output = "";
    words.forEach(function(word) {
     output += formatChord(word);    
    });  
    return output;
}
   
function formatChord(chord) {
    return "(" + chord + ")"
}

function interleaveChords(chords, lyrics) {
    var output = ""; 
    for (let lyricIndex = 0; lyricIndex < lyrics.length; lyricIndex++) {
        if (chords[lyricIndex] && chords[lyricIndex] != " ") {
            var chordString = "";
            for (let chordCharIndex = lyricIndex; chordCharIndex < chords.length; chordCharIndex++) {
                if (chords[chordCharIndex] === " ") break;
                chordString += chords[chordCharIndex];
            }
            output += "(" + chordString + ")";
            output += lyrics.slice(lyricIndex, lyricIndex+chordString.length)
            lyricIndex += chordString.length - 1
        } else {
            output += lyrics[lyricIndex];
        }
    }
    return output;
};
{'fraction_non_alphanumeric': 0.1436372269705603, 'fraction_numerical': 0.004273504273504274, 'mean_word_length': 2.0287562904385332, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, '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': '3382372', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1254, 'n_words': 269}