<commit_before>import {removeTrailingSpaces} from "../whitespace/spaces";

export function removeNbspBetweenMultiCharWords(string, locale) {
	let pattern = "(["+ locale.lowercaseChars + locale.uppercaseChars +"]{2,})(["+ locale.nbsp + locale.narrowNbsp +"])(["+ locale.lowercaseChars + locale.uppercaseChars +"]{2,})";
	let re = new RegExp(pattern, "g");
	string =  string.replace(re, "$1 $3");
	string =  string.replace(re, "$1 $3"); //calling it twice to catch odd/even occurences

	return string;
}



export function addNbspAfterPreposition(string, locale) {
	let pattern =
		"(^|[" + locale.space + "]|[^" + locale.allChars + locale.apostrophe + locale.plus + locale.minus + locale.hyphen + "])"
	+ "([" + locale.allChars + "])"
	+ "([" + locale.space + "])";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp;
	string = string.replace(re, replacement);
	string = string.replace(re, replacement); //calling it twice to catch odd/even occurences

	return string;
}



export function addNbspAfterAmpersand(string, locale) {
	let pattern = "([" + locale.spaces + "])(" + locale.ampersand + ")([" + locale.spaces + "])";
	let re = new RegExp(pattern, "g");
	let replacement = " $2" + locale.nbsp;

	return string.replace(re, replacement);
}



export function addNbspAfterCardinalNumber(string, locale) {
	let pattern = "([^" + locale.nbsp + "]|^)(" + locale.cardinalNumber + ")( )(["+ locale.allChars +"]+)";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp + "$4";

	return string.replace(re, replacement);
}



export function addNbspAfterOrdinalNumber(string, locale) {
	let pattern = "("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)(["+ locale.allChars +"]+)";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp + "$4";

	return string.replace(re, replacement);
}



export function addNbspWithinOrdinalDate(string, locale) {
	let pattern = "("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)("+ locale.cardinalNumber +")";
	let re = new RegExp(pattern, "g");
	let replacement = "";

	switch (locale.locale) {
		case "en-us":
		case "rue":
		case "sk":
		case "cs":
			replacement = "$1$2" + locale.nbsp + "$4$5" + locale.nbsp + "$7";
			break;
		case "de-de":
			replacement = "$1$2" + locale.nbsp + "$4$5" + locale.space + "$7";
			break;
	}

	return string.replace(re, replacement);
}



export function addNbspAfterRomanNumeral(string, locale) {
	// we can identify roman numeral effectively only if it has an ordinal indicator
	if(locale.romanOrdinalIndicator != "") {
		let pattern =
				"(\\b)"
			+ "(["+ locale.romanNumerals + "]+)"
			+ "("+ locale.romanOrdinalIndicator +")"
			+ "(["+ locale.spaces +"]?)"
			+ "([" + locale.allChars + locale.cardinalNumber + "])";
		let re = new RegExp(pattern, "g");
		let replacement = "$1$2$3" + locale.nbsp + "$5";

		return string.replace(re, replacement);
	}

	return string;
}



/*
	Fix non-breaking space around name with regnal number
	Karel IV. → Karel⎵IV.
	Karel IV.⎵byl → Karel⎵IV. byl
	Charles IV → Charles⎵IV

	@param {string} string — input text for identification
	@returns {string} — output with correctly placed non-breaking space
*/
export function fixNbspForNameWithRegnalNumber(string, locale) {
	// if(locale.romanOrdinalIndicator != "") {
		let pattern =
			"(\\b[" + locale.uppercaseChars + "]["+ locale.lowercaseChars +"]+?)"
			+ "([" + locale.spaces + "])"
			+ "([" + locale.romanNumerals +"]+)"
			+ "("  + locale.romanOrdinalIndicator +")"
			+ "([" + locale.nbsp + "]?)";
		let re = new RegExp(pattern, "g");

		return string.replace(re, function($0, $1, $2, $3, $4, $5){
			if ($5 == ""){
				return $1 + locale.nbsp + $3 + $4;
			} else {
				return $1 + locale.nbsp + $3 + $4 + locale.space;
			}
		});

	// }

	// return string;
}



/*
	Fix nbsp before % (percent), ‰ (permille) and ‱ (permyriad)

	@param {string} string — input text for identification
	@returns {string} — output with correctly added non-breaking space
*/
export function addNbspBeforePercent(string, locale) {
	let pattern = "([" + locale.spaces + "])([" + locale.percent + locale.permille + locale.permyriad + "])";
	let re = new RegExp(pattern, "g");
	let replacement = locale.nbsp + "$2";

	return string.replace(re, replacement);
}



/*
	Helper function that adds nbsp after symbols
	in their respective *.js files
<commit_msg>Remove comments<commit_after>import {removeTrailingSpaces} from "../whitespace/spaces";

export function removeNbspBetweenMultiCharWords(string, locale) {
	let pattern = "(["+ locale.lowercaseChars + locale.uppercaseChars +"]{2,})(["+ locale.nbsp + locale.narrowNbsp +"])(["+ locale.lowercaseChars + locale.uppercaseChars +"]{2,})";
	let re = new RegExp(pattern, "g");
	string =  string.replace(re, "$1 $3");
	string =  string.replace(re, "$1 $3"); //calling it twice to catch odd/even occurences

	return string;
}



export function addNbspAfterPreposition(string, locale) {
	let pattern =
		"(^|[" + locale.space + "]|[^" + locale.allChars + locale.apostrophe + locale.plus + locale.minus + locale.hyphen + "])"
	+ "([" + locale.allChars + "])"
	+ "([" + locale.space + "])";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp;
	string = string.replace(re, replacement);
	string = string.replace(re, replacement); //calling it twice to catch odd/even occurences

	return string;
}



export function addNbspAfterAmpersand(string, locale) {
	let pattern = "([" + locale.spaces + "])(" + locale.ampersand + ")([" + locale.spaces + "])";
	let re = new RegExp(pattern, "g");
	let replacement = " $2" + locale.nbsp;

	return string.replace(re, replacement);
}



export function addNbspAfterCardinalNumber(string, locale) {
	let pattern = "([^" + locale.nbsp + "]|^)(" + locale.cardinalNumber + ")( )(["+ locale.allChars +"]+)";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp + "$4";

	return string.replace(re, replacement);
}



export function addNbspAfterOrdinalNumber(string, locale) {
	let pattern = "("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)(["+ locale.allChars +"]+)";
	let re = new RegExp(pattern, "g");
	let replacement = "$1$2" + locale.nbsp + "$4";

	return string.replace(re, replacement);
}



export function addNbspWithinOrdinalDate(string, locale) {
	let pattern = "("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)("+ locale.cardinalNumber +")("+ locale.ordinalIndicator +")(["+ locale.spaces +"]?)("+ locale.cardinalNumber +")";
	let re = new RegExp(pattern, "g");
	let replacement = "";

	switch (locale.locale) {
		case "en-us":
		case "rue":
		case "sk":
		case "cs":
			replacement = "$1$2" + locale.nbsp + "$4$5" + locale.nbsp + "$7";
			break;
		case "de-de":
			replacement = "$1$2" + locale.nbsp + "$4$5" + locale.space + "$7";
			break;
	}

	return string.replace(re, replacement);
}



export function addNbspAfterRomanNumeral(string, locale) {
	// we can identify roman numeral effectively only if it has an ordinal indicator
	if(locale.romanOrdinalIndicator != "") {
		let pattern =
				"(\\b)"
			+ "(["+ locale.romanNumerals + "]+)"
			+ "("+ locale.romanOrdinalIndicator +")"
			+ "(["+ locale.spaces +"]?)"
			+ "([" + locale.allChars + locale.cardinalNumber + "])";
		let re = new RegExp(pattern, "g");
		let replacement = "$1$2$3" + locale.nbsp + "$5";

		return string.replace(re, replacement);
	}

	return string;
}



/*
	Fix non-breaking space around name with regnal number
	Karel IV. → Karel⎵IV.
	Karel IV.⎵byl → Karel⎵IV. byl
	Charles IV → Charles⎵IV

	@param {string} string — input text for identification
	@returns {string} — output with correctly placed non-breaking space
*/
export function fixNbspForNameWithRegnalNumber(string, locale) {
		let pattern =
			"(\\b[" + locale.uppercaseChars + "]["+ locale.lowercaseChars +"]+?)"
			+ "([" + locale.spaces + "])"
			+ "([" + locale.romanNumerals +"]+)"
			+ "("  + locale.romanOrdinalIndicator +")"
			+ "([" + locale.nbsp + "]?)";
		let re = new RegExp(pattern, "g");

		return string.replace(re, function($0, $1, $2, $3, $4, $5){
			if ($5 == ""){
				return $1 + locale.nbsp + $3 + $4;
			} else {
				return $1 + locale.nbsp + $3 + $4 + locale.space;
			}
		});
}



/*
	Fix nbsp before % (percent), ‰ (permille) and ‱ (permyriad)

	@param {string} string — input text for identification
	@returns {string} — output with correctly added non-breaking space
*/
export function addNbspBeforePercent(string, locale) {
	let pattern = "([" + locale.spaces + "])([" + locale.percent + locale.permille + locale.permyriad + "])";
	let re = new RegExp(pattern, "g");
	let replacement = locale.nbsp + "$2";

	return string.replace(re, replacement);
}



/*
	Helper function that adds nbsp after symbols
	in their respective *.js files

{'fraction_non_alphanumeric': 0.18971275885103542, 'fraction_numerical': 0.009574704965486529, 'mean_word_length': 4.174539170506913, 'pattern_counts': {'":': 10, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '329051', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3083, 'n_words': 748}