github-actions[bot] commented on code in PR #67918:
URL: https://github.com/apache/doris/pull/67918#discussion_r4075159842


##########
be/src/storage/index/inverted/tokenizer/pinyin/pinyin_tokenizer.cpp:
##########
@@ -206,14 +220,15 @@ Token* PinyinTokenizer::next(Token* token) {
         size_t size = std::min(text.size(), 
static_cast<size_t>(LUCENE_MAX_WORD_LEN));
         token->setNoCopy(text.data(), 0, static_cast<int32_t>(size));
 
+        int32_t start = item.start_offset;
+        int32_t end = item.end_offset;
         if (config_->ignorePinyinOffset) {
-            int total_byte_length = runes_.empty() ? 0 : 
runes_.back().byte_end;
-            token->setStartOffset(0);
-            token->setEndOffset(total_byte_length);
-        } else {
-            token->setStartOffset(item.start_offset);
-            token->setEndOffset(item.end_offset);
+            start = 0;
+            end = runes_.empty() ? 0 : runes_.back().byte_end;
         }
+        token->setStartOffset(correct_source_start_offset(start));
+        token->setEndOffset(correct_source_offset(end));
+        publishCandidateProvenance(std::string_view(text.data(), size), start, 
end);

Review Comment:
   [P1] Clamp provenance to the emitted Pinyin prefix. With 
`keep_original=true`, `ignore_pinyin_offset=false`, and an input longer than 
`LUCENE_MAX_WORD_LEN`, `setNoCopy` publishes only the capped prefix but this 
call still passes the original full `start`/`end`. `source != term` then 
records a conservative span over the whole input, so a following offset-aware 
Pinyin filter assigns every emitted prefix letter the unpublished suffix as 
well (and the raw byte cap may split UTF-8). Compute a UTF-8-safe prefix and 
narrow the source end/provenance to the represented prefix; please cover the 
over-cap ASCII and split-rune cases.



##########
fe/fe-core/src/main/java/org/apache/doris/indexpolicy/IndexPolicyMgr.java:
##########
@@ -55,13 +56,64 @@ public class IndexPolicyMgr implements Writable, 
GsonPostProcessable {
     private final Map<Long, IndexPolicy> idToIndexPolicy = Maps.newHashMap();
     // Keys are normalized to lowercase for case-insensitive lookup
     private final Map<String, IndexPolicy> nameToIndexPolicy = 
Maps.newHashMap();
+    // Legacy metadata can contain case-distinct names that share a normalized 
key. Keep exact
+    // bindings separately so a saved analyzer continues to resolve its 
original component.
+    private final transient Map<String, IndexPolicy> exactNameToIndexPolicy = 
Maps.newHashMap();
 
     /**
      * Normalize policy name to lowercase for case-insensitive lookup.
      * Policy names are case-insensitive in Doris.
      */
     private static String normalizeKey(String name) {
-        return name == null ? null : name.trim().toLowerCase();
+        return name == null ? null : name.trim().toLowerCase(Locale.ROOT);
+    }
+
+    private static String exactKey(String name) {
+        return name == null ? null : name.trim();
+    }
+
+    // Callers hold either the read or write lock. Prefer an exact legacy name 
binding and
+    // retain normalized lookup only for interactive case-insensitive fallback.
+    private IndexPolicy getPolicyByNameLocked(String name) {
+        IndexPolicy exactPolicy = exactNameToIndexPolicy.get(exactKey(name));
+        return exactPolicy != null ? exactPolicy : 
nameToIndexPolicy.get(normalizeKey(name));
+    }
+
+    // Callers hold either the read or write lock. BE dispatches a canonical 
built-in analyzer, then an
+    // exact policy, then a built-in by normalized name; return that built-in, 
or null for a policy.
+    private String resolveTopLevelBuiltinLocked(String name, Set<String> 
builtins) {
+        String exactName = exactKey(name);
+        if (IndexPolicy.BUILTIN_ANALYZERS.contains(exactName) && 
builtins.contains(exactName)) {
+            return exactName;
+        }
+        if (exactNameToIndexPolicy.containsKey(exactName)) {
+            return null;
+        }
+        String normalizedName = normalizeKey(name);
+        return builtins.contains(normalizedName) ? normalizedName : null;

Review Comment:
   [P1] Preserve the builtin binding selected before canonicalization. If 
replay contains an exact policy named `lowercase`, a user spelling `LowerCase` 
has no exact match and is validated here as the builtin, but returning 
`lowercase` makes `resolveAnalyzerName` persist the exact-colliding spelling. 
BE then checks the exact `lowercase` policy before its builtin-normalizer 
fallback, so indexing/MATCH silently use that custom pipeline (or fail if it is 
the wrong policy family). Retain a spelling that still dispatches to the 
builtin, or encode the binding separately, and cover CREATE/ALTER/MATCH with 
exact `lowercase` plus `LowerCase`.



##########
be/src/storage/index/inverted/similarity/predicate_collector.cpp:
##########
@@ -61,14 +61,22 @@ InvertedIndexAnalyzerCtx analyzer_context_from_properties(
     return analyzer_ctx;
 }
 
-std::vector<TermInfo> analyze_plain_query(const std::string& value,
-                                          const InvertedIndexAnalyzerCtx& 
analyzer_ctx) {
+Result<std::vector<TermInfo>> analyze_plain_query(const std::string& value,

Review Comment:
   [P1] Include provider construction in this `Result` boundary. 
`SearchPredicateCollector` builds `analyzer_context_from_properties` before 
calling `analyze_plain_query`; `create_analyzer_provider` can now throw the new 
wrong-family `Exception` from `process_filter_configs` when persisted/replayed 
analyzer components collide across policy families. That exception therefore 
bypasses both catches here and escapes the `Status`-returning scoring collector 
instead of becoming `INVERTED_INDEX_ANALYZER_ERROR`. Make context/provider 
construction `Result`-returning (or wrap both stages together) and test SEARCH 
scoring with a replayed wrong-family component.



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/invertedindex/AnalyzerIdentityBuilder.java:
##########
@@ -206,17 +853,15 @@ private static String resolveTokenFilterIdentity(String 
filterList) {
         String[] filters = filterList.split(",\\s*");
         // DO NOT sort - filter order is semantically significant
 
-        for (int i = 0; i < filters.length; i++) {
-            String filter = filters[i].trim();
-            if (i > 0) {
-                sb.append(",");
+        for (String filterName : filters) {

Review Comment:
   [P1] Collapse adjacent duplicate `lowercase` filters. `keyword -> lowercase` 
and `keyword -> lowercase -> lowercase` emit the same term and preserve the 
same offsets/provenance: the second filter sees an already-lowercased token and 
delegates the first filter's mapping unchanged. Because this loop retains both 
entries, distinct analyzer names get different identities and pass the 
CREATE/ALTER duplicate fences. Deduplicate only filters proven idempotent (at 
least adjacent `lowercase`), without reordering, and keep a repeated 
non-idempotent filter as a negative test.



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/invertedindex/AnalyzerIdentityBuilder.java:
##########
@@ -150,47 +282,562 @@ private static String 
buildIdentityFromPolicyProperties(IndexPolicyTypeEnum type
      * Resolve a component (tokenizer) to its identity.
      */
     private static String resolveComponentIdentity(String name, 
IndexPolicyTypeEnum expectedType) {
+        return resolveComponentIdentity(name, expectedType, null);
+    }
+
+    /** {@code fold} is the case-folding context of a char filter, or null 
without a downstream fold. */
+    private static String resolveComponentIdentity(
+            String name, IndexPolicyTypeEnum expectedType, FoldContext fold) {
         if (Strings.isNullOrEmpty(name)) {
             return "";
         }
 
-        // Check if it's a built-in component
-        if (expectedType == IndexPolicyTypeEnum.TOKENIZER
-                && IndexPolicy.BUILTIN_TOKENIZERS.contains(name)) {
-            return name;
+        // Existing named policies take precedence over built-ins for upgrade 
compatibility.
+        try {
+            Env env = Env.getCurrentEnv();
+            if (env != null && env.getIndexPolicyMgr() != null) {
+                IndexPolicy policy = 
env.getIndexPolicyMgr().getPolicyByName(name);
+                if (policy != null && policy.getType() == expectedType) {
+                    if (policy.isInvalid()) {
+                        return "invalid-policy:" + policy.getId() + ":" + 
policy.getName();
+                    }
+                    Map<String, String> props = policy.getProperties();
+                    if (props != null && !props.isEmpty()) {
+                        TreeMap<String, String> sortedProps = new 
TreeMap<>(props);
+                        String type = sortedProps.get(IndexPolicy.PROP_TYPE);
+                        String normalizedType = 
normalizeBuiltinComponentName(type, expectedType);
+                        if (normalizedType != null) {
+                            if ("empty".equals(normalizedType)) {
+                                return "";
+                            }
+                            sortedProps.put(IndexPolicy.PROP_TYPE, 
normalizedType);
+                            canonicalizeEffectiveComponentProperties(
+                                    sortedProps, normalizedType, expectedType);
+                            if (sortedProps.size() == 1) {
+                                return normalizedType;
+                            }
+                        }
+                        if (expectedType == IndexPolicyTypeEnum.TOKENIZER
+                                && 
"ngram".equals(sortedProps.get(IndexPolicy.PROP_TYPE))) {
+                            // This setting only limits policy creation; it 
does not change emitted tokens.
+                            sortedProps.remove(PROP_MAX_NGRAM_DIFF);
+                        }
+                        if (expectedType == IndexPolicyTypeEnum.CHAR_FILTER

Review Comment:
   [P1] Collapse the explicit factory defaults to the builtin `char_replace` 
identity. A bare `char_replace` reference is valid and BE instantiates it with 
pattern `,._` and a single-space replacement, while a named policy that states 
`pattern=,._` and omits `replacement` reaches this branch as `{pattern=,._, 
replacement= , type=char_replace}`. Those identical filters therefore receive 
different identities and can coexist on one column under distinct analyzer 
names. Cover builtin versus explicit-default aliases in CREATE/ALTER, retaining 
a nondefault pattern/replacement negative.



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/invertedindex/AnalyzerIdentityBuilder.java:
##########
@@ -116,6 +220,28 @@ private static String resolveAnalyzerIdentity(String 
analyzerName, String defaul
         }
     }
 
+    /** Whether BE builds the built-in normalizer for this name; an exact 
legacy policy shadows it. */
+    private static boolean isBuiltinNormalizerBinding(String name) {
+        if (!IndexPolicy.BUILTIN_NORMALIZERS.contains(name)) {
+            return false;
+        }
+        try {
+            Env env = Env.getCurrentEnv();
+            return env == null || env.getIndexPolicyMgr() == null
+                    || env.getIndexPolicyMgr().getPolicyByExactName(name) == 
null;
+        } catch (RuntimeException e) {
+            return true;
+        }
+    }
+
+    /**
+     * BE builds a built-in normalizer as the keyword tokenizer plus the 
built-in token filter of
+     * the same name, so it shares the identity of that custom pipeline.
+     */
+    private static String builtinNormalizerIdentity(String name) {
+        return IndexPolicyTypeEnum.NORMALIZER.name() + ":" + 
IndexPolicy.PROP_TOKEN_FILTER + "=" + name + ";";

Review Comment:
   [P1] Canonicalize this to the effective keyword pipeline, not the 
`NORMALIZER` container. BE's `CustomNormalizer` is exactly a keyword tokenizer 
followed by the configured char/token filters, so a normalizer with 
`token_filter=lowercase` and an ANALYZER with `tokenizer=keyword, 
token_filter=lowercase` emit the same terms, offsets, and provenance. Their 
family-prefixed identities and distinct names currently pass both CREATE and 
ALTER duplicate fences. Add the cross-family alias case, with a non-keyword 
tokenizer as the negative.



##########
fe/fe-core/src/main/java/org/apache/doris/analysis/invertedindex/AnalyzerIdentityBuilder.java:
##########
@@ -150,47 +282,562 @@ private static String 
buildIdentityFromPolicyProperties(IndexPolicyTypeEnum type
      * Resolve a component (tokenizer) to its identity.
      */
     private static String resolveComponentIdentity(String name, 
IndexPolicyTypeEnum expectedType) {
+        return resolveComponentIdentity(name, expectedType, null);
+    }
+
+    /** {@code fold} is the case-folding context of a char filter, or null 
without a downstream fold. */
+    private static String resolveComponentIdentity(
+            String name, IndexPolicyTypeEnum expectedType, FoldContext fold) {
         if (Strings.isNullOrEmpty(name)) {
             return "";
         }
 
-        // Check if it's a built-in component
-        if (expectedType == IndexPolicyTypeEnum.TOKENIZER
-                && IndexPolicy.BUILTIN_TOKENIZERS.contains(name)) {
-            return name;
+        // Existing named policies take precedence over built-ins for upgrade 
compatibility.
+        try {
+            Env env = Env.getCurrentEnv();
+            if (env != null && env.getIndexPolicyMgr() != null) {
+                IndexPolicy policy = 
env.getIndexPolicyMgr().getPolicyByName(name);
+                if (policy != null && policy.getType() == expectedType) {
+                    if (policy.isInvalid()) {
+                        return "invalid-policy:" + policy.getId() + ":" + 
policy.getName();
+                    }
+                    Map<String, String> props = policy.getProperties();
+                    if (props != null && !props.isEmpty()) {
+                        TreeMap<String, String> sortedProps = new 
TreeMap<>(props);
+                        String type = sortedProps.get(IndexPolicy.PROP_TYPE);
+                        String normalizedType = 
normalizeBuiltinComponentName(type, expectedType);
+                        if (normalizedType != null) {
+                            if ("empty".equals(normalizedType)) {
+                                return "";
+                            }
+                            sortedProps.put(IndexPolicy.PROP_TYPE, 
normalizedType);
+                            canonicalizeEffectiveComponentProperties(
+                                    sortedProps, normalizedType, expectedType);
+                            if (sortedProps.size() == 1) {
+                                return normalizedType;
+                            }
+                        }
+                        if (expectedType == IndexPolicyTypeEnum.TOKENIZER
+                                && 
"ngram".equals(sortedProps.get(IndexPolicy.PROP_TYPE))) {
+                            // This setting only limits policy creation; it 
does not change emitted tokens.
+                            sortedProps.remove(PROP_MAX_NGRAM_DIFF);
+                        }
+                        if (expectedType == IndexPolicyTypeEnum.CHAR_FILTER
+                                && 
"char_replace".equals(sortedProps.get(IndexPolicy.PROP_TYPE))) {
+                            String replacement = 
sortedProps.getOrDefault("replacement", " ");
+                            String pattern = canonicalizeCharReplacePattern(
+                                    sortedProps.get("pattern"), replacement, 
fold);
+                            if (pattern.isEmpty()) {
+                                return "";
+                            }
+                            sortedProps.put("pattern", pattern);
+                            sortedProps.put("replacement", replacement);
+                        }
+                        if (normalizedType != null && sortedProps.size() == 1) 
{
+                            return normalizedType;
+                        }
+                        return sortedProps.toString();
+                    }
+                }
+            }
+        } catch (RuntimeException e) {
+            // Fall through to built-in resolution or the original name.
+        }
+
+        String normalizedName = normalizeBuiltinComponentName(name, 
expectedType);
+        return "empty".equals(normalizedName) ? "" : normalizedName == null ? 
name : normalizedName;
+    }
+
+    private static void canonicalizeEffectiveComponentProperties(
+            TreeMap<String, String> properties, String type, 
IndexPolicyTypeEnum expectedType) {
+        if ("pinyin".equals(type)) {
+            removeBooleanDefaults(properties, true,
+                    "keep_first_letter", "keep_full_pinyin", 
"keep_none_chinese",
+                    "keep_none_chinese_together", 
"keep_none_chinese_in_first_letter",
+                    "lowercase", "trim_whitespace", "ignore_pinyin_offset",
+                    "none_chinese_pinyin_tokenize");
+            removeBooleanDefaults(properties, false,
+                    "keep_separate_first_letter", "keep_joined_full_pinyin", 
"keep_original",
+                    "keep_none_chinese_in_joined_full_pinyin", 
"remove_duplicated_term",
+                    "fixed_pinyin_offset", "keep_separate_chinese");
+            removeIntegerDefault(properties, "limit_first_letter_length", 16);
+            canonicalizePinyinDependencies(properties, expectedType);
+            return;
         }
 
-        // For custom component, get its properties
+        if (expectedType == IndexPolicyTypeEnum.TOKEN_FILTER) {
+            if ("asciifolding".equals(type)) {
+                removeBooleanDefaults(properties, false, "preserve_original");
+            } else if ("word_delimiter".equals(type)) {
+                removeBooleanDefaults(properties, true, "generate_word_parts", 
"generate_number_parts",
+                        "split_on_case_change", "split_on_numerics", 
"stem_english_possessive");
+                removeBooleanDefaults(properties, false, "catenate_words", 
"catenate_numbers",
+                        "catenate_all", "preserve_original");
+                canonicalizeWordSet(properties, "protected_words");
+                canonicalizeTypeTable(properties);
+            } else if ("icu_normalizer".equals(type)) {
+                canonicalizeIcuNormalizerDefaults(properties, false);
+            }
+            return;
+        }
+
+        if (expectedType == IndexPolicyTypeEnum.CHAR_FILTER) {
+            if ("icu_normalizer".equals(type)) {
+                canonicalizeIcuNormalizerDefaults(properties, true);
+            }
+            return;
+        }
+
+        if (expectedType != IndexPolicyTypeEnum.TOKENIZER) {
+            return;
+        }
+        switch (type) {
+            case "ngram":
+            case "edge_ngram":
+                removeIntegerDefault(properties, "min_gram", 1);
+                removeIntegerDefault(properties, "max_gram", 2);
+                canonicalizeWordSet(properties, "token_chars");
+                canonicalizeCustomTokenChars(properties);
+                break;
+            case "standard":
+                removeIntegerDefault(properties, "max_token_length", 255);
+                break;
+            case "char_group":
+                removeIntegerDefault(properties, "max_token_length", 255);
+                canonicalizeTokenizeOnChars(properties);
+                break;
+            case "keyword":
+                // BE only range-checks buffer_size; the emitted term is 
always capped by a constant.
+                properties.remove("buffer_size");
+                break;
+            case "basic":
+                canonicalizeBasicExtraChars(properties);
+                break;
+            default:
+                break;
+        }
+    }
+
+    private static void removeBooleanDefaults(
+            TreeMap<String, String> properties, boolean defaultValue, 
String... keys) {
+        for (String key : keys) {
+            String value = properties.get(key);
+            if (value == null || !("true".equalsIgnoreCase(value) || 
"false".equalsIgnoreCase(value))) {
+                continue;
+            }
+            boolean parsed = Boolean.parseBoolean(value);
+            if (parsed == defaultValue) {
+                properties.remove(key);
+            } else {
+                properties.put(key, Boolean.toString(parsed));
+            }
+        }
+    }
+
+    private static void removeIntegerDefault(
+            TreeMap<String, String> properties, String key, int defaultValue) {
+        String value = properties.get(key);
+        if (value == null) {
+            return;
+        }
         try {
-            Env env = Env.getCurrentEnv();
-            if (env == null || env.getIndexPolicyMgr() == null) {
-                return name;
+            int parsed = Integer.parseInt(value);
+            if (parsed == defaultValue) {
+                properties.remove(key);
+            } else {
+                properties.put(key, Integer.toString(parsed));
             }
+        } catch (NumberFormatException e) {
+            // Invalid policies keep their original identity.
+        }
+    }
 
-            IndexPolicy policy = env.getIndexPolicyMgr().getPolicyByName(name);
-            if (policy == null || policy.getType() != expectedType) {
-                return name;
+    private static void canonicalizeIcuNormalizerDefaults(
+            TreeMap<String, String> properties, boolean hasMode) {
+        String name = properties.get("name");
+        if (name != null) {
+            String normalizedName = name.trim().toLowerCase(Locale.ROOT);
+            if ("nfkc_cf".equals(normalizedName)) {
+                properties.remove("name");
+            } else {
+                properties.put("name", normalizedName);
             }
-            if (policy.isInvalid()) {
-                return "invalid-policy:" + policy.getId() + ":" + 
policy.getName();
+        }
+        String filter = properties.get("unicode_set_filter");
+        if (filter != null && filter.isEmpty()) {
+            // BE treats an explicit empty string like an absent filter.
+            properties.remove("unicode_set_filter");
+        } else if (filter != null) {
+            try {
+                UnicodeSet unicodeSet = new UnicodeSet(filter);
+                if (unicodeSet.isEmpty()) {
+                    properties.remove("unicode_set_filter");
+                } else {
+                    properties.put("unicode_set_filter", 
unicodeSet.toPattern(false));
+                }
+            } catch (IllegalArgumentException e) {
+                // Invalid policies keep their original identity.
             }
+        }
+        if (hasMode) {
+            canonicalizeIcuNormalizerMode(properties);
+        }
+    }
 
-            Map<String, String> props = policy.getProperties();
-            if (props == null || props.isEmpty()) {
-                return name;
+    private static void canonicalizeIcuNormalizerMode(TreeMap<String, String> 
properties) {
+        removeStringDefault(properties, "mode", "compose");
+        if (!"decompose".equals(properties.get("mode"))) {
+            return;
+        }
+        // BE ignores mode for nfd/nfkd, and nfc/nfkc in decompose mode are 
the same ICU instances.
+        String name = properties.get("name");
+        if ("nfc".equals(name) || "nfd".equals(name)) {
+            properties.put("name", "nfd");
+            properties.remove("mode");
+        } else if ("nfkc".equals(name) || "nfkd".equals(name)) {
+            properties.put("name", "nfkd");
+            properties.remove("mode");
+        }
+    }
+
+    // BE reads these settings as unordered sets of trimmed, non-empty words.
+    private static void canonicalizeWordSet(TreeMap<String, String> 
properties, String key) {
+        String value = properties.get(key);
+        if (value == null) {
+            return;
+        }
+        TreeSet<String> words = new TreeSet<>();
+        for (String word : value.split(",")) {
+            String trimmed = trimAsciiWhitespace(word);
+            if (!trimmed.isEmpty()) {
+                words.add(trimmed);
             }
+        }
+        if (words.isEmpty()) {
+            properties.remove(key);
+        } else {
+            properties.put(key, String.join(",", words));
+        }
+    }
+
+    // BE matches custom token characters as a code point set ORed with the 
named classes.
+    private static void canonicalizeCustomTokenChars(TreeMap<String, String> 
properties) {
+        String value = properties.get("custom_token_chars");
+        if (value == null) {
+            return;
+        }
+        String tokenChars = properties.getOrDefault("token_chars", "");
+        Set<String> classes = new TreeSet<>(List.of(tokenChars.split(",")));
+        StringBuilder canonical = new StringBuilder();
+        value.codePoints().distinct().sorted()
+                .filter(codePoint -> !isCoveredByAsciiClass(codePoint, 
classes))
+                .forEach(canonical::appendCodePoint);
+        if (canonical.length() == 0 && !value.isEmpty() && 
classes.remove("custom")) {
+            properties.remove("custom_token_chars");
+            properties.put("token_chars", String.join(",", classes));
+            return;
+        }
+        properties.put("custom_token_chars", canonical.toString());
+    }
 
-            // Build identity from sorted properties
-            TreeMap<String, String> sortedProps = new TreeMap<>(props);
-            if (expectedType == IndexPolicyTypeEnum.TOKENIZER
-                    && "ngram".equals(sortedProps.get(IndexPolicy.PROP_TYPE))) 
{
-                // This setting only limits policy creation; it does not 
change emitted tokens.
-                sortedProps.remove(PROP_MAX_NGRAM_DIFF);
+    // BE collects tokenize_on_chars entries into sets and checks the 
categories before the literals.
+    private static void canonicalizeTokenizeOnChars(TreeMap<String, String> 
properties) {
+        List<String> entries = 
parseEntryList(properties.get("tokenize_on_chars"));
+        if (entries == null) {
+            return;
+        }
+        TreeSet<String> canonical = new TreeSet<>(entries);
+        Set<String> classes = new TreeSet<>(canonical);
+        classes.retainAll(CHAR_GROUP_TYPES);
+        canonical.removeIf(entry -> entry.indexOf('\\') < 0
+                && entry.codePointCount(0, entry.length()) == 1
+                && isCoveredByAsciiClass(entry.codePointAt(0), classes));
+        putEntryList(properties, "tokenize_on_chars", canonical);
+    }
+
+    /**
+     * Whether a named character class of the ngram or char_group tokenizer 
already matches this
+     * code point. Only ASCII is judged: its categories never change between 
the ICU versions FE
+     * and BE link against, and the class predicates agree there.
+     */
+    private static boolean isCoveredByAsciiClass(int codePoint, Set<String> 
classes) {
+        if (codePoint >= 128) {
+            return false;
+        }
+        int type = UCharacter.getType(codePoint);
+        for (String name : classes) {
+            switch (name) {
+                case "letter":
+                    if (UCharacter.isLetter(codePoint)) {
+                        return true;
+                    }
+                    break;
+                case "digit":
+                    if (UCharacter.isDigit(codePoint)) {
+                        return true;
+                    }
+                    break;
+                case "whitespace":
+                    if (UCharacter.isWhitespace(codePoint)) {
+                        return true;
+                    }
+                    break;
+                case "punctuation":
+                    if (type == UCharacter.START_PUNCTUATION || type == 
UCharacter.END_PUNCTUATION
+                            || type == UCharacter.OTHER_PUNCTUATION || type == 
UCharacter.CONNECTOR_PUNCTUATION
+                            || type == UCharacter.DASH_PUNCTUATION || type == 
UCharacter.INITIAL_PUNCTUATION
+                            || type == UCharacter.FINAL_PUNCTUATION) {
+                        return true;
+                    }
+                    break;
+                case "symbol":
+                    if (type == UCharacter.CURRENCY_SYMBOL || type == 
UCharacter.MATH_SYMBOL
+                            || type == UCharacter.OTHER_SYMBOL || type == 
UCharacter.MODIFIER_SYMBOL) {
+                        return true;
+                    }
+                    break;
+                default:
+                    break;
             }
-            return sortedProps.toString();
-        } catch (RuntimeException e) {
-            return name;
         }
+        return false;
+    }
+
+    // BE builds a per-character type map where a later rule for the same 
character wins.
+    private static void canonicalizeTypeTable(TreeMap<String, String> 
properties) {
+        List<String> rules = parseEntryList(properties.get("type_table"));
+        if (rules == null) {
+            return;
+        }
+        TreeMap<Integer, String> types = new TreeMap<>();
+        for (String rule : rules) {
+            int arrow = rule.lastIndexOf("=>");
+            if (arrow < 0 || rule.indexOf('\n') >= 0 || rule.indexOf('\r') >= 
0) {
+                return;
+            }
+            String character = trimAsciiWhitespace(rule.substring(0, arrow));
+            String type = trimAsciiWhitespace(rule.substring(arrow + 2));
+            // Escaped characters keep the original identity rather than 
reproducing BE unescaping.
+            if (character.indexOf('\\') >= 0 || character.codePointCount(0, 
character.length()) != 1
+                    || !WORD_DELIMITER_TYPES.contains(type)) {
+                return;
+            }
+            types.put(character.codePointAt(0), type);
+        }
+        // A rule that restates BE's own classification changes nothing, but a 
table made only of
+        // such rules still replaces BE's default table, which classifies 
Latin-1 differently.
+        TreeMap<Integer, String> effectiveTypes = new TreeMap<>(types);
+        effectiveTypes.entrySet().removeIf(
+                entry -> 
entry.getValue().equals(defaultWordDelimiterType(entry.getKey())));
+        if (effectiveTypes.isEmpty()) {
+            effectiveTypes = types;
+        }
+        List<String> canonicalRules = new ArrayList<>();
+        for (Map.Entry<Integer, String> entry : effectiveTypes.entrySet()) {
+            canonicalRules.add(new String(Character.toChars(entry.getKey())) + 
"=>" + entry.getValue());
+        }
+        putEntryList(properties, "type_table", canonicalRules);
+    }
+
+    /** BE's u_charType classification of an ASCII code point, or null for 
anything else. */
+    private static String defaultWordDelimiterType(int codePoint) {
+        if (codePoint >= 128) {
+            return null;
+        }
+        switch (UCharacter.getType(codePoint)) {
+            case UCharacter.UPPERCASE_LETTER:
+                return "UPPER";
+            case UCharacter.LOWERCASE_LETTER:
+                return "LOWER";
+            case UCharacter.DECIMAL_DIGIT_NUMBER:
+                return "DIGIT";
+            default:
+                return "SUBWORD_DELIM";
+        }
+    }
+
+    /** Parse a bracketed entry list as BE does, or return null for a 
malformed list. */
+    private static List<String> parseEntryList(String value) {
+        if (value == null) {
+            return null;
+        }
+        List<String> entries = new ArrayList<>();
+        String trimmed = trimAsciiWhitespace(value);
+        if (trimmed.isEmpty()) {
+            return entries;
+        }
+        for (String item : ENTRY_SEPARATOR.split(trimmed)) {
+            String entry = trimAsciiWhitespace(item);
+            if (entry.length() < 2 || entry.charAt(0) != '[' || 
entry.charAt(entry.length() - 1) != ']') {
+                return null;
+            }
+            String content = entry.substring(1, entry.length() - 1);
+            if (!content.isEmpty()) {
+                entries.add(content);
+            }
+        }
+        return entries;
+    }
+
+    private static void putEntryList(TreeMap<String, String> properties, 
String key, Collection<String> entries) {
+        if (entries.isEmpty()) {
+            properties.remove(key);
+            return;
+        }
+        StringBuilder canonical = new StringBuilder();
+        for (String entry : entries) {
+            if (canonical.length() > 0) {
+                canonical.append(",");
+            }
+            canonical.append("[").append(entry).append("]");
+        }
+        properties.put(key, canonical.toString());
+    }
+
+    // Trim the same ASCII whitespace that BE trims.
+    private static String trimAsciiWhitespace(String value) {
+        int begin = 0;
+        int end = value.length();
+        while (begin < end && isAsciiWhitespace(value.charAt(begin))) {
+            ++begin;
+        }
+        while (end > begin && isAsciiWhitespace(value.charAt(end - 1))) {
+            --end;
+        }
+        return value.substring(begin, end);
+    }
+
+    private static boolean isAsciiWhitespace(char value) {
+        return value == ' ' || (value >= '\t' && value <= '\r');
+    }
+
+    // BE consumes an ASCII alphanumeric run before it consults extra_chars.
+    private static void canonicalizeBasicExtraChars(TreeMap<String, String> 
properties) {
+        String extraChars = properties.get("extra_chars");
+        if (extraChars == null) {
+            return;
+        }
+        boolean[] present = new boolean[128];
+        for (int i = 0; i < extraChars.length(); ++i) {
+            char value = extraChars.charAt(i);
+            if (value >= present.length) {
+                return;
+            }
+            boolean alphanumeric = (value >= '0' && value <= '9') || (value >= 
'A' && value <= 'Z')
+                    || (value >= 'a' && value <= 'z');
+            present[value] = !alphanumeric;
+        }
+        StringBuilder canonical = new StringBuilder();
+        for (int i = 0; i < present.length; ++i) {
+            if (present[i]) {
+                canonical.append((char) i);
+            }
+        }
+        if (canonical.length() == 0) {
+            properties.remove("extra_chars");
+        } else {
+            properties.put("extra_chars", canonical.toString());
+        }
+    }
+
+    private static void canonicalizePinyinDependencies(

Review Comment:
   [P1] Drop `lowercase` when the Pinyin tokenizer has no case-preserving 
output path. For `keep_first_letter=false`, `keep_none_chinese=false`, 
`keep_original=false`, default `keep_full_pinyin=true`, and joined output 
disabled, BE emits only Chinese full-pinyin from `TONELESS_PINYIN_FORMAT`, 
whose case is already fixed to lowercase. Thus `lowercase=true` and `false` 
tokenize identically, but this canonicalizer leaves different identities and 
admits duplicate indexes. Gate this removal to TOKENIZER policies and retain 
the property whenever original, ASCII, first-letter, or joined output can 
expose case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to