# 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

import typing

from pullenti.morph.LanguageHelper import LanguageHelper
from pullenti.ner.person.internal.PersonNormalItemType import PersonNormalItemType
from pullenti.ner.person.internal.PersonNormalItem import PersonNormalItem

class PersonNormalNode:
    
    def __init__(self, fio_last : bool) -> None:
        self.items = list()
        if (not fio_last): 
            self.items.append(PersonNormalItem._new3450(PersonNormalItemType.LAST))
        self.items.append(PersonNormalItem._new3450(PersonNormalItemType.FIRST))
        self.items.append(PersonNormalItem._new3450(PersonNormalItemType.MIDDLE))
        if (fio_last): 
            self.items.append(PersonNormalItem._new3450(PersonNormalItemType.LAST))
    
    @staticmethod
    def __is_asian(str0_ : str) -> bool:
        gl = 0
        for ch in str0_: 
            if (LanguageHelper.is_cyrillic_vowel(ch)): 
                gl += 1
        if (gl == 1): 
            return True
        return False
    
    def process(self, src : typing.List['PersonItemToken']) -> float:
        i = 0
        i = 0
        while i < len(self.items): 
            self.items[i].init(src)
            i += 1
        if ((len(src) == 3 and PersonNormalNode.__is_asian(src[0].value) and PersonNormalNode.__is_asian(src[1].value)) and PersonNormalNode.__is_asian(src[2].value)): 
            if (self.items[0].typ == PersonNormalItemType.FIRST): 
                return 0
            for i in range(3):
                self.items[i].rnd1 = i
                self.items[i].rnd0 = self.items[i].rnd1
            else: i = 3
            return 1
        best_coef = 0
        for fem in range(2):
            i0 = 0
            first_pass4403 = True
            while True:
                if first_pass4403: first_pass4403 = False
                else: i0 += 1
                if (not (i0 < (len(src) - 2))): break
                self.items[0].ind0 = 0
                self.items[0].ind1 = i0
                self.items[0].gender = (fem + 1)
                co = self.items[0].calc_coef()
                if (co <= best_coef): 
                    continue
                i1 = i0 + 1
                first_pass4404 = True
                while True:
                    if first_pass4404: first_pass4404 = False
                    else: i1 += 1
                    if (not (i1 < (len(src) - 1))): break
                    self.items[1].ind0 = (i0 + 1)
                    self.items[1].ind1 = i1
                    self.items[1].gender = (fem + 1)
                    co1 = self.items[1].calc_coef()
                    if ((co * co1) <= best_coef): 
                        continue
                    i2 = i1 + 1
                    while i2 < len(src): 
                        self.items[2].ind0 = (i1 + 1)
                        self.items[2].ind1 = i2
                        self.items[2].gender = (fem + 1)
                        co2 = self.items[2].calc_coef()
                        co2 *= (co1 * co)
                        i = (i2 + 1)
                        while i < len(src): 
                            if (src[i].chars.is_all_lower): 
                                co2 *= 0.9
                            else: 
                                co2 *= 0.3
                            i += 1
                        if (co2 > best_coef): 
                            best_coef = co2
                            i = 0
                            while i < len(self.items): 
                                self.items[i].fix()
                                i += 1
                        i2 += 1
        if (best_coef == 0 and len(src) == 2): 
            i = 0
            while i < len(self.items): 
                self.items[i].init(src)
                i += 1
            for fem in range(2):
                i0 = 0
                first_pass4405 = True
                while True:
                    if first_pass4405: first_pass4405 = False
                    else: i0 += 1
                    if (not (i0 < (len(src) - 1))): break
                    self.items[0].ind0 = 0
                    self.items[0].ind1 = i0
                    self.items[0].gender = (fem + 1)
                    co = self.items[0].calc_coef()
                    if (co <= best_coef): 
                        continue
                    k = 1
                    if (self.items[k].typ == PersonNormalItemType.MIDDLE): 
                        k += 1
                    i1 = i0 + 1
                    first_pass4406 = True
                    while True:
                        if first_pass4406: first_pass4406 = False
                        else: i1 += 1
                        if (not (i1 < len(src))): break
                        self.items[k].ind0 = (i0 + 1)
                        self.items[k].ind1 = i1
                        self.items[k].gender = (fem + 1)
                        co1 = self.items[k].calc_coef()
                        if ((co * co1) <= best_coef): 
                            continue
                        best_coef = (co * co1)
                        i = 0
                        while i < len(self.items): 
                            self.items[i].fix()
                            i += 1
        return best_coef
    
    def create_result(self, res : 'PersonNormalData') -> None:
        res.gender = self.items[0].render
        for it in self.items: 
            if (it.typ == PersonNormalItemType.FIRST): 
                res.firstname = it.result
                res.firstname_alt = it.result_alt
            elif (it.typ == PersonNormalItemType.MIDDLE): 
                res.middlename = it.result
            elif (it.typ == PersonNormalItemType.LAST): 
                res.lastname = it.result