feat: add latex_figure_context_extractor_mapper operator#923
feat: add latex_figure_context_extractor_mapper operator#923liyuyi-2001 wants to merge 1 commit intodatajuicer:mainfrom
Conversation
Summary of ChangesHello @liyuyi-2001, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a powerful new data processing operator that significantly enhances the ability to extract structured information from LaTeX documents. By intelligently parsing figure environments, identifying associated captions, labels, image paths, and crucially, the textual context that cites these figures, it enables the creation of rich, figure-centric datasets. This functionality is particularly valuable for research papers and technical documents, allowing for deeper analysis and organization of visual content and its narrative integration. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new latex_figure_context_extractor_mapper operator, which is a significant feature for processing LaTeX documents. The implementation is comprehensive, covering various figure and subfigure environments, and is accompanied by a thorough set of unit tests. My review identified a critical bug in the logic for parsing parent figure information when subfigures are present, which could lead to incorrect data extraction. I've provided a fix for this issue. Additionally, I've included a minor suggestion to improve code conciseness. Overall, this is a great contribution once the critical issue is addressed.
| text_outside = fig_text | ||
| all_matches = sorted( | ||
| subfig_env_matches + subfig_cmd_matches | ||
| + subfig_nocap_matches, | ||
| key=lambda m: m.start(), | ||
| reverse=True, | ||
| ) | ||
| for m in all_matches: | ||
| text_outside = ( | ||
| text_outside[:m.start()] | ||
| + text_outside[m.end():] | ||
| ) |
There was a problem hiding this comment.
There is a bug in how text_outside is constructed. The loop modifies text_outside while using indices (m.start(), m.end()) that are relative to the original, unmodified fig_text. This leads to incorrect results when there are multiple subfigure matches. A more robust approach is to build the new string from the parts of the original string that are not within any match.
# Rebuild text_outside from parts of fig_text not in any match
all_matches = sorted(
subfig_env_matches + subfig_cmd_matches
+ subfig_nocap_matches,
key=lambda m: m.start(),
)
parts = []
last_end = 0
for m in all_matches:
parts.append(fig_text[last_end:m.start()])
last_end = m.end()
parts.append(fig_text[last_end:])
text_outside = ''.join(parts)| seen = set() | ||
| merged = [] | ||
| for p in parent_citing + sf_citing: | ||
| if p not in seen: | ||
| seen.add(p) | ||
| merged.append(p) |
There was a problem hiding this comment.
Add a new mapper operator that extracts figures and their citing context from LaTeX source. It parses figure/figure*/wrapfigure environments, handles subfigure environments and \subfigure/\subfloat commands, and finds prose paragraphs that cite each figure via \ref/\cref/\autoref. One input paper row fans out into N output figure rows (one per figure or subfigure). Samples without figures are dropped. Output fields: images, caption, label, citing_paragraphs, parent_caption, parent_label. Includes: - Operator implementation with recursive nested-brace regex support - Config entry in config_all.yaml - Registration in mapper __init__.py - Comprehensive unit tests (21 test cases) - Operator documentation (EN/CN) Made-with: Cursor
ebe11fb to
44659cc
Compare
yxdyc
left a comment
There was a problem hiding this comment.
plz resolve the gemini suggestion & my minor comment
others LGTM.
| Note: this operator expects the full LaTeX source as a single | ||
| string. It does **not** resolve ``\\input`` or ``\\include`` | ||
| directives. If your documents span multiple ``.tex`` files, | ||
| concatenate them into a single text field before applying this |
There was a problem hiding this comment.
Can we have a dedicated op for this step? "If your documents span multiple .tex files,
concatenate them into a single text field before applying this"
Summary
Add a new mapper operator
latex_figure_context_extractor_mapperthat extracts figures and their citing context from LaTeX source.What it does
figure,figure*,wrapfigureenvironmentssubfigureenvironments and\subfigure/\subfloatcommands\includegraphicspaths for each figure\ref/\cref/\Cref/\autorefOutput fields
imageslist[str]\includegraphicscaptionstrlabelstrciting_paragraphslist[str]parent_captionstrparent_labelstrChanges
data_juicer/ops/mapper/latex_figure_context_extractor_mapper.py— operator implementationdata_juicer/ops/mapper/__init__.py— registrationdata_juicer/config/config_all.yaml— config entrytests/ops/mapper/test_latex_figure_context_extractor_mapper.py— 21 unit testsdocs/operators/mapper/latex_figure_context_extractor_mapper.md— documentation (EN/CN)Testing