Replace diacritic (accented) characters

Table of Contents

Background

This article shows how to use a hook script to replace diacritic or accented characters with a normalised version of the character.

Details

The following post process hook script takes a metadata field containing diacritic characters and produces a second normalised field with the accented characters replaced with non-accented versions.

This is useful if you need to strip the characters e.g. on query completion triggers that might need to match the diacritics.

Similar code could be applied in a pre process hook script to typed search queries.

hook_pre_process.groovy
import java.text.Normalizer;
import java.text.Normalizer.Form;

transaction?.response?.resultPacket?.results.each() {

// Created a normalised version of the name metadatafield that removes diacritics
    it.metaData["nameNormalized"] = Normalizer.normalize(it.metaData["name"], Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}