Skip to content

Commit

Permalink
fix: lint issues and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Dec 23, 2024
1 parent 05deade commit 666d272
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
1 change: 0 additions & 1 deletion openedx/core/djangoapps/content/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
from __future__ import annotations

import re
import logging
from hashlib import blake2b

Expand Down
12 changes: 8 additions & 4 deletions openedx/core/djangoapps/content/search/plain_text_math.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Helper class to convert mathjax equations to plain text.
"""

import re

import unicodeit
Expand All @@ -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", ""),
)
Expand All @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions openedx/core/djangoapps/content/search/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] |||"
Expand All @@ -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
Expand All @@ -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ⱼ',
Expand Down

0 comments on commit 666d272

Please sign in to comment.