mathcat 0.7.5

MathCAT: Math Capable Assistive Technology ('Speech and braille from MathML')
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
---
# SWEDISH: This code preserves points in mtext elements. Only the first point, if there are more than one, is currently preserved.
- name: intercept-comma
  tag: mtext
  match: "contains(., '.')"
  replace:
  - x: "substring-before(., '.')"
  - T: "punktum"
  - x: "substring-after(., '.')"

# number-sets are a little messy in that the base was converted to a number-set, so we have to match that (simple) case last
- name: pos-neg-number-sets
  tag: number-sets
  match: "count(*)=2 and *[2][.='+' or .='-']"
  replace:
  # NORWEGIAN: Removed test for verbosity
  #- test:
      #if: "$Verbosity!='Terse'"
      #then:
  - T: "de"      # phrase('the' square root of 25 equals 5)
  - bookmark: "*[2]/@id"
  - test:
    - if: "*[2][.='+']"
      then: [T: "positive"]      # phrase(set of all 'positive' integers less than 10)
      else: [T: "negative"]      # phrase(set of all 'negative' integers less than minus 10)
  - bookmark: "*[1]/@id"
  - test:
    - if: "*[1][.='ℂ']"
      then: [T: "komplekse tallene"]      # phrase('complex numbers' consist of two parts)
    - else_if: "*[1][.='ℕ']"
      then: [T: "naturlige tallene"]      # phrase('natural numbers' are numbers from 1 to infinity)
    - else_if: "*[1][.='ℚ']"
      then: [T: "rasjonale tallene"]      # phrase('rational numbers' are the fraction of 2 integers)
    - else_if: "*[1][.='ℝ']"
      then: [T: "reelle tallene"]      # phrase('real numbers' can be both positive and negative)
    - else_if: "*[1][.='ℤ']"
      then: [T: "heltallene"]      # phrase(positive 'integers' are natural numbers above 0)
      else: [x: "*[1][text()]"] # shouldn't happen

- name: dimension-number-sets    # should be single digit integer at this point (e.g, R^3)
  tag: number-sets
  match: "count(*)=2"
  replace:
  - bookmark: "*[1]/@id"
  - test:
    - if: "*[1][.='ℂ']"
      then: [T: "C"]      # phrase(the letter 'C' used to represent complex number)
    - else_if: "*[1][.='ℕ']"
      then: [T: "N"]      # phrase(the letter 'N' may represent natural numbers)
    - else_if: "*[1][.='ℚ']"
      then: [T: "Q"]      # phrase(the letter 'Q' may represent rational numbers)
    - else_if: "*[1][.='ℝ']"
      then: [T: "R"]      # phrase(the letter 'R' may represent real numbers)
    - else_if: "*[1][.='ℤ']"
      then: [T: "Z"]      # phrase(the letter 'Z' may represent integers)
      else: [x: "*[1][text()]"] # shouldn't happen
  - bookmark: "*[2]/@id"
  - x: "*[2]"

- name: simple-number-sets
  tag: number-sets
  match: "count(*)=0"
  replace:
  - bookmark: "@id"
  - test:
    - if: ".='ℂ'"
      then: [T: "de komplekse tallene"]      # phrase('the complex numbers' include 2 parts)
    - else_if: ".='ℕ'"
      then: [T: "de naturlige tallene"]      # phrase('the natural numbers' begin at 1)
    - else_if: ".='ℚ'"
      then: [T: "de rasjonale tallene"]      # phrase('the rational numbers' are the fraction of 2 integers)
    - else_if: ".='ℝ'"
      then: [T: "de reelle tallene"]      # phrase('the real numbers' can be both positive and negative)
    - else_if: ".='ℤ'"
      then: [T: "heltallene"]      # phrase('the integers' are natural numbers above 0)
      else: [x: "text()"] # shouldn't happen

- name: real-part
  tag: real-part
  match: "."
  replace:
  - bookmark: "@id"
  - T: "den reelle delen"      # phrase('the real part' of a complex number does not include the imaginary part)

- name: imaginary-part
  tag: imaginary-part
  match: "."
  replace:
  - bookmark: "@id"
  - T: "den imaginære delen"      # phrase('the imaginary part' is part of a complex number)

# rules on scripted vertical bars ('evaluated at')
- name: evaluated-at-2
  tag: evaluate
  match: "count(*)=2"
  replace:
  - x: "*[1]"
  - pause: auto
  - test:
      if: "$Verbosity!='Terse'"
      then: [T: "evaluert i"]      # phrase(results were 'evaluated at' a given point)
      else: [T: "i"]
  # SWEDISH/NORWEGIAN: Added a verbosity setting test to allow a shorter form.
  - pause: auto
  - x: "*[2]"

- name: evaluated-at-3
  tag: evaluate
  match: "count(*)=3"
  replace:
  - x: "*[1]"
  - pause: auto
  - test:
      if: "$Verbosity!='Terse'"
      then: [T: "evaluert for øvre verdi"]      # phrase(results were 'evaluated at' a given point)
      else: [T: "øvre verdi"]
  - pause: short
  - x: "*[3]"
  - pause: short
  - T: "og"      # phrase(this result is 'minus the same expression evaluated at' an earlier point)
  - T: "nedre verdi"
  # SWEDISH/NORWEGIAN: Removed the expansion of the expression, keeping the output closer to what is actually shown visually.
  - pause: short
  - x: "*[2]"

- name: permutation
  tag: pochhammer
  match: "count(*)=2"
  replace:
  - T: "antallet permutasjoner av"
  - x: "*[2]"
  - T: "elementer av"      # phrase(the solution  involves several 'permutations of' values)
  - x: "*[1]"

# NORWEGIAN: Removed test for verbosity
- name: intervals
  tag: [open-interval, open-closed-interval, closed-interval, closed-open-interval]
  match: "count(*)=2"
  replace:
  # SWEDISH/NORWEGIAN: The translate function doesn't work for non-English. Used the solution below instead.
  - test:
     - if: "self::m:open-interval"
       then: [T: "det åpne intervallet"]
     - else_if: "self::m:open-closed-interval"
       then: [T: "det halvåpne intervallet"]
     - else_if: "self::m:closed-interval"
       then: [T: "det lukkede intervallet"]
       else: [T: "det halvåpne intervallet"]
  - T: "fra"      # phrase(subtracting 5 'from' 10 gives 5)
  - test:
    - if: "self::m:closed-open-interval"
      then: [T: "og med"]
  - x: "*[1]"
  - T: "til"      # phrase(adding 6 'to' 6 equals  12)
  - test:
    - if: "self::m:open-closed-interval"
      then: [T: "og med"]
  - x: "*[2]"
# removed this option because it doesn't clarify in which end half-open intervals are open
#      else:
#      - x: "*[1]"
#      - T: "komma"      # phrase(use a 'comma' to divide large numbers or as a decimal point)
#      - x: "*[2]"

- name: default-point
  tag: point
  match: "count(*)=2"
  replace:
  - T: "punktet"      # phrase(a decimal 'point' indicates the fraction component of a number)
  - x: "*[1]"
  - T: "komma"      # phrase(use a 'comma' to divide large numbers or as a decimal point)
  - x: "*[2]"

- name: bigop-both
  tag: large-op
  match: "count(*) = 3"
  replace:
  - x: "*[1]"
  - T: "fra"      # phrase(subtracting 5 'from' 10 gives 5)
  - x: "*[2]"
  - pause: short
  - T: "til"      # phrase(adding 6 'to' 6 equals  12)
  - x: "*[3]"
  - pause: short
  - test:
      if: "following-sibling::*"
      then: [T: "av"]      # phrase(the square root 'of' 25 equals 5)
      

- name: bigop-under
  tag: large-op
  match: "count(*)=2"
  replace:
  - x: "*[1]"
  - T: "over"      # phrase(2 'over' 3 equals two thirds)
  - x: "*[2]"
  - pause: short
  - test:
      if: "following-sibling::*"
      then: [T: "av"]      # phrase(the square root 'of' 25 equals 5)

- name: largeop
  tag: mrow
  match: "count(*)=2 and IsInDefinition(*[1], 'Speech', 'LargeOperators')"
  replace:
  - x: "*[1]"
  - T: "av"      # phrase(the square root 'of' 25 equals 5)
  - pause: short
  - x: "*[2]"
  
#NORWEGIAN: Added this rule from English.
- name: repeating-decimal
  tag: repeating-decimal
  match: "."
  replace:
  - x: "*[1]"
  - T: "med repeterende siffer"      # phrase('with repeating digits')
  - spell: "*[2]"

- name: msubsup-skip-super
  # handles single, double, etc., prime
  tag: [skip-super, say-super]
  match: "count(*)=3"
  replace:
  - x: "*[1]"
  - test:
      if: "$Verbosity='Verbose'"
      then: [T: "nedre indeks"]      # phrase(a 'subscript' may be used to indicate an index)
      else: [T: "senket"]            # phrase(the result is 'sub' optimal)
  - x: "*[2]"
  - test:
      if: "not(IsNode(*[2],'leaf'))"
      then:
      - test:
          if: "$Verbosity='Verbose'"
          then: [T: "slutt nedre indeks"]      # phrase(this is the 'end subscript' position)
          else: [T: "slutt senket"]      # phrase(this is the 'end sub' position)
      - pause: short
      else_test:
          if: "*[2][self::m:mi]"   # need a pause in "x sub k prime" so the prime is not associated with the 'k'
          then: [pause: short]
  - test:
      if: "name(.)='say-super'"
      then_test:
        if: "$Verbosity='Verbose'"
        then: [T: "øvre indeks"]      # phrase(a 'superscript' number indicates raised to a power)
        else: [T: "hevet"]      # phrase(this is a 'super' set of numbers)
  - x: "*[3]"
  - pause: short

# need to say "ett" for '1' in front of a neuter unit
# this is extra complicated because we need to allow do it for a 1 in front of a mixed fraction
# note: bias ha->hektar and Pa->Pascal, not si-prefix "a" (annum)
- name: one-for-unit
  tag: mn
  match: ".=1"
  variables:
    # set right level for the location of the common fraction/mixed fraction
  - IsMixedFraction: "following-sibling::*[1][.='\u2064']"
  - UnitSibling: "IfThenElse($IsMixedFraction, .., .)"
    # set the potential unit node -- otherwise set it to a empty node set (use a made up name)
  - UnitNodeLocation: "IfThenElse($UnitSibling/..[self::m:mrow and count(*)=3 and *[2][.='\u2062']],
                                  $UnitSibling/following-sibling::*[2],
                                  self::m:select-no-nodes)"
    # pick out the right node to test for being a neuter unit
  - UnitNode: "IfThenElse($UnitNodeLocation[self::m:unit],
                          $UnitNodeLocation[self::m:unit],
                          $UnitNodeLocation[self::m:fraction[contains(@data-intent-property, ':unit:')]]/*[1])"
    # test the node for being a neuter unit
    # note: bias ha->hektar and Pa->Pascal, not si-prefix "a" (annum)
  - NeuterUnitFollows: "$UnitNode[IsInDefinition(., 'Speech', 'NeuterUnits') or 
                                  IsInDefinition(., 'Speech', 'NeuterUnitsWithoutPrefixes') or
                                  (string-length(.) >= 2 and
                                    . != 'ha' and . != 'Pa' and
                                    DefinitionValue(substring(., 1, 1), 'Speech', 'SIPrefixes') != '' and
                                    IsInDefinition(substring(., 2), 'Speech', 'NeuterUnits') != ''
                                  ) ]"  
  replace:
  - x: "IfThenElse($NeuterUnitFollows, 'ett', 1)"

# the order of matching is
# 1. does it match the base of an SI unit
# 2. does it match an English unit (if in an English language)
# 3. does it match an SI prefix followed by an SI that accepts SI prefixes
# Due to this order, some things like "ft" and "cd" mean "feet" vs "femto-tonnes" and "pints" vs "pico-tonnes"
- name: unit
  tag: unit
  match: "."
  variables:
    # If the coefficient is singular, we don't add the plural ending. Finding the coefficient is tricky
    # Normal case (A) "3m" (parents is mrow), but could also be (B) "3 m^2" (etc.) (parent is power/mrow)
    # But it might be in a fraction as (C) "3 m/s" (parent is fraction/mrow) or (D) "3 m^2/s^2" (parent is power/fraction/mrow)
    #   or even (E) {3 m^2}/s (parent is power/mrow)
    # If in a fraction, only look in the numerator to find the coefficient
    # Note: we have a special case for pseudo-scripts like "°" (degrees) which are not powers -- they are essentially "1^°"
    # The following "IfThenElse" logic returns the mrow that potentially contains the coefficient, if it exists
    # The tests are in the order A, B & E, C, D
  - MRowForCoefficient: "IfThenElse(parent::m:mrow, parent::m:mrow,
                         IfThenElse(parent::m:power, ancestor::*[2][self::m:mrow],
                         IfThenElse(parent::m:fraction and not(preceding-sibling::*), ancestor::*[2][self::m:mrow],
                         IfThenElse(parent::m:power[parent::m:fraction and not(preceding-sibling::*)], ancestor::*[3][self::m:mrow], false()) ) ) )"
  # If a common fraction occurs before the unit, then it is singular, which means we have to copy the rules for common fractions (ugly!!!)
  # It is also singular for a mixed fraction; in this case, the common fraction is buried inside of an mrow and we cheat and check for InvisiblePlus (2064)
  - IsSingular: "(not($MRowForCoefficient) and parent::*[name(.)!='skip-super' or *[1][.=1]]) or
                 ($MRowForCoefficient and
                  $MRowForCoefficient[count(*) = 3 and 
                                      *[2]='\u2062' and
                                      *[1][(self::m:mn and .=1) or
                                           (self::m:fraction and
                                            IfThenElse($SpeechStyle='ClearSpeak',
                                                       $ClearSpeak_Fractions='Auto' or $ClearSpeak_Fractions='Ordinal' or $ClearSpeak_Fractions='EndFrac',
                                                       $SpeechStyle='SimpleSpeak') and
                                            *[1][self::m:mn][not(contains(., $DecimalSeparators)) and ($ClearSpeak_Fractions='Ordinal' or text()<20)] and
                                            *[2][self::m:mn][not(contains(., $DecimalSeparators)) and (2<= text() and text()<=10)]
                                           ) or
                                           (self::m:mrow and *[2][self::m:mo and .='\u2064'])
                                          ]
                                     ]
                  
                 )"
  - Prefix: "''"
  - Word: "''"  
  replace:
  - bookmark: "@id"
  - test:
    # is the whole string match a SI Unit without a prefix?
    - if: "DefinitionValue(., 'Speech', 'SIUnits') != ''"
      then:
      - set_variables: [Word: "DefinitionValue(., 'Speech', 'SIUnits')"]
    - else_if: "DefinitionValue(., 'Speech', 'UnitsWithoutPrefixes') != ''"
      then:
      - set_variables: [Word: "DefinitionValue(., 'Speech', 'UnitsWithoutPrefixes')"]
    - else_if: "DefinitionValue(., 'Speech', 'EnglishUnits') != ''"
      then:
      - set_variables: [Word: "DefinitionValue(., 'Speech', 'EnglishUnits')"]

    # do the first two chars match "da" and the remainder match an SIUnit
    - else_if: "string-length(.) >= 3 and 
                substring(., 1, 2) = 'da' and
                DefinitionValue(substring(., 3), 'Speech', 'SIUnits') != ''"
      then:
      - set_variables:
        - Prefix: "DefinitionValue('da', 'Speech', 'SIPrefixes')"
        - Word: "DefinitionValue(substring(., 3), 'Speech', 'SIUnits')"

    # does the first char match a prefix and the remainder match an SIUnit
    - else_if: "string-length(.) >= 2 and 
                DefinitionValue(substring(., 1, 1), 'Speech', 'SIPrefixes') != ''  and
                DefinitionValue(substring(., 2), 'Speech', 'SIUnits') != ''"
      then:
      - set_variables:
        - Prefix: "DefinitionValue(substring(., 1, 1), 'Speech', 'SIPrefixes')"
        - Word: "DefinitionValue(substring(., 2), 'Speech', 'SIUnits')"

    # not a known unit -- just speak the text, possibly as a plural
    - else:
      - set_variables:
        - Word: "text()"

  # somewhat complicated logic to avoid spaces around "-" as in "centi-grams" vs "centi - grams" -- probably doesn't matter
  - test:
      if: "$Prefix = ''"
      then:
      - test:
        - if: "$IsSingular"
          # HACK: '\uF8FE' is used internally for the concatenation char by 'ct' -- this gets the prefix concatinated to the base
          then: [x: "$Word"]
        - else_if: "DefinitionValue($Word, 'Speech', 'PluralForms') != ''"
          then: [x: "DefinitionValue($Word, 'Speech', 'PluralForms')"]
          else: [x: "$Word"]
      else:
      - x: "$Prefix"
      - ct: "-"
      - test:
        - if: "$IsSingular"
          # HACK: '\uF8FE' is used internally for the concatenation char by 'ct' -- this gets the prefix concatinated to the base
          then: [x: "concat('\uF8FE', $Word)"]
        - else_if: "DefinitionValue($Word, 'Speech', 'PluralForms') != ''"
          then: [x: "concat('\uF8FE', DefinitionValue($Word, 'Speech', 'PluralForms'))"]
          else: [x: "concat('\uF8FE', $Word)"]


- name: sin
  tag: mi
  match: ".='sin'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "sin"]        # phrase('sin' is the abbreviation for sine)
      else: [T: "sinus"]      # phrase(the 'sine' of the angle)
      # SWEDISH/NORWEGIAN: Added short form.

- name: cos
  tag: mi
  match: ".='cos'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "cos"]      # phrase('cos' is the abbreviation for cosine)
      else: [T: "cosinus"]      # phrase(find the 'cosine' in a right-angle triangle)

- name: tan
  tag: mi
  match: ".='tan' or .='tg'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "tan"]      # phrase(the 'tan' is the ratio of the opposite to the adjacent side of a right-angled triangle)
      else: [T: "tangens"]      # phrase(a 'tangent' is a straight line that touches a curve)

- name: sec
  tag: mi
  match: ".='sec'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "sec"]         # phrase(to 'seek' a solution)
      else: [T: "sekans"]     # phrase(a 'secant' intersects a curve at two or more points)

- name: csc
  tag: mi
  match: ".='csc'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "cosec"]         # phrase(we will 'cosecant' a solution)
      else: [T: "cosekans"]      # phrase(the 'cosecant' is the reciprocal of the secant)

- name: cot
  tag: mi
  match: ".='cot'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "cotan"]          # phrase(find the 'cotangent' in a right-angle triangle)
      else: [T: "cotangens"]      # phrase(the 'cotangent' is the reciprocal of the tangent)

# NORWEGIAN: Removed tests for verbosity. We don't say "sin hype" etc., and "sinh" sounds like "sin"
- name: sinh
  tag: mi
  match: ".='sinh'"
  replace:
  - bookmark: "@id"
  - T: "sinus hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "sin hype"}                # phrase(the word 'sinch' is an abbreviation for hyperbolic sine)
      #else: {T: "sinus hyperbolicus"}      # phrase(the 'hyperbolic sine' is used in mathematics) 

- name: cosh
  tag: mi
  match: ".='cosh'"
  replace:
  - bookmark: "@id"
  - T: "cosinus hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "cos hype"}                  # phrase('cosh' is an abbreviation of hyperbolic cosine)
      #else: {T: "cosinus hyperbolicus"}      # phrase(the 'hyperbolic cosine' is a mathematical function) 

- name: tanh
  tag: mi
  match: ".='tanh'"
  replace:
  - bookmark: "@id"
  - T: "tangens hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "tan hype"}                  # phrase('tanch' is shorthand for hyperbolic tangent)
      #else: {T: "tangens hyperbolicus"}      # phrase('hyperbolic tangent' is a mathematical function)

- name: sech
  tag: mi
  match: ".='sech'"
  replace:
  - bookmark: "@id"
  - T: "sekans hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "sec hype"}                   # phrase('sheck' is shorthand for hyperbolic secant)
      #else: {T: "sekans hyperbolicus"}        # phrase('hyperbolic secant' is a mathematical function) 

- name: csch
  tag: mi
  match: ".='csch'"
  replace:
  - bookmark: "@id"
  - T: "cosekans hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "cosec hype"}                   # phrase('cosheck' is shorthand for hyperbolic cosecant)
      #else: {T: "cosekans hyperbolicus"}        # phrase('hyperbolic cosecant' is a mathematical function) 

- name: coth
  tag: mi
  match: ".='coth'"
  replace:
  - bookmark: "@id"
  - T: "cotangens hyperbolicus"
  #- test:
      #if: "$Verbosity='Terse'"
      #then: {T: "cotan hype"}                  # phrase('cotanch' is shorthand for hyperbolic cotangent)
      #else: {T: "cotangens hyperbolicus"}      # phrase(the 'hyperbolic cotangent' is a mathematical function) 

# NORWEGIAN: Added this rule from English version
- name: exponential
  tag: mi
  match: ".='exp'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$Verbosity='Terse'"
      then: [T: "exp"]      # phrase('exp' means exponential function)
      else: [T: "eksponensialfunksjonen"]      # phrase('exponential' function)  #Norwegian: Spelled wrong for correct pronunciation

# NORWEGIAN: Added this rule from English version, but removed test for verbosity.    
- name: covariance
  tag: mi
  match: ".='Cov'"
  replace:
  - bookmark: "@id"
  - T: "kovarians"

- # handle both log and ln
  name: log
  tag: mi
  variables: [log_is_simple: "IsNode(following-sibling::*[2],'simple')"] # skip function apply
  match: ".='log' or .='ln' or .='lg'"
  replace:
  - bookmark: "@id"
  - test:
      if: "$log_is_simple"
      then_test:
      - if: ".='log'"
        then: [T: "log"]      # phrase(the 'log' function is used in mathematics)
      - else_if: ".='lg'"
        then: [spell: "'lg'"]      # phrase(the 'lg' of x)
      # SWEDISH/NORWEGIAN: We have added the term "lg" for the logarithm with base 10. Also added a verbosity test for long and short form.
      - else_if: "$Verbosity!='Verbose'"
        then: [spell: "'ln'"]
        else: [T: "den naturlige logaritmen"]      # phrase(the 'natural log' function is used in mathematics)
      else:
      - test:
        - if: ".='log'"
          then_test: 
          - if: "$Verbosity!='Verbose'"
            then: [T: "log"]      # phrase(the 'log' function is used in mathematics)
            else: [T: "logaritmen"]
        - else_if: ".='lg'"
          then_test:
          - if: "$Verbosity!='Verbose'"
            then: [spell: "'lg'"]      # phrase(the 'lg' of x)
            else: [T: "tier-logaritmen"]
        - else_if: "$Verbosity='Terse'"
          then: [spell: "'ln'"]
          else: [T: "den naturlige logaritmen"]      # phrase(the 'natural log' function is used in mathematics)


# NORWEGIAN: Changed from Swedish to English version.
- name: multi-line
  #   that eliminates the need for the if: else_if: ...
  # IDEA:  set a variable with the word to say for the row (e.g., RowLabel = Row/Case/Line/...)
  tag: [piecewise, system-of-equations, lines]
  match: "."
  variables:
    # Wikipedia has some tables where all the entire first column is empty (e.g., https://en.wikipedia.org/wiki/List_of_trigonometric_identities)
  - LineCount: "count(*[not(contains(@data-intent-property, ':continued-row:'))])"
  - NextLineIsContinuedRow: "false()"   # default value
  - IsColumnSilent: true()
  replace:
  - x: "$LineCount"
  - test:
    - if: "self::m:piecewise"
      then:
        - test:
          - if: "$LineCount != 1"
            then: [T: "tilfeller"]
            else: [T: "tilfelle"]      # phrase(this is the first 'case' of three cases)
    - else_if: "self::m:system-of-equations"
      then:
        - test:
          - if: "$LineCount != 1"
            then: [T: "likninger"]
            else: [T: "likning"]      # phrase(this is the first 'equation' of three equations)
    - else:
      - test:
        - if: "$LineCount != 1"
          then: [T: "rader"]
          else: [T: "rad"]      # phrase(this is the first 'line' of three lines)
  - pause: short
  - x: "*"

# NORWEGIAN: Changed from Swedish to English version.
- name: 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 contains(@data-intent-property, ':continued-row:'))"
      then:
      - pause: medium
      - test:
        - if: "parent::m:piecewise"
          then: [T: "tilfelle"]     # phrase('case' 1 of 10 cases)
        - else_if: "parent::m:system-of-equations"
          then: [T: "likning"] # phrase('equation' 1 of 10 equations)
          else: [T: "rad"]     # phrase('line 1 of 10 lines)
      - x: "count(preceding-sibling::*[not(contains(@data-intent-property, ':continued-row:'))]) + 1"
  - test:
      if: "self::m:mlabeledtr"
      then:
      - T: "med etiketten"      # phrase(the diagram is complete 'with label')
      - x: "*[1]/*"
  - test:
      if: "not(contains(@data-intent-property, ':continued-row:'))"
      then: [pause: medium]
  - test:
      if: "self::m:mlabeledtr"
      then: [x: "*[position()>1]"]
      else: [x: "*"]

# NORWEGIAN: Changed from Swedish to English version.
- name: default-multiline
  tag: mtd
  match: "parent::*[parent::m:piecewise or parent::m:system-of-equations or parent::m:lines]"
  variables: [LongPause: "$SpeechStyle = 'ClearSpeak' and $ClearSpeak_MultiLinePausesBetweenColumns = 'Long'"]
  replace:
  - test:
      if: "IsInDefinition(*[1], 'Speech', 'ComparisonOperators')"
      then: [pause: short]
  - test:
      if: "*[1][@data-added!='missing-content']"
      then: [x: "*"]
  - test:
    # no pause after each element; medium pause if last element in a row; long pause for last element in matrix unless ClearSpeak override
    - if: "count(following-sibling::*) = 0 and not($NextLineIsContinuedRow)"
      then_test:
          if: "count(../following-sibling::*) > 0"
          then_test:
              if: "$LongPause"
              then: [pause: long]
              else: [pause: medium]
      else_test:
          if: "IsInDefinition(*[1], 'Speech', 'ComparisonOperators')"
          then: [pause: short]
          else: [pause: auto]

# Matrix/Determinant rules
# matrix and determinant are the same other than "matrix"/"determinant" based on the bracketing chars
# the pausing logic is pushed down to the <mtd>
# the rules either speak the <mtr>s (to get "row n") or the <mtd>s. "column n" spoken if $IsColumnSilent is false
- name: 1x1-matrix
  tag: [matrix, determinant]
  variables: [IsColumnSilent: true()]
  match: "count(*)=1 and *[self::m:mtr][count(*) = 1]"
  replace:
  - T: "1 ganger 1"      # phrase(the '1 by 1' matrix)
  - test:
      if: "self::m:determinant" # just need to check the first bracket since we know it must be (, [, or |
      then: [T: "determinant"]      # phrase(the 2 by 2 'determinant'))
      else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')
      
  - T: "med element"      # phrase(the 2 by 2 matrix 'with entry' x)
  - x: "*[1]/*"


# simpler reading methods for special case matrices
# NORWEGIAN: Added these rules from the English.
- name: zero-matrix
  tag: matrix
  # select all the non-zero entries -- if there are none of them, then it is a zero matrix
  match: "not( */*/*[not(self::m:mn and .= 0)] )"
  replace:
  - x: count(*)
  - T: "ganger"       # phrase(the 1 'by' 2 matrix)
  - x: count(*[self::m:mtr][1]/*)
  - T: "null-matrise"   # phrase(the 2 by 2 'zero matrix')
  - pause: long

- name: identity-matrix
  tag: matrix
  # select all the non-zero entries...if they are not on the diagonal, or are != 1
  #   if there are any of them, then this isn't an identity matrix
  match:
  - "count(*) = count(*[1]/*) and "
  - "not( */*/*[not(self::m:mn and .= 0)]"
  - "          [count(../preceding-sibling::*)!=count(../../preceding-sibling::*) or .!= 1]"
  - "    )"
  replace:
  - x: count(*)
  - T: "ganger"       # phrase(the 1 'by' 2 matrix)
  - x: count(*[self::m:mtr][1]/*)
  - T: "identitetsmatrise"   # phrase(the 2 by 2 'identity matrix')
  - pause: long

- name: diagonal-matrix
  tag: matrix
  # select all the non-zero entries...if they are not on the diagonal
  #   if there are any of them, then this isn't an identity matrix
  match:
  - "count(*) = count(*[1]/*) and "
  - "not( */*/*[not(self::m:mn and .= 0)]"
  - "          [count(../preceding-sibling::*)!=count(../../preceding-sibling::*)]"
  - "    )"
  replace:
  - x: count(*)
  - T: "ganger"       # phrase(the 1 'by' 2 matrix)
  - x: count(*[self::m:mtr][1]/*)
  - T: "diagonalmatrise"   # phrase(the 2 by 2 'diagonal matrix')
  - pause: long
  - insert:
      # this lists the diagonal 'mtd's to be read, and they say "column nnn" before reading the contents
      # there seems to be an xpath bug -- without the parens, the match fails for the
      #   test Languages::en::mtable::diagonal_matrix due to match failure (the third matching element seems to be missing)
      nodes: "(*/*/*[not(self::m:mn and .= 0)]/..)"
      replace: [pause: auto]
  - pause: long

# simpler reading methods for smaller matrices if the entries are simple
- name: 2-or-3x1-matrix
  tag: matrix
  variables: [IsColumnSilent: true()]
  match:
  - "$ClearSpeak_Matrix != 'SpeakColNum' and " # "simple" isn't used for this preference
  - "*[self::m:mtr][count(*) = 1] and " # one column
  - count(*)<=3 and # at least two rows
  - IsNode(*/*/*,'simple') # IsNode() returns true if all the nodes are simple
  replace:
  - x: count(*)
  - T: "ganger 1"      # phrase(the 2 'by 1 column' matrix)
  - test:
      if: "$ClearSpeak_Matrix = 'Vector' or $ClearSpeak_Matrix = 'EndVector'"
      then: [T: "kolonne-vektor"]      # phrase(the 2 by 2 'vector')
      else: [T: "kolonne-matrise"]      # phrase(the 2 by 2 'matrix')
  - pause: long
  - x: "*/*"
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase('end' of matrix)
      - test:
          if: $ClearSpeak_Matrix = 'EndVector'
          then: [T: "vektor"]      # phrase(the 2 column 'vector')
          else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')

- name: default-column-matrix
  tag: matrix
  variables: [IsColumnSilent: true()]
  match: "*[self::m:mtr][count(*) = 1]"
  replace:
  - x: "count(*)"
  - T: "ganger 1"      # phrase(the 2 'by 1 column' matrix)
  - test:
      if: "$ClearSpeak_Matrix = 'Vector' or $ClearSpeak_Matrix = 'EndVector'"
      then: [T: "kolonne-vektor"]      # phrase(the 2 column 'vector')
      else: [T: "kolonne-matrise"]      # phrase(the 2 by 2 'matrix')
  - pause: long
  - x: "*" # select the rows (mtr)
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase('end' of matrix)
      - test:
          if: $ClearSpeak_Matrix = 'EndVector'
          then: [T: "vektor"]      # phrase(the 2 column 'vector')
          else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')

- name: 1x2-or-3-matrix
  tag: matrix
  variables: [IsColumnSilent: "$SpeechStyle = 'SimpleSpeak' or ($SpeechStyle = 'ClearSpeak' and $ClearSpeak_Matrix != 'SpeakColNum')"]
  match:
  - "$ClearSpeak_Matrix != 'SpeakColNum' and " # "simple" isn't used for this preference
  - count(*)=1  and # one row
  - count(*[1]/*)<=3 and # at least two cols
  - IsNode(*/*/*,'simple') # IsNode() returns true if all the nodes are simple
  replace:
  - T: "1 ganger"      # phrase('the 1 by' 2 matrix)
  - x: count(*/*)
  - test:
      if: "$ClearSpeak_Matrix = 'Vector' or $ClearSpeak_Matrix = 'EndVector'"
      then: [T: "rad-vektor"]      # phrase('the 1 by' 2 row 'vector')
      else: [T: "rad-matrise"]      # phrase('the 1 by' 2 'matrix')
  - pause: long
  - x: "*/*"
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase(the 'end' of matrix has been reached)
      - test:
          if: $ClearSpeak_Matrix = 'EndMatrix'
          then: [T: "matrise"]      # phrase(the 2 by 2 'matrix')
          else: [T: "vektor"]      # phrase(the 2 by 1 'vector')

- name: default-row-matrix
  tag: matrix
  variables: [IsColumnSilent: "$SpeechStyle = 'ClearSpeak' and $ClearSpeak_Matrix = 'SilentColNum'"]
  match: "count(*)=1" # one row
  replace:
  - T: "1 ganger"      # phrase('the 1 by' 2 matrix)
  - x: "count(*/*)"
  - test:
      if: "$ClearSpeak_Matrix = 'Vector' or $ClearSpeak_Matrix = 'EndVector'"
      then: [T: "rad-vektor"]      # phrase(the 2 by 1 'vector')
      else: [T: "rad-matrise"]      # phrase(the 2 by 2 'matrix')
  - pause: long
  - pause: medium
  - x: "*/*" # select the cols (mtd)
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase(the 'end' of matrix has been reached)
      - test:
          if: $ClearSpeak_Matrix = 'EndMatrix'
          then: [T: "matrise"]      # phrase(the 2 by 2 'matrix')
          else: [T: "vektor"]      # phrase(the 2 by 1 'vector')

- name: simple-small-matrix
  tag: [matrix, determinant]
  match:
  - "$ClearSpeak_Matrix != 'SpeakColNum' and " # "simple" isn't used for this preference
  - (count(*)<=3 and count(*[1]/*)<=3) and # no bigger than a 3x3 matrix
  - IsNode(*/*/*,'simple') # IsNode() returns true if all the nodes are simple
  variables: [IsColumnSilent: "$SpeechStyle = 'SimpleSpeak' or ($SpeechStyle = 'ClearSpeak' and $ClearSpeak_Matrix != 'SpeakColNum')"]
  replace:
  - x: count(*)
  - T: "ganger"      # phrase(the 1 'by' 2 matrix)
  - x: count(*[self::m:mtr][1]/*)
  - test:
      if: "self::m:determinant"
      then: [T: "determinant"]      # phrase(the 2 by 2 'determinant')
      else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')
  - pause: long
  - x: "*"
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase(the 'end' of matrix has been reached)
      - test:
          if: "self::m:determinant"
          then: [T: "determinant"]      # phrase(the 2 by 2 'determinant')
          else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')

- name: default-matrix
  tag: [matrix, determinant]
  variables: [IsColumnSilent: "$SpeechStyle = 'ClearSpeak' and $ClearSpeak_Matrix = 'SilentColNum'"]
  match: "."
  replace:
  - x: "count(*)"
  - T: "ganger"      # phrase(the 1 'by' 2 matrix)
  - x: "count(*[self::m:mtr][1]/*)"
  - test:
      if: "self::m:determinant"
      then: [T: "determinant"]      # phrase(the 2 by 2 'determinant')
      else: [T: "matrise"]      # phrase(the 2 by 2 'matrix')
  - pause: long
  - x: "*"
  - test:
      if: "$ClearSpeak_Matrix = 'EndMatrix' or $ClearSpeak_Matrix = 'EndVector'"
      then:
      - T: "slutt"      # phrase(the 'end' of matrix has been reached)
      - test:
          if: "self::m:determinant"
          then: [T: "determinant"]      # phrase(the 2 by 2 'determinant')
          else: [T: "matrise"]      # phrase(the 2 by 2 'matrix's)

- name: chemistry-msub
  tag: [chemical-formula]
  match: "*[1][.='msub']"
  replace:
  - x: "*[2]"
  - test:
      if: "$Verbosity='Verbose'"
      then: [T: "nedre indeks"]      # phrase(H 'subscript' 2)
      else_test:
        if: "$Verbosity='Medium'"
        then: [T: "senket"]      # phrase(H 'sub' 2)
  - x: "*[3]"
  - test: 
      if: "$Verbosity='Terse'"
      then: [pause: short]

#NORWEGIAN: Added from English. Not tested
- name: dimension-by
  tag: mrow
  match: dimension-product
  replace:
  - insert:
      nodes: "*"
      replace: [T: "ganger", pause: auto]      # phrase(3 'by' 5 matrix)
      
- name: chemistry-msup
  tag: [chemical-formula]
  match: "count(*)=3 and *[1][.='msup']"
  replace:
  - x: "*[2]"
  - test:
      if: "$Verbosity='Verbose'"
      then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
      else_test:
        if: "$Verbosity='Medium'"
        then: [T: "hevet"]      # phrase(H 'super' 2)
  - x: "*[3]"
  - test:
      if: "following-sibling::*[1][.='+' or .='-']" # a little lazy -- assumes chemistry superscripts end with + or -
      then: [pause: medium]

-
  # There currently is no way to do sub/super for n-ary number of args
  # Instead, we just deal with up to two prescripts and up to four postscripts (repeating blocks of similar code [UGLY!])
  # This hopefully covers all reasonable cases...
  name: chemistry-scripts
  tag: [chemical-formula, chemical-nuclide]
  variables:
  # computing the number of postscripts is messy because of <mprescripts> being optionally present -- we use "mod" to get the count right
  - Prescripts: "m:mprescripts/following-sibling::*"
  - NumChildren: "count(*)" # need to stash this since the count is wrong inside '*[...]' below
  - Postscripts: "*[position()>1 and position() < (last() + ($NumChildren mod 2) -count($Prescripts))]"
  match: . # should only be msubsup or mmultiscripts at this point
  replace:
  - test:
      if: "$Prescripts" # we have at least one pre sub/super 
      then:
      # nuclide: speak the superscript first
      - test:
          if: "not($Prescripts[2][self::m:none])"
          then:
          - test:
              if: "$Verbosity='Verbose'"
              then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
              else_test:
                if: "$Verbosity='Medium'"
                then: [T: "hevet"]      # phrase(H 'super' 2)
          - x: "$Prescripts[2]"
          - pause: "short"
      - test:
          if: "not($Prescripts[1][self::m:none])"
          then:
          - test:
              if: "$Verbosity='Verbose'"
              then: [T: "nedre indeks"]      # phrase(a 'subscript' may be used to indicate an index)
              else_test:
                if: "$Verbosity='Medium'"
                then: [T: "senket"]      # phrase(here is a 'sub' total)
          - x: "$Prescripts[1]"
          - pause: "short"
      - test:
          if: "count($Prescripts) > 2" # can this happen for chemistry??? we allow for one *extra* pre sub/super pair
          then:
          - test:
              if: "not($Prescripts[4][self::m:none])"
              then:
              - test:
                  if: "$Verbosity='Verbose'"
                  then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
                  else_test:
                    if: "$Verbosity='Medium'"
                    then: [T: "hevet"]      # phrase(H 'super' 2)
              - x: "$Prescripts[4]"
              - pause: "short"
          - test:
              if: "not($Prescripts[3][self::m:none])"
              then:
              - test:
                  if: "$Verbosity='Verbose'"
                  then: [T: "nedre indeks"]      # phrase(H 'subscript' 2)
                  else_test:
                    if: "$Verbosity='Medium'"
                    then: [T: "senket"]      # phrase(H 'sub' 2)
              - x: "$Prescripts[3]"
              - pause: "short"
  - x: "*[1]" # base
  - test:
      if: "$Postscripts"
      then:
      - test:
          if: "not($Postscripts[1][self::m:none])"
          then:
          - test:
              if: "$Verbosity='Verbose'"
              then: [T: "nedre indeks"]      # phrase(phrase(H 'subscript' 2)
              else_test:
                if: "$Verbosity='Medium'"
                then: [T: "senket"]      # phrase(phrase(H 'sub' 2)
          - x: "$Postscripts[1]"
          - pause: "short"
      - test:
          if: "not($Postscripts[2][self::m:none])"
          then:
          - test:
              if: "$Verbosity='Verbose'"
              then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
              else_test:
                if: "$Verbosity='Medium'"
                then: [T: "hevet"]          # phrase(H 'super' 2)
          - x: "$Postscripts[2]"
          - pause: "short"
      - test:
          if: "count($Postscripts) > 2"
          then:
          - test:
              if: "not($Postscripts[3][self::m:none])"
              then:
              - test:
                  if: "$Verbosity='Verbose'"
                  then: [T: "nedre indeks"]      # phrase(H 'subscript' 2)
                  else_test:
                    if: "$Verbosity='Medium'"
                    then: [T: "senket"]          # phrase(H 'sub' 2)
              - x: "$Postscripts[3]"
              - pause: "short"
          - test:
              if: "not($Postscripts[4][self::m:none])"
              then:
              - test:
                  if: "$Verbosity='Verbose'"
                  then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
                  else_test:
                    if: "$Verbosity='Medium'"
                    then: [T: "hevet"]          # phrase(H 'super' 2)
              - x: "$Postscripts[4]"
              - pause: "short"
          - test:
              if: "count($Postscripts) > 4"
              then:
              - test:
                  if: "not($Postscripts[5][self::m:none])"
                  then:
                  - test:
                      if: "$Verbosity='Verbose'"
                      then: [T: "nedre indeks"]    # phrase(H 'subscript' 2)
                      else_test:
                        if: "$Verbosity='Medium'"
                        then: [T: "senket"]        # phrase(H 'sub' 2)
                  - x: "$Postscripts[5]"
                  - pause: "short"
              - test:
                  if: "not($Postscripts[6][self::m:none])"
                  then:
                  - test:
                      if: "$Verbosity='Verbose'"
                      then: [T: "øvre indeks"]  # phrase(H 'superscript' 2)
                      else_test:
                        if: "$Verbosity='Medium'"
                        then: [T: "hevet"]      # phrase(H 'super' 2)
                  - x: "$Postscripts[6]"
                  - pause: "short"
              - test:
                  if: "count($Postscripts) > 6"
                  then:
                  - test:
                      if: "not($Postscripts[7][self::m:none])"
                      then:
                      - test:
                          if: "$Verbosity='Verbose'"
                          then: [T: "nedre indeks"]      # phrase(H 'subscript' 2)
                          else_test:
                            if: "$Verbosity='Medium'"
                            then: [T: "senket"]      # phrase(H 'sub' 2)
                      - x: "$Postscripts[7]"
                      - pause: "short"
                  - test:
                      if: "not($Postscripts[8][self::m:none])"
                      then:
                      - test:
                          if: "$Verbosity='Verbose'"
                          then: [T: "øvre indeks"]      # phrase(H 'superscript' 2)
                          else_test:
                            if: "$Verbosity='Medium'"
                            then: [T: "hevet"]      # phrase(H 'super' 2)
                      - x: "$Postscripts[8]"
                      - pause: "short"
      - test:
          if: "$Postscripts[last()][not(self::m:none)] and following-sibling::*[1][.='+' or .='-']"
          then: [pause: medium]

- name: chemistry
  tag: chemical-equation
  match: "."
  replace:
  - x: "*"


- name: chemical-element
  tag: chemical-element
  match: "."
  replace:
  - bookmark: "@id"
  - spell: text()
  - pause: short

- name: chemical-state
  tag: chemical-state
  match: "count(*)=1"
  replace:
  - bookmark: "*[1]/@id"
  - test:
    - if: ".='s'"
      then: [T: "fast"]      # phrase(Boron is a 'solid' in its natural state)
    - else_if: ".='l'"
      then: [T: "flytende"]      # phrase(water is a 'liquid')
    - else_if: ".='g'"
      then: [T: "gass"]      # phrase(hydrogen is a 'gass' )
      else: [T: "løst i vann"]      # phrase(an 'aqueous' solution is contained in water)
  - pause: short

- name: chemical-formula-operator-bond
  tag: chemical-formula-operator
  match: "@data-chemical-bond"
  replace:
  # FIX: this might be better/more efficient if in unicode.yaml
  - bookmark: "@id"
  - test:
    - if: ".='-' or .=':'"
      then: [T: "enkeltbinding"]      # phrase(a 'single bond' is formed when two atoms share one pair of electrons)
    - else_if: ".='=' or .='∷'"
      then: [T: "dobbeltbinding"]      # phrase(a 'double bond' may occur when two atoms share two pairs of electrons)
    - else_if: ".='≡'"
      then: [T: "trippelbindning"]      # phrase(a 'triple bond' occurs when two atoms share three pairs of electrons)
    - else_if: ".='≣'"
      then: [T: "firedobbel binding"]      # phrase(a 'quadruple bond' occurs when two atoms share four pairs of electrons)
      else: [x: "text()"]

- name: chemical-formula-operator
  tag: chemical-formula-operator
  match: "."
  replace:
    x: "text()"

- name: chemical-arrow-operator
  tag: chemical-arrow-operator
  match: "."
  replace:
  # FIX: this might be better/more efficient if in unicode.yaml
  - bookmark: "@id"
  - test:
    - if: ".='→' or .='⟶'"
      then_test:
        if: "$Verbosity='Terse'"
        then: [T: "danner"]      # phrase(hydrogen and oxygen 'forms' water )
        else: [T: "reaksjon danner"]      # phrase(hydrogen and oxygen 'reacts to form' water)
    - else_if: ".='⇌' or .='\u1f8d2'"
      then: [T: "er i likevekt med"]      # phrase(a reactant 'is in equilibrium with' a product)
    - else_if: ".='\u1f8d4'"
      then: [T: "er i likevekt med, venstre"]      # phrase(the reactant 'is in equilibrium biased to the left with' the product)
    - else_if: ".='\u1f8d3'"
      then: [T: "er i likevekt med, høyre"]      # phrase(the reactant 'is in equilibrium biased to the right with' the product)
      else: [x: "*"]

- name: chemical-equation-operator
  tag: chemical-equation-operator
  match: "."
  replace:
  - bookmark: "@id"
  - x: "text()"

- name: none
  tag: none
  match: "../../*[self::m:chemical-formula or self::m:chemical-nuclide]"
  replace:
  - T: "" # don't say anything

- name: ignore-intent-wrapper
  tag: intent-wrapper
  match: "."
  replace:
  - x: "*"