mathspeak_korean_util.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { Span } from '../audio/span.js';
  2. import * as MathspeakUtil from './mathspeak_util.js';
  3. import { LOCALE } from '../l10n/locale.js';
  4. import * as XpathUtil from '../common/xpath_util.js';
  5. export function nestedFraction(node, expr, opt_end) {
  6. const depth = MathspeakUtil.fractionNestingDepth(node);
  7. const annotation = [...Array(depth)].map((_x) => expr);
  8. if (opt_end) {
  9. annotation.unshift(opt_end);
  10. }
  11. return annotation.join(LOCALE.MESSAGES.regexp.JOINER_FRAC);
  12. }
  13. export function openingFractionVerbose(node) {
  14. return Span.singleton(nestedFraction(node, LOCALE.MESSAGES.MS.START, LOCALE.MESSAGES.MS.FRAC_V));
  15. }
  16. export function closingFractionVerbose(node) {
  17. return Span.singleton(nestedFraction(node, LOCALE.MESSAGES.MS.END, LOCALE.MESSAGES.MS.FRAC_V));
  18. }
  19. export function openingFractionBrief(node) {
  20. return Span.singleton(nestedFraction(node, LOCALE.MESSAGES.MS.START, LOCALE.MESSAGES.MS.FRAC_B));
  21. }
  22. export function closingFractionBrief(node) {
  23. return Span.singleton(nestedFraction(node, LOCALE.MESSAGES.MS.END, LOCALE.MESSAGES.MS.FRAC_B));
  24. }
  25. export function openingFractionSbrief(node) {
  26. const depth = MathspeakUtil.fractionNestingDepth(node);
  27. if (depth === 1) {
  28. return Span.singleton(LOCALE.MESSAGES.MS.FRAC_S);
  29. }
  30. return Span.singleton(LOCALE.FUNCTIONS.combineNestedFraction(LOCALE.FUNCTIONS.radicalNestDepth(depth - 1), LOCALE.MESSAGES.MS.NEST_FRAC, LOCALE.MESSAGES.MS.FRAC_S));
  31. }
  32. export function closingFractionSbrief(node) {
  33. const depth = MathspeakUtil.fractionNestingDepth(node);
  34. if (depth === 1) {
  35. return Span.singleton(LOCALE.MESSAGES.MS.ENDFRAC);
  36. }
  37. return Span.singleton(LOCALE.FUNCTIONS.combineNestedFraction(LOCALE.FUNCTIONS.radicalNestDepth(depth - 1), LOCALE.MESSAGES.MS.NEST_FRAC, LOCALE.MESSAGES.MS.ENDFRAC));
  38. }
  39. export function overFractionSbrief(node) {
  40. const depth = MathspeakUtil.fractionNestingDepth(node);
  41. if (depth === 1) {
  42. return Span.singleton(LOCALE.MESSAGES.MS.FRAC_OVER);
  43. }
  44. return Span.singleton(LOCALE.FUNCTIONS.combineNestedFraction(LOCALE.FUNCTIONS.radicalNestDepth(depth - 1), LOCALE.MESSAGES.MS.NEST_FRAC, LOCALE.MESSAGES.MS.FRAC_OVER));
  45. }
  46. export function isSimpleIndex(node) {
  47. const index = XpathUtil.evalXPath('children/*[1]', node)[0]
  48. .toString()
  49. .match(/[^>⁢>]+<\/[^>]*>/g);
  50. return index.length === 1 ? [node] : [];
  51. }
  52. export function nestedRadical(node, prefix, postfix) {
  53. const depth = MathspeakUtil.radicalNestingDepth(node);
  54. if (depth === 1)
  55. return postfix;
  56. return LOCALE.FUNCTIONS.combineNestedRadical(LOCALE.FUNCTIONS.radicalNestDepth(depth - 1), prefix, postfix);
  57. }
  58. export function openingRadicalVerbose(node) {
  59. return Span.singleton(nestedRadical(node, LOCALE.MESSAGES.MS.NESTED, LOCALE.MESSAGES.MS.STARTROOT));
  60. }
  61. export function closingRadicalVerbose(node) {
  62. return Span.singleton(nestedRadical(node, LOCALE.MESSAGES.MS.NESTED, LOCALE.MESSAGES.MS.ENDROOT));
  63. }
  64. export function openingRadicalBrief(node) {
  65. return Span.singleton(nestedRadical(node, LOCALE.MESSAGES.MS.NEST_ROOT, LOCALE.MESSAGES.MS.STARTROOT));
  66. }
  67. export function closingRadicalBrief(node) {
  68. return Span.singleton(nestedRadical(node, LOCALE.MESSAGES.MS.NEST_ROOT, LOCALE.MESSAGES.MS.ENDROOT));
  69. }
  70. export function openingRadicalSbrief(node) {
  71. return Span.singleton(nestedRadical(node, LOCALE.MESSAGES.MS.NEST_ROOT, LOCALE.MESSAGES.MS.ROOT));
  72. }
  73. export function getRootIndex(node) {
  74. const content = XpathUtil.evalXPath('children/*[1]', node)[0].textContent.trim();
  75. return LOCALE.MESSAGES.MSroots[content] || content + '제곱근';
  76. }
  77. export function indexRadical(node, postfix) {
  78. const index = getRootIndex(node);
  79. return index ? index : postfix;
  80. }
  81. export function indexRadicalVerbose(node) {
  82. return Span.singleton(indexRadical(node, LOCALE.MESSAGES.MS.ROOTINDEX));
  83. }
  84. export function indexRadicalBrief(node) {
  85. return Span.singleton(indexRadical(node, LOCALE.MESSAGES.MS.ROOTINDEX));
  86. }
  87. export function indexRadicalSbrief(node) {
  88. return Span.singleton(indexRadical(node, LOCALE.MESSAGES.MS.INDEX));
  89. }
  90. export function ordinalConversion(node) {
  91. const children = XpathUtil.evalXPath('children/*', node);
  92. return Span.singleton(LOCALE.NUMBERS.wordOrdinal(children.length));
  93. }
  94. export function decreasedOrdinalConversion(node) {
  95. const children = XpathUtil.evalXPath('children/*', node);
  96. return Span.singleton(LOCALE.NUMBERS.wordOrdinal(children.length - 1));
  97. }
  98. export function listOrdinalConversion(node) {
  99. const children = XpathUtil.evalXPath('children/*', node);
  100. const content = XpathUtil.evalXPath('content/*', node);
  101. return Span.singleton(LOCALE.NUMBERS.wordOrdinal(children.length - content.length));
  102. }
  103. export function checkDepth(node) {
  104. const roleList = [];
  105. const depth = getDepthValue(node, roleList);
  106. return depth > 3 ? [] : [node];
  107. }
  108. export function getDepthValue(node, roleList) {
  109. const role = node.getAttribute('role');
  110. const index = roleList.indexOf(role) > -1;
  111. if (!index) {
  112. roleList.push(role);
  113. }
  114. const children = XpathUtil.evalXPath('children/*', node);
  115. let max = 0, cur = 0;
  116. if (children.length) {
  117. children.forEach((child) => {
  118. cur = getDepthValue(child, roleList);
  119. max = cur > max ? cur : max;
  120. });
  121. return max + 1;
  122. }
  123. return 0;
  124. }