Mapping indices from the normalized string back to the original string is a bit of a challenge, especially with the string replacement patterns you're using. The key is to keep track of all original indices and how they shift during the normalization process.
To achieve this, you can create a mapping that keeps track of block replacement operations and their effects on indices.
Let's break it down step-by-step:
- Track replacements: As you replace formatting characters, track positions in both the original and the normalized strings.
- Create a mapping: For each character in the normalized string, map it back to an index in the original string.
Here is an approach in Javascript:
const createIndexMapping = (text, normalizeShell) => {
const original = text.split('');
let normalized = text;
const mapping = [];
// Track initial indices
for (let i = 0; i < original.length; i++) {
mapping.push(i);
}
// Create a replacement function that handles index mapping
const replaceWithMapping = (regex, replacement) => {
const normalizedParts = [];
let i = 0;
normalized.replace(regex, (match, offset) => {
normalizedParts.push(normalized.slice(i, offset));
i = offset + match.length;
// Collect the mapping for the replaced section
for (let j = 0; j < replacement.length; j++) {
mapping.push(mapping[(offset + j) % original.length] || mapping[original.length - 1]);
}
normalizedParts.push(replacement);
return replacement;
});
normalizedParts.push(normalized.slice(i));
normalized = normalizedParts.join('');
};
replaceWithMapping(/[\s\n\r\t]/g, "$X@#$");
replaceWithMapping(/[-`]/g, "$X@#$");
replaceWithMapping(/\*\*/g, "$X@#$");
replaceWithMapping(/\b\d{1}\./g, "$X@#$");
replaceWithMapping(/\b\d{2}\./g, "$X@#$");
return {
original,
normalized,
mapping
};
};
// Example of usage
const text = "This is a test string with formatting like 1. and 10. and **bold** formatting.";
const { original, normalized, mapping } = createIndexMapping(text, normalizeShell);
console.log("Original: ", original.join(''));
console.log("Normalized: ", normalized);
console.log("Mapping: ", mapping);
// Function to map normalized index back to original index
const mapNormalizedToOriginal = (normalizedIndex, mapping) => {
if (normalizedIndex >= 0 && normalizedIndex < mapping.length) {
return mapping[normalizedIndex];
}
return -1; // or handle out of range indices as needed
};
// Example map: character `i` in normalized string back to original string
const normalizedIndex = 5; // Replace with desired index
console.log("Character at normalized index 5:", normalized[normalizedIndex]);
const originalIndex = mapNormalizedToOriginal(normalizedIndex, mapping);
console.log("Mapped original index:", originalIndex);
console.log("Character at original index:", original[originalIndex]);
This script carries out the following:
- Initial Mapping: Initializes a mapping from each character in the original text to its index.
- Replacement Function: The
replaceWithMapping function does regex replacements and logs index mappings.
- Usage Example: After normalizing, maps the indices from the normalized string back to the original string, demonstrating it with an example index.
Make sure to test this thoroughly, as edge cases might still exist depending on the complexity of the replacement patterns and the input string.