Esempio n. 1
0
 def __attach_spec_postfix(t: 'Token') -> 'NumberExToken':
     if (t is None):
         return None
     if (t.is_char_of("%")):
         return NumberExToken(t, t, "", NumberSpellingType.DIGIT,
                              NumberExType.PERCENT)
     money = NumberHelper._is_money_char(t)
     if (money is not None):
         return NumberExToken._new415(t, t, "", NumberSpellingType.DIGIT,
                                      NumberExType.MONEY, money)
     return None
Esempio n. 2
0
 def try_parse_number_with_postfix(t: 'Token') -> 'NumberExToken':
     if (t is None):
         return None
     t0 = t
     is_dollar = None
     if (t.length_char == 1 and t.next0_ is not None):
         is_dollar = NumberHelper._is_money_char(t)
         if ((is_dollar) is not None):
             t = t.next0_
     nt = Utils.asObjectOrNull(t, NumberToken)
     if (nt is None):
         if ((not (isinstance(t.previous, NumberToken)) and t.is_char('(')
              and (isinstance(t.next0_, NumberToken)))
                 and t.next0_.next0_ is not None
                 and t.next0_.next0_.is_char(')')):
             toks1 = NumberExHelper._m_postfixes.try_parse(
                 t.next0_.next0_.next0_, TerminParseAttr.NO)
             if (toks1 is not None
                     and (Utils.valToEnum(toks1.termin.tag, NumberExType))
                     == NumberExType.MONEY):
                 nt0 = Utils.asObjectOrNull(t.next0_, NumberToken)
                 res = NumberExToken._new405(t, toks1.end_token, nt0.value,
                                             nt0.typ, NumberExType.MONEY,
                                             nt0.real_value,
                                             toks1.begin_token.morph)
                 return NumberExHelper.__correct_money(
                     res, toks1.begin_token)
         tt = Utils.asObjectOrNull(t, TextToken)
         if (tt is None or not tt.morph.class0_.is_adjective):
             return None
         val = tt.term
         i = 4
         first_pass3038 = True
         while True:
             if first_pass3038: first_pass3038 = False
             else: i += 1
             if (not (i < (len(val) - 5))): break
             v = val[0:0 + i]
             li = NumberHelper._m_nums.find_termins_by_string(
                 v, tt.morph.language)
             if (li is None):
                 continue
             vv = val[i:]
             lii = NumberExHelper._m_postfixes.find_termins_by_string(
                 vv, tt.morph.language)
             if (lii is not None and len(lii) > 0):
                 re = NumberExToken._new406(
                     t, t, str(li[0].tag), NumberSpellingType.WORDS,
                     Utils.valToEnum(lii[0].tag, NumberExType), t.morph)
                 NumberExHelper.__correct_ext_types(re)
                 return re
             break
         return None
     if (t.next0_ is None and is_dollar is None):
         return None
     f = nt.real_value
     if (math.isnan(f)):
         return None
     t1 = nt.next0_
     if (((t1 is not None and t1.is_char_of(",.")))
             or (((isinstance(t1, NumberToken)) and
                  (t1.whitespaces_before_count < 3)))):
         d = 0
         tt11 = NumberHelper.try_parse_real_number(nt, False, False)
         if (tt11 is not None):
             t1 = tt11.end_token.next0_
             f = tt11.real_value
     if (t1 is None):
         if (is_dollar is None):
             return None
     elif ((t1.next0_ is not None and t1.next0_.is_value("С", "З")
            and t1.next0_.next0_ is not None)
           and t1.next0_.next0_.is_value("ПОЛОВИНА", None)):
         f += 0.5
         t1 = t1.next0_.next0_
     if (t1 is not None and t1.is_hiphen and t1.next0_ is not None):
         t1 = t1.next0_
     det = False
     altf = f
     if (((isinstance(t1, NumberToken)) and t1.previous is not None
          and t1.previous.is_hiphen) and t1.int_value == 0
             and t1.length_char == 2):
         t1 = t1.next0_
     if ((t1 is not None and t1.next0_ is not None and t1.is_char('('))
             and (((isinstance(t1.next0_, NumberToken))
                   or t1.next0_.is_value("НОЛЬ", None)))
             and t1.next0_.next0_ is not None):
         nt1 = Utils.asObjectOrNull(t1.next0_, NumberToken)
         val = 0
         if (nt1 is not None):
             val = nt1.real_value
         if (math.floor(f) == math.floor(val)):
             ttt = t1.next0_.next0_
             if (ttt.is_char(')')):
                 t1 = ttt.next0_
                 det = True
                 if ((isinstance(t1, NumberToken))
                         and t1.int_value is not None
                         and t1.int_value == 0):
                     t1 = t1.next0_
             elif (((((isinstance(ttt, NumberToken)) and
                      (ttt.real_value < 100) and ttt.next0_ is not None)
                     and ttt.next0_.is_char('/')
                     and ttt.next0_.next0_ is not None)
                    and ttt.next0_.next0_.get_source_text() == "100"
                    and ttt.next0_.next0_.next0_ is not None)
                   and ttt.next0_.next0_.next0_.is_char(')')):
                 rest = NumberExHelper.__get_decimal_rest100(f)
                 if (ttt.int_value is not None and rest == ttt.int_value):
                     t1 = ttt.next0_.next0_.next0_.next0_
                     det = True
             elif ((ttt.is_value("ЦЕЛЫХ", None) and
                    (isinstance(ttt.next0_, NumberToken))
                    and ttt.next0_.next0_ is not None)
                   and ttt.next0_.next0_.next0_ is not None
                   and ttt.next0_.next0_.next0_.is_char(')')):
                 num2 = Utils.asObjectOrNull(ttt.next0_, NumberToken)
                 altf = num2.real_value
                 if (ttt.next0_.next0_.is_value("ДЕСЯТЫЙ", None)):
                     altf /= (10)
                 elif (ttt.next0_.next0_.is_value("СОТЫЙ", None)):
                     altf /= (100)
                 elif (ttt.next0_.next0_.is_value("ТЫСЯЧНЫЙ", None)):
                     altf /= (1000)
                 elif (ttt.next0_.next0_.is_value("ДЕСЯТИТЫСЯЧНЫЙ", None)):
                     altf /= (10000)
                 elif (ttt.next0_.next0_.is_value("СТОТЫСЯЧНЫЙ", None)):
                     altf /= (100000)
                 elif (ttt.next0_.next0_.is_value("МИЛЛИОННЫЙ", None)):
                     altf /= (1000000)
                 if (altf < 1):
                     altf += val
                     t1 = ttt.next0_.next0_.next0_.next0_
                     det = True
             else:
                 toks1 = NumberExHelper._m_postfixes.try_parse(
                     ttt, TerminParseAttr.NO)
                 if (toks1 is not None):
                     if ((Utils.valToEnum(
                             toks1.termin.tag,
                             NumberExType)) == NumberExType.MONEY):
                         if (toks1.end_token.next0_ is not None
                                 and toks1.end_token.next0_.is_char(')')):
                             res = NumberExToken._new407(
                                 t, toks1.end_token.next0_, nt.value,
                                 nt.typ, NumberExType.MONEY, f, altf,
                                 toks1.begin_token.morph)
                             return NumberExHelper.__correct_money(
                                 res, toks1.begin_token)
                 res2 = NumberExHelper.try_parse_number_with_postfix(
                     t1.next0_)
                 if (res2 is not None and res2.end_token.next0_ is not None
                         and res2.end_token.next0_.is_char(')')):
                     res2.begin_token = t
                     res2.end_token = res2.end_token.next0_
                     res2.alt_real_value = res2.real_value
                     res2.real_value = f
                     NumberExHelper.__correct_ext_types(res2)
                     if (res2.whitespaces_after_count < 2):
                         toks2 = NumberExHelper._m_postfixes.try_parse(
                             res2.end_token.next0_, TerminParseAttr.NO)
                         if (toks2 is not None):
                             if ((Utils.valToEnum(
                                     toks2.termin.tag,
                                     NumberExType)) == NumberExType.MONEY):
                                 res2.end_token = toks2.end_token
                     return res2
         elif (nt1 is not None and nt1.typ == NumberSpellingType.WORDS
               and nt.typ == NumberSpellingType.DIGIT):
             altf = nt1.real_value
             ttt = t1.next0_.next0_
             if (ttt.is_char(')')):
                 t1 = ttt.next0_
                 det = True
             if (not det):
                 altf = f
     if ((t1 is not None and t1.is_char('(') and t1.next0_ is not None)
             and t1.next0_.is_value("СУММА", None)):
         br = BracketHelper.try_parse(t1, BracketParseAttr.NO, 100)
         if (br is not None):
             t1 = br.end_token.next0_
     if (is_dollar is not None):
         te = None
         if (t1 is not None):
             te = t1.previous
         else:
             t1 = t0
             while t1 is not None:
                 if (t1.next0_ is None):
                     te = t1
                 t1 = t1.next0_
         if (te is None):
             return None
         if (te.is_hiphen and te.next0_ is not None):
             if (te.next0_.is_value("МИЛЛИОННЫЙ", None)):
                 f *= (1000000)
                 altf *= (1000000)
                 te = te.next0_
             elif (te.next0_.is_value("МИЛЛИАРДНЫЙ", None)):
                 f *= (1000000000)
                 altf *= (1000000000)
                 te = te.next0_
         if (not te.is_whitespace_after
                 and (isinstance(te.next0_, TextToken))):
             if (te.next0_.is_value("M", None)):
                 f *= (1000000)
                 altf *= (1000000)
                 te = te.next0_
             elif (te.next0_.is_value("BN", None)):
                 f *= (1000000000)
                 altf *= (1000000000)
                 te = te.next0_
         return NumberExToken._new408(t0, te, "", nt.typ,
                                      NumberExType.MONEY, f, altf,
                                      is_dollar)
     if (t1 is None or ((t1.is_newline_before and not det))):
         return None
     toks = NumberExHelper._m_postfixes.try_parse(t1, TerminParseAttr.NO)
     if ((toks is None and det and (isinstance(t1, NumberToken)))
             and t1.value == "0"):
         toks = NumberExHelper._m_postfixes.try_parse(
             t1.next0_, TerminParseAttr.NO)
     if (toks is None and t1.is_char('р')):
         cou = 10
         ttt = t0.previous
         first_pass3039 = True
         while True:
             if first_pass3039: first_pass3039 = False
             else:
                 ttt = ttt.previous
                 cou -= 1
             if (not (ttt is not None and cou > 0)): break
             if (ttt.is_value("СУММА", None)
                     or ttt.is_value("НАЛИЧНЫЙ", None)
                     or ttt.is_value("БАЛАНС", None)):
                 pass
             elif (ttt.get_referent() is not None
                   and ttt.get_referent().type_name == "MONEY"):
                 pass
             else:
                 continue
             toks = TerminToken._new409(
                 t1, t1,
                 NumberExHelper._m_postfixes.find_termins_by_canonic_text(
                     "RUB")[0])
             if (t1.next0_ is not None and t1.next0_.is_char('.')):
                 toks.end_token = t1.next0_
             ty = Utils.valToEnum(toks.termin.tag, NumberExType)
             return NumberExToken._new410(t, toks.end_token, nt.value,
                                          nt.typ, ty, f, altf,
                                          toks.begin_token.morph, "RUB")
     if (toks is not None):
         t1 = toks.end_token
         if (not t1.is_char('.') and t1.next0_ is not None
                 and t1.next0_.is_char('.')):
             if ((isinstance(t1, TextToken)) and t1.is_value(
                     toks.termin.terms[0].canonical_text, None)):
                 pass
             elif (not t1.chars.is_letter):
                 pass
             else:
                 t1 = t1.next0_
         if (toks.termin.canonic_text == "LTL"):
             return None
         if (toks.begin_token == t1):
             if (t1.morph.class0_.is_preposition
                     or t1.morph.class0_.is_conjunction):
                 if (t1.is_whitespace_before and t1.is_whitespace_after):
                     return None
         ty = Utils.valToEnum(toks.termin.tag, NumberExType)
         res = NumberExToken._new407(t, t1, nt.value, nt.typ, ty, f, altf,
                                     toks.begin_token.morph)
         if (ty != NumberExType.MONEY):
             NumberExHelper.__correct_ext_types(res)
             return res
         return NumberExHelper.__correct_money(res, toks.begin_token)
     pfx = NumberExHelper.__attach_spec_postfix(t1)
     if (pfx is not None):
         pfx.begin_token = t
         pfx.value = nt.value
         pfx.typ = nt.typ
         pfx.real_value = f
         pfx.alt_real_value = altf
         return pfx
     if (t1.next0_ is not None and ((t1.morph.class0_.is_preposition
                                     or t1.morph.class0_.is_conjunction))):
         if (t1.is_value("НА", None)):
             pass
         else:
             nn = NumberExHelper.try_parse_number_with_postfix(t1.next0_)
             if (nn is not None):
                 return NumberExToken._new412(t, t, nt.value, nt.typ,
                                              nn.ex_typ, f, altf,
                                              nn.ex_typ2, nn.ex_typ_param)
     if (not t1.is_whitespace_after and (isinstance(t1.next0_, NumberToken))
             and (isinstance(t1, TextToken))):
         term = t1.term
         ty = NumberExType.UNDEFINED
         if (term == "СМХ" or term == "CMX"):
             ty = NumberExType.SANTIMETER
         elif (term == "MX" or term == "МХ"):
             ty = NumberExType.METER
         elif (term == "MMX" or term == "ММХ"):
             ty = NumberExType.MILLIMETER
         if (ty != NumberExType.UNDEFINED):
             return NumberExToken._new413(t, t1, nt.value, nt.typ, ty, f,
                                          altf, True)
     return None