---
- name: capital-letter-function-none
  tag: mi
  match: "string-length(.)=1 and contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', text()) and $ClearSpeak_Functions='None' and following-sibling::*[1][.='⁢' and @data-function-guess]"
  replace:
  - x: "translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"
  - t: "большое"

- name: pause
  tag: "!*"
  match: "not(self::m:math) and not($MatchingPause) and @data-intent-property[contains(., ':pause')]"
  replace:
   - with:
      variables: [MatchingPause: "true()"]
      replace:
      - test:
        - if: "contains(@data-intent-property, ':pause-long')"
          then: [pause: long]
        - else_if: "contains(@data-intent-property, ':pause-short')"
          then: [pause: short]
          else: [pause: medium]
      - x: "."

- name: intent-literal-silent
  tag: [mi, mo, mn]
  match: "contains(@data-intent-property, ':silent:')"
  replace: []

- name: intent-literal-negative-number
  tag: mn
  match: "starts-with(text(), '-')"
  replace:
  - t: "минус"
  - x: "translate(text(), '-_', '')"

- name: even-number-text
  tag: mtext
  match: "contains(., 'even') and contains(., 'number')"
  replace:
  - t: "икс — чётное число"

- name: default
  tag: square-root
  match: "."
  replace:
  - test:
      if: "$Verbosity!='Terse'"
      then: [t: ""]
  - t: "квадратный корень"
  - test:
      if: "$Verbosity!='Terse'"
      then: [t: "из"]
      else: [pause: short]
  - x: "*[1]"
  - test:
    - if: "$ClearSpeak_Roots = 'RootEnd' or $ClearSpeak_Roots = 'PosNegSqRootEnd'"
      then:
      - pause: short
      - t: "конец корня"
      - pause: medium
    - else_if: "IsNode(*[1], 'simple')"
      then:
      - test:
          if: "(following-sibling::* or parent::m:minus/following-sibling::*) and $ClearSpeak_Roots = 'PosNegSqRoot'"
          then: [pause: long]
      - test:
          if: "(following-sibling::* or parent::m:minus/following-sibling::*) and not($ClearSpeak_Roots = 'PosNegSqRoot')"
          then: [pause: medium]
          else: [pause: short]
      else: [pause: long]

- name: default
  tag: root
  match: "."
  replace:
  - test:
      if: "$Verbosity!='Terse'"
      then: [t: ""]
  - test:
      if: "parent::m:minus and preceding::m:square-root and $ClearSpeak_Roots = 'PosNegSqRootEnd'"
      then: [pause: short]
  - test:
      if: "*[2][.='3'] and $ClearSpeak_Roots = 'PosNegSqRootEnd'"
      then: [pause: short]
  - test:
      if: "*[2][self::m:mn and not(contains(., '.'))]"
      then_test:
      - if: "*[2][.='2']"
        then: [t: "квадратный корень"]
      - else_if: "*[2][.='3']"
        then: [t: "кубический корень"]
      - else: [t: "корень", x: "ToOrdinal(*[2])", t: "степени"]
      else:
      - t: "корень степени"
      - x: "*[2]"
  - test:
      if: "$Verbosity!='Terse'"
      then: [t: "из"]
  - x: "*[1]"
  - test:
      if: "$ClearSpeak_Roots = 'RootEnd' or $ClearSpeak_Roots = 'PosNegSqRootEnd'"
      then:
      - pause: short
      - t: "конец корня"
      - pause: medium
      else_test:
        if: IsNode(*[1], 'simple')
        then:
        - test:
            if: "(following-sibling::* or parent::m:minus/following-sibling::*) and $ClearSpeak_Roots = 'PosNegSqRoot'"
            then: [pause: long]
        - test:
            if: "(following-sibling::* or parent::m:minus/following-sibling::*) and not($ClearSpeak_Roots = 'PosNegSqRoot')"
            then: [pause: medium]
            else: [pause: short]
        else: [pause: long]

- name: negative_and_positive
  tag: [plus, minus]
  match: "count(*)=1 and contains(@data-intent-property, ':prefix:')"
  replace:
  - bookmark: "@id"
  - test:
      if: "self::m:minus"
      then: [t: "минус"]
      else: [t: "плюс"]
  - test:
      if: "self::m:minus and *[1][self::m:root] and $ClearSpeak_Roots = 'PosNegSqRootEnd'"
      then: [pause: short]
  - x: "*[1]"

- name: per-fraction
  tag: fraction
  match:
  - "$ClearSpeak_Fractions='Per' or"
  - "(BaseNode(*[1])[contains(@data-intent-property, ':unit') or"
  - "               (self::m:mrow and count(*)=3 and *[1][self::m:mn] and *[2][.='⁢'] and BaseNode(*[3])[contains(@data-intent-property, ':unit')])] and"
  - " BaseNode(*[2])[contains(@data-intent-property, ':unit')])"
  replace:
  - x: "*[1]"
  - test:
      if: "BaseNode(*[2])[contains(@data-intent-property, ':unit')] and BaseNode(*[2])[.='h' or .='hr' or .='Hr']"
      then: [t: "в"]
      else_test:
        if: "BaseNode(*[2])[contains(@data-intent-property, ':unit')]"
        then: [t: "на"]
        else: [t: "разделить на"]
  - test:
      if: "BaseNode(*[2])[contains(@data-intent-property, ':unit')] and BaseNode(*[2])[.='mi']"
      then: [t: "милю"]
      else: [x: "*[2]"]

- name: common-fraction
  tag: fraction
  match:
  - "($ClearSpeak_Fractions='Auto' or $ClearSpeak_Fractions='Ordinal' or $ClearSpeak_Fractions='EndFrac') and"
  - "*[1][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or text()<20)]   and"
  - "*[2][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or (2<= text() and text()<=10))]"
  variables: [IsPlural: "*[1]!=1"]
  replace:
  - test:
    - if: "DefinitionValue(*[1], 'Speech', 'FractionNumerators') != ''"
      then: [x: "DefinitionValue(*[1], 'Speech', 'FractionNumerators')"]
      else: [x: "*[1]"]
  - x: "ToOrdinal(*[2], true(), $IsPlural)"

- name: common-fraction-mixed-number
  tag: fraction
  match:
  - "preceding-sibling::*[1][self::m:mo][.='⁤'] and"
  - "($ClearSpeak_Fractions='Auto' or $ClearSpeak_Fractions='Ordinal' or $ClearSpeak_Fractions='EndFrac') and"
  - "*[1][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or text()<20)]   and"
  - "*[2][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or (2<= text() and text()<=10) or text()=83)]"
  variables: [IsPlural: "*[1]!=1"]
  replace:
  - test:
    - if: "DefinitionValue(*[1], 'Speech', 'FractionNumerators') != ''"
      then: [x: "DefinitionValue(*[1], 'Speech', 'FractionNumerators')"]
      else: [x: "*[1]"]
  - test:
      if: "*[2][.='83']"
      then: [t: "восемьдесят третьих"]
      else: [x: "ToOrdinal(*[2], true(), $IsPlural)"]

- name: fraction-over-simple
  tag: fraction
  match:
  - "($ClearSpeak_Fractions='Over' or $ClearSpeak_Fractions='FracOver' or $ClearSpeak_Fractions='OverEndFrac') or"
  - "( not($ClearSpeak_Fractions='General' or $ClearSpeak_Fractions='GeneralEndFrac') and"
  - "  (IsNode(*[1],'simple') and IsNode(*[2],'simple')) )"
  replace:
  - test:
      if: "$ClearSpeak_Fractions='FracOver'"
      then:
      - test:
          if: "$Verbosity!='Terse'"
          then: [ot: ""]
      - t: "дробь"
  - x: "*[1]"
  - test:
      if: "$ClearSpeak_Fractions='Auto' and $Verbosity='Medium' and *[1][self::m:mrow and (*[2][.='('] or (*[2][.='⁡'] and *[3][IsBracketed(., '(', ')')]))] and *[2][self::m:mn]"
      then: [t: "на"]
      else: [t: "разделить на"]
  - x: "*[2]"
  - test:
      if: "$ClearSpeak_Fractions='OverEndFrac' or ($ClearSpeak_Fractions='EndFrac' and not( ($ClearSpeak_Fractions='Auto' or $ClearSpeak_Fractions='Ordinal' or $ClearSpeak_Fractions='EndFrac') and *[1][*[1][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or text()<20)]   and *[2][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or (2<= text() and text()<=10))] ] and *[2][*[1][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or text()<20)]   and *[2][self::m:mn][not(contains(., '.')) and ($ClearSpeak_Fractions='Ordinal' or (2<= text() and text()<=10))] ] ) )"
      then:
      - pause: short
      - t: "конец дроби"
      - pause: short

- name: fraction-over-text
  tag: fraction
  match:
  - "not($ClearSpeak_Fractions='General' or $ClearSpeak_Fractions='GeneralEndFrac') and"
  - "( "
  - "  ((*[1][self::m:mi or self::m:mtext][string-length(.)>1]) or "
  - "   (*[1][self::m:mrow][count(*)=3][ "
  - "        *[1][self::m:mn] and "
  - "        *[2][self::m:mo][.='⁢'] and "
  - "        *[3][self::m:mi or self::m:mtext][string-length(.)>1] ]) ) and"
  - "  ((*[2][self::m:mi or self::m:mtext][string-length(.)>1]) or "
  - "   (*[2][self::m:mrow][count(*)=3][ "
  - "        *[1][self::m:mn] and "
  - "        *[2][self::m:mo][.='⁢'] and "
  - "        *[3][self::m:mi or self::m:mtext][string-length(.)>1] ]) )"
  - ")"
  replace:
  - x: "*[1]"
  - t: "на"
  - x: "*[2]"
  - test:
      if: "$ClearSpeak_Fractions='EndFrac' or $ClearSpeak_Fractions='OverEndFrac'"
      then:
      - pause: short
      - t: "конец дроби"
      - pause: short

- name: default
  tag: fraction
  match: "."
  replace:
  - ot: ""
  - t: "дробь, числитель:"
  - x: "*[1]"
  - pause: medium
  - t: "знаменатель:"
  - x: "*[2]"
  - pause: long
  - test:
      if: "$ClearSpeak_Fractions='EndFrac' or $ClearSpeak_Fractions='GeneralEndFrac'"
      then:
      - pause: short
      - t: "конец дроби"
      - pause: short

- name: ClearSpeak-function-inverse
  tag: inverse-function
  match: "."
  replace:
  - test:
    - if: $ClearSpeak_Trig = 'TrigInverse'
      then:
      - t: "обратный"
      - x: "*[1]"
    - else_if: "*[1][.='sin']"
      then: [bookmark: "*[2]/@id", t: "арксинус"]
    - else_if: "*[1][.='cos' or .='cosh']"
      then: [bookmark: "*[2]/@id", t: "ареакосинус"]
    - else_if: "*[1][.='tan' or .='tg']"
      then: [bookmark: "*[2]/@id", t: "арктангенс"]
      else: [bookmark: "*[2]/@id", t: "обратный", x: "*[1]"]

- name: function-squared-or-cubed
  tag: power
  match:
  - "*[2][self::m:mn][.='2' or .='3'] and"
  - "following-sibling::*[1][self::m:mo][.='⁡']"
  replace:
  - x: "*[1]"
  - bookmark: "*[2]/@id"
  - test:
      if: "*[2][.='2']"
      then: [t: "в квадрате"]
      else: [t: "в кубе"]

- name: function-power
  tag: power
  match:
  - "following-sibling::*[1][self::m:mo][.='⁡']"
  replace:
  - x: "*[1]"
  - bookmark: "*[2]/@id"
  - t: "в"
  - test:
    - if: "*[2][self::m:mn][.='4']"
      then: [t: "четвёртой"]
    - else_if: "*[2][self::m:mn][not(contains(., '.'))]"
      then: [x: "ToOrdinal(*[2])"]
      else:
      - t: "степени"
      - x: "*[2]"
  - test:
      if: "*[2][self::m:mn][not(contains(., '.'))]"
      then: [t: "степени"]

- name: AfterPower-nested
  tag: power
  match:
  - "$ClearSpeak_Exponents = 'AfterPower' and"
  - "*[2][self::m:power or self::m:power or self::m:mrow[m:power]]"
  replace:
  - x: "*[1]"
  - t: "в степени (показатель:"
  - x: "*[2]"
  - pause: short
  - t: "конец показателя)"

- name: AfterPower-default
  tag: power
  match: "$ClearSpeak_Exponents = 'AfterPower'"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - test:
      if: "*[2][self::m:mn][contains(., '.')]"
      then: [x: "*[2]/text()"]
      else: [x: "*[2]"]

- name: squared
  tag: power
  match: "*[2][self::m:mn][.='2'] and $ClearSpeak_Exponents = 'Auto'"
  replace:
  - x: "*[1]"
  - bookmark: "*[2]/@id"
  - t: "в квадрате"

- name: cubed
  tag: power
  match: "*[2][self::m:mn][.='3'] and $ClearSpeak_Exponents = 'Auto'"
  replace:
  - x: "*[1]"
  - bookmark: "*[2]/@id"
  - t: "в кубе"

- name: simple-integer
  tag: power
  match: "*[2][self::m:mn][not(contains(., '.'))]"
  replace:
  - x: "*[1]"
  - test:
      if: "*[2][.>=0]"
      then:
      - test:
          if: "*[2][.='2']"
          then: [t: "во"]
          else: [t: "в"]
      - x: "ToOrdinal(*[2])"
      - test:
          if: "$ClearSpeak_Exponents != 'Ordinal'"
          then: [t: "степени"]
      else:
      - t: "в степени"
      - x: "*[2]"

- name: simple-negative-integer
  tag: power
  match:
  - "*[2][self::m:minus and count(*)=1 and "
  - "     *[1][self::m:mn][not(contains(., '.'))]"
  - "    ]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - x: "*[2]"

- name: simple-var
  tag: power
  match: "*[2][self::m:mi][string-length(.)=1]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - x: "*[2]"

- name: nested-squared-or-cubed
  tag: power
  match:
  - "$ClearSpeak_Exponents = 'Auto' and"
  - "*[2][self::m:power]["
  - "     *[2][self::m:mn][.='2' or .='3'] and "
  - "     *[1][self::m:mn or self::m:mi or "
  - "          self::m:fraction[*[1][self::m:mn][not(contains(., '.')) and text()<20]   and"
  - "                        *[2][self::m:mn][not(contains(., '.')) and 2<= text() and text()<=10]]"
  - "         ]"
  - "    ]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - x: "*[2]"

- name: nested-negative-squared-or-cubed
  tag: power
  match:
  - "$ClearSpeak_Exponents = 'Auto' and"
  - " *[2][self::m:minus and count(*)=1 and "
  - "      *[1]/*[1][self::m:power]["
  - "                *[2][self::m:mn][.='2' or .='3'] and "
  - "                *[1][self::m:mn or self::m:mi or "
  - "                     self::m:fraction[*[1][self::m:mn][not(contains(., '.')) and text()<20]   and"
  - "                                   *[2][self::m:mn][not(contains(., '.')) and 2<= text() and text()<=10]]"
  - "               ]"
  - "          ]"
  - "     ]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - t: "минус"
  - x: "*[2]/*[1]"

- name: nested-var-squared-or-cubed
  tag: power
  match:
  - "$ClearSpeak_Exponents = 'Auto' and"
  - "  *[2][self::m:mrow][count(*)=3][ "
  - "       *[3][self::m:power]["
  - "            *[2][self::m:mn][.='2' or .='3'] and "
  - "            *[1][self::m:mi]"
  - "           ] and "
  - "       *[2][self::m:mo][.='⁢'] and "
  - "       *[1][self::m:mn or "
  - "            self::m:fraction[*[1][self::m:mn][not(contains(., '.')) and text()<20]   and"
  - "                          *[2][self::m:mn][not(contains(., '.')) and 2<= text() and text()<=10]]"
  - "           ]"
  - "      ]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - x: "*[2]"

- name: nested-negative-var-squared-or-cubed
  tag: power
  match:
  - "$ClearSpeak_Exponents = 'Auto' and"
  - "  *[2][self::m:mrow][count(*)=3][ "
  - "       *[3][self::m:power]["
  - "            *[2][self::m:mn][.='2' or .='3'] and "
  - "            *[1][self::m:mi]"
  - "           ] and "
  - "       *[2][self::m:mo][.='⁢'] and "
  - "       *[1][self::m:minus and count(*)=1 and "
  - "            *[1][self::m:mn or "
  - "                 self::m:fraction[*[1][self::m:mn][not(contains(., '.')) and text()<20]   and"
  - "                                  *[2][self::m:mn][not(contains(., '.')) and 2<= text() and text()<=10]]"
  - "                ]"
  - "           ]"
  - "      ]"
  replace:
  - x: "*[1]"
  - t: "в степени"
  - x: "*[2]"

- name: default-exponent-power
  tag: power
  match:
  - "*[2][self::m:power or self::m:power or self::m:mrow[m:power]]"
  replace:
  - x: "*[1]"
  - t: "в степени (показатель:"
  - x: "*[2]"
  - pause: short
  - t: "конец показателя)"

- name: default
  tag: power
  match: "."
  replace:
  - x: "*[1]"
  - t: "в степени"
  - test:
      if: "*[2][self::m:mn][contains(., '.')]"
      then: [x: "*[2]/text()"]
      else: [x: "*[2]"]

- name: ClearSpeak-absolute-value
  tag: absolute-value
  match: "."
  replace:
    - test:
        if: "ancestor::*[name()='set']"
        then:
        - t: "модуль"
        - x: "*[1]"
        - pause: medium
        else:
        - test:
            if: "$Verbosity!='Terse'"
            then: [t: ""]
        - test:
            if: "$ClearSpeak_AbsoluteValue = 'Cardinality'"
            then: [t: "мощность множества"]
            else: [t: "модуль"]
        - t: "из"
        - x: "*[1]"
        - test:
            if: "$ClearSpeak_AbsoluteValue = 'AbsEnd'"
            then:
              - pause: short
              - t: "конец"
              - test:
                  if: "$ClearSpeak_AbsoluteValue = 'Cardinality'"
                  then: [t: "мощности множества"]
                  else: [t: "модуля"]
        - pause: short

- name: set
  tag: set
  match: "."
  replace:
  - test:
    - if: "count(*)=0"
      then: [t: "пустое множество"]
    - else_if: "count(*)=2"
      then:
      - test:
          if: "$Verbosity!='Terse'"
          then: [t: ""]
      - t: "пустое множество"
    - else_if: "count(*)>=7 and *[2][.='∈'] and *[4][.=':' or .='|' or .='∣']"
      then:
      - test:
          if: "$Verbosity!='Terse'"
          then: [t: ""]
      - t: "множество"
      - test:
          if: $ClearSpeak_Sets != 'woAll'
          then: [t: "всех"]
      - x: "*[1]"
      - test:
        - if: "$ClearSpeak_SetMemberSymbol='Belongs'"
          then:
          - pause: short
          - t: "входящих в"
          - test:
            - if: "*[3][.='ℤ']"
              then: [t: "целые числа"]
            - else: [x: "*[3]"]
        - else_if: "$ClearSpeak_SetMemberSymbol='Element'"
          then:
          - pause: short
          - t: "являющихся элементами"
          - test:
            - if: "*[3][.='ℤ']"
              then: [t: "целых чисел"]
            - else: [x: "*[3]"]
        - else_if: "$ClearSpeak_SetMemberSymbol='Auto' or $ClearSpeak_SetMemberSymbol='Member'"
          then:
          - pause: short
          - t: "принадлежащих"
          - test:
            - if: "*[3][.='ℤ']"
              then: [t: "целым числам"]
            - else: [x: "*[3]"]
        - else:
          - t: "из"
          - test:
            - if: "*[3][.='ℤ']"
              then: [t: "целых чисел"]
            - else: [x: "*[3]"]
      - pause: short
      - t: "таких что"
      - x: "*[position()>4]"
    - else_if: "count(*)=3 and *[1][self::m:mrow and count(*)=3 and *[2][.='∈']] and *[2][.='|' or .='∣']"
      then:
      - test:
          if: "$Verbosity!='Terse'"
          then: [t: ""]
      - t: "множество"
      - test:
          if: $ClearSpeak_Sets != 'woAll'
          then: [t: "всех"]
      - x: "*[1]"
      - pause: short
      - t: "таких что"
      - test:
        - if: "*[3][self::m:mrow and count(*)=2 and *[2][self::m:mtext[contains(., 'even number')]]]"
          then: [x: "*[3]/*[2]"]
        - else: [x: "*[3]"]
    - else_if: "count(*[1]/*)=3 and *[1]/*[2][self::m:mo][.=':' or .='|' or .='∣']"
      then:
      - test:
          if: "$Verbosity!='Terse'"
          then: [t: ""]
      - t: "множество"
      - test:
          if: $ClearSpeak_Sets != 'woAll'
          then: [t: "всех"]
      - test:
          if: "*[1]/*[1][self::m:mi[.='x']]"
          then: [t: "икс"]
          else: [x: "*[1]/*[1]"]
      - test:
          if: "*[1]//*[.='∈']"
          then: [t: "таких что"]
          else: [t: "таких, что"]
      - test:
        - if: "*[1]/*[3][self::m:mrow and count(*)=3 and *[1][self::m:mi[.='x']]]"
          then:
          - t: "икс"
          - x: "*[1]/*[3]/*[2]"
          - x: "*[1]/*[3]/*[3]"
        - else_if: "*[1]/*[3][self::m:mrow and count(*)=3 and *[1]//*[name()='absolute-value']]"
          then:
          - t: "модуль"
          - t: "икс"
          - pause: medium
          - x: "*[1]/*[3]/*[2]"
          - x: "*[1]/*[3]/*[3]"
        - else: [x: "*[1]/*[3]"]
      else:
      - test:
          if: $ClearSpeak_Sets != 'SilentBracket'
          then:
          - test:
              if: "$Verbosity!='Terse'"
              then: [t: ""]
          - t: "множество"
      - x: "*[1]"

- name: ClearSpeak-intervals
  variables:
  - is_intervals_start_infinity: "*[1][self::m:minus and count(*)=1 and *[1][.='∞']]"
  - is_intervals_end_infinity: "*[2][.='∞'or (self::m:plus and count(*)=1 and *[1][.='∞'])]"
  tag: [open-interval, open-closed-interval, closed-interval, closed-open-interval]
  match: "."
  replace:
  - t: "интервал от"
  - test:
      if: "$is_intervals_start_infinity"
      then: [t: "минус бесконечности"]
      else: [x: "*[1]"]
  - t: "до"
  - test:
    - if: "*[2][self::m:plus and count(*)=1 and *[1][.='∞']]"
      then: [t: "плюс бесконечности"]
    - else_if: "*[2][.='∞']"
      then: [t: "бесконечности"]
      else: [x: "*[2]"]
  - pause: short
  - test:
      if: "not($is_intervals_start_infinity)"
      then:
      - test:
          if: "starts-with(name(.), 'open')"
          then: [t: "не включая"]
          else: [t: "включая"]
      - x: "*[1]"
  - test:
      if: "not($is_intervals_start_infinity or $is_intervals_end_infinity)"
      then_test:
      - if: "name(.)='open-interval'"
        then: [t: "и"]
      - else_if: "name(.)='closed-interval'"
        then: [t: "и"]
        else: [pause: short, t: "но"]
  - test:
      if: not($is_intervals_end_infinity)
      then:
      - test:
          if: not( name(.)='open-interval' or name(.)='closed-interval' ) or $is_intervals_start_infinity
          then:
          - test:
              if: "name(.) = 'open-interval' or name(.) = 'closed-open-interval'"
              then: [t: "не включая"]
              else: [t: "включая"]
      - x: "*[2]"

- name: binomial-frac-vector
  tag: matrix
  match:
  - "$ClearSpeak_Matrix = 'Combinatorics' and "
  - "count(*[1]/*)=1 and count(*)=2"
  replace:
  - t: "число сочетаний из"
  - x: "*[1]/*[1]/*"
  - t: "по"
  - x: "*[2]/*[1]/*"

- name: ClearSpeak-default
  tag: [mtr, mlabeledtr]
  match: "parent::m:matrix or parent::m:determinant"
  variables: [NextLineIsContinuedRow: "following-sibling::*[1][contains(@data-intent-property, ':continued-row:')]"]
  replace:
  - pause: medium
  - t: "строка"
  - x: "count(preceding-sibling::*)+1"
  - test:
      if: .[self::m:mlabeledtr]
      then:
      - t: "с меткой"
      - x: "*[1]/*"
      - pause: short
  - pause: medium
  - test:
      if: .[self::m:mlabeledtr]
      then: [x: "*[position()>1]"]
      else: {x: "*"}

- name: equality-with-root
  tag: mrow
  match:
  - "count(*)=3 and *[2][.='='] and"
  - "(*[1][self::m:mrow[m:square-root or m:root or m:msqrt or m:mroot]] or"
  - " *[3][self::m:square-root or self::m:root or self::m:msqrt or self::m:mroot])"
  replace:
  - x: "*[1]"
  - pause: medium
  - x: "*[2]"
  - pause: short
  - x: "*[3]"

- name: mixed-number
  tag: mrow
  match: "count(*)=3 and *[1][self::m:mn] and *[2][self::m:mo][.='⁤'] and *[3][self::m:fraction]"
  replace:
  - x: "*[1]"
  - t: "целых"
  - x: "*[3]"

- name: equality-with-none-implied-times
  tag: mrow
  match:
  - "count(*)=3 and *[2][.='='] and"
  - "$ClearSpeak_ImpliedTimes='None' and $ClearSpeak_Functions='None' and"
  - "*[3][self::m:mrow[*[2][.='⁢']]]"
  replace:
  - x: "*[1]"
  - pause: medium
  - x: "*[2]"
  - pause: medium
  - x: "*[3]"

- name: equality-with-piecewise
  tag: [math, mrow]
  match: "count(*[.='='])=1 and *[.='=']/following-sibling::*[1][self::m:piecewise or .//m:piecewise]"
  replace:
  - x: "*[.='=']/preceding-sibling::*"
  - x: "*[.='=']"
  - pause: medium
  - x: "*[.='=']/following-sibling::*"

- name: functions-none-plus
  tag: mrow
  match:
  - "count(*)=3 and *[2][.='+'] and $ClearSpeak_Functions='None' and"
  - "*[1][self::m:mrow[*[2][.='⁡']]] and *[3][self::m:mrow[*[2][.='⁡']]]"
  replace:
  - x: "*[1]"
  - pause: medium
  - x: "*[2]"
  - pause: short
  - x: "*[3]"

- name: ClearSpeak-log
  tag: mrow
  variables: [{log_is_simple: "IsNode(*[3],'simple')"}]
  match:
  - "count(*)=3 and"
  - "*[1][self::m:mi][.='log' or .='ln'] and"
  - "*[2][self::m:mo][.='⁡']"
  replace:
  - test:
      if: "$log_is_simple"
      then_test:
      - if: "*[1][.='log']"
        then: [t: "логарифм"]
      - else_if: $ClearSpeak_Log = 'LnAsNaturalLog'
        then: [t: "натуральный логарифм"]
        else: [t: "эл эн"]
      else:
      - test:
          if: "$Verbosity!='Terse' and not(log_is_simple)"
          then: [t: ""]
      - test:
        - if: "*[1][.='log']"
          then: [t: "логарифм"]
        - else_if: $ClearSpeak_Log = 'LnAsNaturalLog'
          then: [t: "натуральный логарифм"]
          else: [t: "эл эн"]
      - t: "от"
  - x: "*[3]"

- name: ClearSpeak-log-with-base
  tag: mrow
  match: "count(*)=3 and *[1][self::m:logarithm-with-base] and *[2][.='⁡']"
  replace:
  - t: "логарифм"
  - x: "*[3]"
  - pause: short
  - t: "по основанию"
  - x: "*[1]/*[1]"

- name: trig-function-noninteger-power
  tag: mrow
  match:
  - "count(*)=3 and *[1][self::m:power and IsInDefinition(*[1], 'TrigFunctionNames') and not(*[2][self::m:mn])] and"
  - "*[2][.='⁡']"
  replace:
  - x: "*[1]/*[1]"
  - x: "*[3]"
  - t: "в степени"
  - x: "*[1]/*[2]"

- name: simple-parens-after-apply-or-times
  tag: mrow
  match:
  - "IsBracketed(., '(', ')') and IsNode(*[2], 'simple') and "
  - "preceding-sibling::*[1][(.='⁡' and not($ClearSpeak_Functions='None')) or (.='⁢' and not(@data-function-guess))]"
  replace:
  - x: "*[2]"

- name: functions-none-comma-arguments
  tag: mrow
  match:
  - "count(*)=3 and *[2][.=','] and $ClearSpeak_Functions='None' and"
  - "parent::*[self::m:mrow and IsBracketed(., '(', ')') and preceding-sibling::*[1][.='⁢' and @data-function-guess]]"
  replace:
  - x: "*[1]"
  - t: "запятая"
  - x: "*[3]"

- name: function-argument-parens
  tag: mrow
  match:
  - "IsBracketed(., '(', ')') and "
  - "(preceding-sibling::*[1][.='⁡'] or preceding-sibling::*[2][self::m:mi])"
  replace:
  - test:
      if: "not(IsNode(*[2], 'simple')) and
           (.//m:mo[.='+'] or preceding-sibling::*[1][@data-function-guess and $ClearSpeak_Functions='None']) and
           not(preceding-sibling::*[2][self::m:logarithm-with-base])"
      then: [pause: short]
  - test:
      if: "ancestor-or-self::*[contains(@data-intent-property, ':literal:')]"
      then: [t: "открывающая круглая скобка"]
      else_test:
      - if: ".//m:mo[.='|' or .='∣' or .='∩'] or .//m:mtext[contains(., 'and')] or .//*[contains(name(.), 'given')]"
        then: [t: "открывающая круглая скобка"]
      - else_if: "ancestor::*[self::m:fraction] and not(IsNode(*[2], 'simple'))"
        then: [t: "скобка открывается"]
      - else: [t: "скобка открывается"]
  - test:
      if: ".//m:mo[.='|' or .='∣' or .='∩'] or .//m:mtext[contains(., 'and')] or .//*[contains(name(.), 'given')]"
      then: [pause: short]
  - x: "*[2]"
  - test:
      if: "not(IsNode(*[2], 'simple'))"
      then: [pause: short]
  - test:
      if: "ancestor-or-self::*[contains(@data-intent-property, ':literal:')]"
      then: [t: "закрывающая круглая скобка"]
      else_test:
      - if: ".//m:mo[.='|' or .='∣' or .='∩'] or .//m:mtext[contains(., 'and')] or .//*[contains(name(.), 'given')]"
        then: [t: "закрывающая круглая скобка"]
      - else_if: "ancestor::*[self::m:fraction] and not(IsNode(*[2], 'simple'))"
        then: [t: "скобка закрывается"]
      - else: [t: "скобка закрывается"]
  - test:
      if: "following-sibling::*[1][.='+' or .='=']"
      then: [pause: medium]

- name: exponent-argument-parens
  tag: mrow
  match:
  - "IsBracketed(., '(', ')') and "
  - "ancestor::*[self::m:power]"
  replace:
  - test:
      if: "*[2][self::m:fraction]"
      then: [pause: medium]
  - test:
      if: "$ClearSpeak_ImpliedTimes='MoreImpliedTimes'"
      then: [t: "скобка открывается"]
      else: [t: "скобка открывается"]
  - test:
      if: "*[2][self::m:fraction]"
      then: [pause: medium]
  - x: "*[2]"
  - test:
      if: "*[2][self::m:fraction or .//m:mo[.='+' or .='-' or .='−']]"
      then: [pause: short]
  - test:
      if: "$ClearSpeak_ImpliedTimes='MoreImpliedTimes'"
      then: [t: "скобка закрывается"]
      else: [t: "скобка закрывается"]

- name: ClearSpeak-multi-line
  tag: [piecewise, system-of-equations, lines]
  match: "."
  variables:
  - LineCount: "count(*[not(contains(@data-intent-property, ':continued-row:'))])"
  - NextLineIsContinuedRow: "false()"
  - IsColumnSilent: true()
  replace:
  - test:
    - if: $ClearSpeak_MultiLineOverview = 'Auto'
      then:
      - x: "$LineCount"
      - test:
        - if: "($ClearSpeak_MultiLineLabel = 'Auto' and self::m:piecewise) or $ClearSpeak_MultiLineLabel = 'Case'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "случай"]
            else: [t: "случая"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Auto' or $ClearSpeak_MultiLineLabel = 'Line' or $ClearSpeak_MultiLineLabel = 'None'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "строка"]
            else: [t: "строки"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Constraint'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "ограничение"]
            else: [t: "ограничения"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Equation'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "уравнение"]
            else: [t: "уравнения"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Row'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "строка"]
            else: [t: "строки"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Step'"
          then_test:
            if: "$LineCount = 1"
            then: [t: "шаг"]
            else: [t: "шага"]
      - pause: short
  - x: "*"
  - pause: long

- name: ClearSpeak-default-multiline
  tag: [mtr, mlabeledtr]
  match: "parent::m:piecewise or parent::m:system-of-equations or parent::m:lines"
  variables: [NextLineIsContinuedRow: "following-sibling::*[1][contains(@data-intent-property, ':continued-row:')]"]
  replace:
  - test:
      if: "not($LineCount=1 or $ClearSpeak_MultiLineLabel='None' or contains(@data-intent-property, ':continued-row:'))"
      then:
      - pause: medium
      - test:
        - if: "($ClearSpeak_MultiLineLabel = 'Auto' and parent::m:piecewise) or $ClearSpeak_MultiLineLabel = 'Case'"
          then: [t: "случай"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Auto' or $ClearSpeak_MultiLineLabel = 'Line'"
          then: [t: "строка"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Constraint'"
          then: [t: "ограничение"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Equation'"
          then: [t: "уравнение"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Row'"
          then: [t: "строка"]
        - else_if: "$ClearSpeak_MultiLineLabel = 'Step'"
          then: [t: "шаг"]
      - x: "count(preceding-sibling::*[not(contains(@data-intent-property, ':continued-row:'))]) + 1"
      - test:
          if: "self::m:mlabeledtr"
          then:
          - t: "с меткой"
          - x: "*[1]/*"
  - test:
    - if: "$ClearSpeak_MultiLineLabel='None'"
      then: [pause: long]
    - else_if: "not(contains(@data-intent-property, ':continued-row:'))"
      then: [pause: medium]
  - test:
      if: "self::m:mlabeledtr"
      then: [x: "*[position()>1]"]
      else: [x: "*"]

- name: ClearSpeak_Functions_None
  tag: mo
  match:
  - ".='⁡' and $ClearSpeak_Functions = 'None' and"
  - "not(preceding-sibling::*[1][IsInDefinition(., 'TrigFunctionNames')])"
  replace:
    test:
      if: "$ClearSpeak_ImpliedTimes = 'None'"
      then: [t: ""]
      else: [t: "умножить на"]

- name: no-times
  tag: mo
  match:
  - ".='⁢' and $ClearSpeak_ImpliedTimes = 'None'"
  replace:
  - test:
    - if: "preceding-sibling::*[1][self::m:mrow and (IsBracketed(., '(', ')') or IsBracketed(., '[', ']'))]"
      then: [pause: medium]
    - else_if: "following-sibling::*[1][self::m:mrow and (IsBracketed(., '(', ')') or IsBracketed(., '[', ']'))] and not(preceding-sibling::*[1][self::m:mi])"
      then: [pause: short]

- name: no-times-before-exponent-brackets
  tag: mo
  match: ".='⁢' and ancestor::*[self::m:power] and following-sibling::*[1][IsBracketed(., '(', ')')]"
  replace:
  - pause: medium

- name: ClearSpeak-times
  tag: mo
  match:
  - ".='⁢' and (not(@data-function-guess) or $ClearSpeak_Functions = 'None') and"
  - "not(ancestor-or-self::*[contains(@data-intent-property, ':literal:')]) and ("
  - "  $ClearSpeak_ImpliedTimes = 'MoreImpliedTimes'"
  - " or "
  - "  ancestor::*[self::m:square-root or self::m:msqrt or self::m:root or self::m:mroot]"
  - " or "
  - "  following-sibling::*[1]["
  - "    IsBracketed(., '(', ')') or IsBracketed(., '[', ']') or IsBracketed(., '|', '|') or "
  - "    self::m:matrix or self::m:determinant or self::m:binomial or"
  - "    self::m:square-root or self::m:msqrt or self::m:root or self::m:mroot or"
  - "    (self::m:msub or self::m:msubsup or"
  - "    ((self::m:msup or self::m:power) and not(IsNode(*[1], 'leaf') and *[2][self::m:mn and (.=2 or '.=3')]))) and "
  - "     (*[1][self::m:mrow[IsBracketed(., '(', ')') or IsBracketed(., '[', ']') or IsBracketed(., '|', '|')] or "
  - "           self::m:matrix or self::m:determinant] or"
  - "      not(IsNode(*[2], 'simple')) or "
  - "      (self::m:msubsup and not(IsNode(*[3], 'simple')))"
  - "     )"
  - "  ]"
  - " or "
  - "  preceding-sibling::*[1]["
  - "    IsBracketed(., '(', ')') or IsBracketed(., '[', ']') or IsBracketed(., '|', '|')]"
  - " )"
  replace:
  - t: "умножить на"
  - test:
      if: "@data-function-guess and following-sibling::*[1][self::m:mrow and IsBracketed(., '(', ')')] and not($ClearSpeak_Functions='None')"
      then: [pause: short]

- name: no-say-parens
  tag: mrow
  match:
  - "parent::*[not(self::m:msup) and not(self::m:msub) and not(self::m:msubsup) and not(self::m:power) and"
  - "          not(self::m:math) ] and "
  - "( IsBracketed(., '(', ')') or IsBracketed(., '[', ']') ) and "
  - "not( $ClearSpeak_Functions = 'None' and "
  - "     (preceding-sibling::*[1][.='⁡'] or following-sibling::*[1][.='⁡']) ) and "
  - "not( $ClearSpeak_ImpliedTimes = 'None' and "
  - "     (preceding-sibling::*[1][.='⁢'] or following-sibling::*[1][.='⁢']) ) and "
  - "IsNode(*[2], 'simple') and"
  - "not(preceding-sibling::*[1][self::m:mi]) and"
  - "not(preceding-sibling::*[1][.='\u2062' and @data-function-guess]) and"
  - "not(ancestor-or-self::*[contains(@data-intent-property, ':literal:')])"
  replace:
  - x: "*[2]"

- name: say-bracketed-mrow
  tag: mrow
  match: "IsBracketed(., '(', ')') or IsBracketed(., '[', ']')"
  replace:
  - test:
      if: "preceding-sibling::*[1][self::m:mi] or preceding-sibling::*[1][.='⁢' and @data-function-guess and $ClearSpeak_Functions='None']"
      then: [pause: short]
  - test:
      if: "IsBracketed(., '[', ']')"
      then: [t: "квадратная скобка открывается"]
      else_test:
        if: "ancestor-or-self::*[contains(@data-intent-property, ':literal:')] or .//m:mo[.='|' or .='∩'] or .//*[contains(., 'and')]"
        then: [t: "открывающая круглая скобка"]
        else: [t: "скобка открывается"]
  - x: "*[2]"
  - test:
      if: "IsBracketed(., '(', ')') and .//m:mo[.='+' or .='-' or .='−']"
      then: [pause: short]
  - test:
      if: "IsBracketed(., '[', ']')"
      then: [t: "квадратная скобка закрывается"]
      else_test:
        if: "ancestor-or-self::*[contains(@data-intent-property, ':literal:')] or .//m:mo[.='|' or .='∩'] or .//*[contains(., 'and')]"
        then: [t: "закрывающая круглая скобка"]
        else: [t: "скобка закрывается"]
  - test:
      if: "following-sibling::*[1][.='+' or .='=']"
      then: [pause: medium]

- include: "SharedRules/geometry.yaml"
- include: "SharedRules/linear-algebra.yaml"
- include: "SharedRules/calculus.yaml"
- include: "SharedRules/general.yaml"
- include: "SharedRules/default.yaml"
