feat(jats/inline-formula): Support emphasis elements and Inline formulas#3726
feat(jats/inline-formula): Support emphasis elements and Inline formulas#3726taru-garg-2000 wants to merge 6 commits into
Conversation
Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
|
✅ DCO Check Passed Thanks @taru-garg-2000, all your commits are properly signed off. 🎉 |
Merge Protections🔴 1 of 2 protections blocking · waiting on 👀 reviews
🔴 Require two reviewer for test updatesWaiting for
This rule is failing.When test data is updated, we require two reviewers
Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This is correct and it affects all parsers. Just accept it as the normal behavior for now. There is another PR in docling-core that will fix it. |
ceberam
left a comment
There was a problem hiding this comment.
Thanks @taru-garg-2000 for your PR. Please, see my comments below.
I was also wondering if you could extend the styling of text beyond the inline formulas, since it is still missing. I guess it would be pretty simple to reuse the static methods you created to add the formatting of text.
| math_parts = tex_math.text.split("$$") | ||
| return math_parts[1] if len(math_parts) == 3 else tex_math.text.strip() | ||
| math_parts = node.text.split("$$") | ||
| return math_parts[1] if len(math_parts) == 3 else None |
There was a problem hiding this comment.
Why did you remove the explicit fallback for tex-math content that is not wrapped in $$...$$?
Real JATS files from publishers frequently emit bare tex-math without $$ delimiters. Those formulas now silently disappear.
There was a problem hiding this comment.
So I saw that in the previous PR the behaviour diverged for how tex-math is handled in inline-formula and how it is handled in _add_equation with this PR I wanted to reduce the duplication and since _add_equation existed before, I adapted my implementation considering that as the base, will add back the fallback if that is more favourable.
There was a problem hiding this comment.
It's a good point to align the implementation for both cases. Actually the best approach would have been to use a utility function to avoid the risk of divergence (or we could just reuse _extract_tex_math).
Instead of regressing the code above, we should improve _extract_tex_math by adding the fallback. Actually, according to JATS, the formula inside <tex-math> should not be surrounded by $$ or $ symbols.
Also please note that the original code was treating <tex-math> regardless of the container and creating a Docling document item of type DocItemLabel.FORMULA . The issue we want to resolve here is to enable inline formulas through a Docling document inline group. Therefore it is not enough to add a block elif child.tag == "inline-formula" in _walk_linear to capture the container of the inline formula. We should also consider the container of the block-level formula, which is <disp-formula> to distinguish both cases and then be able to reuse the handing of <tex-math>.
It would also help having a real JATS document in the test suite that includes both inline and block-level formulas. Let me know if you need any help here.
There was a problem hiding this comment.
For the real JATS document I was wondering if the 2nd document from here would be acceptable ? It seems to contain good number of samples of inline-formula, disp-formula and emphasis tags.
There was a problem hiding this comment.
That looks like a nice sample document with all kind of formulas. Unfortunately, I don't see an explicit open license in the document. We should only include CC0 or CC-BY documents.
There was a problem hiding this comment.
Ahh okay no worries, I will test on it for now, and see how the changes perform, and find another doc for adding hopefully I find one with an open license. Apologies for the delay on this, I hope to rework it within this week.
|
Hi @ceberam , Thanks a lot for the review! Happy to extend it for generalised implementation on formatting tags, I was wondering though if it would be better to keep it as a separate PR ? |
Let's do it in this PR since it will help us better design the formatting in a reusable manner. |
Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
Generalize the inline walker to accumulate styled InlineSegments so bold, italic, underline, strike, sub, and sup formatting is preserved for all paragraph text, not only inside inline formulas. Regenerate JATS groundtruth to include the recovered body emphasis. Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
|
Hi @ceberam, There's some polishing still left I feel, however was looking to get an early review if the approach looks good ? For \documentclass[12pt]{minimal} \usepackage{wasysym} \usepackage[substack]{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage[mathscr]{eucal} \usepackage{mathrsfs} \DeclareFontFamily{T1}{linotext}{} \DeclareFontShape{T1}{linotext}{m}{n} { <-> linotext }{} \DeclareSymbolFont{linotext}{T1}{linotext}{m}{n} \DeclareSymbolFontAlphabet{\mathLINOTEXT}{linotext} \begin{document} $$ \frac{dA(t)}{dt}={\gamma}_{i,j,B} \left( { \,\substack{ ^{3} \\ {\sum} \\ _{i=1} }\, }I_{i,F,B}(t)+I_{i,M,B}(t) \right) -{\mu}_{A}A(t)-{\delta}A(t), $$ \end{document}Which kind of corrupts the render of actual formula \frac{dA(t)}{dt}={\gamma}_{i,j,B} \left( { \,\substack{ ^{3} \\ {\sum} \\ _{i=1} }\, }I_{i,F,B}(t)+I_{i,M,B}(t) \right) -{\mu}_{A}A(t)-{\delta}A(t)Although it is latex, we don't really render it correctly when the conversion to markdown happens. |
Condense the multi-line docstring/comments added for the inline emphasis walker, and remove the markdown-rendering test that duplicated the italic-inside-formula structural case. Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
…ula-emphasis Signed-off-by: Taru Garg <taru.garg@hashicorp.com>
|
Polished up the remaining bit, so for the current implementation we check if the text in a |
#3696 landed inline
tex-mathrendering for JATS as the first step. This continues that work by handling the styling that can sit alongside the formula inside an<inline-formula>.When we walk an
<inline-formula>, we now break it into ordered inline pieces instead of one flat string: thetex-mathbecomes the formula, and emphasis around it (italic,bold,underline,strike,sub,sup) is kept as styled text. MathML<mml:math>or other alternatives are skipped — as MathML isn't parsed yet, so we rely on thetex-mathsibling instead. The pieces go into an inline group so everything still reads as one continuous line.A couple of examples:
The
xstays italic right next to thea^2formula.One thing I noticed with things like Superscript or Subscript, there is a space in between the element which carry that super/sub-script and the actual element in the sub/super-script. To explain, when parsing something like
We would expect to be rendered as x2 but it actually renders as x
2 (notice the space), it seems this is because how the elements are serialized, but I haven't looked into it that much.