package examples;

import com.aliyun.odps.udf.UDF;

import java.text.Normalizer;

public final class ClearTitleSignal extends UDF {

    public String evaluate(String s) {
        if (s == null) {
            return null;
        }
        return normalizeText(s);
    }

    private static String normalizeText(String input) {
        // Remove emoji and special characters
        String noEmoji = input.replaceAll("[^\\p{L}\\p{N}\\p{P}\\p{Z}]", "");

        // Remove punctuation and numbers from the start and end of the string
        while (noEmoji.length() > 0 && noEmoji.substring(0, 1).matches("[\\p{P}]")) {
            noEmoji = noEmoji.substring(1);
        }
        while (noEmoji.length() > 0 && noEmoji.substring(noEmoji.length() - 1).matches("[\\p{P}]")) {
            noEmoji = noEmoji.substring(0, noEmoji.length() - 1);
        }

        // Normalize to remove accents and diacritics
        String normalized = Normalizer.normalize(noEmoji, Normalizer.Form.NFD);
        normalized = normalized.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");

        // Remove extra spaces and trim

        return normalized.trim().replaceAll(" +", " ");
    }

}