init: WordFileParse docx→HTML 解析服务(含 Word EQ 域公式修复)
服务:Spring Boot 3.2 + docx4j 8.3.9,POST /api/v3/server_office_file/file/parse
接受 .docx,返回 HTML(公式 → LaTeX,图片 → RustFS 外链)
公式支持三类来源:
- OMML(Word 2007+ 公式) → Python omml_mathml2latex
- MathType OLE 对象 → Ruby mathtype2mathml → MathML → LaTeX
- Word EQ 域(老教辅主力) → EqToLatexUtil(本次新增)
本次改动:
1. 新增 java-svc/.../core/EqToLatexUtil.java:
移植自 jby backend-services office-core,EQ 语法 → LaTeX
(\f→\dfrac、\r→\sqrt、\i→\int/\sum/\prod、\s→^{}/_{}、\b→\left\right、
\a→array、\x→\boxed/\overline、\o\al 堆叠、圈码、化学符号、极限、连等式)
品牌标记 JBYU* → WPU*,与采集端一致
2. CtoMathFinder:接上 EQ 分支(原为 not-ported 桩),域代码经
EqToLatexUtil.convert 转 LaTeX 写入 eqLatexMap(WPEQNUM 占位符)
3. WordParseUtil.dealPlaceholder:EQ 的 LaTeX 同样加 \( \) 包裹
(原先因 WPEQNUM 例外不加包裹,前端/MathJax 不渲染)
修前症状:教辅 Word 里分式/方程组/上下标整段丢失(如 {−1/3, 1} 变成 {-,1})
修后实测:46 份教辅 2908 处 EQ 域全部还原,公式总数 2954 处、零未包裹残留
安全:application.yml(含 RustFS 密钥)不入库,提供 application.yml.example;
OssService 的 @Value 默认密钥已清空
This commit is contained in:
194
scripts/omml_mathml2latex/visitors/mspace_visitor.py
Normal file
194
scripts/omml_mathml2latex/visitors/mspace_visitor.py
Normal file
@@ -0,0 +1,194 @@
|
||||
r"""
|
||||
Visitor for <mspace> (spacing) MathML elements.
|
||||
|
||||
The <mspace> element represents horizontal spacing in mathematical expressions.
|
||||
It uses the width attribute to specify the amount of space.
|
||||
|
||||
Common spacing types:
|
||||
- Named spaces: thinmathspace, mediummathspace, thickmathspace
|
||||
- Negative spaces: negativethinmathspace, etc.
|
||||
- Explicit sizes: 0.1em, 1em, 2em
|
||||
|
||||
This visitor maps MathML spacing to LaTeX spacing commands.
|
||||
"""
|
||||
|
||||
from lxml import etree as ET
|
||||
from ..core.interfaces import Visitor, Strategy
|
||||
from ..utils import local_name as _local
|
||||
|
||||
|
||||
class MspaceVisitor(Visitor):
|
||||
r"""
|
||||
Handles <mspace> (spacing) MathML elements.
|
||||
|
||||
Converts spacing specifications to LaTeX spacing commands.
|
||||
|
||||
Examples:
|
||||
Thin space:
|
||||
<mspace width="thinmathspace"/>
|
||||
→ \,
|
||||
|
||||
Medium space:
|
||||
<mspace width="mediummathspace"/>
|
||||
→ \:
|
||||
|
||||
Thick space:
|
||||
<mspace width="thickmathspace"/>
|
||||
→ \;
|
||||
|
||||
Quad space:
|
||||
<mspace width="1em"/>
|
||||
→ \quad
|
||||
|
||||
Qquad space:
|
||||
<mspace width="2em"/>
|
||||
→ \qquad
|
||||
|
||||
Negative space:
|
||||
<mspace width="negativethinmathspace"/>
|
||||
→ \!
|
||||
|
||||
Default (no width):
|
||||
<mspace/>
|
||||
→ \, (thin space)
|
||||
"""
|
||||
|
||||
# Mapping from MathML spacing names to LaTeX commands
|
||||
SPACING_MAP = {
|
||||
# Named mathematical spaces
|
||||
"thinmathspace": "\\,",
|
||||
"mediummathspace": "\\:",
|
||||
"thickmathspace": "\\;",
|
||||
"verythinmathspace": "\\!",
|
||||
"veryverythinmathspace": "\\!",
|
||||
|
||||
# Negative spaces (for fine-tuning)
|
||||
"negativethinmathspace": "\\!",
|
||||
"negativemediummathspace": "\\!",
|
||||
"negativethickmathspace": "\\!",
|
||||
|
||||
# Explicit em-based sizes
|
||||
"0em": "", # No space
|
||||
"0.1em": "\\,",
|
||||
"0.2em": "\\:",
|
||||
"0.3em": "\\;",
|
||||
"0.5em": "\\;",
|
||||
"1em": "\\quad",
|
||||
"2em": "\\qquad",
|
||||
|
||||
# Alternative common values
|
||||
"thin": "\\,",
|
||||
"medium": "\\:",
|
||||
"thick": "\\;",
|
||||
}
|
||||
|
||||
def __init__(self, strategy: Strategy):
|
||||
"""Initialize the Mspace visitor."""
|
||||
super().__init__(strategy)
|
||||
|
||||
def visit(self, node: ET.Element) -> str:
|
||||
r"""
|
||||
Convert <mspace> element to LaTeX spacing.
|
||||
|
||||
Args:
|
||||
node: The <mspace> element to convert
|
||||
|
||||
Returns:
|
||||
LaTeX spacing command (e.g., \,, \:, \;, \quad, \qquad)
|
||||
|
||||
Raises:
|
||||
ValueError: If node is not an <mspace> element
|
||||
"""
|
||||
if not self.can_handle(node):
|
||||
raise ValueError(f"MspaceVisitor cannot handle element '{node.tag}'")
|
||||
|
||||
# Get width attribute
|
||||
width = node.get("width", "")
|
||||
|
||||
# Handle empty width (default to thin space)
|
||||
if not width:
|
||||
return "\\,"
|
||||
|
||||
# Check if width is in our mapping
|
||||
if width in self.SPACING_MAP:
|
||||
return self.SPACING_MAP[width]
|
||||
|
||||
# Try to parse numeric width (e.g., "0.5em", "1.5em")
|
||||
spacing_cmd = self._parse_numeric_width(width)
|
||||
if spacing_cmd:
|
||||
return spacing_cmd
|
||||
|
||||
# Default: thin space
|
||||
return "\\,"
|
||||
|
||||
def _parse_numeric_width(self, width: str) -> str:
|
||||
"""
|
||||
Parse numeric width values and convert to LaTeX spacing.
|
||||
|
||||
Args:
|
||||
width: Width string (e.g., "0.5em", "1.5em")
|
||||
|
||||
Returns:
|
||||
LaTeX spacing command, or empty string if cannot parse
|
||||
"""
|
||||
# Remove whitespace
|
||||
width = width.strip()
|
||||
|
||||
# Try to extract numeric value from em-based widths
|
||||
if width.endswith("em"):
|
||||
try:
|
||||
value = float(width[:-2])
|
||||
|
||||
# Map numeric values to spacing commands
|
||||
if value <= 0:
|
||||
return ""
|
||||
elif value < 0.15:
|
||||
return "\\," # thin
|
||||
elif value < 0.25:
|
||||
return "\\:" # medium
|
||||
elif value < 0.6:
|
||||
return "\\;" # thick
|
||||
elif value < 1.5:
|
||||
return "\\quad"
|
||||
else:
|
||||
return "\\qquad"
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# Try to extract numeric value from pt-based widths
|
||||
if width.endswith("pt"):
|
||||
try:
|
||||
value = float(width[:-2])
|
||||
|
||||
# Map pt values to spacing (rough approximation)
|
||||
# 1em ≈ 10pt in typical math fonts
|
||||
if value <= 0:
|
||||
return ""
|
||||
elif value < 2:
|
||||
return "\\,"
|
||||
elif value < 4:
|
||||
return "\\:"
|
||||
elif value < 6:
|
||||
return "\\;"
|
||||
elif value < 12:
|
||||
return "\\quad"
|
||||
else:
|
||||
return "\\qquad"
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# Could not parse
|
||||
return ""
|
||||
|
||||
def can_handle(self, node: ET.Element) -> bool:
|
||||
"""Check if this visitor can handle the given node."""
|
||||
return _local(node.tag) == 'mspace'
|
||||
|
||||
@property
|
||||
def element_name(self) -> str:
|
||||
"""Get the element name this visitor handles."""
|
||||
return 'mspace'
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation for debugging."""
|
||||
return f"MspaceVisitor(strategy={self.strategy.name})"
|
||||
Reference in New Issue
Block a user