diff --git a/openedx/core/djangoapps/content/search/documents.py b/openedx/core/djangoapps/content/search/documents.py index f675998d54ce..98cd7d576e0a 100644 --- a/openedx/core/djangoapps/content/search/documents.py +++ b/openedx/core/djangoapps/content/search/documents.py @@ -3,7 +3,6 @@ """ from __future__ import annotations -import re import logging from hashlib import blake2b diff --git a/openedx/core/djangoapps/content/search/plain_text_math.py b/openedx/core/djangoapps/content/search/plain_text_math.py index 644e9826d297..c896815b7ac4 100644 --- a/openedx/core/djangoapps/content/search/plain_text_math.py +++ b/openedx/core/djangoapps/content/search/plain_text_math.py @@ -1,3 +1,7 @@ +""" +Helper class to convert mathjax equations to plain text. +""" + import re import unicodeit @@ -21,7 +25,7 @@ class PlainTextMath: ("\\cot", "cot"), ("\\sec", "sec"), ("\\csc", "csc"), - # Is used for matching brackets in mathjax, should be required in plain text. + # Is used for matching brackets in mathjax, should not be required in plain text. ("\\left", ""), ("\\right", ""), ) @@ -31,15 +35,15 @@ class PlainTextMath: def _fraction_handler(self, equation: str) -> str: """ - Converts `\frac{x}{y}` to `(x/y)` while handling nested `{}`. + Converts `\\frac{x}{y}` to `(x/y)` while handling nested `{}`. - For example: `\frac{2}{\sqrt{1+y}}` is converted to `(2/\sqrt{1+y})`. + For example: `\\frac{2}{\\sqrt{1+y}}` is converted to `(2/\\sqrt{1+y})`. Args: equation: string Returns: - String with `\frac` replaced by normal `/` symbol. + String with `\\frac` replaced by normal `/` symbol. """ start_index = equation.find("\\frac{") if start_index == -1: diff --git a/openedx/core/djangoapps/content/search/tests/test_documents.py b/openedx/core/djangoapps/content/search/tests/test_documents.py index efeb7efbcb61..d8d3ccd28fcb 100644 --- a/openedx/core/djangoapps/content/search/tests/test_documents.py +++ b/openedx/core/djangoapps/content/search/tests/test_documents.py @@ -490,16 +490,16 @@ def test_mathjax_plain_text_conversion_for_search(self): editor="raw", use_latex_compiler=True, data=( - "Simple addition: \( 2 + 3 \) |||" - " Simple subtraction: \( 5 - 2 \) |||" - " Simple multiplication: \( 4 * 6 \) |||" - " Simple division: \( 8 / 2 \) |||" - " Mixed arithmetic: \( 2 + 3 4 \) |||" - " Simple exponentiation: \[ 2^3 \] |||" - " Root extraction: \[ 16^{1/2} \] |||" - " Exponent with multiple terms: \[ (2 + 3)^2 \] |||" - " Nested exponents: \[ 2^(3^2) \] |||" - " Mixed roots: \[ 8^{1/2} 3^2 \] |||" + "Simple addition: \\( 2 + 3 \\) |||" + " Simple subtraction: \\( 5 - 2 \\) |||" + " Simple multiplication: \\( 4 * 6 \\) |||" + " Simple division: \\( 8 / 2 \\) |||" + " Mixed arithmetic: \\( 2 + 3 4 \\) |||" + " Simple exponentiation: \\[ 2^3 \\] |||" + " Root extraction: \\[ 16^{1/2} \\] |||" + " Exponent with multiple terms: \\[ (2 + 3)^2 \\] |||" + " Nested exponents: \\[ 2^(3^2) \\] |||" + " Mixed roots: \\[ 8^{1/2} 3^2 \\] |||" " Simple fraction: [mathjaxinline] 3/4 [/mathjaxinline] |||" " Decimal to fraction conversion: [mathjaxinline] 0.75 = 3/4 [/mathjaxinline] |||" " Mixed fractions: [mathjaxinline] 1 1/2 = 3/2 [/mathjaxinline] |||" @@ -508,15 +508,15 @@ def test_mathjax_plain_text_conversion_for_search(self): " Trig identities: [mathjaxinline] \\sin(x + y) = \\sin(x) \\cos(y) + \\cos(x) \\sin(y) [/mathjaxinline] |||" " Hyperbolic trig functions: [mathjaxinline] \\sinh(x) [/mathjaxinline] [mathjaxinline] \\cosh(x) [/mathjaxinline] |||" " Simple derivative: [mathjax] f(x) = x^2, f'(x) = 2x [/mathjax] |||" - " Double integral: [mathjax] int\int (x + y) dxdy [/mathjax] |||" - " Partial derivatives: [mathjax] f(x,y) = xy, \frac{\partial f}{\partial x} = y [/mathjax] [mathjax] \frac{\partial f}{\partial y} = x [/mathjax] |||" - " Mean and standard deviation: [mathjax] mu = 2, \sigma = 1 [/mathjax] |||" - " Binomial probability: [mathjax] P(X = k) = (\binom{n}{k} p^k (1-p)^{n-k}) [/mathjax] |||" - " Gaussian distribution: [mathjax] N(\mu, \sigma^2) [/mathjax] |||" + " Double integral: [mathjax] int\\int (x + y) dxdy [/mathjax] |||" + " Partial derivatives: [mathjax] f(x,y) = xy, \\frac{\\partial f}{\\partial x} = y [/mathjax] [mathjax] \\frac{\\partial f}{\\partial y} = x [/mathjax] |||" + " Mean and standard deviation: [mathjax] mu = 2, \\sigma = 1 [/mathjax] |||" + " Binomial probability: [mathjax] P(X = k) = (\\binom{n}{k} p^k (1-p)^{n-k}) [/mathjax] |||" + " Gaussian distribution: [mathjax] N(\\mu, \\sigma^2) [/mathjax] |||" " Greek letters: [mathjaxinline] \\alpha [/mathjaxinline] [mathjaxinline] \\beta [/mathjaxinline] [mathjaxinline] \\gamma [/mathjaxinline] |||" " Subscripted variables: [mathjaxinline] x_i [/mathjaxinline] [mathjaxinline] y_j [/mathjaxinline] |||" " Superscripted variables: [mathjaxinline] x^{i} [/mathjaxinline] |||" - " Not supported: \( \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I \)" + " Not supported: \\( \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I \\)" ), ) # pylint: enable=line-too-long @@ -543,9 +543,9 @@ def test_mathjax_plain_text_conversion_for_search(self): 'Hyperbolic trig functions: sinh(x) cosh(x)', "Simple derivative: f(x) = x², f'(x) = 2x", 'Double integral: int∫ (x + y) dxdy', - 'Partial derivatives: f(x,y) = xy, rac{∂ f}{∂ x} = y rac{∂ f}{∂ y} = x', + 'Partial derivatives: f(x,y) = xy, (∂ f/∂ x) = y (∂ f/∂ y) = x', 'Mean and standard deviation: mu = 2, σ = 1', - 'Binomial probability: P(X = k) = (inom{n}{k} pᵏ (1−p)ⁿ⁻ᵏ)', + 'Binomial probability: P(X = k) = (\\binom{n}{k} pᵏ (1−p)ⁿ⁻ᵏ)', 'Gaussian distribution: N(μ, σ²)', 'Greek letters: α β γ', 'Subscripted variables: xᵢ yⱼ',