aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/document/NormalizeLevel.java
blob: bc8e4ac41f2136b117a02f660f6ed64a55b7f8d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.document;

/**
 * class representing the character normalization
 * we want to do on query and indexed text.
 * Levels are strict subsets, so doing accent
 * removal means doing codepoint normalizing
 * and case normalizing also.
 */
// TODO: Missing author
public class NormalizeLevel {

    /**
     * The current levels are as follows:
     * NONE: no changes to input text
     * CODEPOINT: convert text into Unicode
     * Normalization Form Compatibility Composition
     * LOWERCASE: also convert text into lowercase letters
     * ACCENT: do both above and remove accents on characters
     */
    public enum Level {
        NONE, CODEPOINT, LOWERCASE, ACCENT
    }

    private boolean userSpecified = false;
    private Level level = Level.ACCENT;

    /**
     * Returns whether accents should be removed from text
     */
    public boolean doRemoveAccents() { return level == Level.ACCENT; }

    /**
     * Construct a default (full) normalizelevel,
     */
    public NormalizeLevel() {}

    /**
     * Construct for a specific level, possibly user specified
     *
     * @param level which level to use
     * @param fromUser whether this was specified by the user
     */
    public NormalizeLevel(Level level, boolean fromUser) {
        this.level = level;
        this.userSpecified = fromUser;
    }

    /**
     * Change the current level to CODEPOINT as inferred
     * by other features' needs.  If the current level
     * was user specified it will not change; also this
     * will not increase the level.
     */
    public void inferCodepoint() {
        if (userSpecified) {
            // ignore inferred changes if user specified something
            return;
        }
        // do not increase level
        if (level != Level.NONE) level = Level.CODEPOINT;
    }

    /**
     * Change the current level to LOWERCASE as inferred
     * by other features' needs.  If the current level
     * was user specified it will not change; also this
     * will not increase the level.
     */
    public void inferLowercase() {
        if (userSpecified) {
            // ignore inferred changes if user specified something
            return;
        }
        // do not increase level
        if (level == Level.NONE) return;
        if (level == Level.CODEPOINT) return;

        level = Level.LOWERCASE;
    }

    public Level getLevel() {
        return level;
    }

}