/*
 * SDK Pullenti Lingvo, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software.
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.ner.person.internal;

public class PersonNormalItem {

    public PersonNormalItemType typ = PersonNormalItemType.LAST;

    public int ind0;

    public int ind1;

    public java.util.ArrayList<PersonItemToken> src;

    public int gender;

    public int rnd0;

    public int rnd1;

    public int render;

    @Override
    public String toString() {
        if ((rnd0 < 0) || (rnd1 < rnd0) || src == null) 
            return "?";
        if (rnd0 == rnd1) 
            return (typ.toString() + ": " + src.get(rnd0).value);
        StringBuilder res = new StringBuilder();
        res.append(typ.toString()).append(": ");
        for (int i = rnd0; i <= rnd1; i++) {
            res.append((i > rnd0 ? "-" : "")).append(src.get(i).value);
        }
        return res.toString();
    }

    public String resultAlt;

    public String getResult() {
        resultAlt = null;
        if ((rnd0 < 0) || (rnd1 < rnd0) || src == null) 
            return null;
        if ((rnd0 + 1) == rnd1 && typ != PersonNormalItemType.LAST) {
            if (src.get(rnd0).value.length() == 1 && src.get(rnd1).value.length() > 1) {
                if (!src.get(rnd0).getEndToken().getNext().isHiphen()) 
                    return src.get(rnd1).value;
            }
            if (src.get(rnd0).value.length() > 1 && src.get(rnd1).value.length() == 1) 
                return src.get(rnd0).value;
        }
        String res = src.get(rnd0).value;
        for (int i = rnd0 + 1; i <= rnd1; i++) {
            if (typ == PersonNormalItemType.LAST && (rnd0 + 1) == rnd1 && res.length() == 1) {
                if (com.pullenti.unisharp.Utils.stringsEq(res, "Д") || com.pullenti.unisharp.Utils.stringsEq(res, "О")) {
                    res = (res + "'" + src.get(i).value);
                    continue;
                }
                if (!com.pullenti.morph.LanguageHelper.isCyrillicVowel(res.charAt(0))) {
                    res = (res + src.get(i).value);
                    continue;
                }
            }
            if (i == rnd1 && !src.get(i).getEndToken().getPrevious().isHiphen()) {
                int ii;
                String v = src.get(i).value;
                for (ii = 1; ii < v.length(); ii++) {
                    if (v.charAt(ii) != v.charAt(0)) 
                        break;
                }
                if (ii >= v.length()) 
                    continue;
            }
            res = (res + "-" + src.get(i).value);
        }
        if (typ == PersonNormalItemType.FIRST && rnd0 == rnd1) {
            PersonItemToken pit = src.get(rnd0);
            if (pit.firstname != null && pit.firstname.isInDictionary && pit.firstname.vars.size() > 0) {
                for (PersonItemToken.MorphPersonItemVariant v : pit.firstname.vars) {
                    if (v.getGender() == com.pullenti.morph.MorphGender.MASCULINE && render == 1) {
                        boolean exi = false;
                        for (PersonItemToken.MorphPersonItemVariant vv : pit.firstname.vars) {
                            if (vv.getGender() == com.pullenti.morph.MorphGender.MASCULINE && render == 1 && com.pullenti.unisharp.Utils.stringsEq(vv.value, pit.value)) 
                                exi = true;
                        }
                        if ((!exi && com.pullenti.unisharp.Utils.stringsNe(v.value, pit.value) && com.pullenti.unisharp.Utils.stringsNe(pit.value, "ВЛАД")) && com.pullenti.unisharp.Utils.stringsNe(pit.value, "АЛЕКС")) {
                            resultAlt = pit.value;
                            return v.value;
                        }
                    }
                    else if (v.getGender() == com.pullenti.morph.MorphGender.FEMINIE && render == 2) {
                        boolean exi = false;
                        for (PersonItemToken.MorphPersonItemVariant vv : pit.firstname.vars) {
                            if (vv.getGender() == com.pullenti.morph.MorphGender.FEMINIE && render == 2 && com.pullenti.unisharp.Utils.stringsEq(vv.value, pit.value)) 
                                exi = true;
                        }
                        if ((!exi && com.pullenti.unisharp.Utils.stringsNe(v.value, pit.value) && com.pullenti.unisharp.Utils.stringsNe(pit.value, "КАТЕРИНА")) && com.pullenti.unisharp.Utils.stringsNe(pit.value, "МАРЬЯ") && com.pullenti.unisharp.Utils.stringsNe(pit.value, "МАРИЯ")) {
                            resultAlt = pit.value;
                            if (com.pullenti.unisharp.Utils.stringsEq(pit.value, "ЛЕНА")) 
                                return "ЕЛЕНА";
                            return v.value;
                        }
                    }
                }
            }
        }
        if ((typ == PersonNormalItemType.LAST && rnd0 == rnd1 && ((res.charAt(0) == 'О' || res.charAt(0) == 'Д'))) && res.length() > 3) {
            PersonItemToken pit = src.get(rnd0);
            String txt = pit.getSourceText();
            for (int i = 0; (i < txt.length()) && (i < 3); i++) {
                if (com.pullenti.ner.core.BracketHelper.isBracketChar(txt.charAt(i), false)) {
                    res = (String.valueOf(res.charAt(0)) + "'" + res.substring(1));
                    break;
                }
            }
        }
        return res;
    }


    public void init(java.util.ArrayList<PersonItemToken> _src) {
        ind0 = 0;
        ind1 = -1;
        rnd0 = (rnd1 = -1);
        src = _src;
        render = 0;
        gender = 0;
    }

    public void fix() {
        rnd0 = ind0;
        rnd1 = ind1;
        render = gender;
    }

    public double calcCoef() {
        if (ind1 < ind0) {
            if (typ == PersonNormalItemType.MIDDLE) 
                return 1.0;
            return 0.0;
        }
        if (ind1 > ind0) {
            for (int i = ind0; i < ind1; i++) {
                if (src.get(i).getWhitespacesAfterCount() >= 4) 
                    return 0.0;
            }
        }
        if (ind0 > 0 && src.get(ind0).getBeginToken().getPrevious().isHiphen()) {
            if (src.get(ind0).getWhitespacesBeforeCount() < 4) 
                return 0.0;
        }
        if (((ind1 + 1) < src.size()) && src.get(ind1).getEndToken().getNext().isHiphen()) {
            if (src.get(ind1).getEndToken().getNext().getWhitespacesAfterCount() < 4) 
                return 0.0;
        }
        double co = 1.0;
        for (int i = ind0; i <= ind1; i++) {
            PersonItemToken pit = src.get(i);
            if (typ == PersonNormalItemType.FIRST) {
                if (ind0 == ind1 && pit.value.length() == 1) {
                }
                else if (pit.firstname != null) {
                    if (gender == 2 && pit.firstname.getMorph().getGender() == com.pullenti.morph.MorphGender.FEMINIE) {
                    }
                    else if (gender == 1 && pit.firstname.getMorph().getGender() == com.pullenti.morph.MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.98;
                }
                else if (pit.middlename != null) 
                    co *= 0.8;
                else if (pit.lastname != null && ((pit.lastname.isInDictionary || pit.lastname.isLastnameHasStdTail))) 
                    co *= 0.8;
                else if (pit.lastname != null && pit.lastname.isLastnameHasStdTail) 
                    co *= 0.9;
                else if (gender == 2 && pit.getMorph().getGender() == com.pullenti.morph.MorphGender.FEMINIE) 
                    co *= 0.98;
                else if (gender == 1 && pit.getMorph().getGender() == com.pullenti.morph.MorphGender.MASCULINE) 
                    co *= 0.98;
                else 
                    co *= 0.7;
            }
            else if (typ == PersonNormalItemType.MIDDLE) {
                if (ind0 == ind1 && pit.value.length() == 1) {
                }
                else if (pit.middlename != null) {
                    if (gender == 2 && pit.middlename.getMorph().getGender() == com.pullenti.morph.MorphGender.FEMINIE) {
                    }
                    else if (gender == 1 && pit.middlename.getMorph().getGender() == com.pullenti.morph.MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.7;
                }
                else if (((pit.value.endsWith("ВНА") || pit.value.endsWith("ЧНА") || pit.value.endsWith("КЫЗЫ"))) && gender == 2) {
                }
                else if (((pit.value.endsWith("ИЧ") || pit.value.endsWith("ОГЛЫ"))) && gender == 1) 
                    co *= 0.99;
                else if (pit.firstname != null) 
                    co *= 0.98;
                else if (((gender == 2 && pit.value.endsWith("ВА") && ind0 == ind1) && ind0 == (src.size() - 1) && src.get(0).lastname != null) && src.get(0).value.length() > 2) 
                    co *= 0.99;
                else if (pit.lastname != null && pit.lastname.isInDictionary) 
                    co *= 0.8;
                else 
                    co *= 0.7;
            }
            else if (typ == PersonNormalItemType.LAST) {
                if (pit.value.endsWith("КЫЗЫ")) 
                    return 0.0;
                if (ind0 == ind1 && pit.value.length() == 1) {
                    if (com.pullenti.morph.LanguageHelper.isCyrillicVowel(pit.value.charAt(0))) 
                        co *= 0.5;
                    else 
                        return 0.0;
                }
                if (pit.value.endsWith("ВНА") || pit.value.endsWith("ЧНА")) {
                    if (gender == 2) {
                        if (ind0 == 0) 
                            co *= 0.7;
                        else 
                            co *= 0.2;
                    }
                    else 
                        co *= 0.3;
                }
                else if ((gender == 2 && pit.value.endsWith("ВА") && ind0 == ind1) && ind0 == 2 && src.get(0).lastname != null) 
                    co *= 0.3;
                else if (pit.value.endsWith("ИЧ")) {
                    if ((i + 1) >= src.size()) 
                        co *= 0.3;
                    else if (ind0 == 0 || gender == 2) {
                    }
                    else 
                        co *= 0.85;
                }
                else if (pit.value.endsWith("ОГЛЫ")) 
                    co *= 0.3;
                else if (pit.lastname != null && ((pit.lastname.isInDictionary || pit.lastname.isLastnameHasStdTail))) {
                    if (gender == 2 && pit.lastname.getMorph().getGender() == com.pullenti.morph.MorphGender.FEMINIE) {
                    }
                    else if (gender == 1 && pit.lastname.getMorph().getGender() == com.pullenti.morph.MorphGender.MASCULINE) {
                    }
                    else 
                        co *= 0.98;
                }
                else if (pit.firstname != null || pit.middlename != null) 
                    co *= 0.8;
                else 
                    co *= 0.98;
            }
        }
        if ((typ == PersonNormalItemType.LAST && ind0 == 0 && ind1 > (ind0 + 1)) && (co < 0.6)) 
            co *= 1.5;
        return co;
    }

    public static PersonNormalItem _new3450(PersonNormalItemType _arg1) {
        PersonNormalItem res = new PersonNormalItem();
        res.typ = _arg1;
        return res;
    }
    public PersonNormalItem() {
    }
}
