/*
 * 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 PersonNormalNode {

    public PersonNormalNode(boolean fioLast) {
        if (!fioLast) 
            items.add(PersonNormalItem._new3450(PersonNormalItemType.LAST));
        items.add(PersonNormalItem._new3450(PersonNormalItemType.FIRST));
        items.add(PersonNormalItem._new3450(PersonNormalItemType.MIDDLE));
        if (fioLast) 
            items.add(PersonNormalItem._new3450(PersonNormalItemType.LAST));
    }

    public java.util.ArrayList<PersonNormalItem> items = new java.util.ArrayList<PersonNormalItem>();

    private static boolean isAsian(String str) {
        int gl = 0;
        for (char ch : str.toCharArray()) {
            if (com.pullenti.morph.LanguageHelper.isCyrillicVowel(ch)) 
                gl++;
        }
        if (gl == 1) 
            return true;
        return false;
    }

    public double process(java.util.ArrayList<PersonItemToken> src) {
        int i;
        for (i = 0; i < items.size(); i++) {
            items.get(i).init(src);
        }
        if ((src.size() == 3 && isAsian(src.get(0).value) && isAsian(src.get(1).value)) && isAsian(src.get(2).value)) {
            if (items.get(0).typ == PersonNormalItemType.FIRST) 
                return 0.0;
            for (i = 0; i < 3; i++) {
                items.get(i).rnd0 = (items.get(i).rnd1 = i);
            }
            return 1.0;
        }
        double bestCoef = 0.0;
        for (int fem = 0; fem < 2; fem++) {
            for (int i0 = 0; i0 < (src.size() - 2); i0++) {
                items.get(0).ind0 = 0;
                items.get(0).ind1 = i0;
                items.get(0).gender = fem + 1;
                double co = items.get(0).calcCoef();
                if (co <= bestCoef) 
                    continue;
                for (int i1 = i0 + 1; i1 < (src.size() - 1); i1++) {
                    items.get(1).ind0 = i0 + 1;
                    items.get(1).ind1 = i1;
                    items.get(1).gender = fem + 1;
                    double co1 = items.get(1).calcCoef();
                    if ((co * co1) <= bestCoef) 
                        continue;
                    for (int i2 = i1 + 1; i2 < src.size(); i2++) {
                        items.get(2).ind0 = i1 + 1;
                        items.get(2).ind1 = i2;
                        items.get(2).gender = fem + 1;
                        double co2 = items.get(2).calcCoef();
                        co2 *= (co1 * co);
                        for (i = i2 + 1; i < src.size(); i++) {
                            if (src.get(i).chars.isAllLower()) 
                                co2 *= 0.9;
                            else 
                                co2 *= 0.3;
                        }
                        if (co2 > bestCoef) {
                            bestCoef = co2;
                            for (i = 0; i < items.size(); i++) {
                                items.get(i).fix();
                            }
                        }
                    }
                }
            }
        }
        if (bestCoef == 0 && src.size() == 2) {
            for (i = 0; i < items.size(); i++) {
                items.get(i).init(src);
            }
            for (int fem = 0; fem < 2; fem++) {
                for (int i0 = 0; i0 < (src.size() - 1); i0++) {
                    items.get(0).ind0 = 0;
                    items.get(0).ind1 = i0;
                    items.get(0).gender = fem + 1;
                    double co = items.get(0).calcCoef();
                    if (co <= bestCoef) 
                        continue;
                    int k = 1;
                    if (items.get(k).typ == PersonNormalItemType.MIDDLE) 
                        k++;
                    for (int i1 = i0 + 1; i1 < src.size(); i1++) {
                        items.get(k).ind0 = i0 + 1;
                        items.get(k).ind1 = i1;
                        items.get(k).gender = fem + 1;
                        double co1 = items.get(k).calcCoef();
                        if ((co * co1) <= bestCoef) 
                            continue;
                        bestCoef = co * co1;
                        for (i = 0; i < items.size(); i++) {
                            items.get(i).fix();
                        }
                    }
                }
            }
        }
        return bestCoef;
    }

    public void createResult(com.pullenti.ner.person.PersonNormalData res) {
        res.gender = items.get(0).render;
        for (PersonNormalItem it : items) {
            if (it.typ == PersonNormalItemType.FIRST) {
                res.firstname = it.getResult();
                res.firstnameAlt = it.resultAlt;
            }
            else if (it.typ == PersonNormalItemType.MIDDLE) 
                res.middlename = it.getResult();
            else if (it.typ == PersonNormalItemType.LAST) 
                res.lastname = it.getResult();
        }
    }
    public PersonNormalNode() {
    }
}
