pluralizeRule.ts 3.4 KB
Newer Older
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * 复数变化规则
 * 
 * 
 */
export class PluralizeRule{

    /**
     * 不规则
     *  
     * @protected
     * @type {string[]}
     * @memberof PluralizeRule
     */
    protected irregular: Map<string, string> = new Map();

    /**
     * 不可数
     *  
     * @protected
     * @type {string[]}
     * @memberof PluralizeRule
     */
     protected uncountable: string[] = [
        "equipment", "information", "rice", "money", "species", "series", "fish", "sheep", "people", "men",
        "children", "sexes", "moves", "stadiums", "oxen", "octopi", "viri", "aliases", "quizzes",
    ];

    /**
     * 初始化pluralizeRule对象
     * 
     * @param opts 额外参数
     * @memberof PluralizeRule
     */
    public constructor(opts: any = {}) {
        this.initIrregular();
    }

    /**
     * 初始化不规则变化
     * 
     * @param opts 额外参数
     * @memberof PluralizeRule
     */
    protected initIrregular(){
        this.irregular.set("person", "people");
        this.irregular.set("man", "men");
        this.irregular.set("child", "children");
        this.irregular.set("sex", "sexes");
        this.irregular.set("move", "moves");
        this.irregular.set("stadium", "stadiums");
    }

    /**
     * 是否为不可数
     * 
     * @param word 单词
     * @returns 返回判断
     * @memberof PluralizeRule
     */
    public isUncountable(word: string){
        const index: number = this.uncountable.findIndex((wordStr: string) =>{
            return Object.is(word, wordStr);
        })
        if (index == -1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 不规则变化
     * 
     * @param word 单词
     * @returns 返回变化值
     * @memberof PluralizeRule
     */
    public irregularChange(word: string){
        for(let item of this.irregular.entries()){
            if(word && word.endsWith(item[0])){
                return word.replace(new RegExp(item[0],'g'),item[1]);
            }
        }
        return this.irregular.get(word);
    }

    /**
     * 规则变化
     * 
     * @param word 单词
     * @returns 返回变化值
     * @memberof PluralizeRule
     */
    public ruleChange(word: string){
        if(/(ax|test)is$/.test(word)) return word.replace(/is$/,"es");
        if(/(octop|vir)us$/.test(word)) return word.replace(/us$/,"i");
        if(/(octop|vir)i$/.test(word)) return word;
        if(/(alias|status)$/.test(word)) return word+"es";
        if(/(bu)s$/.test(word)) return word.replace(/s$/,"ses");
        if(/(buffal|tomat)o$/.test(word)) return word.replace(/o$/,"oes");
        if(/([ti])um$/.test(word)) return word.replace(/um$/,"a");
        if(/([ti])a$/.test(word)) return word;
        if(/sis$/.test(word)) return word.replace(/sis$/,"ses");
        if(/(?:([^f])fe|([lr])f)$/.test(word)) return word.substring(0, word.length - 1)+"ves";
        if(/(hive)$/.test(word)) return word+"s";
        if(/([^aeiouy]|qu)y$/.test(word)) return word.replace(/y$/,"ies");
        if(/(x|ch|ss|sh)$/.test(word)) return word+"es";
        if(/(matr|vert|ind)ix|ex$/.test(word)) return word.replace(/ix|ex$/,"ices");
        if(/([m|l])ouse$/.test(word)) return word.replace(/ouse$/,"ice");
        if(/([m|l])ice$/.test(word)) return word;
        if(/^(ox)$$/.test(word)) return word+"en";
        if(/(quiz)$/.test(word)) return word+"zes";
        if(/s$/.test(word)) return word.replace(/s$/,"s");
        return word+"s";
    }
}