diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d0b032272..295552ab2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,6 +8,9 @@ exclude: | vlmeval/dataset/utils/megabench/ | vlmeval/dataset/utils/vgrpbench/ | vlmeval/dataset/utils/chartmimic/ | + vlmeval/dataset/MDPBench/dataset/ | + vlmeval/dataset/MDPBench/metrics/ | + vlmeval/dataset/MDPBench/utils/ | vlmeval/vlm/ola/ | vlmeval/vlm/ursa/ | vlmeval/vlm/ovis/ | diff --git a/vlmeval/dataset/MDPBench/README.md b/vlmeval/dataset/MDPBench/README.md new file mode 100644 index 000000000..9b0ab2b86 --- /dev/null +++ b/vlmeval/dataset/MDPBench/README.md @@ -0,0 +1,30 @@ +# MDPBench Evaluation Pipeline + +MDPBench is a specialized dataset for Multimodal Document Processing and OCR evaluation within the VLMEvalKit framework. + +## 🚀 Installation & Environment Setup + +### 1. Install Python Dependencies +All required Python packages, including standard metrics and CDM (Comprehensive Distance Metric) evaluation dependencies, are unified in `vlmeval/dataset/MDPBench/requirements.txt`: + +From the VLMEvalKit repository root: + +```bash +pip install -r vlmeval/dataset/MDPBench/requirements.txt +``` + +### 2. Configure CDM Environment +CDM metric performs visual rendering comparison for complex formulas and tables. It dynamically detects whether the system environment is correctly configured. If missing, it will gracefully degrade and skip the CDM score without crashing. + +To **enable CDM**, install the following system-level packages: + +**Ubuntu / Debian:** +```bash +sudo apt-get update +sudo apt-get install -y nodejs npm imagemagick texlive texlive-latex-extra +``` + +**macOS:** +```bash +brew install node imagemagick texlive +``` diff --git a/vlmeval/dataset/MDPBench/__init__.py b/vlmeval/dataset/MDPBench/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/vlmeval/dataset/MDPBench/dataset/__init__.py b/vlmeval/dataset/MDPBench/dataset/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/vlmeval/dataset/MDPBench/dataset/end2end_dataset.py b/vlmeval/dataset/MDPBench/dataset/end2end_dataset.py new file mode 100644 index 000000000..416627f80 --- /dev/null +++ b/vlmeval/dataset/MDPBench/dataset/end2end_dataset.py @@ -0,0 +1,425 @@ +import json +import os +import sys +import time +import traceback +from collections import defaultdict + +import Levenshtein +from func_timeout import FunctionTimedOut, func_timeout +from loguru import logger +from pylatexenc.latex2text import LatexNodes2Text +from tqdm import tqdm + +from ..registry.registry import DATASET_REGISTRY +from ..utils.data_preprocess import clean_string, normalized_table +from ..utils.extract import md_tex_filter +from ..utils.match import match_gt2pred_no_split, match_gt2pred_simple +from ..utils.match_quick import match_gt2pred_quick +# from utils.match_full import match_gt2pred_full, match_gt2pred_textblock_full +from ..utils.read_files import read_md_file +from .recog_dataset import RecognitionTableDataset + + +@DATASET_REGISTRY.register("end2end_dataset") +class End2EndDataset(): + def __init__(self, cfg_task): + gt_path = cfg_task['dataset']['ground_truth']['data_path'] + pred_folder = cfg_task['dataset']['prediction']['data_path'] + self.match_method = cfg_task['dataset'].get('match_method', 'quick_match') + self.slim_mode = cfg_task['dataset'].get('slim_mode', False) + filtered_types = cfg_task['dataset'].get('filter') + + with open(gt_path, 'r') as f: + gt_samples = json.load(f) + + filtered_gt_samples = [] + if filtered_types: + for gt_sample in gt_samples: + select_flag = True + for k, v in filtered_types.items(): + if gt_sample["page_info"]["page_attribute"][k] != v: + select_flag = False + if select_flag: + filtered_gt_samples.append(gt_sample) + else: + filtered_gt_samples = gt_samples + + self.samples = self.get_matched_elements(filtered_gt_samples, pred_folder) + + def __getitem__(self, cat_name, idx): + return self.samples[cat_name][idx] + + def get_page_elements(self, selected_annos): + + saved_element_dict = defaultdict(list) + related_truncated = [] + truncated_all = {} + for relation in selected_annos["extra"]["relation"]: + if relation["relation_type"] == 'truncated': + truncated_all[relation["source_anno_id"]] = "" + truncated_all[relation["target_anno_id"]] = "" + exist_flag = False + for merge_list in related_truncated: + if relation["source_anno_id"] in merge_list or relation["target_anno_id"] in merge_list: + merge_list.append(relation["source_anno_id"]) + merge_list.append(relation["target_anno_id"]) + exist_flag = True + if not exist_flag: + related_truncated.append( + [relation["source_anno_id"], relation["target_anno_id"]]) + + for item in selected_annos['layout_dets']: + if item['anno_id'] not in truncated_all.keys(): + saved_element_dict[item["category_type"]].append(item) + else: + truncated_all[item['anno_id']] = item + + for merge_list in related_truncated: + text_block_list = [truncated_all[key] for key in merge_list] + sorted_block = sorted(text_block_list, key=lambda x: x['order']) + text = "" + for block in sorted_block: + text += block['text'] + merged_block = { + "category_type": sorted_block[0]["category_type"], + "order": sorted_block[0]["order"], + "anno_id": sorted_block[0]["anno_id"], + "text": text, + "merge_list": sorted_block + } + saved_element_dict[sorted_block[0]["category_type"]].append(merged_block) + + return saved_element_dict + + def get_page_elements_list(self, gt_page_elements, category_list): + element_list = [] + for category_type in category_list: + if gt_page_elements.get(category_type): + element_list.extend(gt_page_elements[category_type]) + return element_list + + def get_sorted_text_list(self, selected_annos): + text_list = [] + for item in selected_annos: + if item.get('order'): + order = item['order'] + else: + order = 0 + text_list.append((order, item)) + sorted_text_list = sorted(text_list, key=lambda x: x[0]) + return [_[1] for _ in sorted_text_list] + + def filtered_out_ignore(self, items, ignore_category_list): + filted_items = [] + for item in items: + if item['gt_category_type'] not in ignore_category_list: + filted_items.append(item) + return filted_items + + def get_order_paired(self, order_match_s, img_name): + matched = [(item['gt_position'], item['pred_position']) for item in order_match_s if ( + item['gt_position'] != [""] and item['pred_position'] != "")] + gt_idx_all = [item['gt_position'] + for item in order_match_s if (item['gt_position'] != [""])] + # Sort by pred idx to get Pred ordered GT_idx + read_order_pred = [i[0] for i in sorted(matched, key=lambda x: x[1])] + read_order_gt = sum(gt_idx_all, []) # Convert to one-dimensional list + # For truncated merges, some discarded classes may be merged in, remove + # them when calculating edit distance + read_order_gt = [x for x in read_order_gt if x] + gt = sorted(read_order_gt) # Sort by all GT idx to get GT ordered GT_idx + pred = sum(read_order_pred, []) + pred = [x for x in pred if x] + if len(pred) > 0 or len(gt) > 0: + edit = Levenshtein.distance(gt, pred) / max(len(pred), len(gt)) + return { + 'gt': gt, + 'pred': pred, + 'img_id': img_name, + 'edit': edit + } + else: + return {} # If both GT and pred are empty for the page, return empty + + def formula_format(self, formula_matches, img_name): + # formated_list = [] + for i, item in enumerate(formula_matches): + item["img_id"] = img_name + '_' + str(i) + return formula_matches + + def get_matched_elements(self, gt_samples, pred_folder): + plain_text_match = [] + display_formula_match = [] + html_table_match = [] + latex_table_match = [] + order_match = [] + save_time = time.time() + + # Pre-fetch all prediction files to support one-to-many matching + try: + all_pred_files = os.listdir(pred_folder) + except FileNotFoundError: + print(f"Prediction folder not found: {pred_folder}") + all_pred_files = [] + + process_bar = tqdm(gt_samples, ascii=True, ncols=140) + for sample in process_bar: + img_name = os.path.basename(sample["page_info"]["image_path"]) + base_name = os.path.splitext(img_name)[0] + + # Find all matching prediction files (original + variants) + matched_pred_files = [] + for f in all_pred_files: + if not (f.endswith('.md') or f.endswith('.mmd')): + continue + f_no_ext = os.path.splitext(f)[0] + + # Match exact name or name with suffix (e.g., base_name + '_indoor...') + # Ensure we don't match en_book_1 to en_book_10 by checking the character + # after base_name is '_' or end of string + if self.slim_mode: + if (f_no_ext == base_name or f_no_ext.startswith(base_name + '_')): + matched_pred_files.append(f) + else: + if f_no_ext == base_name: + matched_pred_files.append(f) + + if not matched_pred_files: + print(f'!!!WARNING: No prediction for {img_name}') + continue + + for pred_file in matched_pred_files: + pred_path = os.path.join(pred_folder, pred_file) + process_bar.set_description(f'Processing {pred_file}') + pred_content = read_md_file(pred_path) + + # Use the prediction filename (without extension) as the unique ID for + # this evaluation instance + current_img_id = os.path.splitext(pred_file)[0] + + # Determine if this is the original file + is_digit = len([p for p in current_img_id.split("_") if p]) == 3 + + result = self.process_get_matched_elements( + sample, pred_content, current_img_id, save_time, is_digit) # Don't use timeout logic + + [plain_text_match_clean, + formated_display_formula, + latex_table_match_s, + html_table_match_s, + order_match_single] = result + + if order_match_single: + order_match.append(order_match_single) + if plain_text_match_clean: + plain_text_match.extend(plain_text_match_clean) + if formated_display_formula: + display_formula_match.extend(formated_display_formula) + if latex_table_match_s: + latex_table_match.extend(latex_table_match_s) + if html_table_match_s: + html_table_match.extend(html_table_match_s) + + display_formula_match_clean, display_formula_match_others = [], [] + for item in display_formula_match: + pred_category_type = item.get("pred_category_type", None) + if pred_category_type not in ['equation_inline', 'equation_isolated', '']: + gt = item.get('gt', None) + item.get('norm_gt', None) + try: + item['gt'] = LatexNodes2Text().latex_to_text(gt) + except Exception as e: + logger.warning(f"Failed to convert latex to text: {gt[:50]}... Error: {e}") + + item['norm_gt'] = clean_string(item['gt']) + display_formula_match_others.append(item) + else: + display_formula_match_clean.append(item) + display_formula_match = display_formula_match_clean + if display_formula_match_others and plain_text_match: + plain_text_match.extend(display_formula_match_others) + + if latex_table_match: + latex_to_html = [] + for latex_table in latex_table_match: + for k, v in latex_table.items(): + if 'pred' in k: + latex_table[k] = "" + latex_table['edit'] = 1 + latex_to_html.append(latex_table) + html_table_match.extend(latex_to_html) + + if len(latex_table_match) > len(html_table_match): + table_match = latex_table_match + table_format = 'latex' + else: + table_match = html_table_match + table_format = 'html' + + matched_samples_all = { + 'text_block': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(plain_text_match), + 'display_formula': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(display_formula_match), + 'table': DATASET_REGISTRY.get('recogition_end2end_table_dataset')(table_match, table_format), + 'reading_order': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(order_match) + } + + return matched_samples_all + + def process_get_matched_elements(self, sample, pred_content, + img_name, save_time, is_digit=True): + if self.match_method == 'simple_match': # add match choice + match_gt2pred = match_gt2pred_simple + elif self.match_method == 'quick_match': + match_gt2pred = match_gt2pred_quick + elif self.match_method == 'no_split': + match_gt2pred = match_gt2pred_no_split + else: + print('Invalid match method name. The quick_match will be used.') + match_gt2pred = match_gt2pred_quick + + pred_dataset = md_tex_filter(pred_content) + gt_page_elements = self.get_page_elements(sample) + + gt_mix, pred_dataset_mix = [], [] + for category in pred_dataset: + if category not in ['html_table', 'latex_table', 'md2html_table']: + pred_dataset_mix.extend(pred_dataset[category]) + gt_mix = self.get_page_elements_list(gt_page_elements, ['text_block', 'title', 'code_txt', 'code_txt_caption', 'reference', 'equation_caption', + 'figure_caption', 'figure_footnote', 'table_caption', 'table_footnote', 'code_algorithm', 'code_algorithm_caption', + 'header', 'footer', 'page_footnote', 'page_number', 'equation_isolated']) + if gt_mix: + gt_mix = self.get_sorted_text_list(gt_mix) + + display_formula_match_s = [] + plain_text_match_clean = [] + latex_table_match_s = [] + html_table_match_s = [] + order_match_single = [] + + if gt_page_elements.get('table'): + gt_table = self.get_sorted_text_list(gt_page_elements['table']) + latex_table_len = len(pred_dataset['latex_table'] + ) if pred_dataset['latex_table'] else 0 + html_table_len = len(pred_dataset['html_table']) if pred_dataset['html_table'] else 0 + if latex_table_len == html_table_len and latex_table_len == 0: + html_table_match_s, unmatch_table_pred = match_gt2pred_simple( + gt_table, [], 'html_table', img_name) # Don't consider truncated merging for tables + html_table_match_s = [ + x for x in html_table_match_s if x['gt_idx'] != [""]] # Remove extra preds + elif latex_table_len > html_table_len: + latex_table_match_s, unmatch_table_pred = match_gt2pred_simple( + gt_table, pred_dataset['latex_table'], 'latex_table', img_name) # Don't consider truncated merging for tables + latex_table_match_s = [ + x for x in latex_table_match_s if x['gt_idx'] != [""]] # Remove extra preds + else: + html_table_match_s, unmatch_table_pred = match_gt2pred_simple( + gt_table, pred_dataset['html_table'], 'html_table', img_name) # Don't consider truncated merging for tables + html_table_match_s = [ + x for x in html_table_match_s if x['gt_idx'] != [""]] # Remove extra preds + + if unmatch_table_pred: + pred_dataset_mix.extend(unmatch_table_pred) + + try: + match = func_timeout( + 30, + match_gt2pred, + args=( + gt_mix, + pred_dataset_mix, + 'text_all', + img_name)) + except FunctionTimedOut: + # print(f'Time out for plain text match of {img_name}, match_gt2pred_simple will be used.') + match, _ = match_gt2pred_simple(gt_mix, pred_dataset_mix, 'text_all', img_name) + except Exception: + # print(str(e)) + print(traceback.format_exc()) + sys.exit() + + plain_text_match_s = [] + for item in match: + gt_category = item.get('gt_category_type', None) + if gt_category in ['text_block', 'title', 'code_txt', 'code_txt_caption', 'reference', 'equation_caption', + 'figure_caption', 'figure_footnote', 'table_caption', 'table_footnote', 'code_algorithm', 'code_algorithm_caption', + 'header', 'footer', 'page_footnote', 'page_number']: + plain_text_match_s.append(item) + elif gt_category == 'equation_isolated': + display_formula_match_s.append(item) + + display_formula_match_s = [x for x in display_formula_match_s if x['gt_idx'] != [""]] + + if not plain_text_match_s: + # print(f'Time out for text match of {img_name}. The plain text match will be empty.') + # print(f'No text match of {img_name}. The plain text match will be empty.') + pass + else: + # Categories that need to be ignored for text + plain_text_match_clean = self.filtered_out_ignore( + plain_text_match_s, + [ + 'figure_caption', + 'figure_footnote', + 'table_caption', + 'table_footnote', + 'code_algorithm', + 'code_algorithm_caption', + 'header', + 'footer', + 'page_footnote', + 'page_number', + 'equation_caption']) + # plain_text_match_clean = self.filtered_out_ignore(plain_text_match_s, ['figure_footnote', 'table_footnote', 'code_algorithm', 'code_algorithm_caption', 'header', 'footer', 'page_footnote', 'page_number']) + order_match_s = plain_text_match_clean + if order_match_s: + order_match_single = self.get_order_paired(order_match_s, img_name) + + for lst in [plain_text_match_clean, display_formula_match_s, + latex_table_match_s, html_table_match_s, order_match_single]: + if isinstance(lst, list): + for item in lst: + item['is_digit'] = is_digit + item['page_id'] = img_name + elif isinstance(lst, dict) and lst: + lst['is_digit'] = is_digit + lst['page_id'] = img_name + + return [plain_text_match_clean, display_formula_match_s, + latex_table_match_s, html_table_match_s, order_match_single] + + +@DATASET_REGISTRY.register("recogition_end2end_base_dataset") +class RecognitionEnd2EndBaseDataset(): + def __init__(self, samples): + img_id = 0 + for sample in samples: + if not sample.get('img_id'): + sample['img_id'] = img_id + img_id += 1 + self.samples = samples + + def __getitem__(self, idx): + return self.samples[idx] + + +@DATASET_REGISTRY.register("recogition_end2end_table_dataset") +class RecognitionEnd2EndTableDataset(RecognitionTableDataset): + def __init__(self, samples, table_format): + self.pred_table_format = table_format + self.samples = self.normalize_data(samples) + + def normalize_data(self, samples): + img_id = 0 + + for sample in samples: + p = sample['pred'] + r = sample['gt'] + p = normalized_table(p, self.pred_table_format) + r = normalized_table(r) + sample['norm_gt'] = r + sample['norm_pred'] = p + sample['img_id'] = sample['img_id'] if sample.get('img_id') else img_id + img_id += 1 + + return samples diff --git a/vlmeval/dataset/MDPBench/dataset/recog_dataset.py b/vlmeval/dataset/MDPBench/dataset/recog_dataset.py new file mode 100644 index 000000000..25fc9b91b --- /dev/null +++ b/vlmeval/dataset/MDPBench/dataset/recog_dataset.py @@ -0,0 +1,232 @@ +import json +import os +import re +import shutil + +from tqdm import tqdm + +from ..registry.registry import DATASET_REGISTRY + +try: + from ..utils.ocr_utils import get_text_for_block +except Exception: + def get_text_for_block(*args, **kwargs): + return '' +from ..utils.data_preprocess import (clean_string, normalized_formula, normalized_table, + textblock2unicode) + + +@DATASET_REGISTRY.register("recogition_text_dataset") +class RecognitionTextDataset(): + # Evaluate at text block granularity, without considering one-to-one bbox matching + def __init__(self, cfg_task): + gt_file = cfg_task['dataset']['ground_truth']['data_path'] + pred_folder = cfg_task['dataset']['prediction']['data_path'] + self.samples = self.load_data(gt_file, pred_folder) + + def load_data(self, gt_file, pred_folder): + samples = [] + with open(gt_file, 'r') as f: + gts = json.load(f) + + for gt in gts: + img_name = os.path.basename(gt['image_path']) + gt_text = gt['text'] + pred_file = os.path.join(pred_folder, img_name[:-4] + '.json') + if not os.path.exists(pred_file): + print(f'Cannot find pred for {img_name}') + continue + else: + with open(pred_file, 'r') as f: + pred_spans = json.load(f) + pred_text = get_text_for_block(gt, pred_spans) + samples.append({ + "gt": gt_text, + 'pred': pred_text, + 'img_id': img_name + }) + return samples + + +@DATASET_REGISTRY.register("omnidocbench_single_module_dataset") +class OmiDocBenchSingleModuleDataset(): + # Evaluate at text block granularity, without considering one-to-one bbox matching + def __init__(self, cfg_task): + gt_key = cfg_task['dataset']['ground_truth']['data_key'] + pred_file = cfg_task['dataset']['ground_truth']['data_path'] + pred_key = cfg_task['dataset']['prediction']['data_key'] + + self.category_filter = cfg_task['dataset']['ground_truth'].get('category_filter', []) + self.category_type = cfg_task['dataset'].get('category_type') + self.samples = self.load_data(pred_file, pred_key, gt_key) + + def load_data(self, pred_file, pred_key, gt_key): + samples = [] + with open(pred_file, 'r') as f: + preds = json.load(f) + count = 0 + for pred in preds: + img_name = os.path.basename(pred['page_info']['image_path']) + for i, ann in enumerate(pred['layout_dets']): + if not ann.get(gt_key): + continue + if self.category_filter: + if ann['category_type'] not in self.category_filter: + continue + if not ann.get(pred_key): + # print(f'Cannot find pred for {img_name}. ann is {ann}') + # pdb.set_trace() + count += 1 + continue + else: + gt_text = ann[gt_key] + norm_gt = gt_text + pred_text = ann[pred_key] + norm_pred = pred_text + if self.category_type: + if self.category_type == 'text': + norm_gt = clean_string(textblock2unicode(ann[gt_key])) + norm_pred = clean_string(textblock2unicode(ann[pred_key])) + elif self.category_type == 'formula': + norm_gt = normalized_formula(ann[gt_key]) + norm_pred = normalized_formula(ann[pred_key]) + elif self.category_type == 'table': + norm_gt = normalized_table(ann[gt_key], gt_key) + norm_pred = normalized_table(ann[pred_key], gt_key) + else: + raise ValueError(f'Invalid category type: {self.category_type}') + + samples.append({ + "gt": gt_text, + "norm_gt": norm_gt, + "gt_attribute": [ann['attribute']], + 'pred': pred_text, + "norm_pred": norm_pred, + 'img_id': img_name + }) + print(f'Cannot find pred for {count} samples.') + + return samples + + +@DATASET_REGISTRY.register("recogition_formula_dataset") +class RecognitionFormulaDataset(): + def __init__(self, cfg_task): + gt_file = cfg_task['dataset']['ground_truth']['data_path'] + pred_file = cfg_task['dataset']['prediction']['data_path'] + + self.samples = self.load_data(gt_file, pred_file) + + def load_data(self, gt_file, pred_file): + """ + Load a list of image paths and their corresponding formulas. + The function skips empty lines and lines without corresponding images. + + Args: + image_path (str): The path to the directory containing the image files. + math_file (str): The path to the text file containing the formulas. + + Returns: + list, list: A list of image paths and a list of corresponding formula + """ + + with open(gt_file, 'r') as f: + math_gts = [line.strip() for line in f.readlines()] + + with open(pred_file, 'r') as f: + math_preds = [line.strip() for line in f.readlines()] + + if len(math_preds) != len(math_gts): + raise ValueError("The number of prediction does not match the number of ground truth.") + + norm_gts = [self.normalize_text(gt) for gt in math_gts] # Formula normalization + norm_preds = [self.normalize_text(pred) for pred in math_preds] + + samples = [] + img_id = 0 + for gt, pred in zip(norm_gts, norm_preds): + samples.append({ + 'gt': gt, + 'pred': pred, + 'img_id': img_id + }) + img_id += 1 + + return samples + + def normalize_text(self, text): + """Remove unnecessary whitespace from LaTeX code.""" + text_reg = r'(\\(operatorname|mathrm|text|mathbf)\s?\*? {.*?})' + letter = '[a-zA-Z]' + noletter = '[\\W_^\\d]' + names = [x[0].replace(' ', '') for x in re.findall(text_reg, text)] + text = re.sub(text_reg, lambda match: str(names.pop(0)), text) + news = text + while True: + text = news + news = re.sub(r'(?!\\ )(%s)\s+?(%s)' % (noletter, noletter), r'\1\2', text) + news = re.sub(r'(?!\\ )(%s)\s+?(%s)' % (noletter, letter), r'\1\2', news) + news = re.sub(r'(%s)\s+?(%s)' % (letter, noletter), r'\1\2', news) + if news == text: + break + return text + + def __getitem__(self, idx): + return self.samples[idx] + + +@DATASET_REGISTRY.register("recogition_table_dataset") +class RecognitionTableDataset(): + def __init__(self, cfg_task): + gt_file = cfg_task['dataset']['ground_truth']['data_path'] + pred_file = cfg_task['dataset']['prediction']['data_path'] + self.pred_table_format = cfg_task['dataset']['prediction'].get('table_format', 'html') + + references, predictions = self.load_data(gt_file), self.load_data(pred_file) + self.samples = self.normalize_data(references, predictions) + + def normalize_data(self, references, predictions): + if self.pred_table_format == 'latex2html': + os.makedirs('./temp', exist_ok=True) + + samples = [] + ref_keys = list(references.keys()) + + for img in tqdm(ref_keys, total=len(ref_keys), ncols=140, + ascii=True, desc='Normalizing data'): + if self.pred_table_format == 'html': + r = references[img]['html'] + p = predictions[img]['html'] + elif self.pred_table_format == 'latex': + r = references[img]['latex'] + p = predictions[img]['latex'] + else: + raise ValueError(f'Invalid table format: {self.pred_table_format}') + + img_id = references[img]["page_image_name"] + p = normalized_table(p, self.pred_table_format) + r = normalized_table(r, self.pred_table_format) + # print('p:', p) + # print('r:', r) + samples.append({ + 'gt': p, + 'pred': r, + 'img_id': img_id, + 'gt_attribute': [references[img]['attribute']], + }) + + if self.pred_table_format == 'latex2html': + shutil.rmtree('./temp') + return samples + + def __getitem__(self, idx): + return self.samples[idx] + + def load_data(self, data_path): + result_dict = {} + with open(data_path, 'r') as f: + samples = json.load(f) + for sample in samples: + result_dict[sample["image_path"]] = sample + + return result_dict diff --git a/vlmeval/dataset/MDPBench/evaluator.py b/vlmeval/dataset/MDPBench/evaluator.py new file mode 100644 index 000000000..a01ffa380 --- /dev/null +++ b/vlmeval/dataset/MDPBench/evaluator.py @@ -0,0 +1,411 @@ +import json +import os +import re +import time +from collections import defaultdict +from typing import Dict, Optional + +import numpy as np +import pandas as pd +from tqdm import tqdm + +import vlmeval.dataset.MDPBench.metrics.cal_metric # noqa: F401 +from vlmeval.smp import dump, get_intermediate_file_path, load +from .dataset.end2end_dataset import End2EndDataset +from .metrics.show_result import get_full_labels_results, get_page_split, show_result +from .registry.registry import DATASET_REGISTRY, METRIC_REGISTRY + + +class NativeEnd2EndEvaluator: + def __init__( + self, + eval_file: str, + tsv_path: str, + match_method: str = 'quick_match', + filter_types: Optional[Dict] = None, + ): + self.eval_file = eval_file + self.match_method = match_method + self.filter_types = filter_types + + predictions = load(eval_file) + references = load(tsv_path) + + if 'index' not in predictions.columns or 'index' not in references.columns: + raise ValueError('Both prediction file and dataset TSV must contain an index column.') + + df = pd.merge(predictions, references, on='index', how='inner') + pred_col = 'prediction_x' if 'prediction_x' in df.columns else 'prediction' + ans_col = 'answer_y' if 'answer_y' in df.columns else 'answer' + + if ans_col not in df.columns: + raise ValueError('Dataset TSV must contain answer column for MDPBench evaluation.') + if pred_col not in df.columns: + df['prediction'] = '' + pred_col = 'prediction' + + self.references = [] + self.predictions = [] + for _, row in df.iterrows(): + ans = row.get(ans_col) + pred = row.get(pred_col, '') + try: + if isinstance(ans, str): + ans = json.loads(ans) + if isinstance(ans, dict): + ans = dict(ans) + ans['index'] = str(row.get('index', '')) + self.references.append(ans) + self.predictions.append(str(pred) if pred is not None else '') + except Exception: + continue + + if self.filter_types: + filtered_refs = [] + filtered_preds = [] + for gt_sample, pred in zip(self.references, self.predictions): + select_flag = True + page_attr = gt_sample.get('page_info', {}).get('page_attribute', {}) + if isinstance(page_attr, list) and page_attr: + page_attr = page_attr[0] + for k, v in self.filter_types.items(): + if not isinstance(page_attr, dict) or page_attr.get(k) != v: + select_flag = False + break + if select_flag: + filtered_refs.append(gt_sample) + filtered_preds.append(pred) + self.references = filtered_refs + self.predictions = filtered_preds + + self.dafault_metircs_dict = { + 'text_block': {'metric': ['Edit_dist']}, + 'display_formula': {'metric': ['Edit_dist', 'CDM']}, + 'table': {'metric': ['TEDS', 'Edit_dist']}, + 'reading_order': {'metric': ['Edit_dist']}, + } + + self.dataset_runner = End2EndDataset.__new__(End2EndDataset) + self.dataset_runner.match_method = self.match_method + self.dataset_runner.slim_mode = False + + def _postprocess_matches(self, plain_text_match, display_formula_match, + html_table_match, latex_table_match): + from pylatexenc.latex2text import LatexNodes2Text + + from .utils.data_preprocess import clean_string + + display_formula_match_clean, display_formula_match_others = [], [] + for item in display_formula_match: + pred_category_type = item.get('pred_category_type', None) + if pred_category_type not in ['equation_inline', 'equation_isolated', '']: + gt = item.get('gt', None) + try: + item['gt'] = LatexNodes2Text().latex_to_text(gt) + except Exception: + pass + item['norm_gt'] = clean_string(item['gt']) + display_formula_match_others.append(item) + else: + display_formula_match_clean.append(item) + + display_formula_match = display_formula_match_clean + if display_formula_match_others and plain_text_match: + plain_text_match.extend(display_formula_match_others) + + if latex_table_match: + latex_to_html = [] + for latex_table in latex_table_match: + for k in list(latex_table.keys()): + if 'pred' in k: + latex_table[k] = '' + latex_table['edit'] = 1 + latex_to_html.append(latex_table) + html_table_match.extend(latex_to_html) + + if len(latex_table_match) > len(html_table_match): + table_match = latex_table_match + table_format = 'latex' + else: + table_match = html_table_match + table_format = 'html' + + return plain_text_match, display_formula_match, table_match, table_format + + def get_matched_elements(self): + plain_text_match = [] + display_formula_match = [] + html_table_match = [] + latex_table_match = [] + order_match = [] + save_time = time.time() + + for i, sample in tqdm(list(enumerate(self.references)), + desc='Matching elements', ascii=True): + pred_content = self.predictions[i] if i < len(self.predictions) else '' + img_name = os.path.basename(sample.get('page_info', {}).get('image_path', f'page_{i}')) + img_id = os.path.splitext(img_name)[0] if img_name else f'page_{i}' + index_name = os.path.splitext(os.path.basename(str(sample.get('index', ''))))[0] + # Keep the matching id rule aligned with original MDPBench: use current instance id. + current_img_id = index_name if index_name else img_id + is_digit = len([p for p in current_img_id.split('_') if p]) == 3 + + result = self.dataset_runner.process_get_matched_elements( + sample, + pred_content, + current_img_id, + save_time, + bool(is_digit), + ) + + ( + plain_text_match_clean, + formated_display_formula, + latex_table_match_s, + html_table_match_s, + order_match_single, + ) = result + + if order_match_single: + order_match.append(order_match_single) + if plain_text_match_clean: + plain_text_match.extend(plain_text_match_clean) + if formated_display_formula: + display_formula_match.extend(formated_display_formula) + if latex_table_match_s: + latex_table_match.extend(latex_table_match_s) + if html_table_match_s: + html_table_match.extend(html_table_match_s) + + plain_text_match, display_formula_match, table_match, table_format = self._postprocess_matches( + plain_text_match, + display_formula_match, + html_table_match, + latex_table_match, + ) + + matched_samples_all = { + 'text_block': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(plain_text_match), + 'display_formula': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(display_formula_match), + 'table': DATASET_REGISTRY.get('recogition_end2end_table_dataset')(table_match, table_format), + 'reading_order': DATASET_REGISTRY.get('recogition_end2end_base_dataset')(order_match), + } + return matched_samples_all + + @staticmethod + def _page_id_from_indexed_key(key: str) -> str: + m = re.match(r'^(.*)_\[(\d+)\]$', key) + if m: + return m.group(1) + parts = key.rsplit('_', 1) + if len(parts) == 2 and parts[1].isdigit(): + return parts[0] + return key + + @staticmethod + def _lang_from_img_id(img_id: str) -> str: + if not isinstance(img_id, str) or '_' not in img_id: + return 'UNKNOWN' + lang = img_id.split('_', 1)[0].upper() + return 'ZH-T' if lang == 'ZH-CHT' else lang + + @staticmethod + def _compute_present_tasks_overall(pages: pd.DataFrame) -> pd.DataFrame: + df = pages.copy() + for k in ['text', 'formula', 'table']: + if f'n_{k}' not in df: + df[f'n_{k}'] = 0.0 + else: + df[f'n_{k}'] = df[f'n_{k}'].fillna(0).astype(float) + if f's_{k}' not in df: + df[f's_{k}'] = np.nan + else: + df[f's_{k}'] = df[f's_{k}'].astype(float) + + use_text = (df['n_text'] > 0) & df['s_text'].notna() + use_formula = (df['n_formula'] > 0) & df['s_formula'].notna() + use_table = (df['n_table'] > 0) & df['s_table'].notna() + + denom = use_text.astype(int) + use_formula.astype(int) + use_table.astype(int) + numer = ( + df['s_text'].fillna(0) * use_text.astype(int) + + df['s_formula'].fillna(0) * use_formula.astype(int) + + df['s_table'].fillna(0) * use_table.astype(int) + ) + + df['overall_i_scheme2'] = (numer / denom.replace({0: np.nan})).clip(0, 1) + return df + + def _split_filtered_samples(self, samples, split_tag: str): + if split_tag == 'All': + return list(samples) + want_is_digit = split_tag == 'digit' + out = [] + for s in samples: + is_digit = bool(s.get('is_digit', True)) + if is_digit == want_is_digit: + out.append(s) + return out + + def _summary_like_notebook_cell3(self, sample_cache: Dict[str, list]) -> pd.DataFrame: + split_tags = ['All', 'digit', 'photo'] + split_rename_map = {'digit': 'Digit.', 'photo': 'Photo.', 'All': 'All'} + latin_langs = ['DE', 'EN', 'ES', 'FR', 'ID', 'IT', 'NL', 'PT', 'VI'] + non_latin_langs = ['AR', 'HI', 'JP', 'KO', 'RU', 'TH', 'ZH', 'ZH-T'] + + model_data = {} + + for split in split_tags: + text_samples = self._split_filtered_samples(sample_cache.get('text_block', []), split) + formula_samples = self._split_filtered_samples( + sample_cache.get('display_formula', []), split) + table_samples = self._split_filtered_samples(sample_cache.get('table', []), split) + + # text: per-page s_text = 1 - weighted Edit_dist + text_edit_num = defaultdict(float) + text_upper = defaultdict(float) + text_cnt = defaultdict(int) + for s in text_samples: + pid = s.get('image_name') or s.get('page_id') or s.get('img_id') + if not pid: + continue + if s.get('upper_len', 0) > 0 and s.get('Edit_num') is not None: + text_edit_num[pid] += float(s.get('Edit_num', 0.0)) + text_upper[pid] += float(s.get('upper_len', 0.0)) + text_cnt[pid] += 1 + s_text = {k: max( + 0.0, min(1.0, 1.0 - (text_edit_num[k] / text_upper[k]))) for k in text_upper if text_upper[k] > 0} + + # formula: per-page mean CDM + formula_sum = defaultdict(float) + formula_cnt = defaultdict(int) + for s in formula_samples: + pid = s.get('image_name') or s.get('page_id') or s.get('img_id') + if not pid: + continue + cdm = (s.get('metric') or {}).get('CDM') + if cdm is None: + continue + formula_sum[pid] += float(cdm) + formula_cnt[pid] += 1 + s_formula = {k: formula_sum[k] / formula_cnt[k] + for k in formula_cnt if formula_cnt[k] > 0} + + # table: per-page mean TEDS + table_sum = defaultdict(float) + table_cnt = defaultdict(int) + for s in table_samples: + pid = s.get('image_name') or s.get('page_id') or s.get('img_id') + if not pid: + continue + teds = (s.get('metric') or {}).get('TEDS') + if teds is None: + continue + table_sum[pid] += float(teds) + table_cnt[pid] += 1 + s_table = {k: table_sum[k] / table_cnt[k] for k in table_cnt if table_cnt[k] > 0} + + all_page_ids = set(text_cnt) | set(formula_cnt) | set(table_cnt) + if not all_page_ids: + continue + + pages = pd.DataFrame(index=sorted(all_page_ids)) + pages.index.name = 'img_id' + pages['s_text'] = pd.Series(s_text) + pages['n_text'] = pd.Series(text_cnt) + pages['s_formula'] = pd.Series(s_formula) + pages['n_formula'] = pd.Series(formula_cnt) + pages['s_table'] = pd.Series(s_table) + pages['n_table'] = pd.Series(table_cnt) + pages['lang'] = [self._lang_from_img_id(i) for i in pages.index] + + pages_scheme2 = self._compute_present_tasks_overall(pages) + + if split == 'All': + lang_group = pages_scheme2.groupby('lang')['overall_i_scheme2'].mean() + for lang, score in lang_group.items(): + model_data[(lang, '')] = round(float(score) * 100, 1) + + overall_score = pages_scheme2['overall_i_scheme2'].mean(skipna=True) + model_data[('Overall', split_rename_map[split])] = round(float(overall_score) * 100, 1) + + latin_scores = [model_data[(lang, '')] for lang in latin_langs if (lang, '') in model_data] + if latin_scores: + model_data[('Latin_Avg', '')] = round(sum(latin_scores) / len(latin_scores), 1) + + non_latin_scores = [model_data[(lang, '')] for lang in non_latin_langs if (lang, '') in model_data] + if non_latin_scores: + model_data[('Non-Latin_Avg', '')] = round(sum(non_latin_scores) + / len(non_latin_scores), 1) + + row_entry = {} + for lang in latin_langs: + row_entry[f'Latin_{lang}'] = model_data.get((lang, ''), np.nan) + row_entry['Latin_Avg'] = model_data.get(('Latin_Avg', ''), np.nan) + + for lang in non_latin_langs: + row_entry[f'NonLatin_{lang}'] = model_data.get((lang, ''), np.nan) + row_entry['NonLatin_Avg'] = model_data.get(('Non-Latin_Avg', ''), np.nan) + + row_entry['Overall_All'] = model_data.get(('Overall', 'All'), np.nan) + row_entry['Overall_Digit'] = model_data.get(('Overall', 'Digit.'), np.nan) + row_entry['Overall_Photo'] = model_data.get(('Overall', 'Photo.'), np.nan) + + return pd.DataFrame([row_entry], index=['end2end']).round(3) + + def process_generated_metric_results(self, samples, save_name: str = 'mdpbench_end2end'): + result_all = {} + page_info = {} + metircs_dict = self.dafault_metircs_dict + pages = self.references + sample_cache = {} + + for page in pages: + img_path = os.path.basename(page.get('page_info', {}).get('image_path', '')) + page_info[img_path] = page.get('page_info', {}).get('page_attribute', {}) + + for element in metircs_dict.keys(): + result = {} + group_info = metircs_dict[element].get('group', []) + cur_samples = samples[element] + + for metric in metircs_dict[element]['metric']: + metric_val = METRIC_REGISTRY.get(metric) + cur_samples, result_s = metric_val(cur_samples).evaluate( + group_info, f'{save_name}_{element}') + if result_s: + result.update(result_s) + + if result: + print(element) + show_result(result) + + group_result = get_full_labels_results(cur_samples) + page_result = get_page_split(cur_samples, page_info) + + result_all[element] = {'all': result, 'group': group_result, 'page': page_result} + + saved_samples = cur_samples if isinstance(cur_samples, list) else cur_samples.samples + sample_cache[element] = saved_samples + result_file = get_intermediate_file_path( + self.eval_file, f'_{save_name}_{element}_result', 'json') + dump(saved_samples, result_file) + + metric_result_file = get_intermediate_file_path( + self.eval_file, f'_{save_name}_metric_result', 'json') + dump(result_all, metric_result_file) + + df = self._summary_like_notebook_cell3(sample_cache) + + e2e_eval_file = get_intermediate_file_path(self.eval_file, '_End2End_Evaluation', 'json') + dump(result_all, e2e_eval_file) + overall_file = get_intermediate_file_path(self.eval_file, '_overall') + dump(df, overall_file) + + print(f'The save path of End2End_Evaluation is: {e2e_eval_file}') + print(f'The save path of overall metrics is: {overall_file}') + return df + + def score(self): + samples = self.get_matched_elements() + return self.process_generated_metric_results(samples) diff --git a/vlmeval/dataset/MDPBench/mdpbench.py b/vlmeval/dataset/MDPBench/mdpbench.py new file mode 100644 index 000000000..d99970c38 --- /dev/null +++ b/vlmeval/dataset/MDPBench/mdpbench.py @@ -0,0 +1,44 @@ +from ..image_base import ImageBaseDataset +from .metrics import mdpbench_evaluator + + +class MDPBench(ImageBaseDataset): + MODALITY = 'IMAGE' + TYPE = 'QA' + + # Kept empty until dataset is published. + DATASET_URL = { + 'MDPBench': + 'https://huggingface.co/datasets/Delores-Lin/MDPBench-VLMEvalKit/resolve/main/MDPBench_public.tsv' + } + DATASET_MD5 = {'MDPBench': '6aa9a03dcea532be3f92c81635d21883'} + system_prompt = ( + 'You are an advanced hybrid OCR engine capable of processing multilingual text mixed ' + 'with mathematical notation. ' + 'Your goal is to transcribe the content with high fidelity. Strict Rules:\n\n' + '1. Multilingual Precision: Transcribe text exactly as it appears in the original language. ' + 'Do not translate, summarize, or correct original spelling errors.\n\n' + '2. Math Formatting: Identify all mathematical expressions and convert them into LaTeX.\n\n' + '3. Inline Math: Use single dollar signs ($x$) for inline math (formulas within a sentence).\n\n' + '4. Display Math: Use double dollar signs ($$x$$) for display math ' + '(standalone formulas on their own lines).\n\n' + '5. Layout & Structure: Use Markdown to preserve the visual structure (headers, paragraphs, lists).\n\n' + '6. Table Formatting: Use HTML tags (e.g., , ,
, ) ' + 'to generate any tables found in the text.\n\n' + '7. Output Only: Output the transcribed text directly without any conversational filler.' + ) + + def __init__(self, dataset='MDPBench', **kwargs): + super().__init__(dataset, **kwargs) + + def build_prompt(self, line): + image_path = self.dump_image(line)[0] + msg = [ + dict(type='image', value=image_path), + dict(type='text', value=self.system_prompt) + ] + return msg + + def evaluate(self, eval_file, **judge_kwargs): + evaluator = mdpbench_evaluator(eval_file, self.data_path, **judge_kwargs) + return evaluator.score() diff --git a/vlmeval/dataset/MDPBench/metrics/__init__.py b/vlmeval/dataset/MDPBench/metrics/__init__.py new file mode 100644 index 000000000..651f551d9 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/__init__.py @@ -0,0 +1,63 @@ +import json + +import pandas as pd + +from vlmeval.smp import load +from ..evaluator import NativeEnd2EndEvaluator + + +class mdpbench_evaluator: + def __init__(self, eval_file, tsv_path, **judge_kwargs): + self.eval_file = eval_file + self.tsv_path = tsv_path + self.judge_kwargs = judge_kwargs or {} + + predictions = load(eval_file) + references = load(tsv_path) + + if 'index' not in predictions.columns or 'index' not in references.columns: + raise ValueError('Both prediction file and dataset TSV must contain an index column.') + + merged = pd.merge(predictions, references, on='index', how='inner') + ans_col = 'answer_y' if 'answer_y' in merged.columns else 'answer' + if ans_col not in merged.columns: + raise ValueError('Dataset TSV must contain answer column for MDPBench evaluation.') + + if not self._detect_layout_answer(merged, ans_col): + raise ValueError( + 'MDPBench now only supports layout_dets-based end2end evaluation. ' + 'Please provide layout_dets ground truth in answer column.' + ) + + @staticmethod + def _parse_answer(answer): + if isinstance(answer, dict): + return answer + if isinstance(answer, str): + try: + return json.loads(answer) + except Exception: + return {'text': answer} + return {'text': str(answer)} + + @classmethod + def _detect_layout_answer(cls, merged_df, answer_col): + for val in merged_df[answer_col].tolist(): + obj = cls._parse_answer(val) + if isinstance(obj, dict) and isinstance(obj.get('layout_dets'), list): + return True + return False + + def score(self): + match_method = self.judge_kwargs.get('match_method', 'quick_match') + filter_types = self.judge_kwargs.get('filter_types', None) + evaluator = NativeEnd2EndEvaluator( + self.eval_file, + self.tsv_path, + match_method=match_method, + filter_types=filter_types, + ) + return evaluator.score() + + +__all__ = ['mdpbench_evaluator'] diff --git a/vlmeval/dataset/MDPBench/metrics/cal_metric.py b/vlmeval/dataset/MDPBench/metrics/cal_metric.py new file mode 100644 index 000000000..32fe36c72 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cal_metric.py @@ -0,0 +1,431 @@ +# import evaluate +# import random +import copy +import json +import os +import random +from collections import defaultdict +from concurrent.futures import ProcessPoolExecutor, as_completed + +import evaluate +# from rapidfuzz.distance import Levenshtein +import Levenshtein +import pandas as pd + +from ..registry.registry import METRIC_REGISTRY +from .cdm_metric import CDM +from .table_metric import TEDS + + +def _ensure_result_dir(save_name): + os.makedirs(f"./result/{save_name}", exist_ok=True) + + +def get_groups(samples, group_info): + group_samples = defaultdict(list) + for sample in samples: + group_samples['all'].append(sample) + for group in group_info: + select_flag = True + for k, v in group.items(): + # gt_attribute is a list containing all merged gt attributes + for gt_attribute in sample['gt_attribute']: + if not gt_attribute: # if no GT attributes, don't include in calculation + select_flag = False + elif gt_attribute[k] != v: # if any gt attribute doesn't meet criteria, don't select + select_flag = False + if select_flag: + group_samples[str(group)].append(sample) + return group_samples + + +@METRIC_REGISTRY.register("TEDS") +class call_TEDS(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix=''): + teds = TEDS(structure_only=False) + teds_structure_only = TEDS(structure_only=True) + group_scores = defaultdict(list) + group_scores_structure_only = defaultdict(list) + samples = self.samples + per_table_score = {} + for sample in samples: + gt = sample['norm_gt'] if sample.get('norm_gt') else sample['gt'] + pred = sample['norm_pred'] if sample.get('norm_pred') else sample['pred'] + try: + score = teds.evaluate(pred, gt) + except BaseException: + score = 0 + print( + f'TEDS score error for table {sample["gt_idx"]} in {sample["img_id"]}. The score is set to 0.') + try: + score_structure_only = teds_structure_only.evaluate(pred, gt) + except BaseException: + score_structure_only = 0 + print( + f'TEDS_structure_only score error for table {sample["gt_idx"]} in {sample["img_id"]}. The score is set to 0.') + # print('TEDS score:', score) + group_scores['all'].append(score) + group_scores_structure_only['all'].append(score_structure_only) + if not sample.get('metric'): + sample['metric'] = {} + sample['metric']['TEDS'] = score + sample['metric']['TEDS_structure_only'] = score_structure_only + per_table_score[sample['img_id'] + '_' + + str(sample['gt_idx'])] = {'TEDS': score, 'TEDS_structure_only': score_structure_only} + for group in group_info: + select_flag = True + for k, v in group.items(): + # gt_attribute is a list containing all merged gt attributes + for gt_attribute in sample['gt_attribute']: + if not gt_attribute: # if no GT attributes, don't include in calculation + select_flag = False + elif gt_attribute[k] != v: # if any gt attribute doesn't meet criteria, don't select + select_flag = False + if select_flag: + group_scores[str(group)].append(score) + _ensure_result_dir(save_name) + with open(f'./result/{save_name}/{save_name}_{metric_suffix}_per_table_TEDS.json', 'w', encoding='utf-8') as f: + json.dump(per_table_score, f, indent=4, ensure_ascii=False) + result = {} + for group_name, scores in group_scores.items(): + if len(scores) > 0: + # average of normalized scores at sample level + result[group_name] = sum(scores) / len(scores) + else: + result[group_name] = 'NaN' + print(f'Warning: Empyty matched samples for {group_name}.') + + structure_only_result = {} + for group_name, scores in group_scores_structure_only.items(): + if len(scores) > 0: + # average of normalized scores at sample level + structure_only_result[group_name] = sum(scores) / len(scores) + else: + structure_only_result[group_name] = 'NaN' + print(f'Warning: Empyty matched samples for {group_name}.') + + return samples, {'TEDS': result, 'TEDS_structure_only': structure_only_result} + + +@METRIC_REGISTRY.register("BLEU") +class call_BLEU(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix=''): + group_samples = get_groups(self.samples, group_info) + result = {} + for group_name, samples in group_samples.items(): + predictions, references = [], [] + for sample in samples: + gt = sample['norm_gt'] if sample.get('norm_gt') else sample['gt'] + pred = sample['norm_pred'] if sample.get('norm_pred') else sample['pred'] + predictions.append(gt) + references.append(pred) + try: + bleu = evaluate.load( + "bleu", + keep_in_memory=True, + experiment_id=random.randint( + 1, + 1e8)) + bleu_results = bleu.compute(predictions=predictions, references=references) + result[group_name] = bleu_results["bleu"] + except Exception as e: + print(f'Warning: BLEU unavailable for {group_name}: {e}') + result[group_name] = 'NaN' + + return self.samples, {'BLEU': result} + + +@METRIC_REGISTRY.register("METEOR") +class call_METEOR(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix=''): + group_samples = get_groups(self.samples, group_info) + result = {} + for group_name, samples in group_samples.items(): + predictions, references = [], [] + for sample in samples: + gt = sample['norm_gt'] if sample.get('norm_gt') else sample['gt'] + pred = sample['norm_pred'] if sample.get('norm_pred') else sample['pred'] + predictions.append(gt) + references.append(pred) + try: + meteor = evaluate.load( + 'meteor', + keep_in_memory=True, + experiment_id=random.randint( + 1, + 1e8)) + meteor_results = meteor.compute(predictions=predictions, references=references) + result[group_name] = meteor_results['meteor'] + except Exception as e: + print(f'Warning: METEOR unavailable for {group_name}: {e}') + result[group_name] = 'NaN' + + return self.samples, {'METEOR': result} + + +@METRIC_REGISTRY.register("Edit_dist") +class call_Edit_dist(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix=''): + samples = self.samples + for sample in samples: + if sample.get('page_id'): + img_name = sample['page_id'] + sample['image_name'] = img_name + img_id = img_name + else: + img_id = sample['img_id'] + low = img_id.lower() + if low.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp')): + img_id_no_ext = os.path.splitext(os.path.basename(img_id))[0] + else: + img_id_no_ext = img_id + + head_tail = img_id_no_ext.rsplit('_', 1) + + def _suffix_is_element_index(suffix: str) -> bool: + if not suffix.isdigit(): + return False + suffix_int = int(suffix) + for k in ('gt_position', 'pred_position', 'gt_idx', 'pred_idx'): + v = sample.get(k) + if v is None: + continue + if isinstance(v, list): + for item in v: + try: + if int(item) == suffix_int: + return True + except Exception: + continue + else: + try: + if int(v) == suffix_int: + return True + except Exception: + continue + return False + + if len(head_tail) == 2 and _suffix_is_element_index(head_tail[1]): + img_name = head_tail[0] + else: + img_name = img_id_no_ext + sample['image_name'] = img_name + gt = sample['norm_gt'] if sample.get('norm_gt') else sample['gt'] + pred = sample['norm_pred'] if sample.get('norm_pred') else sample['pred'] + upper_len = max(len(pred), len(gt)) + sample['upper_len'] = upper_len + if len(pred) > 0 or len(gt) > 0: + edit_dist = Levenshtein.distance(pred, gt) + if not sample.get('metric'): + sample['metric'] = {} + sample['metric']['Edit_dist'] = edit_dist / upper_len + sample['Edit_num'] = edit_dist + + if isinstance(samples, list): + saved_samples = samples + else: + saved_samples = samples.samples + + if not saved_samples: + return samples, {'Edit_dist': {'ALL_page_avg': 'NaN'}} + + df = pd.DataFrame(saved_samples) + # page level, sum of edits divided by sum of max(gt,pred) lengths for each sample + up_total_avg = df.groupby("image_name").apply( + lambda x: x['Edit_num'].sum() / x['upper_len'].sum()) + df['Edit_num'].sum() / df['upper_len'].sum() + # all_total_avg = df["Edit_dist"].mean() + per_img_score = up_total_avg.to_dict() + _ensure_result_dir(save_name) + with open(f'./result/{save_name}/{save_name}_{metric_suffix}_per_page_edit.json', 'w', encoding='utf-8') as f: + json.dump(per_img_score, f, indent=4, ensure_ascii=False) + + # if 'display_formula' in save_name: + # return samples, {'Edit_dist': {'ALL_page_avg': all_total_avg}} + # else: + # return samples, {'Edit_dist': {'ALL_page_avg': up_total_avg.mean()}} + + edit_whole = df['Edit_num'].sum() / df['upper_len'].sum() + df['ratio'] = df['Edit_num'] / df['upper_len'] + edit_sample_avg = df['ratio'].mean() + # edit_sample_avg = df['metric']['Edit_dist'].mean() + return samples, {'Edit_dist': {'ALL_page_avg': up_total_avg.mean( + ), 'edit_whole': edit_whole, 'edit_sample_avg': edit_sample_avg}} + + +def _process_single_cdm_sample(args): + """Worker function to process a single CDM sample""" + idx, sample, output_root, group_info = args + + # Create a new CDM instance for this worker to avoid thread safety issues + cal_cdm = CDM(output_root=output_root) + + # Prepare sample data + sample_copy = copy.deepcopy(sample) + sample_copy['img_id_cdm'] = str(idx) + sample_copy['gt'] = sample_copy['gt'].lstrip("$$").rstrip("$$").strip() + sample_copy['gt'] = sample_copy['gt'].lstrip("$").rstrip("$").strip() + sample_copy['pred'] = sample_copy['pred'].split("```latex")[-1].split("```")[0] + sample_copy['pred'] = sample_copy['pred'].lstrip("$$").rstrip("$$").strip() + sample_copy['pred'] = sample_copy['pred'].lstrip("$").rstrip("$").strip() + + # Calculate CDM score + cdm_score = cal_cdm.evaluate( + sample_copy['gt'], + sample_copy['pred'], + sample_copy['img_id_cdm'])["F1_score"] + + # Add metric to sample + if not sample_copy.get('metric'): + sample_copy['metric'] = {} + sample_copy['metric']['CDM'] = cdm_score + + # Check which groups this sample belongs to + matched_groups = [] + for group in group_info: + select_flag = True + for k, v in group.items(): + for gt_attribute in sample_copy['gt_attribute']: + if not gt_attribute: + select_flag = False + elif gt_attribute[k] != v: + select_flag = False + if select_flag: + matched_groups.append(str(group)) + + return { + 'sample': sample_copy, + 'cdm_score': cdm_score, + 'sample_key': sample_copy['img_id'] + '_' + str(sample_copy['gt_idx']), + 'matched_groups': matched_groups, + 'original_index': idx + } + + +@METRIC_REGISTRY.register("CDM") +class call_CDM(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix='', max_workers=32): + group_scores = defaultdict(list) + output_root = f'./result/{save_name}/CDM' + + if isinstance(self.samples, list): + original_samples = self.samples + else: + original_samples = self.samples.samples + + # Prepare arguments for concurrent processing + worker_args = [] + for idx, sample in enumerate(original_samples): + worker_args.append((idx, sample, output_root, group_info)) + + # Use concurrent execution + per_sample_score = {} + cdm_samples = [] + results = [] + + with ProcessPoolExecutor(max_workers=max_workers) as executor: + # Submit all tasks + future_to_idx = { + executor.submit( + _process_single_cdm_sample, + args): args[0] for args in worker_args} + + # Collect results as they complete + for future in as_completed(future_to_idx): + try: + result = future.result() + results.append(result) + except Exception as exc: + idx = future_to_idx[future] + print(f'Sample {idx} generated an exception: {exc}') + # Create a default result for failed samples + sample_copy = copy.deepcopy(original_samples[idx]) + sample_copy['img_id_cdm'] = str(idx) + if not sample_copy.get('metric'): + sample_copy['metric'] = {} + sample_copy['metric']['CDM'] = 0.0 + results.append({ + 'sample': sample_copy, + 'cdm_score': 0.0, + 'sample_key': sample_copy['img_id'] + '_' + str(sample_copy['gt_idx']), + 'matched_groups': [], + 'original_index': idx + }) + + # Sort results by original index to maintain order + results.sort(key=lambda x: x['original_index']) + + # Process results + for result in results: + sample = result['sample'] + cdm_score = result['cdm_score'] + sample_key = result['sample_key'] + matched_groups = result['matched_groups'] + + cdm_samples.append(sample) + per_sample_score[sample_key] = cdm_score + group_scores['all'].append(cdm_score) + + # Add scores to matched groups + for group_name in matched_groups: + group_scores[group_name].append(cdm_score) + + # Save results to files + _ensure_result_dir(save_name) + with open(f'./result/{save_name}/{save_name}_{metric_suffix}_per_sample_CDM.json', 'w', encoding='utf-8') as f: + json.dump(per_sample_score, f, indent=4, ensure_ascii=False) + + _ensure_result_dir(save_name) + with open(f'./result/{save_name}/{save_name}_{metric_suffix}_result.json', 'w', encoding='utf-8') as f: + json.dump(cdm_samples, f, indent=4, ensure_ascii=False) + + # Calculate final results + result = {} + for group_name, scores in group_scores.items(): + if len(scores) > 0: + # average of normalized scores at sample level + result[group_name] = sum(scores) / len(scores) + else: + result[group_name] = 'NaN' + print(f'Warning: Empty matched samples for {group_name}.') + + return cdm_samples, {'CDM': result} + + +@METRIC_REGISTRY.register("CDM_plain") +class call_CDM_plain(): + def __init__(self, samples): + self.samples = samples + + def evaluate(self, group_info=[], save_name='default', metric_suffix=''): + if isinstance(self.samples, list): + cdm_samples = copy.deepcopy(self.samples) + else: + cdm_samples = copy.deepcopy(self.samples.samples) + for idx, sample in enumerate(cdm_samples): + sample['img_name'] = sample['img_id'] + sample['img_id'] = str(idx) + sample['gt'] = sample['gt'].lstrip("$$").rstrip("$$").strip() + sample['pred'] = sample['pred'].split("```latex")[-1].split("```")[0] + sample['pred'] = sample['pred'].lstrip("$$").rstrip("$$").strip() + + # time_stap = time.time() + _ensure_result_dir(save_name) + with open(f'./result/{save_name}/{save_name}_{metric_suffix}_formula.json', 'w', encoding='utf-8') as f: + json.dump(cdm_samples, f, indent=4, ensure_ascii=False) + return self.samples, False diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/README-CN.md b/vlmeval/dataset/MDPBench/metrics/cdm/README-CN.md new file mode 100644 index 000000000..f2be85b3b --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/README-CN.md @@ -0,0 +1,153 @@ +
+ +[English](./README.md) | [简体中文] + +

Image Over Text: Transforming Formula Recognition Evaluation with Character Detection Matching

+ +[[ 论文 ]](https://arxiv.org/pdf/2409.03643) [[ 网站 ]](https://github.com/opendatalab/UniMERNet/tree/main/cdm) +[[在线Demo 🤗(Hugging Face)]](https://huggingface.co/spaces/opendatalab/CDM-Demo) + +
+ + +# 概述 + +公式识别因其复杂的结构和多样的符号表示而面临重大挑战。尽管公式识别模型不断进步,但现有评估指标如 BLEU 和编辑距离仍存在显著局限性。这些指标忽视了同一公式的多种表示形式,并对训练数据的分布高度敏感,导致评估不公。为此,我们提出了字符检测匹配(CDM)指标,通过设计基于图像而非 LaTeX 的评分方法来确保评估的客观性。具体而言,CDM 将模型预测的 LaTeX 和真实 LaTeX 公式渲染为图像格式,然后使用视觉特征提取和定位技术进行精确的字符级匹配,结合空间位置信息。相比于仅依赖文本字符匹配的 BLEU 和编辑距离,CDM 提供了更准确和公平的评估。 + +CDM与BLEU、EditDistance等指标对比示意图: + +
+ Demo +
+ +> 从上述对比图中可以看出: +- Case1: 模型预测正确,理论上ExpRate/BLEU/EditDist应该为1/1/0,实际上为0/0.449/0.571,完全无法反应识别准确性; +- Case2 Vs Case1: 预测错误的模型(Case2) BLEU/EditDist指标确远优于识别正确的模型结果(Case1); +- Case3: 模型预测错误较多,而BLEU指标确高达0.907,不符合直觉。 + + +CDM的算法流程图如下: + +
+ Overview +
+ +可以看到CDM基于渲染图像的字符匹配方式,结果更加直观,且不受公式表达多样性影响。 + + + +# + +# 使用方法 + +## 在线Demo体验 + +请点击HuggingFace Demo链接: [(Hugging Face)🤗](https://huggingface.co/spaces/opendatalab/CDM-Demo) + +## 本地安装CDM + +CDM需要对公式进行渲染,需要相关依赖包,推荐在Linux系统安装配置 + +## 准备环境 + +需要的依赖包括:Nodejs, imagemagic, pdflatex,请按照下面的指令进行安装: + +### 步骤.1 安装 nodejs + +``` +wget https://registry.npmmirror.com/-/binary/node/latest-v16.x/node-v16.13.1-linux-x64.tar.gz + +tar -xvf node-v16.13.1-linux-x64.tar.gz + +mv node-v16.13.1-linux-x64/* /usr/local/nodejs/ + +ln -s /usr/local/nodejs/bin/node /usr/local/bin + +ln -s /usr/local/nodejs/bin/npm /usr/local/bin + +node -v +``` + +### 步骤.2 安装 imagemagic + +`apt-get`命令安装的imagemagic版本是6.x,我们需要安装7.x的,所以从源码编译安装: + +(编译前需要确认系统内安装有libpng-dev,否则编译出来的magick无法支持cdm使用) +``` +git clone https://github.com/ImageMagick/ImageMagick.git ImageMagick-7.1.1 + +cd ImageMagick-7.1.1 + +./configure + +make + +sudo make install + +sudo ldconfig /usr/local/lib + +convert --version +``` + +### 步骤.3 安装 latexpdf + +渲染中文公式需要中文字体,当前cdm中使用的是思源黑体Source Han Sans SC. + +``` +apt-get update + +sudo apt-get install texlive-full +``` + +### step.4 安装 python 依赖 + +``` +pip install -r requirements.txt +``` + + +## 使用CDM + +如果安装过程顺利,现在可以使用CDM对公式识别的结果进行评测了。 + +### 1. 批量评测 + +- 准备输入的json文件 + +在UniMERNet上评测,可以用下面的脚本获取json文件: + +``` +python convert2cdm_format.py -i {UniMERNet predictions} -o {save path} +``` + +或者,也可以参考下面的格式自行准备json文件: + +``` +[ + { + "img_id": "case_1", # 非必须的key + "gt": "y = 2z + 3x", + "pred": "y = 2x + 3z" + }, + { + "img_id": "case_2", + "gt": "y = x^2 + 1", + "pred": "y = x^2 + 1" + }, + ... +] +``` + +`注意在json文件中,一些特殊字符比如 "\" 需要进行转义, 比如 "\begin" 在json文件中就需要保存为 "\\begin".` + +- 评测: + +``` +python evaluation.py -i {path_to_your_input_json} +``` + +### 2. 启动 gradio demo + +``` +python app.py +``` diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/README.md b/vlmeval/dataset/MDPBench/metrics/cdm/README.md new file mode 100644 index 000000000..bdd0bbdea --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/README.md @@ -0,0 +1,147 @@ +
+ +English | [简体中文](./README-CN.md) + +

Image Over Text: Transforming Formula Recognition Evaluation with Character Detection Matching

+ +[[ Paper ]](https://arxiv.org/pdf/2409.03643) [[ Website ]](https://github.com/opendatalab/UniMERNet/tree/main/cdm) +[[Demo 🤗(Hugging Face)]](https://huggingface.co/spaces/opendatalab/CDM-Demo) + +
+ + +# Overview + +Formula recognition presents significant challenges due to the complicated structure and varied notation of mathematical expressions. Despite continuous advancements in formula recognition models, the evaluation metrics employed by these models, such as BLEU and Edit Distance, still exhibit notable limitations. They overlook the fact that the same formula has diverse representations and is highly sensitive to the distribution of training data, thereby causing the unfairness in formula recognition evaluation. To this end, we propose a Character Detection Matching (CDM) metric, ensuring the evaluation objectivity by designing a image-level rather than LaTex-level metric score. Specifically, CDM renders both the model-predicted LaTeX and the ground-truth LaTeX formulas into image-formatted formulas, then employs visual feature extraction and localization techniques for precise character-level matching, incorporating spatial position information. Such a spatially-aware and character-matching method offers a more accurate and equitable evaluation compared with previous BLEU and Edit Distance metrics that rely solely on text-based character matching. + +Comparison between CDM and BLEU, Edit Distance metrics: +
+ Demo +
+ + +The algorithm flow of CDM is as follows: + +
+ Overview +
+ + +CDM's character matching method based on rendered images provides more intuitive results and is not affected by the diversity of formula representations. + + + +# Usage + +## Try Online Demo + +Try CDM on our online demo: [(Hugging Face)🤗](https://huggingface.co/spaces/opendatalab/CDM-Demo) + +## Install CMD Locally + +Given CDM's complex environment dependencies, we recommend trying it on Linux systems. + +## prepare environment + +Nodejs, imagemagic, pdflatex are requried packages when render pdf files and convert them to images, here are installation guides. + +### step.1 install nodejs + +``` +wget https://registry.npmmirror.com/-/binary/node/latest-v16.x/node-v16.13.1-linux-x64.tar.gz + +tar -xvf node-v16.13.1-linux-x64.tar.gz + +mv node-v16.13.1-linux-x64/* /usr/local/nodejs/ + +ln -s /usr/local/nodejs/bin/node /usr/local/bin + +ln -s /usr/local/nodejs/bin/npm /usr/local/bin + +node -v +``` + +### step.2 install imagemagic + +the version of imagemagic installed by `apt-get` usually be 6.x, so we also install it from source code. + +(Before compiling, ensure that libpng-dev is installed on the system; otherwise, the compiled magick will not support CDM usage.) +``` +git clone https://github.com/ImageMagick/ImageMagick.git ImageMagick-7.1.1 + +cd ImageMagick-7.1.1 + +./configure + +make + +sudo make install + +sudo ldconfig /usr/local/lib + +convert --version +``` + +### step.3 install latexpdf + +Rendering Chinese formulas requires a Chinese font, Source Han Sans SC is currently used . + +``` +apt-get update + +sudo apt-get install texlive-full +``` + +### step.4 install python requriements + +``` +pip install -r requirements.txt +``` + + +## Use CDM Locally + +Should the installation goes well, you may now use CDM to evaluate your formula recognition results. + +### 1. batch evaluation + +- prepare input json + +evaluate on UniMERNet results, use this convert script to get json file: + +``` +python convert2cdm_format.py -i {UniMERNet predictions} -o {save path} +``` + +otherwise, prepare a json file follow this format: + +``` +[ + { + "img_id": "case_1", # optional key + "gt": "y = 2z + 3x", + "pred": "y = 2x + 3z" + }, + { + "img_id": "case_2", + "gt": "y = x^2 + 1", + "pred": "y = x^2 + 1" + }, + ... +] +``` + +`Note that in json files, some special characters such as "\" need escaped character, for example "\begin" should be written as "\\begin".` + +- evaluate: + +``` +python evaluation.py -i {path_to_your_input_json} +``` + + +### 2. launch a gradio demo + +``` +python app.py +``` diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/app.py b/vlmeval/dataset/MDPBench/metrics/cdm/app.py new file mode 100644 index 000000000..cafe3291d --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/app.py @@ -0,0 +1,391 @@ +import json +import os +import shutil +import time +from datetime import datetime + +import gradio as gr +import numpy as np +from modules.latex2bbox_color import latex2bbox_color +from modules.visual_matcher import HungarianMatcher, SimpleAffineTransform +from PIL import Image, ImageDraw +from skimage.measure import ransac + +DATA_ROOT = "output" + + +def gen_color_list(num=10, gap=15): + num += 1 + single_num = 255 // gap + 1 + max_num = single_num ** 3 + num = min(num, max_num) + color_list = [] + for idx in range(num): + R = idx // single_num**2 + GB = idx % single_num**2 + G = GB // single_num + B = GB % single_num + + color_list.append((R * gap, G * gap, B * gap)) + return color_list[1:] + + +def process_latex(groundtruths, predictions, user_id="test"): + data_root = DATA_ROOT + temp_dir = os.path.join(data_root, "temp_dir") + + data_root = os.path.join(data_root, user_id) + output_dir_info = {} + for subset, latex_list in zip(['gt', 'pred'], [groundtruths, predictions]): + sub_temp_dir = os.path.join(temp_dir, f"{user_id}_{subset}") + os.makedirs(sub_temp_dir, exist_ok=True) + + output_path = os.path.join(data_root, subset) + output_dir_info[output_path] = [] + + os.makedirs(os.path.join(output_path, 'bbox'), exist_ok=True) + os.makedirs(os.path.join(output_path, 'vis'), exist_ok=True) + + total_color_list = gen_color_list(num=5800) + + for idx, latex in enumerate(latex_list): + basename = f"sample_{idx}" + input_arg = latex, basename, output_path, sub_temp_dir, total_color_list + time.time() + latex2bbox_color(input_arg) + time.time() + + for subset in ['gt', 'pred']: + shutil.rmtree(os.path.join(temp_dir, f"{user_id}_{subset}")) + + +def update_inliers(ori_inliers, sub_inliers): + inliers = np.copy(ori_inliers) + sub_idx = -1 + for idx in range(len(ori_inliers)): + if ori_inliers[idx] == False: + sub_idx += 1 + if sub_inliers[sub_idx] == True: + inliers[idx] = True + return inliers + + +def reshape_inliers(ori_inliers, sub_inliers): + inliers = np.copy(ori_inliers) + sub_idx = -1 + for idx in range(len(ori_inliers)): + if ori_inliers[idx] == False: + sub_idx += 1 + if sub_inliers[sub_idx] == True: + inliers[idx] = True + else: + inliers[idx] = False + return inliers + + +def evaluation(user_id="test"): + data_root = DATA_ROOT + data_root = os.path.join(data_root, user_id) + gt_box_dir = os.path.join(data_root, "gt") + pred_box_dir = os.path.join(data_root, "pred") + match_vis_dir = os.path.join(data_root, "vis_match") + os.makedirs(match_vis_dir, exist_ok=True) + + max_iter = 3 + min_samples = 3 + residual_threshold = 25 + max_trials = 50 + + metrics_per_img = {} + gt_basename_list = [item.split(".")[0] + for item in os.listdir(os.path.join(gt_box_dir, 'bbox'))] + for basename in gt_basename_list: + gt_valid, pred_valid = True, True + if not os.path.exists(os.path.join(gt_box_dir, 'bbox', basename + ".jsonl")): + gt_valid = False + else: + with open(os.path.join(gt_box_dir, 'bbox', basename + ".jsonl"), 'r') as f: + box_gt = [] + for line in f: + info = json.loads(line) + if info['bbox']: + box_gt.append(info) + if not box_gt: + gt_valid = False + if not gt_valid: + continue + + if not os.path.exists(os.path.join(pred_box_dir, 'bbox', basename + ".jsonl")): + pred_valid = False + else: + with open(os.path.join(pred_box_dir, 'bbox', basename + ".jsonl"), 'r') as f: + box_pred = [] + for line in f: + info = json.loads(line) + if info['bbox']: + box_pred.append(info) + if not box_pred: + pred_valid = False + if not pred_valid: + metrics_per_img[basename] = { + "recall": 0, + "precision": 0, + "F1_score": 0, + } + continue + gt_img_path = os.path.join(gt_box_dir, 'vis', basename + "_base.png") + pred_img_path = os.path.join(pred_box_dir, 'vis', basename + "_base.png") + + img_gt = Image.open(gt_img_path) + img_pred = Image.open(pred_img_path) + + matcher = HungarianMatcher() + matched_idxes = matcher(box_gt, box_pred, img_gt.size, img_pred.size) + src = [] + dst = [] + for (idx1, idx2) in matched_idxes: + x1min, y1min, x1max, y1max = box_gt[idx1]['bbox'] + x2min, y2min, x2max, y2max = box_pred[idx2]['bbox'] + x1_c, y1_c = float((x1min + x1max) / 2), float((y1min + y1max) / 2) + x2_c, y2_c = float((x2min + x2max) / 2), float((y2min + y2max) / 2) + src.append([y1_c, x1_c]) + dst.append([y2_c, x2_c]) + + src = np.array(src) + dst = np.array(dst) + if src.shape[0] <= min_samples: + inliers = np.array([True for _ in matched_idxes]) + else: + inliers = np.array([False for _ in matched_idxes]) + for i in range(max_iter): + if src[inliers == False].shape[0] <= min_samples: + break + model, inliers_1 = ransac((src[inliers == False], dst[inliers == False]), SimpleAffineTransform, + min_samples=min_samples, residual_threshold=residual_threshold, max_trials=max_trials) + if inliers_1 is not None and inliers_1.any(): + inliers = update_inliers(inliers, inliers_1) + else: + break + if len(inliers[inliers == True]) >= len(matched_idxes): + break + + for idx, (a, b) in enumerate(matched_idxes): + if inliers[idx] == True and matcher.cost['token'][a, b] == 1: + inliers[idx] = False + + final_match_num = len(inliers[inliers == True]) + recall = round(final_match_num / (len(box_gt)), 3) + precision = round(final_match_num / (len(box_pred)), 3) + F1_score = round(2 * final_match_num / (len(box_gt) + len(box_pred)), 3) + metrics_per_img[basename] = { + "recall": recall, + "precision": precision, + "F1_score": F1_score, + } + + if True: + gap = 5 + W1, H1 = img_gt.size + W2, H2 = img_pred.size + H = H1 + H2 + gap + W = max(W1, W2) + + vis_img = Image.new('RGB', (W, H), (255, 255, 255)) + vis_img.paste(img_gt, (0, 0)) + vis_img.paste(Image.new('RGB', (W, gap), (0, 150, 200)), (0, H1)) + vis_img.paste(img_pred, (0, H1 + gap)) + + match_img = vis_img.copy() + match_draw = ImageDraw.Draw(match_img) + + gt_matched_idx = { + a: flag + for (a, b), flag in + zip(matched_idxes, inliers) + } + pred_matched_idx = { + b: flag + for (a, b), flag in + zip(matched_idxes, inliers) + } + + for idx, box in enumerate(box_gt): + if idx in gt_matched_idx and gt_matched_idx[idx] == True: + color = "green" + else: + color = "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, y_min - 1, x_max + 1, y_max + 1], + fill=None, outline=color, width=2) + + for idx, box in enumerate(box_pred): + if idx in pred_matched_idx and pred_matched_idx[idx] == True: + color = "green" + else: + color = "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, + y_min - 1 + H1 + gap, + x_max + 1, + y_max + 1 + H1 + gap], + fill=None, + outline=color, + width=2) + + vis_img.save(os.path.join(match_vis_dir, basename + "_base.png")) + if W < 500: + padding = (500 - W) // 2 + 1 + reshape_match_img = Image.new('RGB', (500, H), (255, 255, 255)) + reshape_match_img.paste(match_img, (padding, 0)) + reshape_match_img.paste(Image.new('RGB', (500, gap), (0, 150, 200)), (0, H1)) + reshape_match_img.save(os.path.join(match_vis_dir, basename + ".png")) + else: + match_img.save(os.path.join(match_vis_dir, basename + ".png")) + + acc_list = [val['F1_score'] for _, val in metrics_per_img.items()] + metrics_res = { + "mean_score": round(np.mean(acc_list), 3), + "details": metrics_per_img + } + metric_res_path = os.path.join(data_root, "metrics_res.json") + with open(metric_res_path, "w") as f: + f.write(json.dumps(metrics_res, indent=2)) + return metrics_res, metric_res_path, match_vis_dir + + +def calculate_metric_single(groundtruth, prediction): + user_id = datetime.now().strftime('%Y%m%d-%H%M%S') + process_latex([groundtruth], [prediction], user_id) + metrics_res, metric_res_path, match_vis_dir = evaluation(user_id) + basename = "sample_0" + image_path = os.path.join(match_vis_dir, basename + ".png") + sample = metrics_res["details"][basename] + score = sample['F1_score'] + recall = sample['recall'] + precision = sample['precision'] + return score, recall, precision, gr.Image(image_path) + + +def calculate_metric_batch(json_input): + user_id = datetime.now().strftime('%Y%m%d-%H%M%S') + with open(json_input.name, "r") as f: + input_data = json.load(f) + groundtruths = [] + predictions = [] + for item in input_data: + groundtruths.append(item['gt']) + predictions.append(item['pred']) + process_latex(groundtruths, predictions, user_id) + metrics_res, metric_res_path, match_vis_dir = evaluation(user_id) + return metric_res_path + + +def gradio_reset_single(): + return gr.update(value=None, placeholder='type gt latex code here'), gr.update(value=None, placeholder='type pred latex code here'), \ + gr.update(value=None), gr.update(value=None), gr.update(value=None), gr.update(value=None) + + +def gradio_reset_batch(): + return gr.update(value=None), gr.update(value=None) + + +def select_example1(): + gt = "y = 2x + 3z" + pred = "y = 2z + 3x" + return gr.update(value=gt, placeholder='type gt latex code here'), gr.update( + value=pred, placeholder='type pred latex code here') + + +def select_example2(): + gt = "r = \\frac { \\alpha } { \\beta } \\vert \\sin \\beta \\left( \\sigma _ { 1 } \\pm \\sigma _ { 2 } \\right) \\vert" + pred = "r={\\frac{\\alpha}{\\beta}}|\\sin\\beta\\left(\\sigma_{2}+\\sigma_{1}\\right)|" + return gr.update(value=gt, placeholder='type gt latex code here'), gr.update( + value=pred, placeholder='type pred latex code here') + + +def select_example3(): + gt = "\\begin{array} { r l r } & { } & { \\mathbf { J } _ { L } = \\left( \\begin{array} { c c } { 0 } & { 0 } \\\\ { v _ { n } } & { 0 } \\end{array} \\right) , ~ \\mathbf { J } _ { R } = \\left( \\begin{array} { c c } { u _ { n - 1 } } & { 0 } \\\\ { 0 } & { 0 } \\end{array} \\right) , ~ } \\\\ & { } & {\\mathbf { K } = \\left( \\begin{array} { c c } { V _ { n - 1 } } & { u _ { n } } \\\\ { v _ { n - 1 } } & { V _ { n } } \\end{array} \\right) , } \\end{array}" + pred = "\\mathbf{J}_{U}={\\left(\\begin{array}{l l}{0}&{0}\\\\ {v_{n}}&{0}\\end{array}\\right)}\\,,\\ \\mathbf{J}_{R}={\\left(\\begin{array}{l l}{u_{n-1}}&{0}\\\\ {0}&{0}\\end{array}\\right)}\\,,\\mathbf{K}={\\left(\\begin{array}{l l}{V_{n-1}}&{u_{n}}\\\\ {v_{n-1}}&{V_{n}}\\end{array}\\right)}\\,," + return gr.update(value=gt, placeholder='type gt latex code here'), gr.update( + value=pred, placeholder='type pred latex code here') + + +if __name__ == "__main__": + title = """

Character Detection Matching (CDM)

""" + + with gr.Blocks() as demo: + gr.Markdown(title) + + gr.Button( + value="Quick Try: type latex code of gt and pred, get metrics and visualization.", + interactive=False, + variant="primary") + + with gr.Row(): + with gr.Column(): + gt_input = gr.Textbox( + label='gt', + placeholder='type gt latex code here', + interactive=True) + pred_input = gr.Textbox( + label='pred', + placeholder='type pred latex code here', + interactive=True) + with gr.Row(): + clear_single = gr.Button("Clear") + submit_single = gr.Button(value="Submit", interactive=True, variant="primary") + with gr.Accordion("Examples:"): + with gr.Row(): + example1 = gr.Button("Example A(short)") + example2 = gr.Button("Example B(middle)") + example3 = gr.Button("Example C(long)") + with gr.Column(): + with gr.Row(): + score_output = gr.Number(label="F1 Score", interactive=False) + recall_output = gr.Number(label="Recall", interactive=False) + recision_output = gr.Number(label="Precision", interactive=False) + gr.Button( + value="Visualization (green bbox means correcttlly matched, red bbox means missed or wrong.)", + interactive=False) + vis_output = gr.Image(label=" ", interactive=False) + + example1.click(select_example1, inputs=None, outputs=[gt_input, pred_input]) + example2.click(select_example2, inputs=None, outputs=[gt_input, pred_input]) + example3.click(select_example3, inputs=None, outputs=[gt_input, pred_input]) + + clear_single.click( + gradio_reset_single, + inputs=None, + outputs=[ + gt_input, + pred_input, + score_output, + recall_output, + recision_output, + vis_output]) + submit_single.click( + calculate_metric_single, inputs=[ + gt_input, pred_input], outputs=[ + score_output, recall_output, recision_output, vis_output]) + + gr.Button( + value="Batch Run: upload a json file and batch processing, this may take some times according to your latex amount and length.", + interactive=False, + variant="primary") + + with gr.Row(): + with gr.Column(): + json_input = gr.File(label="Input Json", file_types=[".json"]) + json_example = gr.File( + label="Input Example", + value="assets/example/input_example.json") + with gr.Row(): + clear_batch = gr.Button("Clear") + submit_batch = gr.Button(value="Submit", interactive=True, variant="primary") + + metric_output = gr.File(label="Output Metrics") + + clear_batch.click(gradio_reset_batch, inputs=None, outputs=[json_input, metric_output]) + submit_batch.click(calculate_metric_batch, inputs=[json_input], outputs=[metric_output]) + + demo.launch(share=True, server_name="0.0.0.0", server_port=10005, debug=True) diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_demo.png b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_demo.png new file mode 100644 index 000000000..1a886e2e5 Binary files /dev/null and b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_demo.png differ diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework.png b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework.png new file mode 100644 index 000000000..0e5cb3b21 Binary files /dev/null and b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework.png differ diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework_new.png b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework_new.png new file mode 100644 index 000000000..ec89903d4 Binary files /dev/null and b/vlmeval/dataset/MDPBench/metrics/cdm/assets/demo/cdm_framework_new.png differ diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/convert2cdm_format.py b/vlmeval/dataset/MDPBench/metrics/cdm/convert2cdm_format.py new file mode 100644 index 000000000..9d5d6077c --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/convert2cdm_format.py @@ -0,0 +1,37 @@ +import argparse +import json +import os + +from tqdm import tqdm + + +def change_data_format(input_json, output_json): + with open(input_json, 'r') as f: + all_datas = json.load(f) + + data_list = [] + + for key in all_datas.keys(): + subset = key[-4:-1].lower() + for data in tqdm(all_datas[key]['text']): + im_id = os.path.basename(data['image_path'])[0:-4] + basename = f"{subset}_{im_id}" + new_item = { + "img_id": basename, + "gt": data["reference"], + "pred": data["prediction"] + } + data_list.append(new_item) + + with open(output_json, "w") as f: + f.write(json.dumps(data_list, indent=2)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--input', '-i', type=str) + parser.add_argument('--output', '-o', type=str) + args = parser.parse_args() + print(args) + + change_data_format(args.input, args.output) diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/evaluation.py b/vlmeval/dataset/MDPBench/metrics/cdm/evaluation.py new file mode 100644 index 000000000..60983e454 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/evaluation.py @@ -0,0 +1,305 @@ +import argparse +import copy +import json +import os +import shutil +import time +from datetime import datetime +from multiprocessing import Pool + +import numpy as np +from modules.latex2bbox_color import latex2bbox_color +from modules.visual_matcher import HungarianMatcher, SimpleAffineTransform +from PIL import Image, ImageDraw +from skimage.measure import ransac +from tqdm import tqdm + + +def gen_color_list(num=10, gap=15): + num += 1 + single_num = 255 // gap + 1 + max_num = single_num ** 3 + num = min(num, max_num) + color_list = [] + for idx in range(num): + R = idx // single_num**2 + GB = idx % single_num**2 + G = GB // single_num + B = GB % single_num + + color_list.append((R * gap, G * gap, B * gap)) + return color_list[1:] + + +def update_inliers(ori_inliers, sub_inliers): + inliers = np.copy(ori_inliers) + sub_idx = -1 + for idx in range(len(ori_inliers)): + if ori_inliers[idx] == False: + sub_idx += 1 + if sub_inliers[sub_idx] == True: + inliers[idx] = True + return inliers + + +def reshape_inliers(ori_inliers, sub_inliers): + inliers = np.copy(ori_inliers) + sub_idx = -1 + for idx in range(len(ori_inliers)): + if ori_inliers[idx] == False: + sub_idx += 1 + if sub_inliers[sub_idx] == True: + inliers[idx] = True + else: + inliers[idx] = False + return inliers + + +def gen_token_order(box_list): + new_box_list = copy.deepcopy(box_list) + for idx, box in enumerate(new_box_list): + new_box_list[idx]['order'] = idx / len(new_box_list) + return new_box_list + + +def evaluation(data_root, user_id="test"): + data_root = os.path.join(data_root, user_id) + gt_box_dir = os.path.join(data_root, "gt") + pred_box_dir = os.path.join(data_root, "pred") + match_vis_dir = os.path.join(data_root, "vis_match") + os.makedirs(match_vis_dir, exist_ok=True) + + max_iter = 3 + min_samples = 3 + residual_threshold = 25 + max_trials = 50 + + metrics_per_img = {} + gt_basename_list = [item.split(".")[0] + for item in os.listdir(os.path.join(gt_box_dir, 'bbox'))] + for basename in tqdm(gt_basename_list): + gt_valid, pred_valid = True, True + if not os.path.exists(os.path.join(gt_box_dir, 'bbox', basename + ".jsonl")): + gt_valid = False + else: + with open(os.path.join(gt_box_dir, 'bbox', basename + ".jsonl"), 'r') as f: + box_gt = [] + for line in f: + info = json.loads(line) + if info['bbox']: + box_gt.append(info) + if not box_gt: + gt_valid = False + if not gt_valid: + continue + + if not os.path.exists(os.path.join(pred_box_dir, 'bbox', basename + ".jsonl")): + pred_valid = False + else: + with open(os.path.join(pred_box_dir, 'bbox', basename + ".jsonl"), 'r') as f: + box_pred = [] + for line in f: + info = json.loads(line) + if info['bbox']: + box_pred.append(info) + if not box_pred: + pred_valid = False + if not pred_valid: + metrics_per_img[basename] = { + "recall": 0, + "precision": 0, + "F1_score": 0, + } + continue + gt_img_path = os.path.join(gt_box_dir, 'vis', basename + "_base.png") + pred_img_path = os.path.join(pred_box_dir, 'vis', basename + "_base.png") + + img_gt = Image.open(gt_img_path) + img_pred = Image.open(pred_img_path) + + matcher = HungarianMatcher() + matched_idxes = matcher(box_gt, box_pred, img_gt.size, img_pred.size) + src = [] + dst = [] + for (idx1, idx2) in matched_idxes: + x1min, y1min, x1max, y1max = box_gt[idx1]['bbox'] + x2min, y2min, x2max, y2max = box_pred[idx2]['bbox'] + x1_c, y1_c = float((x1min + x1max) / 2), float((y1min + y1max) / 2) + x2_c, y2_c = float((x2min + x2max) / 2), float((y2min + y2max) / 2) + src.append([y1_c, x1_c]) + dst.append([y2_c, x2_c]) + + src = np.array(src) + dst = np.array(dst) + if src.shape[0] <= min_samples: + inliers = np.array([True for _ in matched_idxes]) + else: + inliers = np.array([False for _ in matched_idxes]) + for i in range(max_iter): + if src[inliers == False].shape[0] <= min_samples: + break + model, inliers_1 = ransac((src[inliers == False], dst[inliers == False]), SimpleAffineTransform, + min_samples=min_samples, residual_threshold=residual_threshold, max_trials=max_trials) + if inliers_1 is not None and inliers_1.any(): + inliers = update_inliers(inliers, inliers_1) + else: + break + if len(inliers[inliers == True]) >= len(matched_idxes): + break + + for idx, (a, b) in enumerate(matched_idxes): + if inliers[idx] == True and matcher.cost['token'][a, b] == 1: + inliers[idx] = False + + final_match_num = len(inliers[inliers == True]) + recall = round(final_match_num / (len(box_gt)), 3) + precision = round(final_match_num / (len(box_pred)), 3) + F1_score = round(2 * final_match_num / (len(box_gt) + len(box_pred)), 3) + metrics_per_img[basename] = { + "recall": recall, + "precision": precision, + "F1_score": F1_score, + } + + if True: + gap = 5 + W1, H1 = img_gt.size + W2, H2 = img_pred.size + H = H1 + H2 + gap + W = max(W1, W2) + + vis_img = Image.new('RGB', (W, H), (255, 255, 255)) + vis_img.paste(img_gt, (0, 0)) + vis_img.paste(Image.new('RGB', (W, gap), (120, 120, 120)), (0, H1)) + vis_img.paste(img_pred, (0, H1 + gap)) + + match_img = vis_img.copy() + match_draw = ImageDraw.Draw(match_img) + + gt_matched_idx = { + a: flag + for (a, b), flag in + zip(matched_idxes, inliers) + } + pred_matched_idx = { + b: flag + for (a, b), flag in + zip(matched_idxes, inliers) + } + + for idx, box in enumerate(box_gt): + if idx in gt_matched_idx and gt_matched_idx[idx] == True: + color = "green" + else: + color = "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, y_min - 1, x_max + 1, y_max + 1], + fill=None, outline=color, width=2) + + for idx, box in enumerate(box_pred): + if idx in pred_matched_idx and pred_matched_idx[idx] == True: + color = "green" + else: + color = "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, + y_min - 1 + H1 + gap, + x_max + 1, + y_max + 1 + H1 + gap], + fill=None, + outline=color, + width=2) + + vis_img.save(os.path.join(match_vis_dir, basename + "_base.png")) + match_img.save(os.path.join(match_vis_dir, basename + ".png")) + + score_list = [val['F1_score'] for _, val in metrics_per_img.items()] + exp_list = [1 if score == 1 else 0 for score in score_list] + metrics_res = { + "mean_score": round(np.mean(score_list), 3), + "exp_rate": round(np.mean(exp_list), 3), + "details": metrics_per_img + } + metric_res_path = os.path.join(data_root, "metrics_res.json") + with open(metric_res_path, "w") as f: + f.write(json.dumps(metrics_res, indent=2)) + return metrics_res, metric_res_path, match_vis_dir + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--input', '-i', type=str, default="assets/example/input_example.json") + parser.add_argument('--output', '-o', type=str, default="output") + parser.add_argument('--pools', '-p', type=int, default=240) + args = parser.parse_args() + print(args) + + json_input, data_root, pool_num = args.input, args.output, args.pools + temp_dir = os.path.join(data_root, "temp_dir") + exp_name = os.path.basename(json_input).split('.')[0] + with open(json_input, "r") as f: + input_data = json.load(f) + img_ids = [] + groundtruths = [] + predictions = [] + for idx, item in enumerate(input_data): + if "img_id" in item: + img_ids.append(item["img_id"]) + else: + img_ids.append(f"sample_{idx}") + groundtruths.append(item['gt']) + predictions.append(item['pred']) + + a = time.time() + user_id = exp_name + + total_color_list = gen_color_list(num=5800) + + data_root = os.path.join(data_root, user_id) + output_dir_info = {} + input_args = [] + for subset, latex_list in zip(['gt', 'pred'], [groundtruths, predictions]): + sub_temp_dir = os.path.join(temp_dir, f"{exp_name}_{subset}") + os.makedirs(sub_temp_dir, exist_ok=True) + output_path = os.path.join(data_root, subset) + output_dir_info[output_path] = [] + + os.makedirs(os.path.join(output_path, 'bbox'), exist_ok=True) + os.makedirs(os.path.join(output_path, 'vis'), exist_ok=True) + + for idx, latex in tqdm(enumerate(latex_list), desc=f"collect {subset} latex ..."): + basename = img_ids[idx] + input_arg = latex, basename, output_path, sub_temp_dir, total_color_list + input_args.append(input_arg) + + if pool_num > 1: + print( + datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + "using processpool, pool num:", + pool_num, + ", job num:", + len(input_args)) + myP = Pool(args.pools) + for input_arg in input_args: + myP.apply_async(latex2bbox_color, args=(input_arg,)) + myP.close() + myP.join() + else: + for input_arg in input_args: + latex2bbox_color(input_arg) + b = time.time() + print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + "extract bbox done, time cost:", round(b - a, 3), "s") + + for subset in ['gt', 'pred']: + shutil.rmtree(os.path.join(temp_dir, f"{exp_name}_{subset}")) + + c = time.time() + metrics_res, metric_res_path, match_vis_dir = evaluation(args.output, exp_name) + d = time.time() + print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + "calculate metrics done, time cost:", round(d - c, 3), "s") + + print(f"=> process done, mean f1 score: {metrics_res['mean_score']}.") + print(f"=> more details of metrics are saved in `{metric_res_path}`") + print(f"=> visulization images are saved under `{match_vis_dir}`") diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex2bbox_color.py b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex2bbox_color.py new file mode 100644 index 000000000..0e2863f16 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex2bbox_color.py @@ -0,0 +1,245 @@ +import json +import logging +import os +import subprocess +from threading import Timer + +import numpy as np +from PIL import Image, ImageDraw + +from .latex_processor import normalize_latex, token_add_color_RGB +from .tokenize_latex.tokenize_latex import tokenize_latex + +tabular_template = r""" +\documentclass[12pt]{article} +\usepackage[landscape]{geometry} +\usepackage{geometry} +\geometry{apaper,scale=0.98} +\pagestyle{empty} +\usepackage{booktabs} +\usepackage{multirow} +\usepackage{amssymb} +\usepackage{upgreek} +\usepackage{amsmath} +\usepackage{xcolor} +\begin{document} +\makeatletter +\renewcommand*{\@textcolor}[3]{%% + \protect\leavevmode + \begingroup + \color#1{#2}#3%% + \endgroup +} +\makeatother +\begin{displaymath} +%s +\end{displaymath} +\end{document} +""" + +# 需要配置Source Han Sans SC或其他中文字体 +formular_template = r""" +\documentclass[12pt]{article} +\usepackage[landscape]{geometry} +\usepackage{geometry} +\geometry{apaper,scale=0.98} +\pagestyle{empty} +\usepackage{amsmath} +\usepackage{upgreek} +\usepackage{amssymb} +\usepackage{xcolor} +\usepackage{xeCJK} +\setCJKmainfont{Source Han Sans SC} +\setCJKsansfont{Source Han Sans SC} +\setCJKmonofont{Source Han Sans SC} +\xeCJKsetup{CJKmath=true} +\begin{document} +\makeatletter +\renewcommand*{\@textcolor}[3]{%% + \protect\leavevmode + \begingroup + \color#1{#2}#3%% + \endgroup +} +\makeatother +\begin{displaymath} +%s +\end{displaymath} +\end{document} +""" + + +def run_cmd(cmd, timeout_sec=30, temp_dir=None): + # 设置进程独立的环境变量 + env = os.environ.copy() + if temp_dir: + env['TMPDIR'] = temp_dir + env['TMP'] = temp_dir + env['TEMP'] = temp_dir + env['MAGICK_TMPDIR'] = temp_dir + env['TEXMFCACHE'] = temp_dir + env['TEXMFVAR'] = temp_dir + + proc = subprocess.Popen(cmd, shell=True, env=env) + def kill_proc(p): return p.kill() + timer = Timer(timeout_sec, kill_proc, [proc]) + try: + timer.start() + stdout, stderr = proc.communicate() + finally: + timer.cancel() + + +def convert_pdf2img(pdf_filename, png_filename, temp_dir=None): + cmd = "magick -density 200 -quality 100 \"%s\" \"%s\"" % (pdf_filename, png_filename) + run_cmd(cmd, temp_dir=temp_dir) + + +def crop_image(image_path, pad=8): + img = Image.open(image_path).convert("L") + img_data = np.asarray(img, dtype=np.uint8) + nnz_inds = np.where(img_data != 255) + if len(nnz_inds[0]) == 0: + y_min = 0 + y_max = 10 + x_min = 0 + x_max = 10 + else: + y_min = np.min(nnz_inds[0]) + y_max = np.max(nnz_inds[0]) + x_min = np.min(nnz_inds[1]) + x_max = np.max(nnz_inds[1]) + + img = Image.open(image_path).convert("RGB").crop( + (x_min - pad, y_min - pad, x_max + pad, y_max + pad)) + img.save(image_path) + + +def extrac_bbox_from_color_image(image_path, color_list): + img = Image.open(image_path).convert("RGB") + W, H = img.size + pixels = list(img.getdata()) + + bbox_list = [] + for target_color in color_list: + target_pixels = [i for i, pixel in enumerate(pixels)if pixel == target_color] + x_list = [] + y_list = [] + for idx in target_pixels: + x_list.append(idx % W) + y_list.append(idx // W) + try: + y_min, y_max, x_min, x_max = min(y_list), max(y_list), min(x_list), max(x_list) + bbox_list.append([x_min - 1, y_min - 1, x_max + 1, y_max + 1]) + + except BaseException: + bbox_list.append([]) + continue + + img = img.convert("L") + img_bw = img.point(lambda x: 255 if x == 255 else 0, '1') + img_bw.convert("RGB").save(image_path) + return bbox_list + + +def latex2bbox_color(input_arg): + latex, basename, output_path, temp_dir, total_color_list = input_arg + tabular_template if "tabular" in latex else formular_template + basename = basename.replace('.jpg', '') # ***** + output_bbox_path = os.path.join(output_path, 'bbox', basename + '.jsonl') + output_vis_path = os.path.join(output_path, 'vis', basename + '.png') + output_base_path = os.path.join(output_path, 'vis', basename + '_base.png') + + if os.path.exists(output_bbox_path) and os.path.exists( + output_vis_path) and os.path.exists(output_base_path): + return + + try: + latex = latex.replace("\n", " ") + latex = latex.replace("\\%", "") + ret, new_latex = tokenize_latex( + latex, middle_file=os.path.join( + temp_dir, basename + '.txt')) + if not (ret and new_latex): + log = f"ERROR, Tokenize latex failed: {basename}." + logging.info(log) + new_latex = latex + new_latex = new_latex.replace("< P E R C E N T A G E T O K E N >", "\\%") + latex = normalize_latex(new_latex) + token_list = [] + l_split = latex.strip().split(' ') + color_list = total_color_list[0:len(l_split)] + idx = 0 + while idx < len(l_split): + l_split, idx, token_list = token_add_color_RGB(l_split, idx, token_list) + + rgb_latex = " ".join(l_split) + for idx, color in enumerate(color_list): + R, G, B = color + rgb_latex = rgb_latex.replace(f"", f"{R},{G},{B}") + + if len(token_list) > 1300: + paper_size = 3 + elif len(token_list) > 600: + paper_size = 4 + else: + paper_size = 5 + final_latex = formular_template.replace("", str(paper_size)) % rgb_latex + + except Exception as e: + log = f"ERROR, Preprocess latex failed: {basename}; {e}." + logging.info(log) + return + + pre_name = output_path.replace('/', '_').replace('.', '_') + '_' + basename + tex_filename = os.path.join(temp_dir, pre_name + '.tex') + log_filename = os.path.join(temp_dir, pre_name + '.log') + aux_filename = os.path.join(temp_dir, pre_name + '.aux') + + # print(os.path.exists(tex_filename), tex_filename) + with open(tex_filename, "w") as w: + # print(final_latex, file=w) + w.write(final_latex) + # print(os.path.exists(tex_filename), tex_filename) + # run_cmd(f"pdflatex -interaction=nonstopmode -output-directory={temp_dir} {tex_filename} >/dev/null") + run_cmd( + f"xelatex -interaction=nonstopmode -output-directory={temp_dir} \"{tex_filename}\" >/dev/null", + temp_dir=temp_dir) + try: + os.remove(tex_filename) + os.remove(log_filename) + os.remove(aux_filename) + except BaseException: + pass + pdf_filename = tex_filename[:-4] + '.pdf' + if not os.path.exists(pdf_filename): + log = f"ERROR, Compile pdf failed: {pdf_filename}" + logging.info(log) + else: + convert_pdf2img(pdf_filename, output_base_path) + os.remove(pdf_filename) + + crop_image(output_base_path) + bbox_list = extrac_bbox_from_color_image(output_base_path, color_list) + vis = Image.open(output_base_path) + draw = ImageDraw.Draw(vis) + + with open(output_bbox_path, 'w', encoding='utf-8') as f: + for token, box in zip(token_list, bbox_list): + item = { + "bbox": box, + "token": token + } + f.write(json.dumps(item, ensure_ascii=False) + '\n') + + if not box: + continue + x_min, y_min, x_max, y_max = box + draw.rectangle([x_min, y_min, x_max, y_max], + fill=None, outline=(0, 250, 0), width=1) + try: + draw.text((x_min, y_min), token, (250, 0, 0)) + except BaseException: + pass + + vis.save(output_vis_path) diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_processor.py b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_processor.py new file mode 100644 index 000000000..d6fe47446 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_processor.py @@ -0,0 +1,565 @@ +import re + +SKIP_PATTERNS = [r'\{', r'\}', r'[\[\]]', r'\\begin\{.*?\}', r'\\end\{.*?\}', + r'\^', r'\_', r'\\.*rule.*', r'\\.*line.*', r'\[[\-.0-9]+[epm][xtm]\]'] +SKIP_Tokens = ['\\', '\\\\', '\\index', '\\a', '&', '$', '\\multirow', '\\def', '\\raggedright', '\\url', '\\cr', '\\ensuremath', '\\left', '\\right', + '\\mathchoice', '\\scriptstyle', '\\displaystyle', '\\qquad', '\\quad', '\\,', '\\!', '~', '\\boldmath'] +PHANTOM_Tokens = ['\\fontfamily', '\\vphantom', '\\phantom', '\\rowcolor', '\\ref'] +TWO_Tail_Tokens = ['\\frac', '\\binom'] +AB_Tail_Tokens = ['\\xrightarrow', '\\xleftarrow', '\\sqrt'] # special token \xxx [] {} +TWO_Tail_Invisb_Tokens = ['\\overset', '\\underset', '\\stackrel'] +ONE_Tail_Tokens = ['\\widetilde', '\\overline', '\\hat', '\\widehat', '\\tilde', '\\Tilde', '\\dot', '\\bar', '\\vec', '\\underline', '\\underbrace', '\\check', + '\\breve', '\\Bar', '\\Vec', '\\mathring', '\\ddot'] +ONE_Tail_Invisb_Tokens = ['\\boldsymbol', '\\pmb', '\\textbf', '\\mathrm', '\\mathbf', '\\mathbb', '\\mathcal', '\\textmd', '\\texttt', '\\textnormal', + '\\text', '\\textit', '\\textup', '\\mathop', '\\mathbin', '\\smash', '\\operatorname', '\\textrm', '\\mathfrak', '\\emph', + '\\textsf', '\\textsc'] + + +def flatten_multiline(latex): + brace_map = { + "\\left(": "\\right)", + "\\left[": "\\right]", + "\\left{": "\\right}", + } + l_split = latex.split(' ') + if l_split[0] == "\\begin{array}": + if l_split[-1] == "\\end{array}": + l_split = l_split[2:-1] + else: + l_split = l_split[2:] + + idx = 0 + while idx < len(l_split): + token = l_split[idx] + if token.startswith("\\left") and token in brace_map.keys(): + end_idx = find_matching_brace(l_split, idx, brace=[token, brace_map[token]]) + if end_idx != -1: + idx = end_idx + elif token in ["\\\\", "~", "\\qquad"]: + l_split = l_split[0:idx] + l_split[idx + 1:] + idx -= 1 + idx += 1 + latex = ' '.join(l_split) + return "$ " + latex + " $" + + +def clean_latex(text): + # TODO 让GPT写的去空格函数, 初步测了是没问题的, 不确定是否完全没有bug + cleaned_text = re.sub(r'(?<=[^\\])\s+(?=[^\\])', '', text) + # TODO 有一些不能去掉的空格给补充回来 + for item in ["\\hline", "\\midrule", "\\times", "\\bf", "\\footnotesize", "\\cr", '\\log']: + cleaned_text = cleaned_text.replace(item, item + " ") + cleaned_text = cleaned_text.replace(" \\mathcolor{black}", "\\mathcolor{black}") + return cleaned_text + + +def remove_trailing_latex(formula): + pattern = r'(\\(hspace\*?\{[^{}]*?\}|vspace\*?\{[^{}]*?\}|smallskip|medskip|quad|qquad|bigskip|[;,])|\~|\.)*$' + # Replace the matched pattern with an empty string + cleaned_formula = re.sub(pattern, '', formula, count=1) + return cleaned_formula + + +def find_matching_brace(sequence, start_index, brace=['{', '}']): + # Finds the index of the matching brace for the one at start_index + left_brace, right_brace = brace + depth = 0 + for i, char in enumerate(sequence[start_index:], start=start_index): + if char == left_brace: + depth += 1 + elif char == right_brace: + depth -= 1 + if depth == 0: + return i + if depth > 0: + error_info = "Warning! found no matching brace in sequence !" + raise ValueError(error_info) + return -1 + + +def normalize_latex(l, rm_trail=False): + if "tabular" in l: + latex_type = "tabular" + else: + latex_type = "formula" + + if rm_trail: + l = remove_trailing_latex(l) + l = l.strip().replace(r'\pmatrix', r'\mypmatrix').replace(r'\matrix', r'\mymatrix') + + # TODO \raggedright \arraybackslash, these align method, difficult to handle, remove it. + for item in ['\\raggedright', '\\arraybackslash']: + l = l.replace(item, "") + + for item in ['\\lowercase', '\\uppercase']: + l = l.replace(item, "") + + # TODO \hspace {1 . 5 cm}, for formula, change to \hspace{1.5cm}, for table, remove it. + pattern = r'\\[hv]space { [.0-9a-z ]+ }' + old_token = re.findall(pattern, l, re.DOTALL) + if latex_type == "tabular": + new_token = ["" for item in old_token] + else: + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # TODO take \begin {tabular} {} as one token + # TODO there are \begin{array} in table too,so the process should run in + # both formula and table. + if latex_type == "tabular": + l = l.replace("\\begin {tabular}", "\\begin{tabular}") + l = l.replace("\\end {tabular}", "\\end{tabular}") + l = l.replace("\\begin {array}", "\\begin{array}") + l = l.replace("\\end {array}", "\\end{array}") + l_split = l.split(' ') + idx = 0 + while idx < len(l_split): + token = l_split[idx] + if token == "\\begin{tabular}": + sub_idx = idx + 1 + end_idx = find_matching_brace(l_split, sub_idx) + new_token = "".join(l_split[idx: end_idx + 1]) + l_split = l_split[0:idx] + [new_token] + l_split[end_idx + 1:] + break + idx += 1 + l = ' '.join(l_split) + + # TODO some complex format, hart to deal with re.match, so using brace + # match, such as:\cmidrule ( l { 3 p t } r { 3 p t } ) { 1 - 1 } + l_split = l.split(' ') + idx = 0 + while idx < len(l_split): + token = l_split[idx] + if token in ["\\cmidrule", "\\cline"]: + sub_idx = idx + 1 + if l_split[sub_idx] == "(": + mid_end = find_matching_brace(l_split, sub_idx, brace=['(', ')']) + end_idx = find_matching_brace(l_split, mid_end + 1) + else: + end_idx = find_matching_brace(l_split, sub_idx) + new_token = "".join(l_split[idx: end_idx + 1]) + l_split = l_split[0:idx] + [new_token] + l_split[end_idx + 1:] + idx += 1 + l = ' '.join(l_split) + + pattern = r'\\begin{array} { [lrc ]+ }' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [ + item.replace( + "\\begin{array} ", + "").replace( + " ", + "").replace( + "", + "\\begin{array} ") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # TODO token such \not= should be one token + pattern = r'\\not [<>+=\-]' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # TODO tokens such as \dots \exp \sinh, split them to parts, so the bbox match will be easier. + + l = " " + l + " " + l = l.replace(" \\ldots ", " . . . ") + l = l.replace(" \\cdots ", " . . . ") + l = l.replace(" \\dots ", " . . . ") + l = l.replace(" \\dotsb ", " . . . ") + l = l.replace(" \\log ", " \\mathrm { l o g } ") + l = l.replace(" \\exp ", " \\mathrm { e x p } ") + l = l.replace(" \\sin ", " \\mathrm { s i n } ") + l = l.replace(" \\cos ", " \\mathrm { c o s } ") + l = l.replace(" \\tan ", " \\mathrm { t a n } ") + l = l.replace(" \\tanh ", " \\mathrm { t a n h } ") + l = l.replace(" \\cosh ", " \\mathrm { c o s h } ") + l = l.replace(" \\sinh ", " \\mathrm { s i n h } ") + + # ** token such as \big( should be one token + pattern = r'\\[Bb]ig[g]?[glrm]? [(){}|\[\]] ' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft + " ") + + pattern = r'\\[Bb]ig[g]?[glrm]? \\.*? ' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft + " ") + + # TODO when \operatorname * meets mathcolor it comes error, yet the * is + # useless, so we simply remove it bynow. + pattern = r'\\operatorname \*' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = ["\\operatorname" for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # TODO \lefteqn will lead to letter overlap, it's harmfull for render, so simply remove it. + l = l.replace("\\lefteqn", "") + + # TODO \footnote can not seem as ONE_Tail_Invisb_Tokens(usually this type + # token add color by \mathrm {\color(x)}, yet \footnode should be + # \color{\footnote{x}}), so we simple change it to "^". + l = l.replace("\\footnote ", "^ ") + + # TODO \' can not be rendered separately(cause to different visulize + # performence), so we take these tokens as one token such as \' e -> \'e, + # on the other hand, if { after \' then render them separately. + pattern = r'\\\' [^{] ' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft + " ") + + # TODO [ -1.5ex ] [ 1.5pt ] [ 3 mm ] some layout adjustment, no need to + # render. combine them as one token. + if latex_type == "tabular": + pattern = r'\[ [\-.0-9 ]+[exptcm ]+ \]' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # ** \parbox { 3cm } {} shoudle be combined as one token + pattern = r'\\parbox {[^{]+}' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # ** \raisebox{}[][] {} shoudle be combined as one token, \raisebox{-1.5ex}[0pt] + pattern = r'\\raisebox {[^{]+} [\[\]0-9 exptcm]+{' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft[0:-1] + " {") + + # ** \char shoudle be combined as one token + pattern = r'{ \\char[0-9\' ]+}' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, "{ " + aft[1:-1] + " }") + + # ** \not xx shoudle be combined as one token + pattern = r'\\not [\\=\<\>][^ ]+ ' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft + " ") + + # ** \specialrule{1pt}{2pt}{2pt}, special lines, shoudle be combined as one token + pattern = r'\\specialrule {[ .0-9a-z]+} {[ .0-9a-z]+} {[ .0-9a-z]+}' + old_token = re.findall(pattern, l, re.DOTALL) + new_token = [item.replace(" ", "") for item in old_token] + for bef, aft in zip(old_token, new_token): + l = l.replace(bef, aft) + + # ** for easier add color, the original color should be removed, there are two type of color for now: \color[rgb]{0, 1, 0} and \color{red} + pattern = r'\\colorbox[ \[\]RGBrgb]+{ [A-Za-z 0-9,!]+ } |\\color[ \[\]RGBrgb]+{ [A-Za-z 0-9,!]+ } |\\textcolor[ \[\]RGBrgb]+{ [A-Za-z 0-9,!]+ } |\\cellcolor[ \[\]RGBrgb]+{ [A-Za-z 0-9,!]+ } ' + old_token = re.findall(pattern, l, re.DOTALL) + for bef in old_token: + l = l.replace(bef, "") + + # ** filling the missing brace [] and {} according to token. + l_split = l.split(' ') + idx = 0 + while idx < len(l_split): + token = l_split[idx] + if token in ONE_Tail_Tokens + ONE_Tail_Invisb_Tokens: + # ** normalize tokens such as \hat, fill missing the {}, such as \hat \lambda -> \hat {\lambda} + sub_idx = idx + 1 + while sub_idx < len( + l_split) and l_split[sub_idx] in ONE_Tail_Tokens + ONE_Tail_Invisb_Tokens: + sub_idx += 1 + new_split = l_split[0:idx] + for ii in range(idx, sub_idx): + new_split = new_split + [l_split[ii], "{"] + if l_split[sub_idx] != "{": + new_split = new_split + [l_split[sub_idx]] + ["}"] * (sub_idx - idx) + l_split = new_split + l_split[sub_idx + 1:] + else: + end_idx = find_matching_brace(l_split, sub_idx) + new_split = new_split + l_split[sub_idx + 1:end_idx] + ["}"] * (sub_idx - idx) + l_split = new_split + l_split[end_idx + 1:] + elif token in AB_Tail_Tokens: + # ** normalize special tokens such as \sqrt, fill the missing [] {} in \sqrt [] {}, yet the [] is optional, for example: \sqrt A B -> \sqrt {A} B and \sqrt [A] B -> \sqrt [A] {B} + if l_split[idx + 1] != "[" and l_split[idx + 1] != "{": + l_split = l_split[0:idx + 1] + \ + ["{"] + [l_split[idx + 1]] + ["}"] + l_split[idx + 2:] + else: + if l_split[idx + 1] == "[": + end1 = find_matching_brace(l_split, idx + 1, brace=['[', ']']) + else: + end1 = idx + if l_split[end1 + 1] != "{": + l_split = l_split[0:end1 + 1] + \ + ["{"] + [l_split[end1 + 1]] + ["}"] + l_split[end1 + 2:] + elif token in TWO_Tail_Tokens + TWO_Tail_Invisb_Tokens: + # ** normalize special tokens such as \frac, add missing brace in \frac {A} {B} for example: \frac {\lambda} 2 -> \frac {\lambda} {2} + if l_split[idx + 1] != "{": + l_split = l_split[0:idx + 1] + \ + ["{"] + [l_split[idx + 1]] + ["}"] + l_split[idx + 2:] + end1 = find_matching_brace(l_split, idx + 1) + if l_split[end1 + 1] != "{": + l_split = l_split[0:end1 + 1] + \ + ["{"] + [l_split[end1 + 1]] + ["}"] + l_split[end1 + 2:] + + idx += 1 + l = ' '.join(l_split) + + return l + + +def token_add_color(l_split, idx, render_dict): + token = l_split[idx] + if token in PHANTOM_Tokens: + # ** special tokens that do not need render, skip it + if l_split[idx + 1] == '{': + brace_end = find_matching_brace(l_split, idx + 1) + else: + brace_end = idx + 1 + next_idx = brace_end + 1 + elif token in TWO_Tail_Tokens: + # ** tokens such as \frac A B, and the token needs render too. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + den_start = num_end + 1 + den_end = find_matching_brace(l_split, den_start) + l_split_copy = l_split[:idx] + [r'\mathcolor{black}{' + token + '{'] + \ + [r'\mathcolor{gray}{'] + l_split[num_start + 1:num_end] + \ + ['}'] + [r'}{'] + [r'\mathcolor{gray}{'] + l_split[den_start + 1:den_end] + \ + ['}'] + ['}'] + ['}'] + l_split[den_end + 1:] + + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + next_idx = idx + 1 + elif token in ONE_Tail_Tokens: + # ** tokens such as \hat A, and the token needs render too. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + l_split_copy = l_split[:idx] + [r'\mathcolor{black}{'] + l_split[idx: num_start + 1] + \ + [r'\mathcolor{gray}{'] + l_split[num_start + 1: num_end] + \ + ['}'] + l_split[num_end: num_end + 1] + ['}'] + l_split[num_end + 1:] + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + next_idx = idx + 1 + elif token in ONE_Tail_Invisb_Tokens: + # ** tokens such as \text A B, and the token does not need render. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + sub_idx = num_start + 1 + if num_end - num_start == 2: + l_split_copy = l_split.copy() + l_split_copy[sub_idx] = r'{\mathcolor{black}{' + l_split_copy[sub_idx] + '}}' + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, l_split[sub_idx] + next_idx = num_end + else: + while sub_idx < num_end: + l_split, sub_idx, render_dict = token_add_color(l_split, sub_idx, render_dict) + next_idx = num_end + 1 + elif token in AB_Tail_Tokens: + # ** special token \xrightarrow, could be \xrightarrow [] {} or \xrightarrow {}, process method are different. + if l_split[idx + 1] == '{': + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + l_split_copy = l_split[:idx] + [r'\mathcolor{black}{'] + l_split[idx: idx + 2] \ + + [r'\mathcolor{gray}{'] + l_split[num_start + + 1: num_end] + ['}}'] + l_split[num_end:] + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + sub_idx = num_start + 1 + while sub_idx < num_end: + l_split, sub_idx, render_dict = token_add_color(l_split, sub_idx, render_dict) + next_idx = num_end + 1 + elif l_split[idx + 1] == '[': + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start, brace=['[', ']']) + den_start = num_end + 1 + den_end = find_matching_brace(l_split, den_start) + l_split_copy = l_split[:idx] + [r'{\mathcolor{black}{'] + l_split[idx: idx + 2] \ + + [r'\mathcolor{gray}{'] + l_split[idx + 2: num_end] + ['}'] + l_split[num_end:den_start + 1] \ + + [r'\mathcolor{gray}{'] + l_split[den_start + 1: den_end] + ['}'] + l_split[den_end: den_end + 1] \ + + ['}}'] + l_split[den_end + 1:] + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + sub_idx = num_start + 1 + while sub_idx < num_end: + l_split, sub_idx, render_dict = token_add_color(l_split, sub_idx, render_dict) + sub_idx = den_start + 1 + while sub_idx < den_end: + l_split, sub_idx, render_dict = token_add_color(l_split, sub_idx, render_dict) + next_idx = den_end + 1 + elif token in ["\\multicolumn", "\\multirow"]: + # ** tokens with three {}, such as \multicolumn {} {} {}, the text in third {} need be rendered. + first_start = idx + 1 + first_end = find_matching_brace(l_split, first_start) + second_start = first_end + 1 + second_end = find_matching_brace(l_split, second_start) + third_start = second_end + 1 + third_end = find_matching_brace(l_split, third_start) + + sub_idx = third_start + 1 + while sub_idx < third_end: + l_split, sub_idx, render_dict = token_add_color(l_split, sub_idx, render_dict) + next_idx = third_end + 1 + elif token in SKIP_Tokens + TWO_Tail_Invisb_Tokens or any(re.match(pattern, token) for pattern in SKIP_PATTERNS): + # ** tokens no need render, just skip + # print('skip', idx, token) + # TODO special case :[], could be single, or in \sqrt[]{}. + if (token == "[" and l_split[idx - 1] != "\\sqrt") or (token + == "]" and idx >= 3 and l_split[idx - 3] != "\\sqrt"): + l_split_copy = l_split.copy() + l_split_copy[idx] = r'\mathcolor{black}{ ' + l_split_copy[idx] + ' }' + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + next_idx = idx + 1 + else: + next_idx = idx + 1 + else: + # ** nomal token + l_split_copy = l_split.copy() + # TODO sometimes there is translation after add color, the exp prove that + # \mathcolor{black}{ A } is better than \mathcolor{black}{A} + l_split_copy[idx] = r'\mathcolor{black}{ ' + l_split_copy[idx] + ' }' + + l_new = ' '.join(l_split_copy) + l_new = r'\mathcolor{gray}{ ' + l_new + ' }' + render_dict[str(idx)] = l_new, token + next_idx = idx + 1 + + return l_split, next_idx, render_dict + + +def token_add_color_RGB(l_split, idx, token_list, brace_color=False): + """using \\mathcolor[RGB]{r,g,b} to render latex. + """ + token = l_split[idx] + if not token: + next_idx = idx + 1 + elif token in PHANTOM_Tokens: + # ** special tokens that do not need render, skip it + if l_split[idx + 1] == '{': + brace_end = find_matching_brace(l_split, idx + 1) + else: + brace_end = idx + 1 + next_idx = brace_end + 1 + elif token in TWO_Tail_Tokens: + # ** tokens such as \frac A B, and the token needs render too. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + den_start = num_end + 1 + den_end = find_matching_brace(l_split, den_start) + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + [color_token + token] + \ + l_split[idx + 1: den_end + 1] + ["}"] + l_split[den_end + 1:] + token_list.append(token) + next_idx = idx + 1 + elif token in ONE_Tail_Tokens: + # ** tokens such as \hat A, and the token needs render too. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + if token != "\\underbrace" and num_end + 1 < len(l_split) and l_split[num_end + 1] == "_": + l_split = l_split[:idx] + ["{" + color_token + token] + \ + l_split[idx + 1: num_end + 1] + ["}}"] + l_split[num_end + 1:] + else: + l_split = l_split[:idx] + [color_token + token] + \ + l_split[idx + 1: num_end + 1] + ["}"] + l_split[num_end + 1:] + token_list.append(token) + next_idx = idx + 1 + elif token in ONE_Tail_Invisb_Tokens: + # ** tokens such as \text A B, and the token does not need render. + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + sub_idx = num_start + 1 + if num_end - num_start == 2: + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + token_list.append(l_split[num_start + 1]) + l_split = l_split[:num_start + 1] + [color_token + + l_split[num_start + 1] + "}"] + l_split[num_end:] + else: + while sub_idx < num_end: + l_split, sub_idx, token_list = token_add_color_RGB(l_split, sub_idx, token_list) + next_idx = num_end + 1 + elif token in AB_Tail_Tokens: + # ** special token \xrightarrow, could be \xrightarrow [] {} or \xrightarrow {}, process method are different. + if l_split[idx + 1] == '{': + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start) + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + [color_token + token] + \ + l_split[idx + 1: num_end + 1] + ["}"] + l_split[num_end + 1:] + token_list.append(token) + sub_idx = num_start + 1 + while sub_idx < num_end: + l_split, sub_idx, token_list = token_add_color_RGB(l_split, sub_idx, token_list) + next_idx = num_end + 1 + elif l_split[idx + 1] == '[': + num_start = idx + 1 + num_end = find_matching_brace(l_split, num_start, brace=['[', ']']) + den_start = num_end + 1 + den_end = find_matching_brace(l_split, den_start) + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + [color_token + token] + \ + l_split[idx + 1: den_end + 1] + ["}"] + l_split[den_end + 1:] + token_list.append(token) + sub_idx = num_start + 1 + while sub_idx < num_end: + l_split, sub_idx, token_list = token_add_color_RGB( + l_split, sub_idx, token_list, brace_color=True) + sub_idx = den_start + 1 + while sub_idx < den_end: + l_split, sub_idx, token_list = token_add_color_RGB(l_split, sub_idx, token_list) + next_idx = den_end + 1 + elif token in ["\\multicolumn", "\\multirow"]: + # ** tokens with three {}, such as \multicolumn {} {} {}, the text in third {} need be rendered. + first_start = idx + 1 + first_end = find_matching_brace(l_split, first_start) + second_start = first_end + 1 + second_end = find_matching_brace(l_split, second_start) + third_start = second_end + 1 + third_end = find_matching_brace(l_split, third_start) + + sub_idx = third_start + 1 + while sub_idx < third_end: + l_split, sub_idx, token_list = token_add_color_RGB(l_split, sub_idx, token_list) + next_idx = third_end + 1 + elif token in SKIP_Tokens + TWO_Tail_Invisb_Tokens or any(re.match(pattern, token) for pattern in SKIP_PATTERNS): + # ** tokens no need render, just skip + # print('skip', idx, token) + # TODO special case :[], could be single, or in \sqrt[]{}. + if (token == "[" and l_split[idx - 1] != "\\sqrt") or (token + == "]" and idx >= 3 and l_split[idx - 3] != "\\sqrt"): + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + [color_token + l_split[idx] + "}"] + l_split[idx + 1:] + token_list.append(token) + next_idx = idx + 1 + else: + next_idx = idx + 1 + else: + # ** nomal token + if brace_color or (idx > 1 and l_split[idx - 1] == "_"): + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + ["{" + color_token + l_split[idx] + "}}"] + l_split[idx + 1:] + token_list.append(token) + next_idx = idx + 1 + else: + color_token = "\\color[RGB]{>}{".replace("", str(len(token_list))) + l_split = l_split[:idx] + [color_token + l_split[idx] + "}"] + l_split[idx + 1:] + token_list.append(token) + next_idx = idx + 1 + return l_split, next_idx, token_list diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_render_percentage.py b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_render_percentage.py new file mode 100644 index 000000000..dfc4f2b88 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/latex_render_percentage.py @@ -0,0 +1,114 @@ +import argparse +import json +import os +import shutil +import subprocess +import time +from multiprocessing import Pool + +formular_template = r""" +\documentclass[12pt]{article} +\usepackage[landscape]{geometry} +\usepackage{geometry} +\geometry{a5paper,scale=0.98} +\pagestyle{empty} +# \usepackage{booktabs} +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{xcolor} +\begin{document} +\makeatletter +\renewcommand*{\@textcolor}[3]{%% + \protect\leavevmode + \begingroup + \color#1{#2}#3%% + \endgroup +} +\makeatother +\begin{displaymath} +%s +\end{displaymath} +\end{document} +""" + + +def run_shell_cmd(cmd, max_time=15): + child = subprocess.Popen(cmd, shell=True) + for i in range(max_time): + if child.poll(): + return True + if i == max_time - 1: + child.kill() + return False + time.sleep(1) + return False + + +def render_latex(latex_code, basename, latex_dir, pdf_dir): + latex_path = os.path.join(latex_dir, basename + ".tex") + pdf_path = os.path.join(pdf_dir, basename + ".pdf") + with open(latex_path, "w") as f: + f.write(formular_template % latex_code) +# # cmd = f"pdflatex -interaction=nonstopmode -output-directory={pdf_dir} -output-format=pdf {latex_path} >/dev/null" +# run_cmd( +# f"/mnt/hwfile/opendatalab/guzhuangcheng/programme/texlive/bin/x86_64-linux/xelatex -interaction=nonstopmode -output-directory={temp_dir} {tex_filename} >/dev/null") +# run_shell_cmd(cmd) +# return pdf_path +# + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--input', '-i', type=str, default='data/pred_results/test.json') + parser.add_argument('--clean', action='store_true', default=False) + parser.add_argument('--gt', action='store_true', default=False) + args = parser.parse_args() + + if args.gt: + output_path = os.path.join("output", 'gt.json') + load_key = 'gt' + else: + load_key = 'pred' + output_path = os.path.join("output", os.path.basename(args.input)) + + temp_dir = f"render_temp_dir" + try: + shutil.rmtree(temp_dir) + except BaseException: + pass + latex_dir = os.path.join(temp_dir, "texes") + pdf_dir = os.path.join(temp_dir, "pdfs") + os.makedirs(latex_dir, exist_ok=True) + os.makedirs(pdf_dir, exist_ok=True) + + with open(args.input, "r") as f: + input_data = json.load(f) + + myP = Pool(200) + for idx, item in enumerate(input_data): + basename = f"sample_{idx}" + myP.apply_async(render_latex, args=(item[load_key], basename, latex_dir, pdf_dir)) + myP.close() + print("processing, may take some times.") + myP.join() + + success_num = 0 + total_num = 0 + for idx, item in enumerate(input_data): + basename = f"sample_{idx}" + total_num += 1 + pdf_path = os.path.join(pdf_dir, basename + ".pdf") + if os.path.exists(pdf_path): + success_num += 1 + item['renderable'] = 1 + else: + item['renderable'] = 0 + + print("total num:", total_num, "render success num:", success_num) + with open(output_path, "w") as f: + f.write(json.dumps(input_data, indent=2)) + if args.clean: + try: + shutil.rmtree(temp_dir) + except BaseException: + pass diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_formula.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_formula.js new file mode 100644 index 000000000..883e661a0 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_formula.js @@ -0,0 +1,385 @@ +const path = require('path'); +var katex = require(path.join(__dirname,"third_party/katex/katex.js")) +options = require(path.join(__dirname,"third_party/katex/src/Options.js")) +var readline = require('readline'); +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false +}); + + +rl.on('line', function(line){ + a = line + if (line[0] == "%") { + line = line.substr(1, line.length - 1); + } + line = line.split('%')[0]; + + line = line.split('\\~').join(' '); + + for (var i = 0; i < 300; i++) { + line = line.replace(/\\>/, " "); + line = line.replace('$', ' '); + line = line.replace(/\\label{.*?}/, ""); + } + + if (line.indexOf("matrix") == -1 && line.indexOf("cases")==-1 && + line.indexOf("array")==-1 && line.indexOf("begin")==-1) { + for (var i = 0; i < 300; i++) { + line = line.replace(/\\\\/, "\\,"); + } + } + + + line = line + " " + // global_str is tokenized version (build in parser.js) + // norm_str is normalized version build by renderer below. + try { + + + if (process.argv[2] == "tokenize") { + var tree = katex.__parse(line, {}); + console.log(global_str.replace(/\\label { .*? }/, "")); + } else { + for (var i = 0; i < 300; ++i) { + line = line.replace(/{\\rm/, "\\mathrm{"); + line = line.replace(/{ \\rm/, "\\mathrm{"); + line = line.replace(/\\rm{/, "\\mathrm{"); + } + + var tree = katex.__parse(line, {}); + buildExpression(tree, new options({})); + for (var i = 0; i < 300; ++i) { + norm_str = norm_str.replace('SSSSSS', '$'); + norm_str = norm_str.replace(' S S S S S S', '$'); + } + console.log(norm_str.replace(/\\label { .*? }/, "")); + } + } catch (e) { + console.error(line); + console.error(norm_str); + console.error(e); + console.log(); + } + global_str = "" + norm_str = "" +}) + + + +// This is a LaTeX AST to LaTeX Renderer (modified version of KaTeX AST-> MathML). +norm_str = "" + +var groupTypes = {}; + +groupTypes.mathord = function(group, options) { + if (options.font == "mathrm"){ + for (i = 0; i < group.value.length; ++i ) { + if (group.value[i] == " ") { + norm_str = norm_str + group.value[i] + "\; "; + } else { + norm_str = norm_str + group.value[i] + " "; + } + } + } else { + norm_str = norm_str + group.value + " "; + } +}; + +groupTypes.textord = function(group, options) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.bin = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.rel = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.open = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.close = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.inner = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.punct = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.ordgroup = function(group, options) { + norm_str = norm_str + "{ "; + + buildExpression(group.value, options); + + norm_str = norm_str + "} "; +}; + +groupTypes.text = function(group, options) { + + norm_str = norm_str + "\\mathrm { "; + + buildExpression(group.value.body, options); + norm_str = norm_str + "} "; +}; + +groupTypes.color = function(group, options) { + var inner = buildExpression(group.value.value, options); + + var node = new mathMLTree.MathNode("mstyle", inner); + + node.setAttribute("mathcolor", group.value.color); + + return node; +}; + +groupTypes.supsub = function(group, options) { + buildGroup(group.value.base, options); + + if (group.value.sub) { + norm_str = norm_str + "_ "; + if (group.value.sub.type != 'ordgroup') { + norm_str = norm_str + " { "; + buildGroup(group.value.sub, options); + norm_str = norm_str + "} "; + } else { + buildGroup(group.value.sub, options); + } + + } + + if (group.value.sup) { + norm_str = norm_str + "^ "; + if (group.value.sup.type != 'ordgroup') { + norm_str = norm_str + " { "; + buildGroup(group.value.sup, options); + norm_str = norm_str + "} "; + } else { + buildGroup(group.value.sup, options); + } + } + +}; + +groupTypes.genfrac = function(group, options) { + if (!group.value.hasBarLine) { + norm_str = norm_str + "\\binom "; + } else { + norm_str = norm_str + "\\frac "; + } + buildGroup(group.value.numer, options); + buildGroup(group.value.denom, options); + +}; + +groupTypes.array = function(group, options) { + norm_str = norm_str + "\\begin{array} { "; + if (group.value.cols) { + group.value.cols.map(function(start) { + if (start && start.align) { + norm_str = norm_str + start.align + " ";}}); + } else { + group.value.body[0].map(function(start) { + norm_str = norm_str + "l "; + } ); + } + norm_str = norm_str + "} "; + group.value.body.map(function(row) { + if (row.some(cell => cell.value.length > 0)) { // orginal code: if (row[0].value.length > 0) + out = row.map(function(cell) { + buildGroup(cell, options); + if (norm_str.length > 4 + && norm_str.substring(norm_str.length-4, norm_str.length) == "{ } ") { + norm_str = norm_str.substring(0, norm_str.length-4) ; + } + norm_str = norm_str + "& "; + }); + norm_str = norm_str.substring(0, norm_str.length-2) + "\\\\ "; + } + }); + norm_str = norm_str + "\\end{array} "; +}; + +groupTypes.sqrt = function(group, options) { + var node; + if (group.value.index) { + norm_str = norm_str + "\\sqrt [ "; + buildExpression(group.value.index.value, options); + norm_str = norm_str + "] "; + buildGroup(group.value.body, options); + } else { + norm_str = norm_str + "\\sqrt "; + buildGroup(group.value.body, options); + } +}; + +groupTypes.leftright = function(group, options) { + + + + norm_str = norm_str + "\\left" + group.value.left + " "; + buildExpression(group.value.body, options); + norm_str = norm_str + "\\right" + group.value.right + " "; +}; + +groupTypes.accent = function(group, options) { + if (group.value.base.type != 'ordgroup') { + norm_str = norm_str + group.value.accent + " { "; + buildGroup(group.value.base, options); + norm_str = norm_str + "} "; + } else { + norm_str = norm_str + group.value.accent + " "; + buildGroup(group.value.base, options); + } +}; + +groupTypes.spacing = function(group) { + var node; + if (group.value == " ") { + norm_str = norm_str + "~ "; + } else { + norm_str = norm_str + group.value + " "; + } + return node; +}; + +groupTypes.op = function(group) { + var node; + + // TODO(emily): handle big operators using the `largeop` attribute + + + if (group.value.symbol) { + // This is a symbol. Just add the symbol. + norm_str = norm_str + group.value.body + " "; + + } else { + if (group.value.limits == false) { + norm_str = norm_str + "\\\operatorname { "; + } else { + norm_str = norm_str + "\\\operatorname* { "; + } + for (i = 1; i < group.value.body.length; ++i ) { + norm_str = norm_str + group.value.body[i] + " "; + } + norm_str = norm_str + "} "; + } +}; + +groupTypes.katex = function(group) { + var node = new mathMLTree.MathNode( + "mtext", [new mathMLTree.TextNode("KaTeX")]); + + return node; +}; + + + +groupTypes.font = function(group, options) { + var font = group.value.font; + if (font == "mbox" || font == "hbox") { + font = "mathrm"; + } + norm_str = norm_str + "\\" + font + " "; + buildGroup(group.value.body, options.withFont(font)); +}; + +groupTypes.delimsizing = function(group) { + var children = []; + norm_str = norm_str + group.value.funcName + " " + group.value.value + " "; +}; + +groupTypes.styling = function(group, options) { + norm_str = norm_str + " " + group.value.original + " "; + buildExpression(group.value.value, options); + +}; + +groupTypes.sizing = function(group, options) { + + if (group.value.original == "\\rm") { + norm_str = norm_str + "\\mathrm { "; + buildExpression(group.value.value, options.withFont("mathrm")); + norm_str = norm_str + "} "; + } else { + norm_str = norm_str + " " + group.value.original + " "; + buildExpression(group.value.value, options); + } +}; + +groupTypes.overline = function(group, options) { + norm_str = norm_str + "\\overline { "; + + buildGroup(group.value.body, options); + norm_str = norm_str + "} "; + norm_str = norm_str; + +}; + +groupTypes.underline = function(group, options) { + norm_str = norm_str + "\\underline { "; + buildGroup(group.value.body, options); + norm_str = norm_str + "} "; + + norm_str = norm_str; + +}; + +groupTypes.rule = function(group) { + norm_str = norm_str + "\\rule { "+group.value.width.number+" "+group.value.width.unit+" } { "+group.value.height.number+" "+group.value.height.unit+ " } "; + +}; + +groupTypes.llap = function(group, options) { + norm_str = norm_str + "\\llap "; + buildGroup(group.value.body, options); +}; + +groupTypes.rlap = function(group, options) { + norm_str = norm_str + "\\rlap "; + buildGroup(group.value.body, options); + +}; + +groupTypes.phantom = function(group, options, prev) { + norm_str = norm_str + "\\phantom { "; + buildExpression(group.value.value, options); + norm_str = norm_str + "} "; + +}; + +/** + * Takes a list of nodes, builds them, and returns a list of the generated + * MathML nodes. A little simpler than the HTML version because we don't do any + * previous-node handling. + */ +var buildExpression = function(expression, options) { + var groups = []; + for (var i = 0; i < expression.length; i++) { + var group = expression[i]; + buildGroup(group, options); + } + // console.log(norm_str); + // return groups; +}; + +/** + * Takes a group from the parser and calls the appropriate groupTypes function + * on it to produce a MathML node. + */ +var buildGroup = function(group, options) { + if (groupTypes[group.type]) { + groupTypes[group.type](group, options); + } else { + throw new ParseError( + "Got group of unknown type: '" + group.type + "'"); + } +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_tabular.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_tabular.js new file mode 100644 index 000000000..40ac043bd --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/preprocess_tabular.js @@ -0,0 +1,393 @@ +const path = require('path'); +var katex = require(path.join(__dirname,"third_party/katex/katex.js")) +options = require(path.join(__dirname,"third_party/katex/src/Options.js")) +var readline = require('readline'); +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false +}); + + +rl.on('line', function(line){ + a = line + if (line[0] == "%") { + line = line.substr(1, line.length - 1); + } + // line = line.split('%')[0]; + + line = line.split('\\~').join(' '); + + for (var i = 0; i < 300; i++) { + line = line.replace(/\\>/, " "); + // line = line.replace('$', ' '); + line = line.replace(/\\label{.*?}/, ""); + } + + if (line.indexOf("matrix") == -1 && line.indexOf("cases")==-1 && + line.indexOf("array")==-1 && line.indexOf("begin")==-1) { + for (var i = 0; i < 300; i++) { + line = line.replace(/\\\\/, "\\,"); + } + } + + + line = line + " " + // global_str is tokenized version (build in parser.js) + // norm_str is normalized version build by renderer below. + try { + + + if (process.argv[2] == "tokenize") { + var tree = katex.__parse(line, {}); + console.log(global_str.replace(/\\label { .*? }/, "")); + } else { + for (var i = 0; i < 300; ++i) { + line = line.replace(/{\\rm/, "\\mathrm{"); + line = line.replace(/{ \\rm/, "\\mathrm{"); + line = line.replace(/\\rm{/, "\\mathrm{"); + } + + var tree = katex.__parse(line, {}); + buildExpression(tree, new options({})); + for (var i = 0; i < 300; ++i) { + norm_str = norm_str.replace('SSSSSS', '$'); + norm_str = norm_str.replace(' S S S S S S', '$'); + } + console.log(norm_str.replace(/\\label { .*? }/, "")); + } + } catch (e) { + console.error(line); + console.error(norm_str); + console.error(e); + console.log(""); + } + global_str = "" + norm_str = "" +}) + + + +// This is a LaTeX AST to LaTeX Renderer (modified version of KaTeX AST-> MathML). +norm_str = "" + +var groupTypes = {}; + +groupTypes.mathord = function(group, options) { + if (options.font == "mathrm"){ + for (i = 0; i < group.value.length; ++i ) { + if (group.value[i] == " ") { + norm_str = norm_str + group.value[i] + "\; "; + } else { + norm_str = norm_str + group.value[i] + " "; + } + } + } else { + norm_str = norm_str + group.value + " "; + } +}; + +groupTypes.textord = function(group, options) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.bin = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.rel = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.open = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.close = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.inner = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.punct = function(group) { + norm_str = norm_str + group.value + " "; +}; + +groupTypes.ordgroup = function(group, options) { + norm_str = norm_str + "{ "; + + buildExpression(group.value, options); + + norm_str = norm_str + "} "; +}; + +groupTypes.text = function(group, options) { + + norm_str = norm_str + "\\mathrm { "; + + buildExpression(group.value.body, options); + norm_str = norm_str + "} "; +}; + +groupTypes.color = function(group, options) { + var inner = buildExpression(group.value.value, options); + + var node = new mathMLTree.MathNode("mstyle", inner); + + node.setAttribute("mathcolor", group.value.color); + + return node; +}; + +groupTypes.supsub = function(group, options) { + buildGroup(group.value.base, options); + + if (group.value.sub) { + norm_str = norm_str + "_ "; + if (group.value.sub.type != 'ordgroup') { + norm_str = norm_str + " { "; + buildGroup(group.value.sub, options); + norm_str = norm_str + "} "; + } else { + buildGroup(group.value.sub, options); + } + + } + + if (group.value.sup) { + norm_str = norm_str + "^ "; + if (group.value.sup.type != 'ordgroup') { + norm_str = norm_str + " { "; + buildGroup(group.value.sup, options); + norm_str = norm_str + "} "; + } else { + buildGroup(group.value.sup, options); + } + } + +}; + +groupTypes.genfrac = function(group, options) { + if (!group.value.hasBarLine) { + norm_str = norm_str + "\\binom "; + } else { + norm_str = norm_str + "\\frac "; + } + buildGroup(group.value.numer, options); + buildGroup(group.value.denom, options); + +}; + +groupTypes.array = function(group, options) { + norm_str = norm_str + "\\begin{" + group.value.style + "} "; + + if (group.value.style == "array" || group.value.style == "tabular" || group.value.style == "tabularx") { + norm_str = norm_str + "{ "; + if (group.value.cols) { + group.value.cols.map(function(start) { + if (start) { + if (start.type == "align") { + norm_str = norm_str + start.align + " "; + } else if (start.type == "separator") { + norm_str = norm_str + start.separator + " "; + } + } + }); + } else { + group.value.body[0].map(function(start) { + norm_str = norm_str + "c "; + } ); + } + norm_str = norm_str + "} "; + } + group.value.body.map(function(row) { + if (row.length > 1 || row[0].value.length > 0) { + if (row[0].value[0] && row[0].value[0].value == "\\hline") { + norm_str = norm_str + "\\hline "; + row[0].value = row[0].value.slice(1); + } + out = row.map(function(cell) { + buildGroup(cell, options); + norm_str = norm_str + "& "; + }); + norm_str = norm_str.substring(0, norm_str.length-2) + "\\\\ "; + } + }); + norm_str = norm_str + "\\end{" + group.value.style + "} "; +}; + +groupTypes.sqrt = function(group, options) { + var node; + if (group.value.index) { + norm_str = norm_str + "\\sqrt [ " + group.value.index + " ] "; + buildGroup(group.value.body, options); + } else { + norm_str = norm_str + "\\sqrt "; + buildGroup(group.value.body, options); + } +}; + +groupTypes.leftright = function(group, options) { + + + + norm_str = norm_str + "\\left" + group.value.left + " "; + buildExpression(group.value.body, options); + norm_str = norm_str + "\\right" + group.value.right + " "; +}; + +groupTypes.accent = function(group, options) { + if (group.value.base.type != 'ordgroup') { + norm_str = norm_str + group.value.accent + " { "; + buildGroup(group.value.base, options); + norm_str = norm_str + "} "; + } else { + norm_str = norm_str + group.value.accent + " "; + buildGroup(group.value.base, options); + } +}; + +groupTypes.spacing = function(group) { + var node; + if (group.value == " ") { + norm_str = norm_str + "~ "; + } else { + norm_str = norm_str + group.value + " "; + } + return node; +}; + +groupTypes.op = function(group) { + var node; + + // TODO(emily): handle big operators using the `largeop` attribute + + + if (group.value.symbol) { + // This is a symbol. Just add the symbol. + norm_str = norm_str + group.value.body + " "; + + } else { + if (group.value.limits == false) { + norm_str = norm_str + "\\\operatorname { "; + } else { + norm_str = norm_str + "\\\operatorname* { "; + } + for (i = 1; i < group.value.body.length; ++i ) { + norm_str = norm_str + group.value.body[i] + " "; + } + norm_str = norm_str + "} "; + } +}; + +groupTypes.katex = function(group) { + var node = new mathMLTree.MathNode( + "mtext", [new mathMLTree.TextNode("KaTeX")]); + + return node; +}; + + + +groupTypes.font = function(group, options) { + var font = group.value.font; + if (font == "mbox" || font == "hbox") { + font = "mathrm"; + } + norm_str = norm_str + "\\" + font + " "; + buildGroup(group.value.body, options.withFont(font)); +}; + +groupTypes.delimsizing = function(group) { + var children = []; + norm_str = norm_str + group.value.funcName + " " + group.value.value + " "; +}; + +groupTypes.styling = function(group, options) { + norm_str = norm_str + " " + group.value.original + " "; + buildExpression(group.value.value, options); + +}; + +groupTypes.sizing = function(group, options) { + + if (group.value.original == "\\rm") { + norm_str = norm_str + "\\mathrm { "; + buildExpression(group.value.value, options.withFont("mathrm")); + norm_str = norm_str + "} "; + } else { + norm_str = norm_str + " " + group.value.original + " "; + buildExpression(group.value.value, options); + } +}; + +groupTypes.overline = function(group, options) { + norm_str = norm_str + "\\overline { "; + + buildGroup(group.value.body, options); + norm_str = norm_str + "} "; + norm_str = norm_str; + +}; + +groupTypes.underline = function(group, options) { + norm_str = norm_str + "\\underline { "; + buildGroup(group.value.body, options); + norm_str = norm_str + "} "; + + norm_str = norm_str; + +}; + +groupTypes.rule = function(group) { + norm_str = norm_str + "\\rule { "+group.value.width.number+" "+group.value.width.unit+" } { "+group.value.height.number+" "+group.value.height.unit+ " } "; + +}; + +groupTypes.llap = function(group, options) { + norm_str = norm_str + "\\llap "; + buildGroup(group.value.body, options); +}; + +groupTypes.rlap = function(group, options) { + norm_str = norm_str + "\\rlap "; + buildGroup(group.value.body, options); + +}; + +groupTypes.phantom = function(group, options, prev) { + norm_str = norm_str + "\\phantom { "; + buildExpression(group.value.value, options); + norm_str = norm_str + "} "; + +}; + +/** + * Takes a list of nodes, builds them, and returns a list of the generated + * MathML nodes. A little simpler than the HTML version because we don't do any + * previous-node handling. + */ +var buildExpression = function(expression, options) { + var groups = []; + for (var i = 0; i < expression.length; i++) { + var group = expression[i]; + buildGroup(group, options); + } + // console.log(norm_str); + // return groups; +}; + +/** + * Takes a group from the parser and calls the appropriate groupTypes function + * on it to produce a MathML node. + */ +var buildGroup = function(group, options) { + if (groupTypes[group.type]) { + groupTypes[group.type](group, options); + } else { + throw new ParseError( + "Got group of unknown type: '" + group.type + "'"); + } +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/README.md b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/README.md new file mode 100644 index 000000000..bc74abab4 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/README.md @@ -0,0 +1 @@ +Directly taken from https://github.com/harvardnlp/im2markup diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/README.md b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/README.md new file mode 100644 index 000000000..31cf658d8 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/README.md @@ -0,0 +1,68 @@ +# [KaTeX](https://khan.github.io/KaTeX/) [![Build Status](https://travis-ci.org/Khan/KaTeX.svg?branch=master)](https://travis-ci.org/Khan/KaTeX) + +[![Join the chat at https://gitter.im/Khan/KaTeX](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Khan/KaTeX?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web. + + * **Fast:** KaTeX renders its math synchronously and doesn't need to reflow the page. See how it compares to a competitor in [this speed test](http://jsperf.com/katex-vs-mathjax/). + * **Print quality:** KaTeX’s layout is based on Donald Knuth’s TeX, the gold standard for math typesetting. + * **Self contained:** KaTeX has no dependencies and can easily be bundled with your website resources. + * **Server side rendering:** KaTeX produces the same output regardless of browser or environment, so you can pre-render expressions using Node.js and send them as plain HTML. + +KaTeX supports all major browsers, including Chrome, Safari, Firefox, Opera, and IE 8 - IE 11. A list of supported commands can be on the [wiki](https://github.com/Khan/KaTeX/wiki/Function-Support-in-KaTeX). + +## Usage + +You can [download KaTeX](https://github.com/khan/katex/releases) and host it on your server or include the `katex.min.js` and `katex.min.css` files on your page directly from a CDN: + +```html + + +``` + +#### In-browser rendering + +Call `katex.render` with a TeX expression and a DOM element to render into: + +```js +katex.render("c = \\pm\\sqrt{a^2 + b^2}", element); +``` + +If KaTeX can't parse the expression, it throws a `katex.ParseError` error. + +#### Server side rendering or rendering to a string + +To generate HTML on the server or to generate an HTML string of the rendered math, you can use `katex.renderToString`: + +```js +var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}"); +// '...' +``` + +Make sure to include the CSS and font files, but there is no need to include the JavaScript. Like `render`, `renderToString` throws if it can't parse the expression. + +#### Rendering options + +You can provide an object of options as the last argument to `katex.render` and `katex.renderToString`. Available options are: + +- `displayMode`: `boolean`. If `true` the math will be rendered in display mode, which will put the math in display style (so `\int` and `\sum` are large, for example), and will center the math on the page on its own line. If `false` the math will be rendered in inline mode. (default: `false`) +- `throwOnError`: `boolean`. If `true`, KaTeX will throw a `ParseError` when it encounters an unsupported command. If `false`, KaTeX will render the unsupported command as text in the color given by `errorColor`. (default: `true`) +- `errorColor`: `string`. A color string given in the format `"#XXX"` or `"#XXXXXX"`. This option determines the color which unsupported commands are rendered in. (default: `#cc0000`) + +For example: + +```js +katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, { displayMode: true }); +``` + +#### Automatic rendering of math on a page + +Math on the page can be automatically rendered using the auto-render extension. See [the Auto-render README](contrib/auto-render/README.md) for more information. + +## Contributing + +See [CONTRIBUTING.md](CONTRIBUTING.md) + +## License + +KaTeX is licensed under the [MIT License](http://opensource.org/licenses/MIT). diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/cli.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/cli.js new file mode 100644 index 000000000..b64de377c --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/cli.js @@ -0,0 +1,32 @@ +#!/usr/bin/env node +// Simple CLI for KaTeX. +// Reads TeX from stdin, outputs HTML to stdout. +/* eslint no-console:0 */ + +var katex = require("./"); +var input = ""; + +// Skip the first two args, which are just "node" and "cli.js" +var args = process.argv.slice(2); + +if (args.indexOf("--help") !== -1) { + console.log(process.argv[0] + " " + process.argv[1] + + " [ --help ]" + + " [ --display-mode ]"); + + console.log("\n" + + "Options:"); + console.log(" --help Display this help message"); + console.log(" --display-mode Render in display mode (not inline mode)"); + process.exit(); +} + +process.stdin.on("data", function(chunk) { + input += chunk.toString(); +}); + +process.stdin.on("end", function() { + var options = { displayMode: args.indexOf("--display-mode") !== -1 }; + var output = katex.renderToString(input, options); + console.log(output); +}); diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/katex.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/katex.js new file mode 100644 index 000000000..4d64606bf --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/katex.js @@ -0,0 +1,74 @@ +/* eslint no-console:0 */ +/** + * This is the main entry point for KaTeX. Here, we expose functions for + * rendering expressions either to DOM nodes or to markup strings. + * + * We also expose the ParseError class to check if errors thrown from KaTeX are + * errors in the expression, or errors in javascript handling. + */ + +var ParseError = require("./src/ParseError"); +var Settings = require("./src/Settings"); + +var buildTree = require("./src/buildTree"); +var parseTree = require("./src/parseTree"); +var utils = require("./src/utils"); + +/** + * Parse and build an expression, and place that expression in the DOM node + * given. + */ +var render = function(expression, baseNode, options) { + utils.clearNode(baseNode); + + var settings = new Settings(options); + + var tree = parseTree(expression, settings); + var node = buildTree(tree, expression, settings).toNode(); + + baseNode.appendChild(node); +}; + +// KaTeX's styles don't work properly in quirks mode. Print out an error, and +// disable rendering. +if (typeof document !== "undefined") { + if (document.compatMode !== "CSS1Compat") { + typeof console !== "undefined" && console.warn( + "Warning: KaTeX doesn't work in quirks mode. Make sure your " + + "website has a suitable doctype."); + + render = function() { + throw new ParseError("KaTeX doesn't work in quirks mode."); + }; + } +} + +/** + * Parse and build an expression, and return the markup for that. + */ +var renderToString = function(expression, options) { + var settings = new Settings(options); + + var tree = parseTree(expression, settings); + return buildTree(tree, expression, settings).toMarkup(); +}; + +/** + * Parse an expression and return the parse tree. + */ +var generateParseTree = function(expression, options) { + var settings = new Settings(options); + return parseTree(expression, settings); +}; + +module.exports = { + render: render, + renderToString: renderToString, + /** + * NOTE: This method is not currently recommended for public use. + * The internal tree representation is unstable and is very likely + * to change. Use at your own risk. + */ + __parse: generateParseTree, + ParseError: ParseError, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Lexer.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Lexer.js new file mode 100644 index 000000000..5cb8d3d4e --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Lexer.js @@ -0,0 +1,162 @@ +/** + * The Lexer class handles tokenizing the input in various ways. Since our + * parser expects us to be able to backtrack, the lexer allows lexing from any + * given starting point. + * + * Its main exposed function is the `lex` function, which takes a position to + * lex from and a type of token to lex. It defers to the appropriate `_innerLex` + * function. + * + * The various `_innerLex` functions perform the actual lexing of different + * kinds. + */ + +var matchAt = require("../../match-at"); + +var ParseError = require("./ParseError"); + +// The main lexer class +function Lexer(input) { + this._input = input; +} + +// The resulting token returned from `lex`. +function Token(text, data, position) { + this.text = text; + this.data = data; + this.position = position; +} + +/* The following tokenRegex + * - matches typical whitespace (but not NBSP etc.) using its first group + * - matches symbol combinations which result in a single output character + * - does not match any control character \x00-\x1f except whitespace + * - does not match a bare backslash + * - matches any ASCII character except those just mentioned + * - does not match the BMP private use area \uE000-\uF8FF + * - does not match bare surrogate code units + * - matches any BMP character except for those just described + * - matches any valid Unicode surrogate pair + * - matches a backslash followed by one or more letters + * - matches a backslash followed by any BMP character, including newline + * Just because the Lexer matches something doesn't mean it's valid input: + * If there is no matching function or symbol definition, the Parser will + * still reject the input. + */ +var tokenRegex = new RegExp( + "([ \r\n\t]+)|(" + // whitespace + "---?" + // special combinations + "|[!-\\[\\]-\u2027\u202A-\uD7FF\uF900-\uFFFF]" + // single codepoint + "|[\uD800-\uDBFF][\uDC00-\uDFFF]" + // surrogate pair + "|\\\\(?:[a-zA-Z]+|[^\uD800-\uDFFF])" + // function name + ")" +); + +var whitespaceRegex = /\s*/; + +/** + * This function lexes a single normal token. It takes a position and + * whether it should completely ignore whitespace or not. + */ +Lexer.prototype._innerLex = function(pos, ignoreWhitespace) { + var input = this._input; + if (pos === input.length) { + return new Token("EOF", null, pos); + } + var match = matchAt(tokenRegex, input, pos); + if (match === null) { + throw new ParseError( + "Unexpected character: '" + input[pos] + "'", + this, pos); + } else if (match[2]) { // matched non-whitespace + return new Token(match[2], null, pos + match[2].length); + } else if (ignoreWhitespace) { + return this._innerLex(pos + match[1].length, true); + } else { // concatenate whitespace to a single space + return new Token(" ", null, pos + match[1].length); + } +}; + +// A regex to match a CSS color (like #ffffff or BlueViolet) +var cssColor = /#[a-z0-9]+|[a-z]+/i; + +/** + * This function lexes a CSS color. + */ +Lexer.prototype._innerLexColor = function(pos) { + var input = this._input; + + // Ignore whitespace + var whitespace = matchAt(whitespaceRegex, input, pos)[0]; + pos += whitespace.length; + + var match; + if ((match = matchAt(cssColor, input, pos))) { + // If we look like a color, return a color + return new Token(match[0], null, pos + match[0].length); + } else { + throw new ParseError("Invalid color", this, pos); + } +}; + +// A regex to match a dimension. Dimensions look like +// "1.2em" or ".4pt" or "1 ex" +var sizeRegex = /(-?)\s*(\d+(?:\.\d*)?|\.\d+)\s*([a-z]{2})/; + +/** + * This function lexes a dimension. + */ +Lexer.prototype._innerLexSize = function(pos) { + var input = this._input; + + // Ignore whitespace + var whitespace = matchAt(whitespaceRegex, input, pos)[0]; + pos += whitespace.length; + + var match; + if ((match = matchAt(sizeRegex, input, pos))) { + var unit = match[3]; + // We only currently handle "em" and "ex" units + // if (unit !== "em" && unit !== "ex") { + // throw new ParseError("Invalid unit: '" + unit + "'", this, pos); + // } + return new Token(match[0], { + number: +(match[1] + match[2]), + unit: unit, + }, pos + match[0].length); + } + + throw new ParseError("Invalid size", this, pos); +}; + +/** + * This function lexes a string of whitespace. + */ +Lexer.prototype._innerLexWhitespace = function(pos) { + var input = this._input; + + var whitespace = matchAt(whitespaceRegex, input, pos)[0]; + pos += whitespace.length; + + return new Token(whitespace[0], null, pos); +}; + +/** + * This function lexes a single token starting at `pos` and of the given mode. + * Based on the mode, we defer to one of the `_innerLex` functions. + */ +Lexer.prototype.lex = function(pos, mode) { + if (mode === "math") { + return this._innerLex(pos, true); + } else if (mode === "text") { + return this._innerLex(pos, false); + } else if (mode === "color") { + return this._innerLexColor(pos); + } else if (mode === "size") { + return this._innerLexSize(pos); + } else if (mode === "whitespace") { + return this._innerLexWhitespace(pos); + } +}; + +module.exports = Lexer; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Options.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Options.js new file mode 100644 index 000000000..39ff37bfc --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Options.js @@ -0,0 +1,189 @@ +/** + * This file contains information about the options that the Parser carries + * around with it while parsing. Data is held in an `Options` object, and when + * recursing, a new `Options` object can be created with the `.with*` and + * `.reset` functions. + */ + +/** + * This is the main options class. It contains the style, size, color, and font + * of the current parse level. It also contains the style and size of the parent + * parse level, so size changes can be handled efficiently. + * + * Each of the `.with*` and `.reset` functions passes its current style and size + * as the parentStyle and parentSize of the new options class, so parent + * handling is taken care of automatically. + */ +function Options(data) { + this.style = data.style; + this.color = data.color; + this.size = data.size; + this.phantom = data.phantom; + this.font = data.font; + + if (data.parentStyle === undefined) { + this.parentStyle = data.style; + } else { + this.parentStyle = data.parentStyle; + } + + if (data.parentSize === undefined) { + this.parentSize = data.size; + } else { + this.parentSize = data.parentSize; + } +} + +/** + * Returns a new options object with the same properties as "this". Properties + * from "extension" will be copied to the new options object. + */ +Options.prototype.extend = function(extension) { + var data = { + style: this.style, + size: this.size, + color: this.color, + parentStyle: this.style, + parentSize: this.size, + phantom: this.phantom, + font: this.font, + }; + + for (var key in extension) { + if (extension.hasOwnProperty(key)) { + data[key] = extension[key]; + } + } + + return new Options(data); +}; + +/** + * Create a new options object with the given style. + */ +Options.prototype.withStyle = function(style) { + return this.extend({ + style: style, + }); +}; + +/** + * Create a new options object with the given size. + */ +Options.prototype.withSize = function(size) { + return this.extend({ + size: size, + }); +}; + +/** + * Create a new options object with the given color. + */ +Options.prototype.withColor = function(color) { + return this.extend({ + color: color, + }); +}; + +/** + * Create a new options object with "phantom" set to true. + */ +Options.prototype.withPhantom = function() { + return this.extend({ + phantom: true, + }); +}; + +/** + * Create a new options objects with the give font. + */ +Options.prototype.withFont = function(font) { + return this.extend({ + font: font, + }); +}; + +/** + * Create a new options object with the same style, size, and color. This is + * used so that parent style and size changes are handled correctly. + */ +Options.prototype.reset = function() { + return this.extend({}); +}; + +/** + * A map of color names to CSS colors. + * TODO(emily): Remove this when we have real macros + */ +var colorMap = { + "katex-blue": "#6495ed", + "katex-orange": "#ffa500", + "katex-pink": "#ff00af", + "katex-red": "#df0030", + "katex-green": "#28ae7b", + "katex-gray": "gray", + "katex-purple": "#9d38bd", + "katex-blueA": "#c7e9f1", + "katex-blueB": "#9cdceb", + "katex-blueC": "#58c4dd", + "katex-blueD": "#29abca", + "katex-blueE": "#1c758a", + "katex-tealA": "#acead7", + "katex-tealB": "#76ddc0", + "katex-tealC": "#5cd0b3", + "katex-tealD": "#55c1a7", + "katex-tealE": "#49a88f", + "katex-greenA": "#c9e2ae", + "katex-greenB": "#a6cf8c", + "katex-greenC": "#83c167", + "katex-greenD": "#77b05d", + "katex-greenE": "#699c52", + "katex-goldA": "#f7c797", + "katex-goldB": "#f9b775", + "katex-goldC": "#f0ac5f", + "katex-goldD": "#e1a158", + "katex-goldE": "#c78d46", + "katex-redA": "#f7a1a3", + "katex-redB": "#ff8080", + "katex-redC": "#fc6255", + "katex-redD": "#e65a4c", + "katex-redE": "#cf5044", + "katex-maroonA": "#ecabc1", + "katex-maroonB": "#ec92ab", + "katex-maroonC": "#c55f73", + "katex-maroonD": "#a24d61", + "katex-maroonE": "#94424f", + "katex-purpleA": "#caa3e8", + "katex-purpleB": "#b189c6", + "katex-purpleC": "#9a72ac", + "katex-purpleD": "#715582", + "katex-purpleE": "#644172", + "katex-mintA": "#f5f9e8", + "katex-mintB": "#edf2df", + "katex-mintC": "#e0e5cc", + "katex-grayA": "#fdfdfd", + "katex-grayB": "#f7f7f7", + "katex-grayC": "#eeeeee", + "katex-grayD": "#dddddd", + "katex-grayE": "#cccccc", + "katex-grayF": "#aaaaaa", + "katex-grayG": "#999999", + "katex-grayH": "#555555", + "katex-grayI": "#333333", + "katex-kaBlue": "#314453", + "katex-kaGreen": "#639b24", +}; + +/** + * Gets the CSS color of the current options object, accounting for the + * `colorMap`. + */ +Options.prototype.getColor = function() { + if (this.phantom) { + return "transparent"; + } else { + return colorMap[this.color] || this.color; + } +}; + +module.exports = Options; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/ParseError.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/ParseError.js new file mode 100644 index 000000000..320f0bd69 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/ParseError.js @@ -0,0 +1,40 @@ +/** + * This is the ParseError class, which is the main error thrown by KaTeX + * functions when something has gone wrong. This is used to distinguish internal + * errors from errors in the expression that the user provided. + */ +function ParseError(message, lexer, position) { + var error = "KaTeX parse error: " + message; + + if (lexer !== undefined && position !== undefined) { + // If we have the input and a position, make the error a bit fancier + + // Prepend some information + error += " at position " + position + ": "; + + // Get the input + var input = lexer._input; + // Insert a combining underscore at the correct position + input = input.slice(0, position) + "\u0332" + + input.slice(position); + + // Extract some context from the input and add it to the error + var begin = Math.max(0, position - 15); + var end = position + 15; + error += input.slice(begin, end); + } + + // Some hackery to make ParseError a prototype of Error + // See http://stackoverflow.com/a/8460753 + var self = new Error(error); + self.name = "ParseError"; + self.__proto__ = ParseError.prototype; + + self.position = position; + return self; +} + +// More hackery +ParseError.prototype.__proto__ = Error.prototype; + +module.exports = ParseError; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Parser.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Parser.js new file mode 100644 index 000000000..aca6cd291 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Parser.js @@ -0,0 +1,798 @@ +/* eslint no-constant-condition:0 */ +var functions = require("./functions"); +var environments = require("./environments"); +var Lexer = require("./Lexer"); +var symbols = require("./symbols"); +var utils = require("./utils"); + +var parseData = require("./parseData"); +var ParseError = require("./ParseError"); + +global_str = "" + +/** + * This file contains the parser used to parse out a TeX expression from the + * input. Since TeX isn't context-free, standard parsers don't work particularly + * well. + * + * The strategy of this parser is as such: + * + * The main functions (the `.parse...` ones) take a position in the current + * parse string to parse tokens from. The lexer (found in Lexer.js, stored at + * this.lexer) also supports pulling out tokens at arbitrary places. When + * individual tokens are needed at a position, the lexer is called to pull out a + * token, which is then used. + * + * The parser has a property called "mode" indicating the mode that + * the parser is currently in. Currently it has to be one of "math" or + * "text", which denotes whether the current environment is a math-y + * one or a text-y one (e.g. inside \text). Currently, this serves to + * limit the functions which can be used in text mode. + * + * The main functions then return an object which contains the useful data that + * was parsed at its given point, and a new position at the end of the parsed + * data. The main functions can call each other and continue the parsing by + * using the returned position as a new starting point. + * + * There are also extra `.handle...` functions, which pull out some reused + * functionality into self-contained functions. + * + * The earlier functions return ParseNodes. + * The later functions (which are called deeper in the parse) sometimes return + * ParseFuncOrArgument, which contain a ParseNode as well as some data about + * whether the parsed object is a function which is missing some arguments, or a + * standalone object which can be used as an argument to another function. + */ + +/** + * Main Parser class + */ +function Parser(input, settings) { + // Make a new lexer + this.lexer = new Lexer(input); + // Store the settings for use in parsing + this.settings = settings; +} + +var ParseNode = parseData.ParseNode; + +/** + * An initial function (without its arguments), or an argument to a function. + * The `result` argument should be a ParseNode. + */ +function ParseFuncOrArgument(result, isFunction) { + this.result = result; + // Is this a function (i.e. is it something defined in functions.js)? + this.isFunction = isFunction; +} + +/** + * Checks a result to make sure it has the right type, and throws an + * appropriate error otherwise. + * + * @param {boolean=} consume whether to consume the expected token, + * defaults to true + */ +Parser.prototype.expect = function(text, consume) { + if (this.nextToken.text !== text) { + throw new ParseError( + "Expected '" + text + "', got '" + this.nextToken.text + "'", + this.lexer, this.nextToken.position + ); + } + if (consume !== false) { + this.consume(); + } +}; + +/** + * Considers the current look ahead token as consumed, + * and fetches the one after that as the new look ahead. + */ +Parser.prototype.consume = function() { + this.pos = this.nextToken.position; + + global_str = global_str + " " + this.nextToken.text + this.nextToken = this.lexer.lex(this.pos, this.mode); +}; + +/** + * Main parsing function, which parses an entire input. + * + * @return {?Array.} + */ +Parser.prototype.parse = function() { + // Try to parse the input + this.mode = "math"; + this.pos = 0; + this.nextToken = this.lexer.lex(this.pos, this.mode); + var parse = this.parseInput(); + return parse; +}; + +/** + * Parses an entire input tree. + */ +Parser.prototype.parseInput = function() { + // Parse an expression + var expression = this.parseExpression(false); + // If we succeeded, make sure there's an EOF at the end + this.expect("EOF", false); + return expression; +}; + +var endOfExpression = ["}", "\\end", "\\right", "&", "\\\\", "\\cr"]; + +/** + * Parses an "expression", which is a list of atoms. + * + * @param {boolean} breakOnInfix Should the parsing stop when we hit infix + * nodes? This happens when functions have higher precendence + * than infix nodes in implicit parses. + * + * @param {?string} breakOnToken The token that the expression should end with, + * or `null` if something else should end the expression. + * + * @return {ParseNode} + */ +Parser.prototype.parseExpression = function(breakOnInfix, breakOnToken) { + var body = []; + // Keep adding atoms to the body until we can't parse any more atoms (either + // we reached the end, a }, or a \right) + while (true) { + var lex = this.nextToken; + var pos = this.pos; + if (endOfExpression.indexOf(lex.text) !== -1) { + break; + } + if (breakOnToken && lex.text === breakOnToken) { + break; + } + var atom = this.parseAtom(); + if (!atom) { + if (!this.settings.throwOnError && lex.text[0] === "\\") { + var errorNode = this.handleUnsupportedCmd(); + body.push(errorNode); + + pos = lex.position; + continue; + } + + break; + } + if (breakOnInfix && atom.type === "infix") { + // rewind so we can parse the infix atom again + this.pos = pos; + this.nextToken = lex; + break; + } + body.push(atom); + } + return this.handleInfixNodes(body); +}; + +/** + * Rewrites infix operators such as \over with corresponding commands such + * as \frac. + * + * There can only be one infix operator per group. If there's more than one + * then the expression is ambiguous. This can be resolved by adding {}. + * + * @returns {Array} + */ +Parser.prototype.handleInfixNodes = function(body) { + var overIndex = -1; + var funcName; + + for (var i = 0; i < body.length; i++) { + var node = body[i]; + if (node.type === "infix") { + if (overIndex !== -1) { + throw new ParseError("only one infix operator per group", + this.lexer, -1); + } + overIndex = i; + funcName = node.value.replaceWith; + } + } + + if (overIndex !== -1) { + var numerNode; + var denomNode; + + var numerBody = body.slice(0, overIndex); + var denomBody = body.slice(overIndex + 1); + + if (numerBody.length === 1 && numerBody[0].type === "ordgroup") { + numerNode = numerBody[0]; + } else { + numerNode = new ParseNode("ordgroup", numerBody, this.mode); + } + + if (denomBody.length === 1 && denomBody[0].type === "ordgroup") { + denomNode = denomBody[0]; + } else { + denomNode = new ParseNode("ordgroup", denomBody, this.mode); + } + + var value = this.callFunction( + funcName, [numerNode, denomNode], null); + return [new ParseNode(value.type, value, this.mode)]; + } else { + return body; + } +}; + +// The greediness of a superscript or subscript +var SUPSUB_GREEDINESS = 1; + +/** + * Handle a subscript or superscript with nice errors. + */ +Parser.prototype.handleSupSubscript = function(name) { + var symbol = this.nextToken.text; + var symPos = this.pos; + this.consume(); + var group = this.parseGroup(); + + if (!group) { + if (!this.settings.throwOnError && this.nextToken.text[0] === "\\") { + return this.handleUnsupportedCmd(); + } else { + // throw new ParseError( + // "Expected group after '" + symbol + "'", + // this.lexer, + // symPos + 1 + // ); + } + } else if (group.isFunction) { + // ^ and _ have a greediness, so handle interactions with functions' + // greediness + var funcGreediness = functions[group.result].greediness; + if (funcGreediness > SUPSUB_GREEDINESS) { + return this.parseFunction(group); + } else { + throw new ParseError( + "Got function '" + group.result + "' with no arguments " + + "as " + name, + this.lexer, symPos + 1); + } + } else { + return group.result; + } +}; + +/** + * Converts the textual input of an unsupported command into a text node + * contained within a color node whose color is determined by errorColor + */ +Parser.prototype.handleUnsupportedCmd = function() { + var text = this.nextToken.text; + var textordArray = []; + + for (var i = 0; i < text.length; i++) { + textordArray.push(new ParseNode("textord", text[i], "text")); + } + + var textNode = new ParseNode( + "text", + { + body: textordArray, + type: "text", + }, + this.mode); + + var colorNode = new ParseNode( + "color", + { + color: this.settings.errorColor, + value: [textNode], + type: "color", + }, + this.mode); + + this.consume(); + return colorNode; +}; + +/** + * Parses a group with optional super/subscripts. + * + * @return {?ParseNode} + */ +Parser.prototype.parseAtom = function() { + // The body of an atom is an implicit group, so that things like + // \left(x\right)^2 work correctly. + var base = this.parseImplicitGroup(); + + // In text mode, we don't have superscripts or subscripts + if (this.mode === "text") { + return base; + } + + // Note that base may be empty (i.e. null) at this point. + + var superscript; + var subscript; + while (true) { + // Lex the first token + var lex = this.nextToken; + + if (lex.text === "\\limits" || lex.text === "\\nolimits") { + // We got a limit control + if (!base || base.type !== "op") { + throw new ParseError( + "Limit controls must follow a math operator", + this.lexer, this.pos); + } else { + var limits = lex.text === "\\limits"; + base.value.limits = limits; + base.value.alwaysHandleSupSub = true; + } + this.consume(); + } else if (lex.text === "^") { + // We got a superscript start + // if (superscript) { + // throw new ParseError( + // "Double superscript", this.lexer, this.pos); + // } + superscript = this.handleSupSubscript("superscript"); + } else if (lex.text === "_") { + // We got a subscript start + // if (subscript) { + // throw new ParseError( + // "Double subscript", this.lexer, this.pos); + // } + subscript = this.handleSupSubscript("subscript"); + } else if (lex.text === "'") { + // We got a prime + var prime = new ParseNode("textord", "\\prime", this.mode); + + // Many primes can be grouped together, so we handle this here + var primes = [prime]; + this.consume(); + // Keep lexing tokens until we get something that's not a prime + while (this.nextToken.text === "'") { + // For each one, add another prime to the list + primes.push(prime); + this.consume(); + } + // Put them into an ordgroup as the superscript + superscript = new ParseNode("ordgroup", primes, this.mode); + } else { + // If it wasn't ^, _, or ', stop parsing super/subscripts + break; + } + } + + if (superscript || subscript) { + // If we got either a superscript or subscript, create a supsub + return new ParseNode("supsub", { + base: base, + sup: superscript, + sub: subscript, + }, this.mode); + } else { + // Otherwise return the original body + return base; + } +}; + +// A list of the size-changing functions, for use in parseImplicitGroup +var sizeFuncs = [ + "\\tiny", "\\scriptsize", "\\footnotesize", "\\small", "\\normalsize", + "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge", "\\textrm", "\\rm", "\\cal", + "\\bf", "\\siptstyle", "\\boldmath", "\\it" +]; + +// A list of the style-changing functions, for use in parseImplicitGroup +var styleFuncs = [ + "\\displaystyle", "\\textstyle", "\\scriptstyle", "\\scriptscriptstyle", +]; + +/** + * Parses an implicit group, which is a group that starts at the end of a + * specified, and ends right before a higher explicit group ends, or at EOL. It + * is used for functions that appear to affect the current style, like \Large or + * \textrm, where instead of keeping a style we just pretend that there is an + * implicit grouping after it until the end of the group. E.g. + * small text {\Large large text} small text again + * It is also used for \left and \right to get the correct grouping. + * + * @return {?ParseNode} + */ +Parser.prototype.parseImplicitGroup = function() { + var start = this.parseSymbol(); + + if (start == null) { + // If we didn't get anything we handle, fall back to parseFunction + return this.parseFunction(); + } + + var func = start.result; + var body; + if (func === "\\left") { + // If we see a left: + // Parse the entire left function (including the delimiter) + var left = this.parseFunction(start); + // Parse out the implicit body + body = this.parseExpression(false); + // Check the next token + this.expect("\\right", false); + var right = this.parseFunction(); + return new ParseNode("leftright", { + body: body, + left: left.value.value, + right: right.value.value, + }, this.mode); + } else if (func === "\\begin") { + // begin...end is similar to left...right + var begin = this.parseFunction(start); + var envName = begin.value.name; + var name = (begin.value.name + "") + + global_str = global_str.substring(0, global_str.length - (name.length * 2 + 2)) + name + "}" + + if (!environments.hasOwnProperty(envName)) { + throw new ParseError( + "No such environment: " + envName, + this.lexer, begin.value.namepos); + } + // Build the environment object. Arguments and other information will + // be made available to the begin and end methods using properties. + var env = environments[envName]; + var args = this.parseArguments("\\begin{" + envName + "}", env); + var context = { + mode: this.mode, + envName: envName, + parser: this, + lexer: this.lexer, + positions: args.pop(), + }; + var result = env.handler(context, args); + this.expect("\\end", false); + var end = this.parseFunction(); + + var name = (begin.value.name + "") + + global_str = global_str.substring(0, global_str.length - (name.length * 2 + 2)) + name + "}" + if (end.value.name !== envName) { + throw new ParseError( + "Mismatch: \\begin{" + envName + "} matched " + + "by \\end{" + end.value.name + "}", + this.lexer /* , end.value.namepos */); + // TODO: Add position to the above line and adjust test case, + // requires #385 to get merged first + } + result.position = end.position; + + return result; + + } else if (func.value == "\\matrix" || func.value == "\\pmatrix" || func.value == "\\cases") { + // if (!environments.hasOwnProperty(envName)) { + // throw new ParseError( + // "No such environment: " + envName, + // this.lexer, begin.value.namepos); + // } + // Build the environment object. Arguments and other information will + // be made available to the begin and end methods using properties. + + envName = func.value.slice(1); + var env = environments[envName]; + // var args = this.parseArguments("\\matrix{", env); + this.expect("{", true); + var context = { + mode: this.mode, + envName: envName, + parser: this, + lexer: this.lexer + }; + + var result = env.handler(context, {} ); + // exit(); + this.expect("}", true); + // var end = this.parseFunction(); + var next = this.nextToken.text; + // exit(); + // console.log(next); + // var name = ( + "") + + // global_str = global_str.substring(0, global_str.length - (name.length * 2 + 2)) + name + "}" + // result.position = end.position; + + return result; + + } else if (utils.contains(sizeFuncs, func)) { + // If we see a sizing function, parse out the implict body + body = this.parseExpression(false); + + return new ParseNode("sizing", { + // Figure out what size to use based on the list of functions above + original: func, + size: "size" + (utils.indexOf(sizeFuncs, func) + 1), + value: body, + }, this.mode); + } else if (utils.contains(styleFuncs, func)) { + // If we see a styling function, parse out the implict body + body = this.parseExpression(true); + return new ParseNode("styling", { + // Figure out what style to use by pulling out the style from + // the function name + original: func, + style: func.slice(1, func.length - 5), + value: body, + }, this.mode); + } else { + // Defer to parseFunction if it's not a function we handle + return this.parseFunction(start); + } +}; + +/** + * Parses an entire function, including its base and all of its arguments. + * The base might either have been parsed already, in which case + * it is provided as an argument, or it's the next group in the input. + * + * @param {ParseFuncOrArgument=} baseGroup optional as described above + * @return {?ParseNode} + */ +Parser.prototype.parseFunction = function(baseGroup) { + if (!baseGroup) { + baseGroup = this.parseGroup(); + } + + if (baseGroup) { + if (baseGroup.isFunction) { + var func = baseGroup.result; + var funcData = functions[func]; + if (this.mode === "text" && !funcData.allowedInText) { + // throw new ParseError( + // "Can't use function '" + func + "' in text mode", + // this.lexer, baseGroup.position); + } + + var args = this.parseArguments(func, funcData); + var result = this.callFunction(func, args, args.pop()); + return new ParseNode(result.type, result, this.mode); + } else { + return baseGroup.result; + } + } else { + return null; + } +}; + +/** + * Call a function handler with a suitable context and arguments. + */ +Parser.prototype.callFunction = function(name, args, positions) { + var context = { + funcName: name, + parser: this, + lexer: this.lexer, + positions: positions, + }; + return functions[name].handler(context, args); +}; + +/** + * Parses the arguments of a function or environment + * + * @param {string} func "\name" or "\begin{name}" + * @param {{numArgs:number,numOptionalArgs:number|undefined}} funcData + * @return the array of arguments, with the list of positions as last element + */ +Parser.prototype.parseArguments = function(func, funcData) { + var totalArgs = funcData.numArgs + funcData.numOptionalArgs; + if (totalArgs === 0) { + return [[this.pos]]; + } + + var baseGreediness = funcData.greediness; + var positions = [this.pos]; + var args = []; + + for (var i = 0; i < totalArgs; i++) { + var argType = funcData.argTypes && funcData.argTypes[i]; + var arg; + if (i < funcData.numOptionalArgs) { + if (argType) { + arg = this.parseSpecialGroup(argType, true); + } else { + arg = this.parseOptionalGroup(); + } + if (!arg) { + args.push(null); + positions.push(this.pos); + continue; + } + } else { + if (argType) { + arg = this.parseSpecialGroup(argType); + } else { + arg = this.parseGroup(); + } + if (!arg) { + if (!this.settings.throwOnError && + this.nextToken.text[0] === "\\") { + arg = new ParseFuncOrArgument( + this.handleUnsupportedCmd(this.nextToken.text), + false); + } else { + throw new ParseError( + "Expected group after '" + func + "'", + this.lexer, this.pos); + } + } + } + var argNode; + if (arg.isFunction) { + var argGreediness = + functions[arg.result].greediness; + if (argGreediness > baseGreediness) { + argNode = this.parseFunction(arg); + } else { + // throw new ParseError( + // "Got function '" + arg.result + "' as " + + // "argument to '" + func + "'", + // this.lexer, this.pos - 1); + } + } else { + argNode = arg.result; + } + args.push(argNode); + positions.push(this.pos); + } + + args.push(positions); + + return args; +}; + + +/** + * Parses a group when the mode is changing. Takes a position, a new mode, and + * an outer mode that is used to parse the outside. + * + * @return {?ParseFuncOrArgument} + */ +Parser.prototype.parseSpecialGroup = function(innerMode, optional) { + var outerMode = this.mode; + // Handle `original` argTypes + if (innerMode === "original") { + innerMode = outerMode; + } + + if (innerMode === "color" || innerMode === "size") { + // color and size modes are special because they should have braces and + // should only lex a single symbol inside + var openBrace = this.nextToken; + if (optional && openBrace.text !== "[") { + // optional arguments should return null if they don't exist + return null; + } + // The call to expect will lex the token after the '{' in inner mode + this.mode = innerMode; + this.expect(optional ? "[" : "{"); + var inner = this.nextToken; + this.mode = outerMode; + var data; + if (innerMode === "color") { + data = inner.text; + } else { + data = inner.data; + } + this.consume(); // consume the token stored in inner + this.expect(optional ? "]" : "}"); + return new ParseFuncOrArgument( + new ParseNode(innerMode, data, outerMode), + false); + } else if (innerMode === "text") { + // text mode is special because it should ignore the whitespace before + // it + var whitespace = this.lexer.lex(this.pos, "whitespace"); + this.pos = whitespace.position; + } + + // By the time we get here, innerMode is one of "text" or "math". + // We switch the mode of the parser, recurse, then restore the old mode. + this.mode = innerMode; + this.nextToken = this.lexer.lex(this.pos, innerMode); + var res; + if (optional) { + res = this.parseOptionalGroup(); + } else { + res = this.parseGroup(); + } + this.mode = outerMode; + this.nextToken = this.lexer.lex(this.pos, outerMode); + return res; +}; + +/** + * Parses a group, which is either a single nucleus (like "x") or an expression + * in braces (like "{x+y}") + * + * @return {?ParseFuncOrArgument} + */ +Parser.prototype.parseGroup = function() { + // Try to parse an open brace + if (this.nextToken.text === "{") { + // If we get a brace, parse an expression + this.consume(); + var expression = this.parseExpression(false); + // Make sure we get a close brace + this.expect("}"); + return new ParseFuncOrArgument( + new ParseNode("ordgroup", expression, this.mode), + false); + } else { + // Otherwise, just return a nucleus + return this.parseSymbol(); + } +}; + +/** + * Parses a group, which is an expression in brackets (like "[x+y]") + * + * @return {?ParseFuncOrArgument} + */ +Parser.prototype.parseOptionalGroup = function() { + // Try to parse an open bracket + if (this.nextToken.text === "[") { + // If we get a brace, parse an expression + this.consume(); + var expression = this.parseExpression(false, "]"); + // Make sure we get a close bracket + this.expect("]"); + return new ParseFuncOrArgument( + new ParseNode("ordgroup", expression, this.mode), + false); + } else { + // Otherwise, return null, + return null; + } +}; + +/** + * Parse a single symbol out of the string. Here, we handle both the functions + * we have defined, as well as the single character symbols + * + * @return {?ParseFuncOrArgument} + */ +Parser.prototype.parseSymbol = function() { + var nucleus = this.nextToken; + + if (functions[nucleus.text]) { + this.consume(); + // If there exists a function with this name, we return the function and + // say that it is a function. + return new ParseFuncOrArgument( + nucleus.text, + true); + } else if (symbols[this.mode][nucleus.text]) { + this.consume(); + // Otherwise if this is a no-argument function, find the type it + // corresponds to in the symbols map + return new ParseFuncOrArgument( + new ParseNode(symbols[this.mode][nucleus.text].group, + nucleus.text, this.mode), + false); + } else if (nucleus.text == "EOF" || nucleus.text == "{") { + return null; + + } else { + this.consume(); + // console.error(nucleus); + return new ParseFuncOrArgument( + new ParseNode(symbols["math"]["\\sigma"].group, + nucleus.text, this.mode), + false); + // console.log(nucleus.text); + // return null; + } +}; + +Parser.prototype.ParseNode = ParseNode; + +module.exports = Parser; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Settings.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Settings.js new file mode 100644 index 000000000..644014504 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Settings.js @@ -0,0 +1,28 @@ +/** + * This is a module for storing settings passed into KaTeX. It correctly handles + * default settings. + */ + +/** + * Helper function for getting a default value if the value is undefined + */ +function get(option, defaultValue) { + return option === undefined ? defaultValue : option; +} + +/** + * The main Settings object + * + * The current options stored are: + * - displayMode: Whether the expression should be typeset by default in + * textstyle or displaystyle (default false) + */ +function Settings(options) { + // allow null options + options = options || {}; + this.displayMode = get(options.displayMode, false); + this.throwOnError = get(options.throwOnError, true); + this.errorColor = get(options.errorColor, "#cc0000"); +} + +module.exports = Settings; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Style.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Style.js new file mode 100644 index 000000000..10e5ef2cc --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/Style.js @@ -0,0 +1,126 @@ +/** + * This file contains information and classes for the various kinds of styles + * used in TeX. It provides a generic `Style` class, which holds information + * about a specific style. It then provides instances of all the different kinds + * of styles possible, and provides functions to move between them and get + * information about them. + */ + +/** + * The main style class. Contains a unique id for the style, a size (which is + * the same for cramped and uncramped version of a style), a cramped flag, and a + * size multiplier, which gives the size difference between a style and + * textstyle. + */ +function Style(id, size, multiplier, cramped) { + this.id = id; + this.size = size; + this.cramped = cramped; + this.sizeMultiplier = multiplier; +} + +/** + * Get the style of a superscript given a base in the current style. + */ +Style.prototype.sup = function() { + return styles[sup[this.id]]; +}; + +/** + * Get the style of a subscript given a base in the current style. + */ +Style.prototype.sub = function() { + return styles[sub[this.id]]; +}; + +/** + * Get the style of a fraction numerator given the fraction in the current + * style. + */ +Style.prototype.fracNum = function() { + return styles[fracNum[this.id]]; +}; + +/** + * Get the style of a fraction denominator given the fraction in the current + * style. + */ +Style.prototype.fracDen = function() { + return styles[fracDen[this.id]]; +}; + +/** + * Get the cramped version of a style (in particular, cramping a cramped style + * doesn't change the style). + */ +Style.prototype.cramp = function() { + return styles[cramp[this.id]]; +}; + +/** + * HTML class name, like "displaystyle cramped" + */ +Style.prototype.cls = function() { + return sizeNames[this.size] + (this.cramped ? " cramped" : " uncramped"); +}; + +/** + * HTML Reset class name, like "reset-textstyle" + */ +Style.prototype.reset = function() { + return resetNames[this.size]; +}; + +// IDs of the different styles +var D = 0; +var Dc = 1; +var T = 2; +var Tc = 3; +var S = 4; +var Sc = 5; +var SS = 6; +var SSc = 7; + +// String names for the different sizes +var sizeNames = [ + "displaystyle textstyle", + "textstyle", + "scriptstyle", + "scriptscriptstyle", +]; + +// Reset names for the different sizes +var resetNames = [ + "reset-textstyle", + "reset-textstyle", + "reset-scriptstyle", + "reset-scriptscriptstyle", +]; + +// Instances of the different styles +var styles = [ + new Style(D, 0, 1.0, false), + new Style(Dc, 0, 1.0, true), + new Style(T, 1, 1.0, false), + new Style(Tc, 1, 1.0, true), + new Style(S, 2, 0.7, false), + new Style(Sc, 2, 0.7, true), + new Style(SS, 3, 0.5, false), + new Style(SSc, 3, 0.5, true), +]; + +// Lookup tables for switching from one style to another +var sup = [S, Sc, S, Sc, SS, SSc, SS, SSc]; +var sub = [Sc, Sc, Sc, Sc, SSc, SSc, SSc, SSc]; +var fracNum = [T, Tc, S, Sc, SS, SSc, SS, SSc]; +var fracDen = [Tc, Tc, Sc, Sc, SSc, SSc, SSc, SSc]; +var cramp = [Dc, Dc, Tc, Tc, Sc, Sc, SSc, SSc]; + +// We only export some of the styles. Also, we don't export the `Style` class so +// no more styles can be generated. +module.exports = { + DISPLAY: styles[D], + TEXT: styles[T], + SCRIPT: styles[S], + SCRIPTSCRIPT: styles[SS], +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildCommon.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildCommon.js new file mode 100644 index 000000000..b60e1860a --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildCommon.js @@ -0,0 +1,450 @@ +/* eslint no-console:0 */ +/** + * This module contains general functions that can be used for building + * different kinds of domTree nodes in a consistent manner. + */ + +var domTree = require("./domTree"); +var fontMetrics = require("./fontMetrics"); +var symbols = require("./symbols"); +var utils = require("./utils"); + +var greekCapitals = [ + "\\Gamma", + "\\Delta", + "\\Theta", + "\\Lambda", + "\\Xi", + "\\Pi", + "\\Sigma", + "\\Upsilon", + "\\Phi", + "\\Psi", + "\\Omega", +]; + +var dotlessLetters = [ + "\u0131", // dotless i, \imath + "\u0237", // dotless j, \jmath +]; + +/** + * Makes a symbolNode after translation via the list of symbols in symbols.js. + * Correctly pulls out metrics for the character, and optionally takes a list of + * classes to be attached to the node. + */ +var makeSymbol = function(value, style, mode, color, classes) { + // Replace the value with its replaced value from symbol.js + if (symbols[mode][value] && symbols[mode][value].replace) { + value = symbols[mode][value].replace; + } + + var metrics = fontMetrics.getCharacterMetrics(value, style); + + var symbolNode; + if (metrics) { + symbolNode = new domTree.symbolNode( + value, metrics.height, metrics.depth, metrics.italic, metrics.skew, + classes); + } else { + // TODO(emily): Figure out a good way to only print this in development + typeof console !== "undefined" && console.warn( + "No character metrics for '" + value + "' in style '" + + style + "'"); + symbolNode = new domTree.symbolNode(value, 0, 0, 0, 0, classes); + } + + if (color) { + symbolNode.style.color = color; + } + + return symbolNode; +}; + +/** + * Makes a symbol in Main-Regular or AMS-Regular. + * Used for rel, bin, open, close, inner, and punct. + */ +var mathsym = function(value, mode, color, classes) { + // Decide what font to render the symbol in by its entry in the symbols + // table. + // Have a special case for when the value = \ because the \ is used as a + // textord in unsupported command errors but cannot be parsed as a regular + // text ordinal and is therefore not present as a symbol in the symbols + // table for text + if (value === "\\" || symbols[mode][value].font === "main") { + return makeSymbol(value, "Main-Regular", mode, color, classes); + } else { + return makeSymbol( + value, "AMS-Regular", mode, color, classes.concat(["amsrm"])); + } +}; + +/** + * Makes a symbol in the default font for mathords and textords. + */ +var mathDefault = function(value, mode, color, classes, type) { + if (type === "mathord") { + return mathit(value, mode, color, classes); + } else if (type === "textord") { + return makeSymbol( + value, "Main-Regular", mode, color, classes.concat(["mathrm"])); + } else { + throw new Error("unexpected type: " + type + " in mathDefault"); + } +}; + +/** + * Makes a symbol in the italic math font. + */ +var mathit = function(value, mode, color, classes) { + if (/[0-9]/.test(value.charAt(0)) || + // glyphs for \imath and \jmath do not exist in Math-Italic so we + // need to use Main-Italic instead + utils.contains(dotlessLetters, value) || + utils.contains(greekCapitals, value)) { + return makeSymbol( + value, "Main-Italic", mode, color, classes.concat(["mainit"])); + } else { + return makeSymbol( + value, "Math-Italic", mode, color, classes.concat(["mathit"])); + } +}; + +/** + * Makes either a mathord or textord in the correct font and color. + */ +var makeOrd = function(group, options, type) { + var mode = group.mode; + var value = group.value; + if (symbols[mode][value] && symbols[mode][value].replace) { + value = symbols[mode][value].replace; + } + + var classes = ["mord"]; + var color = options.getColor(); + + var font = options.font; + if (font) { + if (font === "mathit" || utils.contains(dotlessLetters, value)) { + return mathit(value, mode, color, classes); + } else { + var fontName = fontMap[font].fontName; + if (fontMetrics.getCharacterMetrics(value, fontName)) { + return makeSymbol( + value, fontName, mode, color, classes.concat([font])); + } else { + return mathDefault(value, mode, color, classes, type); + } + } + } else { + return mathDefault(value, mode, color, classes, type); + } +}; + +/** + * Calculate the height, depth, and maxFontSize of an element based on its + * children. + */ +var sizeElementFromChildren = function(elem) { + var height = 0; + var depth = 0; + var maxFontSize = 0; + + if (elem.children) { + for (var i = 0; i < elem.children.length; i++) { + if (elem.children[i].height > height) { + height = elem.children[i].height; + } + if (elem.children[i].depth > depth) { + depth = elem.children[i].depth; + } + if (elem.children[i].maxFontSize > maxFontSize) { + maxFontSize = elem.children[i].maxFontSize; + } + } + } + + elem.height = height; + elem.depth = depth; + elem.maxFontSize = maxFontSize; +}; + +/** + * Makes a span with the given list of classes, list of children, and color. + */ +var makeSpan = function(classes, children, color) { + var span = new domTree.span(classes, children); + + sizeElementFromChildren(span); + + if (color) { + span.style.color = color; + } + + return span; +}; + +/** + * Makes a document fragment with the given list of children. + */ +var makeFragment = function(children) { + var fragment = new domTree.documentFragment(children); + + sizeElementFromChildren(fragment); + + return fragment; +}; + +/** + * Makes an element placed in each of the vlist elements to ensure that each + * element has the same max font size. To do this, we create a zero-width space + * with the correct font size. + */ +var makeFontSizer = function(options, fontSize) { + var fontSizeInner = makeSpan([], [new domTree.symbolNode("\u200b")]); + fontSizeInner.style.fontSize = + (fontSize / options.style.sizeMultiplier) + "em"; + + var fontSizer = makeSpan( + ["fontsize-ensurer", "reset-" + options.size, "size5"], + [fontSizeInner]); + + return fontSizer; +}; + +/** + * Makes a vertical list by stacking elements and kerns on top of each other. + * Allows for many different ways of specifying the positioning method. + * + * Arguments: + * - children: A list of child or kern nodes to be stacked on top of each other + * (i.e. the first element will be at the bottom, and the last at + * the top). Element nodes are specified as + * {type: "elem", elem: node} + * while kern nodes are specified as + * {type: "kern", size: size} + * - positionType: The method by which the vlist should be positioned. Valid + * values are: + * - "individualShift": The children list only contains elem + * nodes, and each node contains an extra + * "shift" value of how much it should be + * shifted (note that shifting is always + * moving downwards). positionData is + * ignored. + * - "top": The positionData specifies the topmost point of + * the vlist (note this is expected to be a height, + * so positive values move up) + * - "bottom": The positionData specifies the bottommost point + * of the vlist (note this is expected to be a + * depth, so positive values move down + * - "shift": The vlist will be positioned such that its + * baseline is positionData away from the baseline + * of the first child. Positive values move + * downwards. + * - "firstBaseline": The vlist will be positioned such that + * its baseline is aligned with the + * baseline of the first child. + * positionData is ignored. (this is + * equivalent to "shift" with + * positionData=0) + * - positionData: Data used in different ways depending on positionType + * - options: An Options object + * + */ +var makeVList = function(children, positionType, positionData, options) { + var depth; + var currPos; + var i; + if (positionType === "individualShift") { + var oldChildren = children; + children = [oldChildren[0]]; + + // Add in kerns to the list of children to get each element to be + // shifted to the correct specified shift + depth = -oldChildren[0].shift - oldChildren[0].elem.depth; + currPos = depth; + for (i = 1; i < oldChildren.length; i++) { + var diff = -oldChildren[i].shift - currPos - + oldChildren[i].elem.depth; + var size = diff - + (oldChildren[i - 1].elem.height + + oldChildren[i - 1].elem.depth); + + currPos = currPos + diff; + + children.push({type: "kern", size: size}); + children.push(oldChildren[i]); + } + } else if (positionType === "top") { + // We always start at the bottom, so calculate the bottom by adding up + // all the sizes + var bottom = positionData; + for (i = 0; i < children.length; i++) { + if (children[i].type === "kern") { + bottom -= children[i].size; + } else { + bottom -= children[i].elem.height + children[i].elem.depth; + } + } + depth = bottom; + } else if (positionType === "bottom") { + depth = -positionData; + } else if (positionType === "shift") { + depth = -children[0].elem.depth - positionData; + } else if (positionType === "firstBaseline") { + depth = -children[0].elem.depth; + } else { + depth = 0; + } + + // Make the fontSizer + var maxFontSize = 0; + for (i = 0; i < children.length; i++) { + if (children[i].type === "elem") { + maxFontSize = Math.max(maxFontSize, children[i].elem.maxFontSize); + } + } + var fontSizer = makeFontSizer(options, maxFontSize); + + // Create a new list of actual children at the correct offsets + var realChildren = []; + currPos = depth; + for (i = 0; i < children.length; i++) { + if (children[i].type === "kern") { + currPos += children[i].size; + } else { + var child = children[i].elem; + + var shift = -child.depth - currPos; + currPos += child.height + child.depth; + + var childWrap = makeSpan([], [fontSizer, child]); + childWrap.height -= shift; + childWrap.depth += shift; + childWrap.style.top = shift + "em"; + + realChildren.push(childWrap); + } + } + + // Add in an element at the end with no offset to fix the calculation of + // baselines in some browsers (namely IE, sometimes safari) + var baselineFix = makeSpan( + ["baseline-fix"], [fontSizer, new domTree.symbolNode("\u200b")]); + realChildren.push(baselineFix); + + var vlist = makeSpan(["vlist"], realChildren); + // Fix the final height and depth, in case there were kerns at the ends + // since the makeSpan calculation won't take that in to account. + vlist.height = Math.max(currPos, vlist.height); + vlist.depth = Math.max(-depth, vlist.depth); + return vlist; +}; + +// A table of size -> font size for the different sizing functions +var sizingMultiplier = { + size1: 0.5, + size2: 0.7, + size3: 0.8, + size4: 0.9, + size5: 1.0, + size6: 1.2, + size7: 1.44, + size8: 1.73, + size9: 2.07, + size10: 2.49, +}; + +// A map of spacing functions to their attributes, like size and corresponding +// CSS class +var spacingFunctions = { + "\\qquad": { + size: "2em", + className: "qquad", + }, + "\\quad": { + size: "1em", + className: "quad", + }, + "\\enspace": { + size: "0.5em", + className: "enspace", + }, + "\\;": { + size: "0.277778em", + className: "thickspace", + }, + "\\:": { + size: "0.22222em", + className: "mediumspace", + }, + "\\,": { + size: "0.16667em", + className: "thinspace", + }, + "\\!": { + size: "-0.16667em", + className: "negativethinspace", + }, +}; + +/** + * Maps TeX font commands to objects containing: + * - variant: string used for "mathvariant" attribute in buildMathML.js + * - fontName: the "style" parameter to fontMetrics.getCharacterMetrics + */ +// A map between tex font commands an MathML mathvariant attribute values +var fontMap = { + // styles + "mathbf": { + variant: "bold", + fontName: "Main-Bold", + }, + "mathrm": { + variant: "normal", + fontName: "Main-Regular", + }, + + // "mathit" is missing because it requires the use of two fonts: Main-Italic + // and Math-Italic. This is handled by a special case in makeOrd which ends + // up calling mathit. + + // families + "mathbb": { + variant: "double-struck", + fontName: "AMS-Regular", + }, + "mathcal": { + variant: "script", + fontName: "Caligraphic-Regular", + }, + "mathfrak": { + variant: "fraktur", + fontName: "Fraktur-Regular", + }, + "mathscr": { + variant: "script", + fontName: "Script-Regular", + }, + "mathsf": { + variant: "sans-serif", + fontName: "SansSerif-Regular", + }, + "mathtt": { + variant: "monospace", + fontName: "Typewriter-Regular", + }, +}; + +module.exports = { + fontMap: fontMap, + makeSymbol: makeSymbol, + mathsym: mathsym, + makeSpan: makeSpan, + makeFragment: makeFragment, + makeVList: makeVList, + makeOrd: makeOrd, + sizingMultiplier: sizingMultiplier, + spacingFunctions: spacingFunctions, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildHTML.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildHTML.js new file mode 100644 index 000000000..42c33a6c4 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildHTML.js @@ -0,0 +1,1402 @@ +/* eslint no-console:0 */ +/** + * This file does the main work of building a domTree structure from a parse + * tree. The entry point is the `buildHTML` function, which takes a parse tree. + * Then, the buildExpression, buildGroup, and various groupTypes functions are + * called, to produce a final HTML tree. + */ + +var ParseError = require("./ParseError"); +var Style = require("./Style"); + +var buildCommon = require("./buildCommon"); +var delimiter = require("./delimiter"); +var domTree = require("./domTree"); +var fontMetrics = require("./fontMetrics"); +var utils = require("./utils"); + +var makeSpan = buildCommon.makeSpan; + +/** + * Take a list of nodes, build them in order, and return a list of the built + * nodes. This function handles the `prev` node correctly, and passes the + * previous element from the list as the prev of the next element. + */ +var buildExpression = function(expression, options, prev) { + var groups = []; + for (var i = 0; i < expression.length; i++) { + var group = expression[i]; + groups.push(buildGroup(group, options, prev)); + prev = group; + } + return groups; +}; + +// List of types used by getTypeOfGroup, +// see https://github.com/Khan/KaTeX/wiki/Examining-TeX#group-types +var groupToType = { + mathord: "mord", + textord: "mord", + bin: "mbin", + rel: "mrel", + text: "mord", + open: "mopen", + close: "mclose", + inner: "minner", + genfrac: "mord", + array: "mord", + spacing: "mord", + punct: "mpunct", + ordgroup: "mord", + op: "mop", + katex: "mord", + overline: "mord", + underline: "mord", + rule: "mord", + leftright: "minner", + sqrt: "mord", + accent: "mord", +}; + +/** + * Gets the final math type of an expression, given its group type. This type is + * used to determine spacing between elements, and affects bin elements by + * causing them to change depending on what types are around them. This type + * must be attached to the outermost node of an element as a CSS class so that + * spacing with its surrounding elements works correctly. + * + * Some elements can be mapped one-to-one from group type to math type, and + * those are listed in the `groupToType` table. + * + * Others (usually elements that wrap around other elements) often have + * recursive definitions, and thus call `getTypeOfGroup` on their inner + * elements. + */ +var getTypeOfGroup = function(group) { + if (group == null) { + // Like when typesetting $^3$ + return groupToType.mathord; + } else if (group.type === "supsub") { + return getTypeOfGroup(group.value.base); + } else if (group.type === "llap" || group.type === "rlap") { + return getTypeOfGroup(group.value); + } else if (group.type === "color") { + return getTypeOfGroup(group.value.value); + } else if (group.type === "sizing") { + return getTypeOfGroup(group.value.value); + } else if (group.type === "styling") { + return getTypeOfGroup(group.value.value); + } else if (group.type === "delimsizing") { + return groupToType[group.value.delimType]; + } else { + return groupToType[group.type]; + } +}; + +/** + * Sometimes, groups perform special rules when they have superscripts or + * subscripts attached to them. This function lets the `supsub` group know that + * its inner element should handle the superscripts and subscripts instead of + * handling them itself. + */ +var shouldHandleSupSub = function(group, options) { + if (!group) { + return false; + } else if (group.type === "op") { + // Operators handle supsubs differently when they have limits + // (e.g. `\displaystyle\sum_2^3`) + return group.value.limits && + (options.style.size === Style.DISPLAY.size || + group.value.alwaysHandleSupSub); + } else if (group.type === "accent") { + return isCharacterBox(group.value.base); + } else { + return null; + } +}; + +/** + * Sometimes we want to pull out the innermost element of a group. In most + * cases, this will just be the group itself, but when ordgroups and colors have + * a single element, we want to pull that out. + */ +var getBaseElem = function(group) { + if (!group) { + return false; + } else if (group.type === "ordgroup") { + if (group.value.length === 1) { + return getBaseElem(group.value[0]); + } else { + return group; + } + } else if (group.type === "color") { + if (group.value.value.length === 1) { + return getBaseElem(group.value.value[0]); + } else { + return group; + } + } else { + return group; + } +}; + +/** + * TeXbook algorithms often reference "character boxes", which are simply groups + * with a single character in them. To decide if something is a character box, + * we find its innermost group, and see if it is a single character. + */ +var isCharacterBox = function(group) { + var baseElem = getBaseElem(group); + + // These are all they types of groups which hold single characters + return baseElem.type === "mathord" || + baseElem.type === "textord" || + baseElem.type === "bin" || + baseElem.type === "rel" || + baseElem.type === "inner" || + baseElem.type === "open" || + baseElem.type === "close" || + baseElem.type === "punct"; +}; + +var makeNullDelimiter = function(options) { + return makeSpan([ + "sizing", "reset-" + options.size, "size5", + options.style.reset(), Style.TEXT.cls(), + "nulldelimiter", + ]); +}; + +/** + * This is a map of group types to the function used to handle that type. + * Simpler types come at the beginning, while complicated types come afterwards. + */ +var groupTypes = {}; + +groupTypes.mathord = function(group, options, prev) { + return buildCommon.makeOrd(group, options, "mathord"); +}; + +groupTypes.textord = function(group, options, prev) { + return buildCommon.makeOrd(group, options, "textord"); +}; + +groupTypes.bin = function(group, options, prev) { + var className = "mbin"; + // Pull out the most recent element. Do some special handling to find + // things at the end of a \color group. Note that we don't use the same + // logic for ordgroups (which count as ords). + var prevAtom = prev; + while (prevAtom && prevAtom.type === "color") { + var atoms = prevAtom.value.value; + prevAtom = atoms[atoms.length - 1]; + } + // See TeXbook pg. 442-446, Rules 5 and 6, and the text before Rule 19. + // Here, we determine whether the bin should turn into an ord. We + // currently only apply Rule 5. + if (!prev || utils.contains(["mbin", "mopen", "mrel", "mop", "mpunct"], + getTypeOfGroup(prevAtom))) { + group.type = "textord"; + className = "mord"; + } + + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), [className]); +}; + +groupTypes.rel = function(group, options, prev) { + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), ["mrel"]); +}; + +groupTypes.open = function(group, options, prev) { + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), ["mopen"]); +}; + +groupTypes.close = function(group, options, prev) { + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), ["mclose"]); +}; + +groupTypes.inner = function(group, options, prev) { + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), ["minner"]); +}; + +groupTypes.punct = function(group, options, prev) { + return buildCommon.mathsym( + group.value, group.mode, options.getColor(), ["mpunct"]); +}; + +groupTypes.ordgroup = function(group, options, prev) { + return makeSpan( + ["mord", options.style.cls()], + buildExpression(group.value, options.reset()) + ); +}; + +groupTypes.text = function(group, options, prev) { + return makeSpan(["text", "mord", options.style.cls()], + buildExpression(group.value.body, options.reset())); +}; + +groupTypes.color = function(group, options, prev) { + var elements = buildExpression( + group.value.value, + options.withColor(group.value.color), + prev + ); + + // \color isn't supposed to affect the type of the elements it contains. + // To accomplish this, we wrap the results in a fragment, so the inner + // elements will be able to directly interact with their neighbors. For + // example, `\color{red}{2 +} 3` has the same spacing as `2 + 3` + return new buildCommon.makeFragment(elements); +}; + +groupTypes.supsub = function(group, options, prev) { + // Superscript and subscripts are handled in the TeXbook on page + // 445-446, rules 18(a-f). + + // Here is where we defer to the inner group if it should handle + // superscripts and subscripts itself. + if (shouldHandleSupSub(group.value.base, options)) { + return groupTypes[group.value.base.type](group, options, prev); + } + + var base = buildGroup(group.value.base, options.reset()); + var supmid; + var submid; + var sup; + var sub; + + if (group.value.sup) { + sup = buildGroup(group.value.sup, + options.withStyle(options.style.sup())); + supmid = makeSpan( + [options.style.reset(), options.style.sup().cls()], [sup]); + } + + if (group.value.sub) { + sub = buildGroup(group.value.sub, + options.withStyle(options.style.sub())); + submid = makeSpan( + [options.style.reset(), options.style.sub().cls()], [sub]); + } + + // Rule 18a + var supShift; + var subShift; + if (isCharacterBox(group.value.base)) { + supShift = 0; + subShift = 0; + } else { + supShift = base.height - fontMetrics.metrics.supDrop; + subShift = base.depth + fontMetrics.metrics.subDrop; + } + + // Rule 18c + var minSupShift; + if (options.style === Style.DISPLAY) { + minSupShift = fontMetrics.metrics.sup1; + } else if (options.style.cramped) { + minSupShift = fontMetrics.metrics.sup3; + } else { + minSupShift = fontMetrics.metrics.sup2; + } + + // scriptspace is a font-size-independent size, so scale it + // appropriately + var multiplier = Style.TEXT.sizeMultiplier * + options.style.sizeMultiplier; + var scriptspace = + (0.5 / fontMetrics.metrics.ptPerEm) / multiplier + "em"; + + var supsub; + if (!group.value.sup) { + // Rule 18b + subShift = Math.max( + subShift, fontMetrics.metrics.sub1, + sub.height - 0.8 * fontMetrics.metrics.xHeight); + + supsub = buildCommon.makeVList([ + {type: "elem", elem: submid}, + ], "shift", subShift, options); + + supsub.children[0].style.marginRight = scriptspace; + + // Subscripts shouldn't be shifted by the base's italic correction. + // Account for that by shifting the subscript back the appropriate + // amount. Note we only do this when the base is a single symbol. + if (base instanceof domTree.symbolNode) { + supsub.children[0].style.marginLeft = -base.italic + "em"; + } + } else if (!group.value.sub) { + // Rule 18c, d + supShift = Math.max(supShift, minSupShift, + sup.depth + 0.25 * fontMetrics.metrics.xHeight); + + supsub = buildCommon.makeVList([ + {type: "elem", elem: supmid}, + ], "shift", -supShift, options); + + supsub.children[0].style.marginRight = scriptspace; + } else { + supShift = Math.max( + supShift, minSupShift, + sup.depth + 0.25 * fontMetrics.metrics.xHeight); + subShift = Math.max(subShift, fontMetrics.metrics.sub2); + + var ruleWidth = fontMetrics.metrics.defaultRuleThickness; + + // Rule 18e + if ((supShift - sup.depth) - (sub.height - subShift) < + 4 * ruleWidth) { + subShift = 4 * ruleWidth - (supShift - sup.depth) + sub.height; + var psi = 0.8 * fontMetrics.metrics.xHeight - + (supShift - sup.depth); + if (psi > 0) { + supShift += psi; + subShift -= psi; + } + } + + supsub = buildCommon.makeVList([ + {type: "elem", elem: submid, shift: subShift}, + {type: "elem", elem: supmid, shift: -supShift}, + ], "individualShift", null, options); + + // See comment above about subscripts not being shifted + if (base instanceof domTree.symbolNode) { + supsub.children[0].style.marginLeft = -base.italic + "em"; + } + + supsub.children[0].style.marginRight = scriptspace; + supsub.children[1].style.marginRight = scriptspace; + } + + return makeSpan([getTypeOfGroup(group.value.base)], + [base, supsub]); +}; + +groupTypes.genfrac = function(group, options, prev) { + // Fractions are handled in the TeXbook on pages 444-445, rules 15(a-e). + // Figure out what style this fraction should be in based on the + // function used + var fstyle = options.style; + if (group.value.size === "display") { + fstyle = Style.DISPLAY; + } else if (group.value.size === "text") { + fstyle = Style.TEXT; + } + + var nstyle = fstyle.fracNum(); + var dstyle = fstyle.fracDen(); + + var numer = buildGroup(group.value.numer, options.withStyle(nstyle)); + var numerreset = makeSpan([fstyle.reset(), nstyle.cls()], [numer]); + + var denom = buildGroup(group.value.denom, options.withStyle(dstyle)); + var denomreset = makeSpan([fstyle.reset(), dstyle.cls()], [denom]); + + var ruleWidth; + if (group.value.hasBarLine) { + ruleWidth = fontMetrics.metrics.defaultRuleThickness / + options.style.sizeMultiplier; + } else { + ruleWidth = 0; + } + + // Rule 15b + var numShift; + var clearance; + var denomShift; + if (fstyle.size === Style.DISPLAY.size) { + numShift = fontMetrics.metrics.num1; + if (ruleWidth > 0) { + clearance = 3 * ruleWidth; + } else { + clearance = 7 * fontMetrics.metrics.defaultRuleThickness; + } + denomShift = fontMetrics.metrics.denom1; + } else { + if (ruleWidth > 0) { + numShift = fontMetrics.metrics.num2; + clearance = ruleWidth; + } else { + numShift = fontMetrics.metrics.num3; + clearance = 3 * fontMetrics.metrics.defaultRuleThickness; + } + denomShift = fontMetrics.metrics.denom2; + } + + var frac; + if (ruleWidth === 0) { + // Rule 15c + var candiateClearance = + (numShift - numer.depth) - (denom.height - denomShift); + if (candiateClearance < clearance) { + numShift += 0.5 * (clearance - candiateClearance); + denomShift += 0.5 * (clearance - candiateClearance); + } + + frac = buildCommon.makeVList([ + {type: "elem", elem: denomreset, shift: denomShift}, + {type: "elem", elem: numerreset, shift: -numShift}, + ], "individualShift", null, options); + } else { + // Rule 15d + var axisHeight = fontMetrics.metrics.axisHeight; + + if ((numShift - numer.depth) - (axisHeight + 0.5 * ruleWidth) < + clearance) { + numShift += + clearance - ((numShift - numer.depth) - + (axisHeight + 0.5 * ruleWidth)); + } + + if ((axisHeight - 0.5 * ruleWidth) - (denom.height - denomShift) < + clearance) { + denomShift += + clearance - ((axisHeight - 0.5 * ruleWidth) - + (denom.height - denomShift)); + } + + var mid = makeSpan( + [options.style.reset(), Style.TEXT.cls(), "frac-line"]); + // Manually set the height of the line because its height is + // created in CSS + mid.height = ruleWidth; + + var midShift = -(axisHeight - 0.5 * ruleWidth); + + frac = buildCommon.makeVList([ + {type: "elem", elem: denomreset, shift: denomShift}, + {type: "elem", elem: mid, shift: midShift}, + {type: "elem", elem: numerreset, shift: -numShift}, + ], "individualShift", null, options); + } + + // Since we manually change the style sometimes (with \dfrac or \tfrac), + // account for the possible size change here. + frac.height *= fstyle.sizeMultiplier / options.style.sizeMultiplier; + frac.depth *= fstyle.sizeMultiplier / options.style.sizeMultiplier; + + // Rule 15e + var delimSize; + if (fstyle.size === Style.DISPLAY.size) { + delimSize = fontMetrics.metrics.delim1; + } else { + delimSize = fontMetrics.metrics.getDelim2(fstyle); + } + + var leftDelim; + var rightDelim; + if (group.value.leftDelim == null) { + leftDelim = makeNullDelimiter(options); + } else { + leftDelim = delimiter.customSizedDelim( + group.value.leftDelim, delimSize, true, + options.withStyle(fstyle), group.mode); + } + if (group.value.rightDelim == null) { + rightDelim = makeNullDelimiter(options); + } else { + rightDelim = delimiter.customSizedDelim( + group.value.rightDelim, delimSize, true, + options.withStyle(fstyle), group.mode); + } + + return makeSpan( + ["mord", options.style.reset(), fstyle.cls()], + [leftDelim, makeSpan(["mfrac"], [frac]), rightDelim], + options.getColor()); +}; + +groupTypes.array = function(group, options, prev) { + var r; + var c; + var nr = group.value.body.length; + var nc = 0; + var body = new Array(nr); + + // Horizontal spacing + var pt = 1 / fontMetrics.metrics.ptPerEm; + var arraycolsep = 5 * pt; // \arraycolsep in article.cls + + // Vertical spacing + var baselineskip = 12 * pt; // see size10.clo + // Default \arraystretch from lttab.dtx + // TODO(gagern): may get redefined once we have user-defined macros + var arraystretch = utils.deflt(group.value.arraystretch, 1); + var arrayskip = arraystretch * baselineskip; + var arstrutHeight = 0.7 * arrayskip; // \strutbox in ltfsstrc.dtx and + var arstrutDepth = 0.3 * arrayskip; // \@arstrutbox in lttab.dtx + + var totalHeight = 0; + for (r = 0; r < group.value.body.length; ++r) { + var inrow = group.value.body[r]; + var height = arstrutHeight; // \@array adds an \@arstrut + var depth = arstrutDepth; // to each tow (via the template) + + if (nc < inrow.length) { + nc = inrow.length; + } + + var outrow = new Array(inrow.length); + for (c = 0; c < inrow.length; ++c) { + var elt = buildGroup(inrow[c], options); + if (depth < elt.depth) { + depth = elt.depth; + } + if (height < elt.height) { + height = elt.height; + } + outrow[c] = elt; + } + + var gap = 0; + if (group.value.rowGaps[r]) { + gap = group.value.rowGaps[r].value; + switch (gap.unit) { + case "em": + gap = gap.number; + break; + case "ex": + gap = gap.number * fontMetrics.metrics.emPerEx; + break; + default: + console.error("Can't handle unit " + gap.unit); + gap = 0; + } + if (gap > 0) { // \@argarraycr + gap += arstrutDepth; + if (depth < gap) { + depth = gap; // \@xargarraycr + } + gap = 0; + } + } + + outrow.height = height; + outrow.depth = depth; + totalHeight += height; + outrow.pos = totalHeight; + totalHeight += depth + gap; // \@yargarraycr + body[r] = outrow; + } + + var offset = totalHeight / 2 + fontMetrics.metrics.axisHeight; + var colDescriptions = group.value.cols || []; + var cols = []; + var colSep; + var colDescrNum; + for (c = 0, colDescrNum = 0; + // Continue while either there are more columns or more column + // descriptions, so trailing separators don't get lost. + c < nc || colDescrNum < colDescriptions.length; + ++c, ++colDescrNum) { + + var colDescr = colDescriptions[colDescrNum] || {}; + + var firstSeparator = true; + while (colDescr.type === "separator") { + // If there is more than one separator in a row, add a space + // between them. + if (!firstSeparator) { + colSep = makeSpan(["arraycolsep"], []); + colSep.style.width = + fontMetrics.metrics.doubleRuleSep + "em"; + cols.push(colSep); + } + + if (colDescr.separator === "|") { + var separator = makeSpan( + ["vertical-separator"], + []); + separator.style.height = totalHeight + "em"; + separator.style.verticalAlign = + -(totalHeight - offset) + "em"; + + cols.push(separator); + } else { + throw new ParseError( + "Invalid separator type: " + colDescr.separator); + } + + colDescrNum++; + colDescr = colDescriptions[colDescrNum] || {}; + firstSeparator = false; + } + + if (c >= nc) { + continue; + } + + var sepwidth; + if (c > 0 || group.value.hskipBeforeAndAfter) { + sepwidth = utils.deflt(colDescr.pregap, arraycolsep); + if (sepwidth !== 0) { + colSep = makeSpan(["arraycolsep"], []); + colSep.style.width = sepwidth + "em"; + cols.push(colSep); + } + } + + var col = []; + for (r = 0; r < nr; ++r) { + var row = body[r]; + var elem = row[c]; + if (!elem) { + continue; + } + var shift = row.pos - offset; + elem.depth = row.depth; + elem.height = row.height; + col.push({type: "elem", elem: elem, shift: shift}); + } + + col = buildCommon.makeVList(col, "individualShift", null, options); + col = makeSpan( + ["col-align-" + (colDescr.align || "c")], + [col]); + cols.push(col); + + if (c < nc - 1 || group.value.hskipBeforeAndAfter) { + sepwidth = utils.deflt(colDescr.postgap, arraycolsep); + if (sepwidth !== 0) { + colSep = makeSpan(["arraycolsep"], []); + colSep.style.width = sepwidth + "em"; + cols.push(colSep); + } + } + } + body = makeSpan(["mtable"], cols); + return makeSpan(["mord"], [body], options.getColor()); +}; + +groupTypes.spacing = function(group, options, prev) { + if (group.value === "\\ " || group.value === "\\space" || + group.value === " " || group.value === "~") { + // Spaces are generated by adding an actual space. Each of these + // things has an entry in the symbols table, so these will be turned + // into appropriate outputs. + return makeSpan( + ["mord", "mspace"], + [buildCommon.mathsym(group.value, group.mode)] + ); + } else { + // Other kinds of spaces are of arbitrary width. We use CSS to + // generate these. + return makeSpan( + ["mord", "mspace", + buildCommon.spacingFunctions[group.value].className]); + } +}; + +groupTypes.llap = function(group, options, prev) { + var inner = makeSpan( + ["inner"], [buildGroup(group.value.body, options.reset())]); + var fix = makeSpan(["fix"], []); + return makeSpan( + ["llap", options.style.cls()], [inner, fix]); +}; + +groupTypes.rlap = function(group, options, prev) { + var inner = makeSpan( + ["inner"], [buildGroup(group.value.body, options.reset())]); + var fix = makeSpan(["fix"], []); + return makeSpan( + ["rlap", options.style.cls()], [inner, fix]); +}; + +groupTypes.op = function(group, options, prev) { + // Operators are handled in the TeXbook pg. 443-444, rule 13(a). + var supGroup; + var subGroup; + var hasLimits = false; + if (group.type === "supsub" ) { + // If we have limits, supsub will pass us its group to handle. Pull + // out the superscript and subscript and set the group to the op in + // its base. + supGroup = group.value.sup; + subGroup = group.value.sub; + group = group.value.base; + hasLimits = true; + } + + // Most operators have a large successor symbol, but these don't. + var noSuccessor = [ + "\\smallint", + ]; + + var large = false; + if (options.style.size === Style.DISPLAY.size && + group.value.symbol && + !utils.contains(noSuccessor, group.value.body)) { + + // Most symbol operators get larger in displaystyle (rule 13) + large = true; + } + + var base; + var baseShift = 0; + var slant = 0; + if (group.value.symbol) { + // If this is a symbol, create the symbol. + var style = large ? "Size2-Regular" : "Size1-Regular"; + base = buildCommon.makeSymbol( + group.value.body, style, "math", options.getColor(), + ["op-symbol", large ? "large-op" : "small-op", "mop"]); + + // Shift the symbol so its center lies on the axis (rule 13). It + // appears that our fonts have the centers of the symbols already + // almost on the axis, so these numbers are very small. Note we + // don't actually apply this here, but instead it is used either in + // the vlist creation or separately when there are no limits. + baseShift = (base.height - base.depth) / 2 - + fontMetrics.metrics.axisHeight * + options.style.sizeMultiplier; + + // The slant of the symbol is just its italic correction. + slant = base.italic; + } else { + // Otherwise, this is a text operator. Build the text from the + // operator's name. + // TODO(emily): Add a space in the middle of some of these + // operators, like \limsup + var output = []; + for (var i = 1; i < group.value.body.length; i++) { + output.push(buildCommon.mathsym(group.value.body[i], group.mode)); + } + base = makeSpan(["mop"], output, options.getColor()); + } + + if (hasLimits) { + // IE 8 clips \int if it is in a display: inline-block. We wrap it + // in a new span so it is an inline, and works. + base = makeSpan([], [base]); + + var supmid; + var supKern; + var submid; + var subKern; + // We manually have to handle the superscripts and subscripts. This, + // aside from the kern calculations, is copied from supsub. + if (supGroup) { + var sup = buildGroup( + supGroup, options.withStyle(options.style.sup())); + supmid = makeSpan( + [options.style.reset(), options.style.sup().cls()], [sup]); + + supKern = Math.max( + fontMetrics.metrics.bigOpSpacing1, + fontMetrics.metrics.bigOpSpacing3 - sup.depth); + } + + if (subGroup) { + var sub = buildGroup( + subGroup, options.withStyle(options.style.sub())); + submid = makeSpan( + [options.style.reset(), options.style.sub().cls()], + [sub]); + + subKern = Math.max( + fontMetrics.metrics.bigOpSpacing2, + fontMetrics.metrics.bigOpSpacing4 - sub.height); + } + + // Build the final group as a vlist of the possible subscript, base, + // and possible superscript. + var finalGroup; + var top; + var bottom; + if (!supGroup) { + top = base.height - baseShift; + + finalGroup = buildCommon.makeVList([ + {type: "kern", size: fontMetrics.metrics.bigOpSpacing5}, + {type: "elem", elem: submid}, + {type: "kern", size: subKern}, + {type: "elem", elem: base}, + ], "top", top, options); + + // Here, we shift the limits by the slant of the symbol. Note + // that we are supposed to shift the limits by 1/2 of the slant, + // but since we are centering the limits adding a full slant of + // margin will shift by 1/2 that. + finalGroup.children[0].style.marginLeft = -slant + "em"; + } else if (!subGroup) { + bottom = base.depth + baseShift; + + finalGroup = buildCommon.makeVList([ + {type: "elem", elem: base}, + {type: "kern", size: supKern}, + {type: "elem", elem: supmid}, + {type: "kern", size: fontMetrics.metrics.bigOpSpacing5}, + ], "bottom", bottom, options); + + // See comment above about slants + finalGroup.children[1].style.marginLeft = slant + "em"; + } else if (!supGroup && !subGroup) { + // This case probably shouldn't occur (this would mean the + // supsub was sending us a group with no superscript or + // subscript) but be safe. + return base; + } else { + bottom = fontMetrics.metrics.bigOpSpacing5 + + submid.height + submid.depth + + subKern + + base.depth + baseShift; + + finalGroup = buildCommon.makeVList([ + {type: "kern", size: fontMetrics.metrics.bigOpSpacing5}, + {type: "elem", elem: submid}, + {type: "kern", size: subKern}, + {type: "elem", elem: base}, + {type: "kern", size: supKern}, + {type: "elem", elem: supmid}, + {type: "kern", size: fontMetrics.metrics.bigOpSpacing5}, + ], "bottom", bottom, options); + + // See comment above about slants + finalGroup.children[0].style.marginLeft = -slant + "em"; + finalGroup.children[2].style.marginLeft = slant + "em"; + } + + return makeSpan(["mop", "op-limits"], [finalGroup]); + } else { + if (group.value.symbol) { + base.style.top = baseShift + "em"; + } + + return base; + } +}; + +groupTypes.katex = function(group, options, prev) { + // The KaTeX logo. The offsets for the K and a were chosen to look + // good, but the offsets for the T, E, and X were taken from the + // definition of \TeX in TeX (see TeXbook pg. 356) + var k = makeSpan( + ["k"], [buildCommon.mathsym("K", group.mode)]); + var a = makeSpan( + ["a"], [buildCommon.mathsym("A", group.mode)]); + + a.height = (a.height + 0.2) * 0.75; + a.depth = (a.height - 0.2) * 0.75; + + var t = makeSpan( + ["t"], [buildCommon.mathsym("T", group.mode)]); + var e = makeSpan( + ["e"], [buildCommon.mathsym("E", group.mode)]); + + e.height = (e.height - 0.2155); + e.depth = (e.depth + 0.2155); + + var x = makeSpan( + ["x"], [buildCommon.mathsym("X", group.mode)]); + + return makeSpan( + ["katex-logo", "mord"], [k, a, t, e, x], options.getColor()); +}; + +groupTypes.overline = function(group, options, prev) { + // Overlines are handled in the TeXbook pg 443, Rule 9. + + // Build the inner group in the cramped style. + var innerGroup = buildGroup(group.value.body, + options.withStyle(options.style.cramp())); + + var ruleWidth = fontMetrics.metrics.defaultRuleThickness / + options.style.sizeMultiplier; + + // Create the line above the body + var line = makeSpan( + [options.style.reset(), Style.TEXT.cls(), "overline-line"]); + line.height = ruleWidth; + line.maxFontSize = 1.0; + + // Generate the vlist, with the appropriate kerns + var vlist = buildCommon.makeVList([ + {type: "elem", elem: innerGroup}, + {type: "kern", size: 3 * ruleWidth}, + {type: "elem", elem: line}, + {type: "kern", size: ruleWidth}, + ], "firstBaseline", null, options); + + return makeSpan(["overline", "mord"], [vlist], options.getColor()); +}; + +groupTypes.underline = function(group, options, prev) { + // Underlines are handled in the TeXbook pg 443, Rule 10. + + // Build the inner group. + var innerGroup = buildGroup(group.value.body, options); + + var ruleWidth = fontMetrics.metrics.defaultRuleThickness / + options.style.sizeMultiplier; + + // Create the line above the body + var line = makeSpan( + [options.style.reset(), Style.TEXT.cls(), "underline-line"]); + line.height = ruleWidth; + line.maxFontSize = 1.0; + + // Generate the vlist, with the appropriate kerns + var vlist = buildCommon.makeVList([ + {type: "kern", size: ruleWidth}, + {type: "elem", elem: line}, + {type: "kern", size: 3 * ruleWidth}, + {type: "elem", elem: innerGroup}, + ], "top", innerGroup.height, options); + + return makeSpan(["underline", "mord"], [vlist], options.getColor()); +}; + +groupTypes.sqrt = function(group, options, prev) { + // Square roots are handled in the TeXbook pg. 443, Rule 11. + + // First, we do the same steps as in overline to build the inner group + // and line + var inner = buildGroup(group.value.body, + options.withStyle(options.style.cramp())); + + var ruleWidth = fontMetrics.metrics.defaultRuleThickness / + options.style.sizeMultiplier; + + var line = makeSpan( + [options.style.reset(), Style.TEXT.cls(), "sqrt-line"], [], + options.getColor()); + line.height = ruleWidth; + line.maxFontSize = 1.0; + + var phi = ruleWidth; + if (options.style.id < Style.TEXT.id) { + phi = fontMetrics.metrics.xHeight; + } + + // Calculate the clearance between the body and line + var lineClearance = ruleWidth + phi / 4; + + var innerHeight = + (inner.height + inner.depth) * options.style.sizeMultiplier; + var minDelimiterHeight = innerHeight + lineClearance + ruleWidth; + + // Create a \surd delimiter of the required minimum size + var delim = makeSpan(["sqrt-sign"], [ + delimiter.customSizedDelim("\\surd", minDelimiterHeight, + false, options, group.mode)], + options.getColor()); + + var delimDepth = (delim.height + delim.depth) - ruleWidth; + + // Adjust the clearance based on the delimiter size + if (delimDepth > inner.height + inner.depth + lineClearance) { + lineClearance = + (lineClearance + delimDepth - inner.height - inner.depth) / 2; + } + + // Shift the delimiter so that its top lines up with the top of the line + var delimShift = -(inner.height + lineClearance + ruleWidth) + delim.height; + delim.style.top = delimShift + "em"; + delim.height -= delimShift; + delim.depth += delimShift; + + // We add a special case here, because even when `inner` is empty, we + // still get a line. So, we use a simple heuristic to decide if we + // should omit the body entirely. (note this doesn't work for something + // like `\sqrt{\rlap{x}}`, but if someone is doing that they deserve for + // it not to work. + var body; + if (inner.height === 0 && inner.depth === 0) { + body = makeSpan(); + } else { + body = buildCommon.makeVList([ + {type: "elem", elem: inner}, + {type: "kern", size: lineClearance}, + {type: "elem", elem: line}, + {type: "kern", size: ruleWidth}, + ], "firstBaseline", null, options); + } + + if (!group.value.index) { + return makeSpan(["sqrt", "mord"], [delim, body]); + } else { + // Handle the optional root index + + // The index is always in scriptscript style + var root = buildGroup( + group.value.index, + options.withStyle(Style.SCRIPTSCRIPT)); + var rootWrap = makeSpan( + [options.style.reset(), Style.SCRIPTSCRIPT.cls()], + [root]); + + // Figure out the height and depth of the inner part + var innerRootHeight = Math.max(delim.height, body.height); + var innerRootDepth = Math.max(delim.depth, body.depth); + + // The amount the index is shifted by. This is taken from the TeX + // source, in the definition of `\r@@t`. + var toShift = 0.6 * (innerRootHeight - innerRootDepth); + + // Build a VList with the superscript shifted up correctly + var rootVList = buildCommon.makeVList( + [{type: "elem", elem: rootWrap}], + "shift", -toShift, options); + // Add a class surrounding it so we can add on the appropriate + // kerning + var rootVListWrap = makeSpan(["root"], [rootVList]); + + return makeSpan(["sqrt", "mord"], [rootVListWrap, delim, body]); + } +}; + +groupTypes.sizing = function(group, options, prev) { + // Handle sizing operators like \Huge. Real TeX doesn't actually allow + // these functions inside of math expressions, so we do some special + // handling. + var inner = buildExpression(group.value.value, + options.withSize(group.value.size), prev); + + var span = makeSpan(["mord"], + [makeSpan(["sizing", "reset-" + options.size, group.value.size, + options.style.cls()], + inner)]); + + // Calculate the correct maxFontSize manually + var fontSize = buildCommon.sizingMultiplier[group.value.size]; + span.maxFontSize = fontSize * options.style.sizeMultiplier; + + return span; +}; + +groupTypes.styling = function(group, options, prev) { + // Style changes are handled in the TeXbook on pg. 442, Rule 3. + + // Figure out what style we're changing to. + var style = { + "display": Style.DISPLAY, + "text": Style.TEXT, + "script": Style.SCRIPT, + "scriptscript": Style.SCRIPTSCRIPT, + }; + + var newStyle = style[group.value.style]; + + // Build the inner expression in the new style. + var inner = buildExpression( + group.value.value, options.withStyle(newStyle), prev); + + return makeSpan([options.style.reset(), newStyle.cls()], inner); +}; + +groupTypes.font = function(group, options, prev) { + var font = group.value.font; + return buildGroup(group.value.body, options.withFont(font), prev); +}; + +groupTypes.delimsizing = function(group, options, prev) { + var delim = group.value.value; + + if (delim === ".") { + // Empty delimiters still count as elements, even though they don't + // show anything. + return makeSpan([groupToType[group.value.delimType]]); + } + + // Use delimiter.sizedDelim to generate the delimiter. + return makeSpan( + [groupToType[group.value.delimType]], + [delimiter.sizedDelim( + delim, group.value.size, options, group.mode)]); +}; + +groupTypes.leftright = function(group, options, prev) { + // Build the inner expression + var inner = buildExpression(group.value.body, options.reset()); + + var innerHeight = 0; + var innerDepth = 0; + + // Calculate its height and depth + for (var i = 0; i < inner.length; i++) { + innerHeight = Math.max(inner[i].height, innerHeight); + innerDepth = Math.max(inner[i].depth, innerDepth); + } + + // The size of delimiters is the same, regardless of what style we are + // in. Thus, to correctly calculate the size of delimiter we need around + // a group, we scale down the inner size based on the size. + innerHeight *= options.style.sizeMultiplier; + innerDepth *= options.style.sizeMultiplier; + + var leftDelim; + if (group.value.left === ".") { + // Empty delimiters in \left and \right make null delimiter spaces. + leftDelim = makeNullDelimiter(options); + } else { + // Otherwise, use leftRightDelim to generate the correct sized + // delimiter. + leftDelim = delimiter.leftRightDelim( + group.value.left, innerHeight, innerDepth, options, + group.mode); + } + // Add it to the beginning of the expression + inner.unshift(leftDelim); + + var rightDelim; + // Same for the right delimiter + if (group.value.right === ".") { + rightDelim = makeNullDelimiter(options); + } else { + rightDelim = delimiter.leftRightDelim( + group.value.right, innerHeight, innerDepth, options, + group.mode); + } + // Add it to the end of the expression. + inner.push(rightDelim); + + return makeSpan( + ["minner", options.style.cls()], inner, options.getColor()); +}; + +groupTypes.rule = function(group, options, prev) { + // Make an empty span for the rule + var rule = makeSpan(["mord", "rule"], [], options.getColor()); + + // Calculate the shift, width, and height of the rule, and account for units + var shift = 0; + if (group.value.shift) { + shift = group.value.shift.number; + if (group.value.shift.unit === "ex") { + shift *= fontMetrics.metrics.xHeight; + } + } + + var width = group.value.width.number; + if (group.value.width.unit === "ex") { + width *= fontMetrics.metrics.xHeight; + } + + var height = group.value.height.number; + if (group.value.height.unit === "ex") { + height *= fontMetrics.metrics.xHeight; + } + + // The sizes of rules are absolute, so make it larger if we are in a + // smaller style. + shift /= options.style.sizeMultiplier; + width /= options.style.sizeMultiplier; + height /= options.style.sizeMultiplier; + + // Style the rule to the right size + rule.style.borderRightWidth = width + "em"; + rule.style.borderTopWidth = height + "em"; + rule.style.bottom = shift + "em"; + + // Record the height and width + rule.width = width; + rule.height = height + shift; + rule.depth = -shift; + + return rule; +}; + +groupTypes.accent = function(group, options, prev) { + // Accents are handled in the TeXbook pg. 443, rule 12. + var base = group.value.base; + + var supsubGroup; + if (group.type === "supsub") { + // If our base is a character box, and we have superscripts and + // subscripts, the supsub will defer to us. In particular, we want + // to attach the superscripts and subscripts to the inner body (so + // that the position of the superscripts and subscripts won't be + // affected by the height of the accent). We accomplish this by + // sticking the base of the accent into the base of the supsub, and + // rendering that, while keeping track of where the accent is. + + // The supsub group is the group that was passed in + var supsub = group; + // The real accent group is the base of the supsub group + group = supsub.value.base; + // The character box is the base of the accent group + base = group.value.base; + // Stick the character box into the base of the supsub group + supsub.value.base = base; + + // Rerender the supsub group with its new base, and store that + // result. + supsubGroup = buildGroup( + supsub, options.reset(), prev); + } + + // Build the base group + var body = buildGroup( + base, options.withStyle(options.style.cramp())); + + // Calculate the skew of the accent. This is based on the line "If the + // nucleus is not a single character, let s = 0; otherwise set s to the + // kern amount for the nucleus followed by the \skewchar of its font." + // Note that our skew metrics are just the kern between each character + // and the skewchar. + var skew; + if (isCharacterBox(base)) { + // If the base is a character box, then we want the skew of the + // innermost character. To do that, we find the innermost character: + var baseChar = getBaseElem(base); + // Then, we render its group to get the symbol inside it + var baseGroup = buildGroup( + baseChar, options.withStyle(options.style.cramp())); + // Finally, we pull the skew off of the symbol. + skew = baseGroup.skew; + // Note that we now throw away baseGroup, because the layers we + // removed with getBaseElem might contain things like \color which + // we can't get rid of. + // TODO(emily): Find a better way to get the skew + } else { + skew = 0; + } + + // calculate the amount of space between the body and the accent + var clearance = Math.min(body.height, fontMetrics.metrics.xHeight); + + // Build the accent + var accent = buildCommon.makeSymbol( + group.value.accent, "Main-Regular", "math", options.getColor()); + // Remove the italic correction of the accent, because it only serves to + // shift the accent over to a place we don't want. + accent.italic = 0; + + // The \vec character that the fonts use is a combining character, and + // thus shows up much too far to the left. To account for this, we add a + // specific class which shifts the accent over to where we want it. + // TODO(emily): Fix this in a better way, like by changing the font + var vecClass = group.value.accent === "\\vec" ? "accent-vec" : null; + + var accentBody = makeSpan(["accent-body", vecClass], [ + makeSpan([], [accent])]); + + accentBody = buildCommon.makeVList([ + {type: "elem", elem: body}, + {type: "kern", size: -clearance}, + {type: "elem", elem: accentBody}, + ], "firstBaseline", null, options); + + // Shift the accent over by the skew. Note we shift by twice the skew + // because we are centering the accent, so by adding 2*skew to the left, + // we shift it to the right by 1*skew. + accentBody.children[1].style.marginLeft = 2 * skew + "em"; + + var accentWrap = makeSpan(["mord", "accent"], [accentBody]); + + if (supsubGroup) { + // Here, we replace the "base" child of the supsub with our newly + // generated accent. + supsubGroup.children[0] = accentWrap; + + // Since we don't rerun the height calculation after replacing the + // accent, we manually recalculate height. + supsubGroup.height = Math.max(accentWrap.height, supsubGroup.height); + + // Accents should always be ords, even when their innards are not. + supsubGroup.classes[0] = "mord"; + + return supsubGroup; + } else { + return accentWrap; + } +}; + +groupTypes.phantom = function(group, options, prev) { + var elements = buildExpression( + group.value.value, + options.withPhantom(), + prev + ); + + // \phantom isn't supposed to affect the elements it contains. + // See "color" for more details. + return new buildCommon.makeFragment(elements); +}; + +/** + * buildGroup is the function that takes a group and calls the correct groupType + * function for it. It also handles the interaction of size and style changes + * between parents and children. + */ +var buildGroup = function(group, options, prev) { + if (!group) { + return makeSpan(); + } + + if (groupTypes[group.type]) { + // Call the groupTypes function + var groupNode = groupTypes[group.type](group, options, prev); + var multiplier; + + // If the style changed between the parent and the current group, + // account for the size difference + if (options.style !== options.parentStyle) { + multiplier = options.style.sizeMultiplier / + options.parentStyle.sizeMultiplier; + + groupNode.height *= multiplier; + groupNode.depth *= multiplier; + } + + // If the size changed between the parent and the current group, account + // for that size difference. + if (options.size !== options.parentSize) { + multiplier = buildCommon.sizingMultiplier[options.size] / + buildCommon.sizingMultiplier[options.parentSize]; + + groupNode.height *= multiplier; + groupNode.depth *= multiplier; + } + + return groupNode; + } else { + throw new ParseError( + "Got group of unknown type: '" + group.type + "'"); + } +}; + +/** + * Take an entire parse tree, and build it into an appropriate set of HTML + * nodes. + */ +var buildHTML = function(tree, options) { + // buildExpression is destructive, so we need to make a clone + // of the incoming tree so that it isn't accidentally changed + tree = JSON.parse(JSON.stringify(tree)); + + // Build the expression contained in the tree + var expression = buildExpression(tree, options); + var body = makeSpan(["base", options.style.cls()], expression); + + // Add struts, which ensure that the top of the HTML element falls at the + // height of the expression, and the bottom of the HTML element falls at the + // depth of the expression. + var topStrut = makeSpan(["strut"]); + var bottomStrut = makeSpan(["strut", "bottom"]); + + topStrut.style.height = body.height + "em"; + bottomStrut.style.height = (body.height + body.depth) + "em"; + // We'd like to use `vertical-align: top` but in IE 9 this lowers the + // baseline of the box to the bottom of this strut (instead staying in the + // normal place) so we use an absolute value for vertical-align instead + bottomStrut.style.verticalAlign = -body.depth + "em"; + + // Wrap the struts and body together + var htmlNode = makeSpan(["katex-html"], [topStrut, bottomStrut, body]); + + htmlNode.setAttribute("aria-hidden", "true"); + + return htmlNode; +}; + +module.exports = buildHTML; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildMathML.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildMathML.js new file mode 100644 index 000000000..7bf38e86a --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildMathML.js @@ -0,0 +1,533 @@ +/** + * This file converts a parse tree into a cooresponding MathML tree. The main + * entry point is the `buildMathML` function, which takes a parse tree from the + * parser. + */ + +var buildCommon = require("./buildCommon"); +var fontMetrics = require("./fontMetrics"); +var mathMLTree = require("./mathMLTree"); +var ParseError = require("./ParseError"); +var symbols = require("./symbols"); +var utils = require("./utils"); + +var makeSpan = buildCommon.makeSpan; +var fontMap = buildCommon.fontMap; + +/** + * Takes a symbol and converts it into a MathML text node after performing + * optional replacement from symbols.js. + */ +var makeText = function(text, mode) { + if (symbols[mode][text] && symbols[mode][text].replace) { + text = symbols[mode][text].replace; + } + + return new mathMLTree.TextNode(text); +}; + +/** + * Returns the math variant as a string or null if none is required. + */ +var getVariant = function(group, options) { + var font = options.font; + if (!font) { + return null; + } + + var mode = group.mode; + if (font === "mathit") { + return "italic"; + } + + var value = group.value; + if (utils.contains(["\\imath", "\\jmath"], value)) { + return null; + } + + if (symbols[mode][value] && symbols[mode][value].replace) { + value = symbols[mode][value].replace; + } + + var fontName = fontMap[font].fontName; + if (fontMetrics.getCharacterMetrics(value, fontName)) { + return fontMap[options.font].variant; + } + + return null; +}; + +/** + * Functions for handling the different types of groups found in the parse + * tree. Each function should take a parse group and return a MathML node. + */ +var groupTypes = {}; + +groupTypes.mathord = function(group, options) { + var node = new mathMLTree.MathNode( + "mi", + [makeText(group.value, group.mode)]); + + var variant = getVariant(group, options); + if (variant) { + node.setAttribute("mathvariant", variant); + } + return node; +}; + +groupTypes.textord = function(group, options) { + var text = makeText(group.value, group.mode); + + var variant = getVariant(group, options) || "normal"; + + var node; + if (/[0-9]/.test(group.value)) { + // TODO(kevinb) merge adjacent nodes + // do it as a post processing step + node = new mathMLTree.MathNode("mn", [text]); + if (options.font) { + node.setAttribute("mathvariant", variant); + } + } else { + node = new mathMLTree.MathNode("mi", [text]); + node.setAttribute("mathvariant", variant); + } + + return node; +}; + +groupTypes.bin = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + return node; +}; + +groupTypes.rel = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + return node; +}; + +groupTypes.open = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + return node; +}; + +groupTypes.close = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + return node; +}; + +groupTypes.inner = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + return node; +}; + +groupTypes.punct = function(group) { + var node = new mathMLTree.MathNode( + "mo", [makeText(group.value, group.mode)]); + + node.setAttribute("separator", "true"); + + return node; +}; + +groupTypes.ordgroup = function(group, options) { + var inner = buildExpression(group.value, options); + + var node = new mathMLTree.MathNode("mrow", inner); + + return node; +}; + +groupTypes.text = function(group, options) { + var inner = buildExpression(group.value.body, options); + + var node = new mathMLTree.MathNode("mtext", inner); + + return node; +}; + +groupTypes.color = function(group, options) { + var inner = buildExpression(group.value.value, options); + + var node = new mathMLTree.MathNode("mstyle", inner); + + node.setAttribute("mathcolor", group.value.color); + + return node; +}; + +groupTypes.supsub = function(group, options) { + var children = [buildGroup(group.value.base, options)]; + + if (group.value.sub) { + children.push(buildGroup(group.value.sub, options)); + } + + if (group.value.sup) { + children.push(buildGroup(group.value.sup, options)); + } + + var nodeType; + if (!group.value.sub) { + nodeType = "msup"; + } else if (!group.value.sup) { + nodeType = "msub"; + } else { + nodeType = "msubsup"; + } + + var node = new mathMLTree.MathNode(nodeType, children); + + return node; +}; + +groupTypes.genfrac = function(group, options) { + var node = new mathMLTree.MathNode( + "mfrac", + [buildGroup(group.value.numer, options), + buildGroup(group.value.denom, options)]); + + if (!group.value.hasBarLine) { + node.setAttribute("linethickness", "0px"); + } + + if (group.value.leftDelim != null || group.value.rightDelim != null) { + var withDelims = []; + + if (group.value.leftDelim != null) { + var leftOp = new mathMLTree.MathNode( + "mo", [new mathMLTree.TextNode(group.value.leftDelim)]); + + leftOp.setAttribute("fence", "true"); + + withDelims.push(leftOp); + } + + withDelims.push(node); + + if (group.value.rightDelim != null) { + var rightOp = new mathMLTree.MathNode( + "mo", [new mathMLTree.TextNode(group.value.rightDelim)]); + + rightOp.setAttribute("fence", "true"); + + withDelims.push(rightOp); + } + + var outerNode = new mathMLTree.MathNode("mrow", withDelims); + + return outerNode; + } + + return node; +}; + +groupTypes.array = function(group, options) { + return new mathMLTree.MathNode( + "mtable", group.value.body.map(function(row) { + return new mathMLTree.MathNode( + "mtr", row.map(function(cell) { + return new mathMLTree.MathNode( + "mtd", [buildGroup(cell, options)]); + })); + })); +}; + +groupTypes.sqrt = function(group, options) { + var node; + if (group.value.index) { + node = new mathMLTree.MathNode( + "mroot", [ + buildGroup(group.value.body, options), + buildGroup(group.value.index, options), + ]); + } else { + node = new mathMLTree.MathNode( + "msqrt", [buildGroup(group.value.body, options)]); + } + + return node; +}; + +groupTypes.leftright = function(group, options) { + var inner = buildExpression(group.value.body, options); + + if (group.value.left !== ".") { + var leftNode = new mathMLTree.MathNode( + "mo", [makeText(group.value.left, group.mode)]); + + leftNode.setAttribute("fence", "true"); + + inner.unshift(leftNode); + } + + if (group.value.right !== ".") { + var rightNode = new mathMLTree.MathNode( + "mo", [makeText(group.value.right, group.mode)]); + + rightNode.setAttribute("fence", "true"); + + inner.push(rightNode); + } + + var outerNode = new mathMLTree.MathNode("mrow", inner); + + return outerNode; +}; + +groupTypes.accent = function(group, options) { + var accentNode = new mathMLTree.MathNode( + "mo", [makeText(group.value.accent, group.mode)]); + + var node = new mathMLTree.MathNode( + "mover", + [buildGroup(group.value.base, options), + accentNode]); + + node.setAttribute("accent", "true"); + + return node; +}; + +groupTypes.spacing = function(group) { + var node; + + if (group.value === "\\ " || group.value === "\\space" || + group.value === " " || group.value === "~") { + node = new mathMLTree.MathNode( + "mtext", [new mathMLTree.TextNode("\u00a0")]); + } else { + node = new mathMLTree.MathNode("mspace"); + + node.setAttribute( + "width", buildCommon.spacingFunctions[group.value].size); + } + + return node; +}; + +groupTypes.op = function(group) { + var node; + + // TODO(emily): handle big operators using the `largeop` attribute + + if (group.value.symbol) { + // This is a symbol. Just add the symbol. + node = new mathMLTree.MathNode( + "mo", [makeText(group.value.body, group.mode)]); + } else { + // This is a text operator. Add all of the characters from the + // operator's name. + // TODO(emily): Add a space in the middle of some of these + // operators, like \limsup. + node = new mathMLTree.MathNode( + "mi", [new mathMLTree.TextNode(group.value.body.slice(1))]); + } + + return node; +}; + +groupTypes.katex = function(group) { + var node = new mathMLTree.MathNode( + "mtext", [new mathMLTree.TextNode("KaTeX")]); + + return node; +}; + +groupTypes.font = function(group, options) { + var font = group.value.font; + return buildGroup(group.value.body, options.withFont(font)); +}; + +groupTypes.delimsizing = function(group) { + var children = []; + + if (group.value.value !== ".") { + children.push(makeText(group.value.value, group.mode)); + } + + var node = new mathMLTree.MathNode("mo", children); + + if (group.value.delimType === "open" || + group.value.delimType === "close") { + // Only some of the delimsizing functions act as fences, and they + // return "open" or "close" delimTypes. + node.setAttribute("fence", "true"); + } else { + // Explicitly disable fencing if it's not a fence, to override the + // defaults. + node.setAttribute("fence", "false"); + } + + return node; +}; + +groupTypes.styling = function(group, options) { + var inner = buildExpression(group.value.value, options); + + var node = new mathMLTree.MathNode("mstyle", inner); + + var styleAttributes = { + "display": ["0", "true"], + "text": ["0", "false"], + "script": ["1", "false"], + "scriptscript": ["2", "false"], + }; + + var attr = styleAttributes[group.value.style]; + + node.setAttribute("scriptlevel", attr[0]); + node.setAttribute("displaystyle", attr[1]); + + return node; +}; + +groupTypes.sizing = function(group, options) { + var inner = buildExpression(group.value.value, options); + + var node = new mathMLTree.MathNode("mstyle", inner); + + // TODO(emily): This doesn't produce the correct size for nested size + // changes, because we don't keep state of what style we're currently + // in, so we can't reset the size to normal before changing it. Now + // that we're passing an options parameter we should be able to fix + // this. + node.setAttribute( + "mathsize", buildCommon.sizingMultiplier[group.value.size] + "em"); + + return node; +}; + +groupTypes.overline = function(group, options) { + var operator = new mathMLTree.MathNode( + "mo", [new mathMLTree.TextNode("\u203e")]); + operator.setAttribute("stretchy", "true"); + + var node = new mathMLTree.MathNode( + "mover", + [buildGroup(group.value.body, options), + operator]); + node.setAttribute("accent", "true"); + + return node; +}; + +groupTypes.underline = function(group, options) { + var operator = new mathMLTree.MathNode( + "mo", [new mathMLTree.TextNode("\u203e")]); + operator.setAttribute("stretchy", "true"); + + var node = new mathMLTree.MathNode( + "munder", + [buildGroup(group.value.body, options), + operator]); + node.setAttribute("accentunder", "true"); + + return node; +}; + +groupTypes.rule = function(group) { + // TODO(emily): Figure out if there's an actual way to draw black boxes + // in MathML. + var node = new mathMLTree.MathNode("mrow"); + + return node; +}; + +groupTypes.llap = function(group, options) { + var node = new mathMLTree.MathNode( + "mpadded", [buildGroup(group.value.body, options)]); + + node.setAttribute("lspace", "-1width"); + node.setAttribute("width", "0px"); + + return node; +}; + +groupTypes.rlap = function(group, options) { + var node = new mathMLTree.MathNode( + "mpadded", [buildGroup(group.value.body, options)]); + + node.setAttribute("width", "0px"); + + return node; +}; + +groupTypes.phantom = function(group, options, prev) { + var inner = buildExpression(group.value.value, options); + return new mathMLTree.MathNode("mphantom", inner); +}; + +/** + * Takes a list of nodes, builds them, and returns a list of the generated + * MathML nodes. A little simpler than the HTML version because we don't do any + * previous-node handling. + */ +var buildExpression = function(expression, options) { + var groups = []; + for (var i = 0; i < expression.length; i++) { + var group = expression[i]; + groups.push(buildGroup(group, options)); + } + return groups; +}; + +/** + * Takes a group from the parser and calls the appropriate groupTypes function + * on it to produce a MathML node. + */ +var buildGroup = function(group, options) { + if (!group) { + return new mathMLTree.MathNode("mrow"); + } + + if (groupTypes[group.type]) { + // Call the groupTypes function + return groupTypes[group.type](group, options); + } else { + throw new ParseError( + "Got group of unknown type: '" + group.type + "'"); + } +}; + +/** + * Takes a full parse tree and settings and builds a MathML representation of + * it. In particular, we put the elements from building the parse tree into a + * tag so we can also include that TeX source as an annotation. + * + * Note that we actually return a domTree element with a `` inside it so + * we can do appropriate styling. + */ +var buildMathML = function(tree, texExpression, options) { + var expression = buildExpression(tree, options); + + // Wrap up the expression in an mrow so it is presented in the semantics + // tag correctly. + var wrapper = new mathMLTree.MathNode("mrow", expression); + + // Build a TeX annotation of the source + var annotation = new mathMLTree.MathNode( + "annotation", [new mathMLTree.TextNode(texExpression)]); + + annotation.setAttribute("encoding", "application/x-tex"); + + var semantics = new mathMLTree.MathNode( + "semantics", [wrapper, annotation]); + + var math = new mathMLTree.MathNode("math", [semantics]); + + // You can't style nodes, so we wrap the node in a span. + return makeSpan(["katex-mathml"], [math]); +}; + +module.exports = buildMathML; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildTree.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildTree.js new file mode 100644 index 000000000..4a8c2aeda --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/buildTree.js @@ -0,0 +1,40 @@ +var buildHTML = require("./buildHTML"); +var buildMathML = require("./buildMathML"); +var buildCommon = require("./buildCommon"); +var Options = require("./Options"); +var Settings = require("./Settings"); +var Style = require("./Style"); + +var makeSpan = buildCommon.makeSpan; + +var buildTree = function(tree, expression, settings) { + settings = settings || new Settings({}); + + var startStyle = Style.TEXT; + if (settings.displayMode) { + startStyle = Style.DISPLAY; + } + + // Setup the default options + var options = new Options({ + style: startStyle, + size: "size5", + }); + + // `buildHTML` sometimes messes with the parse tree (like turning bins -> + // ords), so we build the MathML version first. + var mathMLNode = buildMathML(tree, expression, options); + var htmlNode = buildHTML(tree, options); + + var katexNode = makeSpan(["katex"], [ + mathMLNode, htmlNode, + ]); + + if (settings.displayMode) { + return makeSpan(["katex-display"], [katexNode]); + } else { + return katexNode; + } +}; + +module.exports = buildTree; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/delimiter.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/delimiter.js new file mode 100644 index 000000000..168319d41 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/delimiter.js @@ -0,0 +1,542 @@ +/** + * This file deals with creating delimiters of various sizes. The TeXbook + * discusses these routines on page 441-442, in the "Another subroutine sets box + * x to a specified variable delimiter" paragraph. + * + * There are three main routines here. `makeSmallDelim` makes a delimiter in the + * normal font, but in either text, script, or scriptscript style. + * `makeLargeDelim` makes a delimiter in textstyle, but in one of the Size1, + * Size2, Size3, or Size4 fonts. `makeStackedDelim` makes a delimiter out of + * smaller pieces that are stacked on top of one another. + * + * The functions take a parameter `center`, which determines if the delimiter + * should be centered around the axis. + * + * Then, there are three exposed functions. `sizedDelim` makes a delimiter in + * one of the given sizes. This is used for things like `\bigl`. + * `customSizedDelim` makes a delimiter with a given total height+depth. It is + * called in places like `\sqrt`. `leftRightDelim` makes an appropriate + * delimiter which surrounds an expression of a given height an depth. It is + * used in `\left` and `\right`. + */ + +var ParseError = require("./ParseError"); +var Style = require("./Style"); + +var buildCommon = require("./buildCommon"); +var fontMetrics = require("./fontMetrics"); +var symbols = require("./symbols"); +var utils = require("./utils"); + +var makeSpan = buildCommon.makeSpan; + +/** + * Get the metrics for a given symbol and font, after transformation (i.e. + * after following replacement from symbols.js) + */ +var getMetrics = function(symbol, font) { + if (symbols.math[symbol] && symbols.math[symbol].replace) { + return fontMetrics.getCharacterMetrics( + symbols.math[symbol].replace, font); + } else { + return fontMetrics.getCharacterMetrics( + symbol, font); + } +}; + +/** + * Builds a symbol in the given font size (note size is an integer) + */ +var mathrmSize = function(value, size, mode) { + return buildCommon.makeSymbol(value, "Size" + size + "-Regular", mode); +}; + +/** + * Puts a delimiter span in a given style, and adds appropriate height, depth, + * and maxFontSizes. + */ +var styleWrap = function(delim, toStyle, options) { + var span = makeSpan( + ["style-wrap", options.style.reset(), toStyle.cls()], [delim]); + + var multiplier = toStyle.sizeMultiplier / options.style.sizeMultiplier; + + span.height *= multiplier; + span.depth *= multiplier; + span.maxFontSize = toStyle.sizeMultiplier; + + return span; +}; + +/** + * Makes a small delimiter. This is a delimiter that comes in the Main-Regular + * font, but is restyled to either be in textstyle, scriptstyle, or + * scriptscriptstyle. + */ +var makeSmallDelim = function(delim, style, center, options, mode) { + var text = buildCommon.makeSymbol(delim, "Main-Regular", mode); + + var span = styleWrap(text, style, options); + + if (center) { + var shift = + (1 - options.style.sizeMultiplier / style.sizeMultiplier) * + fontMetrics.metrics.axisHeight; + + span.style.top = shift + "em"; + span.height -= shift; + span.depth += shift; + } + + return span; +}; + +/** + * Makes a large delimiter. This is a delimiter that comes in the Size1, Size2, + * Size3, or Size4 fonts. It is always rendered in textstyle. + */ +var makeLargeDelim = function(delim, size, center, options, mode) { + var inner = mathrmSize(delim, size, mode); + + var span = styleWrap( + makeSpan(["delimsizing", "size" + size], + [inner], options.getColor()), + Style.TEXT, options); + + if (center) { + var shift = (1 - options.style.sizeMultiplier) * + fontMetrics.metrics.axisHeight; + + span.style.top = shift + "em"; + span.height -= shift; + span.depth += shift; + } + + return span; +}; + +/** + * Make an inner span with the given offset and in the given font. This is used + * in `makeStackedDelim` to make the stacking pieces for the delimiter. + */ +var makeInner = function(symbol, font, mode) { + var sizeClass; + // Apply the correct CSS class to choose the right font. + if (font === "Size1-Regular") { + sizeClass = "delim-size1"; + } else if (font === "Size4-Regular") { + sizeClass = "delim-size4"; + } + + var inner = makeSpan( + ["delimsizinginner", sizeClass], + [makeSpan([], [buildCommon.makeSymbol(symbol, font, mode)])]); + + // Since this will be passed into `makeVList` in the end, wrap the element + // in the appropriate tag that VList uses. + return {type: "elem", elem: inner}; +}; + +/** + * Make a stacked delimiter out of a given delimiter, with the total height at + * least `heightTotal`. This routine is mentioned on page 442 of the TeXbook. + */ +var makeStackedDelim = function(delim, heightTotal, center, options, mode) { + // There are four parts, the top, an optional middle, a repeated part, and a + // bottom. + var top; + var middle; + var repeat; + var bottom; + top = repeat = bottom = delim; + middle = null; + // Also keep track of what font the delimiters are in + var font = "Size1-Regular"; + + // We set the parts and font based on the symbol. Note that we use + // '\u23d0' instead of '|' and '\u2016' instead of '\\|' for the + // repeats of the arrows + if (delim === "\\uparrow") { + repeat = bottom = "\u23d0"; + } else if (delim === "\\Uparrow") { + repeat = bottom = "\u2016"; + } else if (delim === "\\downarrow") { + top = repeat = "\u23d0"; + } else if (delim === "\\Downarrow") { + top = repeat = "\u2016"; + } else if (delim === "\\updownarrow") { + top = "\\uparrow"; + repeat = "\u23d0"; + bottom = "\\downarrow"; + } else if (delim === "\\Updownarrow") { + top = "\\Uparrow"; + repeat = "\u2016"; + bottom = "\\Downarrow"; + } else if (delim === "[" || delim === "\\lbrack") { + top = "\u23a1"; + repeat = "\u23a2"; + bottom = "\u23a3"; + font = "Size4-Regular"; + } else if (delim === "]" || delim === "\\rbrack") { + top = "\u23a4"; + repeat = "\u23a5"; + bottom = "\u23a6"; + font = "Size4-Regular"; + } else if (delim === "\\lfloor") { + repeat = top = "\u23a2"; + bottom = "\u23a3"; + font = "Size4-Regular"; + } else if (delim === "\\lceil") { + top = "\u23a1"; + repeat = bottom = "\u23a2"; + font = "Size4-Regular"; + } else if (delim === "\\rfloor") { + repeat = top = "\u23a5"; + bottom = "\u23a6"; + font = "Size4-Regular"; + } else if (delim === "\\rceil") { + top = "\u23a4"; + repeat = bottom = "\u23a5"; + font = "Size4-Regular"; + } else if (delim === "(") { + top = "\u239b"; + repeat = "\u239c"; + bottom = "\u239d"; + font = "Size4-Regular"; + } else if (delim === ")") { + top = "\u239e"; + repeat = "\u239f"; + bottom = "\u23a0"; + font = "Size4-Regular"; + } else if (delim === "\\{" || delim === "\\lbrace") { + top = "\u23a7"; + middle = "\u23a8"; + bottom = "\u23a9"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\}" || delim === "\\rbrace") { + top = "\u23ab"; + middle = "\u23ac"; + bottom = "\u23ad"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\lgroup") { + top = "\u23a7"; + bottom = "\u23a9"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\rgroup") { + top = "\u23ab"; + bottom = "\u23ad"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\lmoustache") { + top = "\u23a7"; + bottom = "\u23ad"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\rmoustache") { + top = "\u23ab"; + bottom = "\u23a9"; + repeat = "\u23aa"; + font = "Size4-Regular"; + } else if (delim === "\\surd") { + top = "\ue001"; + bottom = "\u23b7"; + repeat = "\ue000"; + font = "Size4-Regular"; + } + + // Get the metrics of the four sections + var topMetrics = getMetrics(top, font); + var topHeightTotal = topMetrics.height + topMetrics.depth; + var repeatMetrics = getMetrics(repeat, font); + var repeatHeightTotal = repeatMetrics.height + repeatMetrics.depth; + var bottomMetrics = getMetrics(bottom, font); + var bottomHeightTotal = bottomMetrics.height + bottomMetrics.depth; + var middleHeightTotal = 0; + var middleFactor = 1; + if (middle !== null) { + var middleMetrics = getMetrics(middle, font); + middleHeightTotal = middleMetrics.height + middleMetrics.depth; + middleFactor = 2; // repeat symmetrically above and below middle + } + + // Calcuate the minimal height that the delimiter can have. + // It is at least the size of the top, bottom, and optional middle combined. + var minHeight = topHeightTotal + bottomHeightTotal + middleHeightTotal; + + // Compute the number of copies of the repeat symbol we will need + var repeatCount = Math.ceil( + (heightTotal - minHeight) / (middleFactor * repeatHeightTotal)); + + // Compute the total height of the delimiter including all the symbols + var realHeightTotal = + minHeight + repeatCount * middleFactor * repeatHeightTotal; + + // The center of the delimiter is placed at the center of the axis. Note + // that in this context, "center" means that the delimiter should be + // centered around the axis in the current style, while normally it is + // centered around the axis in textstyle. + var axisHeight = fontMetrics.metrics.axisHeight; + if (center) { + axisHeight *= options.style.sizeMultiplier; + } + // Calculate the depth + var depth = realHeightTotal / 2 - axisHeight; + + // Now, we start building the pieces that will go into the vlist + + // Keep a list of the inner pieces + var inners = []; + + // Add the bottom symbol + inners.push(makeInner(bottom, font, mode)); + + var i; + if (middle === null) { + // Add that many symbols + for (i = 0; i < repeatCount; i++) { + inners.push(makeInner(repeat, font, mode)); + } + } else { + // When there is a middle bit, we need the middle part and two repeated + // sections + for (i = 0; i < repeatCount; i++) { + inners.push(makeInner(repeat, font, mode)); + } + inners.push(makeInner(middle, font, mode)); + for (i = 0; i < repeatCount; i++) { + inners.push(makeInner(repeat, font, mode)); + } + } + + // Add the top symbol + inners.push(makeInner(top, font, mode)); + + // Finally, build the vlist + var inner = buildCommon.makeVList(inners, "bottom", depth, options); + + return styleWrap( + makeSpan(["delimsizing", "mult"], [inner], options.getColor()), + Style.TEXT, options); +}; + +// There are three kinds of delimiters, delimiters that stack when they become +// too large +var stackLargeDelimiters = [ + "(", ")", "[", "\\lbrack", "]", "\\rbrack", + "\\{", "\\lbrace", "\\}", "\\rbrace", + "\\lfloor", "\\rfloor", "\\lceil", "\\rceil", + "\\surd", +]; + +// delimiters that always stack +var stackAlwaysDelimiters = [ + "\\uparrow", "\\downarrow", "\\updownarrow", + "\\Uparrow", "\\Downarrow", "\\Updownarrow", + "|", "\\|", "\\vert", "\\Vert", + "\\lvert", "\\rvert", "\\lVert", "\\rVert", + "\\lgroup", "\\rgroup", "\\lmoustache", "\\rmoustache", +]; + +// and delimiters that never stack +var stackNeverDelimiters = [ + "<", ">", "\\langle", "\\rangle", "/", "\\backslash", "\\lt", "\\gt", +]; + +// Metrics of the different sizes. Found by looking at TeX's output of +// $\bigl| // \Bigl| \biggl| \Biggl| \showlists$ +// Used to create stacked delimiters of appropriate sizes in makeSizedDelim. +var sizeToMaxHeight = [0, 1.2, 1.8, 2.4, 3.0]; + +/** + * Used to create a delimiter of a specific size, where `size` is 1, 2, 3, or 4. + */ +var makeSizedDelim = function(delim, size, options, mode) { + // < and > turn into \langle and \rangle in delimiters + if (delim === "<" || delim === "\\lt") { + delim = "\\langle"; + } else if (delim === ">" || delim === "\\gt") { + delim = "\\rangle"; + } + + // Sized delimiters are never centered. + if (utils.contains(stackLargeDelimiters, delim) || + utils.contains(stackNeverDelimiters, delim)) { + return makeLargeDelim(delim, size, false, options, mode); + } else if (utils.contains(stackAlwaysDelimiters, delim)) { + return makeStackedDelim( + delim, sizeToMaxHeight[size], false, options, mode); + } else { + throw new ParseError("Illegal delimiter: '" + delim + "'"); + } +}; + +/** + * There are three different sequences of delimiter sizes that the delimiters + * follow depending on the kind of delimiter. This is used when creating custom + * sized delimiters to decide whether to create a small, large, or stacked + * delimiter. + * + * In real TeX, these sequences aren't explicitly defined, but are instead + * defined inside the font metrics. Since there are only three sequences that + * are possible for the delimiters that TeX defines, it is easier to just encode + * them explicitly here. + */ + +// Delimiters that never stack try small delimiters and large delimiters only +var stackNeverDelimiterSequence = [ + {type: "small", style: Style.SCRIPTSCRIPT}, + {type: "small", style: Style.SCRIPT}, + {type: "small", style: Style.TEXT}, + {type: "large", size: 1}, + {type: "large", size: 2}, + {type: "large", size: 3}, + {type: "large", size: 4}, +]; + +// Delimiters that always stack try the small delimiters first, then stack +var stackAlwaysDelimiterSequence = [ + {type: "small", style: Style.SCRIPTSCRIPT}, + {type: "small", style: Style.SCRIPT}, + {type: "small", style: Style.TEXT}, + {type: "stack"}, +]; + +// Delimiters that stack when large try the small and then large delimiters, and +// stack afterwards +var stackLargeDelimiterSequence = [ + {type: "small", style: Style.SCRIPTSCRIPT}, + {type: "small", style: Style.SCRIPT}, + {type: "small", style: Style.TEXT}, + {type: "large", size: 1}, + {type: "large", size: 2}, + {type: "large", size: 3}, + {type: "large", size: 4}, + {type: "stack"}, +]; + +/** + * Get the font used in a delimiter based on what kind of delimiter it is. + */ +var delimTypeToFont = function(type) { + if (type.type === "small") { + return "Main-Regular"; + } else if (type.type === "large") { + return "Size" + type.size + "-Regular"; + } else if (type.type === "stack") { + return "Size4-Regular"; + } +}; + +/** + * Traverse a sequence of types of delimiters to decide what kind of delimiter + * should be used to create a delimiter of the given height+depth. + */ +var traverseSequence = function(delim, height, sequence, options) { + // Here, we choose the index we should start at in the sequences. In smaller + // sizes (which correspond to larger numbers in style.size) we start earlier + // in the sequence. Thus, scriptscript starts at index 3-3=0, script starts + // at index 3-2=1, text starts at 3-1=2, and display starts at min(2,3-0)=2 + var start = Math.min(2, 3 - options.style.size); + for (var i = start; i < sequence.length; i++) { + if (sequence[i].type === "stack") { + // This is always the last delimiter, so we just break the loop now. + break; + } + + var metrics = getMetrics(delim, delimTypeToFont(sequence[i])); + var heightDepth = metrics.height + metrics.depth; + + // Small delimiters are scaled down versions of the same font, so we + // account for the style change size. + + if (sequence[i].type === "small") { + heightDepth *= sequence[i].style.sizeMultiplier; + } + + // Check if the delimiter at this size works for the given height. + if (heightDepth > height) { + return sequence[i]; + } + } + + // If we reached the end of the sequence, return the last sequence element. + return sequence[sequence.length - 1]; +}; + +/** + * Make a delimiter of a given height+depth, with optional centering. Here, we + * traverse the sequences, and create a delimiter that the sequence tells us to. + */ +var makeCustomSizedDelim = function(delim, height, center, options, mode) { + if (delim === "<" || delim === "\\lt") { + delim = "\\langle"; + } else if (delim === ">" || delim === "\\gt") { + delim = "\\rangle"; + } + + // Decide what sequence to use + var sequence; + if (utils.contains(stackNeverDelimiters, delim)) { + sequence = stackNeverDelimiterSequence; + } else if (utils.contains(stackLargeDelimiters, delim)) { + sequence = stackLargeDelimiterSequence; + } else { + sequence = stackAlwaysDelimiterSequence; + } + + // Look through the sequence + var delimType = traverseSequence(delim, height, sequence, options); + + // Depending on the sequence element we decided on, call the appropriate + // function. + if (delimType.type === "small") { + return makeSmallDelim(delim, delimType.style, center, options, mode); + } else if (delimType.type === "large") { + return makeLargeDelim(delim, delimType.size, center, options, mode); + } else if (delimType.type === "stack") { + return makeStackedDelim(delim, height, center, options, mode); + } +}; + +/** + * Make a delimiter for use with `\left` and `\right`, given a height and depth + * of an expression that the delimiters surround. + */ +var makeLeftRightDelim = function(delim, height, depth, options, mode) { + // We always center \left/\right delimiters, so the axis is always shifted + var axisHeight = + fontMetrics.metrics.axisHeight * options.style.sizeMultiplier; + + // Taken from TeX source, tex.web, function make_left_right + var delimiterFactor = 901; + var delimiterExtend = 5.0 / fontMetrics.metrics.ptPerEm; + + var maxDistFromAxis = Math.max( + height - axisHeight, depth + axisHeight); + + var totalHeight = Math.max( + // In real TeX, calculations are done using integral values which are + // 65536 per pt, or 655360 per em. So, the division here truncates in + // TeX but doesn't here, producing different results. If we wanted to + // exactly match TeX's calculation, we could do + // Math.floor(655360 * maxDistFromAxis / 500) * + // delimiterFactor / 655360 + // (To see the difference, compare + // x^{x^{\left(\rule{0.1em}{0.68em}\right)}} + // in TeX and KaTeX) + maxDistFromAxis / 500 * delimiterFactor, + 2 * maxDistFromAxis - delimiterExtend); + + // Finally, we defer to `makeCustomSizedDelim` with our calculated total + // height + return makeCustomSizedDelim(delim, totalHeight, true, options, mode); +}; + +module.exports = { + sizedDelim: makeSizedDelim, + customSizedDelim: makeCustomSizedDelim, + leftRightDelim: makeLeftRightDelim, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/domTree.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/domTree.js new file mode 100644 index 000000000..e0d8e925a --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/domTree.js @@ -0,0 +1,269 @@ +/** + * These objects store the data about the DOM nodes we create, as well as some + * extra data. They can then be transformed into real DOM nodes with the + * `toNode` function or HTML markup using `toMarkup`. They are useful for both + * storing extra properties on the nodes, as well as providing a way to easily + * work with the DOM. + * + * Similar functions for working with MathML nodes exist in mathMLTree.js. + */ + +var utils = require("./utils"); + +/** + * Create an HTML className based on a list of classes. In addition to joining + * with spaces, we also remove null or empty classes. + */ +var createClass = function(classes) { + classes = classes.slice(); + for (var i = classes.length - 1; i >= 0; i--) { + if (!classes[i]) { + classes.splice(i, 1); + } + } + + return classes.join(" "); +}; + +/** + * This node represents a span node, with a className, a list of children, and + * an inline style. It also contains information about its height, depth, and + * maxFontSize. + */ +function span(classes, children, height, depth, maxFontSize, style) { + this.classes = classes || []; + this.children = children || []; + this.height = height || 0; + this.depth = depth || 0; + this.maxFontSize = maxFontSize || 0; + this.style = style || {}; + this.attributes = {}; +} + +/** + * Sets an arbitrary attribute on the span. Warning: use this wisely. Not all + * browsers support attributes the same, and having too many custom attributes + * is probably bad. + */ +span.prototype.setAttribute = function(attribute, value) { + this.attributes[attribute] = value; +}; + +/** + * Convert the span into an HTML node + */ +span.prototype.toNode = function() { + var span = document.createElement("span"); + + // Apply the class + span.className = createClass(this.classes); + + // Apply inline styles + for (var style in this.style) { + if (Object.prototype.hasOwnProperty.call(this.style, style)) { + span.style[style] = this.style[style]; + } + } + + // Apply attributes + for (var attr in this.attributes) { + if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { + span.setAttribute(attr, this.attributes[attr]); + } + } + + // Append the children, also as HTML nodes + for (var i = 0; i < this.children.length; i++) { + span.appendChild(this.children[i].toNode()); + } + + return span; +}; + +/** + * Convert the span into an HTML markup string + */ +span.prototype.toMarkup = function() { + var markup = " 0) { + span = document.createElement("span"); + span.style.marginRight = this.italic + "em"; + } + + if (this.classes.length > 0) { + span = span || document.createElement("span"); + span.className = createClass(this.classes); + } + + for (var style in this.style) { + if (this.style.hasOwnProperty(style)) { + span = span || document.createElement("span"); + span.style[style] = this.style[style]; + } + } + + if (span) { + span.appendChild(node); + return span; + } else { + return node; + } +}; + +/** + * Creates markup for a symbol node. + */ +symbolNode.prototype.toMarkup = function() { + // TODO(alpert): More duplication than I'd like from + // span.prototype.toMarkup and symbolNode.prototype.toNode... + var needsSpan = false; + + var markup = " 0) { + styles += "margin-right:" + this.italic + "em;"; + } + for (var style in this.style) { + if (this.style.hasOwnProperty(style)) { + styles += utils.hyphenate(style) + ":" + this.style[style] + ";"; + } + } + + if (styles) { + needsSpan = true; + markup += " style=\"" + utils.escape(styles) + "\""; + } + + var escaped = utils.escape(this.value); + if (needsSpan) { + markup += ">"; + markup += escaped; + markup += ""; + return markup; + } else { + return escaped; + } +}; + +module.exports = { + span: span, + documentFragment: documentFragment, + symbolNode: symbolNode, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/environments.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/environments.js new file mode 100644 index 000000000..32aca626e --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/environments.js @@ -0,0 +1,295 @@ +/* eslint no-constant-condition:0 */ +var fontMetrics = require("./fontMetrics"); +var parseData = require("./parseData"); +var ParseError = require("./ParseError"); + +var ParseNode = parseData.ParseNode; + +/** + * Parse the body of the environment, with rows delimited by \\ and + * columns delimited by &, and create a nested list in row-major order + * with one group per cell. + */ +var q = 0 ; +function parseArray(parser, result) { + var row = []; + var body = [row]; + var rowGaps = []; + + while (true) { + + // if (q == 1) console.error(parser.nextToken.text); + try { + var cell = parser.parseExpression(false, null); + } catch (e) { + // console.error(e); + exit(); + } + // if (q == 1) exit(); + row.push(new ParseNode("ordgroup", cell, parser.mode)); + var next = parser.nextToken.text; + if (next === "&") { + parser.consume(); + } else if (next === "\\end" || next == "}") { + break; + } else if (next === "\\\\" || next === "\\cr") { + + var cr = parser.parseFunction(); + + rowGaps.push(cr.value.size); + row = []; + body.push(row); + } else { + // TODO: Clean up the following hack once #385 got merged + var pos = Math.min(parser.pos + 1, parser.lexer._input.length); + throw new ParseError("Expected & or \\\\ or \\end", + parser.lexer, pos); + } + } + result.body = body; + result.rowGaps = rowGaps; + // if (q == 1) exit(); + var node = new ParseNode(result.type, result, parser.mode); + return node; +} + +/* + * An environment definition is very similar to a function definition: + * it is declared with a name or a list of names, a set of properties + * and a handler containing the actual implementation. + * + * The properties include: + * - numArgs: The number of arguments after the \begin{name} function. + * - argTypes: (optional) Just like for a function + * - allowedInText: (optional) Whether or not the environment is allowed inside + * text mode (default false) (not enforced yet) + * - numOptionalArgs: (optional) Just like for a function + * A bare number instead of that object indicates the numArgs value. + * + * The handler function will receive two arguments + * - context: information and references provided by the parser + * - args: an array of arguments passed to \begin{name} + * The context contains the following properties: + * - envName: the name of the environment, one of the listed names. + * - parser: the parser object + * - lexer: the lexer object + * - positions: the positions associated with these arguments from args. + * The handler must return a ParseResult. + */ + +function defineEnvironment(names, props, handler) { + if (typeof names === "string") { + names = [names]; + } + if (typeof props === "number") { + props = { numArgs: props }; + } + // Set default values of environments + var data = { + numArgs: props.numArgs || 0, + argTypes: props.argTypes, + greediness: 1, + allowedInText: !!props.allowedInText, + numOptionalArgs: props.numOptionalArgs || 0, + handler: handler, + }; + for (var i = 0; i < names.length; ++i) { + module.exports[names[i]] = data; + } +} + +// Arrays are part of LaTeX, defined in lttab.dtx so its documentation +// is part of the source2e.pdf file of LaTeX2e source documentation. +defineEnvironment("array", { + numArgs: 1, +}, function(context, args) { + var colalign = args[0]; + colalign = colalign.value.map ? colalign.value : [colalign]; + var cols = colalign.map(function(node) { + var ca = node.value; + if ("lcr".indexOf(ca) !== -1) { + return { + type: "align", + align: ca, + }; + } else if (ca === "|") { + return { + type: "separator", + separator: "|", + }; + } + // throw new ParseError( + // "Unknown column alignment: " + node.value, + // context.lexer, context.positions[1]); + }); + var res = { + type: "array", + cols: cols, + hskipBeforeAndAfter: true, // \@preamble in lttab.dtx + }; + res = parseArray(context.parser, res); + return res; +}); + +defineEnvironment("tabular", { + numArgs: 1, +}, function(context, args) { + var colalign = args[0]; + colalign = colalign.value.map ? colalign.value : [colalign]; + var cols = colalign.map(function(node) { + var ca = node.value; + if ("lcr".indexOf(ca) !== -1) { + return { + type: "align", + align: ca, + }; + } else if (ca === "|") { + return { + type: "separator", + separator: "|", + }; + } + // throw new ParseError( + // "Unknown column alignment: " + node.value, + // context.lexer, context.positions[1]); + }); + var res = { + type: "array", + style: "tabular", + cols: cols, + hskipBeforeAndAfter: true, // \@preamble in lttab.dtx + }; + res = parseArray(context.parser, res); + return res; +}); + +// The matrix environments of amsmath builds on the array environment +// of LaTeX, which is discussed above. +defineEnvironment([ + "matrix", + "pmatrix", + "bmatrix", + "Bmatrix", + "vmatrix", + "Vmatrix", +], { +}, function(context) { + var delimiters = { + "matrix": null, + "pmatrix": ["(", ")"], + "bmatrix": ["[", "]"], + "Bmatrix": ["\\{", "\\}"], + "vmatrix": ["|", "|"], + "Vmatrix": ["\\Vert", "\\Vert"], + }[context.envName]; + var res = { + type: "array", + hskipBeforeAndAfter: false, // \hskip -\arraycolsep in amsmath + }; + q = 1; + res = parseArray(context.parser, res); + + if (delimiters) { + res = new ParseNode("leftright", { + body: [res], + left: delimiters[0], + right: delimiters[1], + }, context.mode); + } + return res; +}); + +// A cases environment (in amsmath.sty) is almost equivalent to +// \def\arraystretch{1.2}% +// \left\{\begin{array}{@{}l@{\quad}l@{}} … \end{array}\right. +defineEnvironment("picture", { +}, function(context) { + var res = { + type: "array", + arraystretch: 1.2, + cols: [{ + type: "align", + align: "l", + pregap: 0, + postgap: fontMetrics.metrics.quad, + }, { + type: "align", + align: "l", + pregap: 0, + postgap: 0, + }], + }; + res = parseArray(context.parser, res); + res = new ParseNode("leftright", { + body: [res], + left: "\\{", + right: ".", + }, context.mode); + return res; +}); + +defineEnvironment("cases", { +}, function(context) { + var res = { + type: "array", + arraystretch: 1.2, + cols: [{ + type: "align", + align: "l", + pregap: 0, + postgap: fontMetrics.metrics.quad, + }, { + type: "align", + align: "l", + pregap: 0, + postgap: 0, + }], + }; + res = parseArray(context.parser, res); + res = new ParseNode("leftright", { + body: [res], + left: "\\{", + right: ".", + }, context.mode); + return res; +}); + +// An aligned environment is like the align* environment +// except it operates within math mode. +// Note that we assume \nomallineskiplimit to be zero, +// so that \strut@ is the same as \strut. +defineEnvironment("aligned", { +}, function(context) { + var res = { + type: "array", + cols: [], + }; + res = parseArray(context.parser, res); + var emptyGroup = new ParseNode("ordgroup", [], context.mode); + var numCols = 0; + res.value.body.forEach(function(row) { + var i; + for (i = 1; i < row.length; i += 2) { + row[i].value.unshift(emptyGroup); + } + if (numCols < row.length) { + numCols = row.length; + } + }); + for (var i = 0; i < numCols; ++i) { + var align = "r"; + var pregap = 0; + if (i % 2 === 1) { + align = "l"; + } else if (i > 0) { + pregap = 2; // one \qquad between columns + } + res.value.cols[i] = { + type: "align", + align: align, + pregap: pregap, + postgap: 0, + }; + } + return res; +}); diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetrics.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetrics.js new file mode 100644 index 000000000..db9e44bfa --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetrics.js @@ -0,0 +1,147 @@ +/* eslint no-unused-vars:0 */ + +var Style = require("./Style"); + +/** + * This file contains metrics regarding fonts and individual symbols. The sigma + * and xi variables, as well as the metricMap map contain data extracted from + * TeX, TeX font metrics, and the TTF files. These data are then exposed via the + * `metrics` variable and the getCharacterMetrics function. + */ + +// These font metrics are extracted from TeX by using +// \font\a=cmmi10 +// \showthe\fontdimenX\a +// where X is the corresponding variable number. These correspond to the font +// parameters of the symbol fonts. In TeX, there are actually three sets of +// dimensions, one for each of textstyle, scriptstyle, and scriptscriptstyle, +// but we only use the textstyle ones, and scale certain dimensions accordingly. +// See the TeXbook, page 441. +var sigma1 = 0.025; +var sigma2 = 0; +var sigma3 = 0; +var sigma4 = 0; +var sigma5 = 0.431; +var sigma6 = 1; +var sigma7 = 0; +var sigma8 = 0.677; +var sigma9 = 0.394; +var sigma10 = 0.444; +var sigma11 = 0.686; +var sigma12 = 0.345; +var sigma13 = 0.413; +var sigma14 = 0.363; +var sigma15 = 0.289; +var sigma16 = 0.150; +var sigma17 = 0.247; +var sigma18 = 0.386; +var sigma19 = 0.050; +var sigma20 = 2.390; +var sigma21 = 1.01; +var sigma21Script = 0.81; +var sigma21ScriptScript = 0.71; +var sigma22 = 0.250; + +// These font metrics are extracted from TeX by using +// \font\a=cmex10 +// \showthe\fontdimenX\a +// where X is the corresponding variable number. These correspond to the font +// parameters of the extension fonts (family 3). See the TeXbook, page 441. +var xi1 = 0; +var xi2 = 0; +var xi3 = 0; +var xi4 = 0; +var xi5 = 0.431; +var xi6 = 1; +var xi7 = 0; +var xi8 = 0.04; +var xi9 = 0.111; +var xi10 = 0.166; +var xi11 = 0.2; +var xi12 = 0.6; +var xi13 = 0.1; + +// This value determines how large a pt is, for metrics which are defined in +// terms of pts. +// This value is also used in katex.less; if you change it make sure the values +// match. +var ptPerEm = 10.0; + +// The space between adjacent `|` columns in an array definition. From +// `\showthe\doublerulesep` in LaTeX. +var doubleRuleSep = 2.0 / ptPerEm; + +/** + * This is just a mapping from common names to real metrics + */ +var metrics = { + xHeight: sigma5, + quad: sigma6, + num1: sigma8, + num2: sigma9, + num3: sigma10, + denom1: sigma11, + denom2: sigma12, + sup1: sigma13, + sup2: sigma14, + sup3: sigma15, + sub1: sigma16, + sub2: sigma17, + supDrop: sigma18, + subDrop: sigma19, + axisHeight: sigma22, + defaultRuleThickness: xi8, + bigOpSpacing1: xi9, + bigOpSpacing2: xi10, + bigOpSpacing3: xi11, + bigOpSpacing4: xi12, + bigOpSpacing5: xi13, + ptPerEm: ptPerEm, + emPerEx: sigma5 / sigma6, + doubleRuleSep: doubleRuleSep, + + // TODO(alpert): Missing parallel structure here. We should probably add + // style-specific metrics for all of these. + delim1: sigma20, + getDelim2: function(style) { + if (style.size === Style.TEXT.size) { + return sigma21; + } else if (style.size === Style.SCRIPT.size) { + return sigma21Script; + } else if (style.size === Style.SCRIPTSCRIPT.size) { + return sigma21ScriptScript; + } + throw new Error("Unexpected style size: " + style.size); + }, +}; + +// This map contains a mapping from font name and character code to character +// metrics, including height, depth, italic correction, and skew (kern from the +// character to the corresponding \skewchar) +// This map is generated via `make metrics`. It should not be changed manually. +var metricMap = require("./fontMetricsData"); + +/** + * This function is a convenience function for looking up information in the + * metricMap table. It takes a character as a string, and a style. + * + * Note: the `width` property may be undefined if fontMetricsData.js wasn't + * built using `Make extended_metrics`. + */ +var getCharacterMetrics = function(character, style) { + var metrics = metricMap[style][character.charCodeAt(0)]; + if (metrics) { + return { + depth: metrics[0], + height: metrics[1], + italic: metrics[2], + skew: metrics[3], + width: metrics[4], + }; + } +}; + +module.exports = { + metrics: metrics, + getCharacterMetrics: getCharacterMetrics, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetricsData.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetricsData.js new file mode 100644 index 000000000..957f55b87 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/fontMetricsData.js @@ -0,0 +1,1752 @@ +module.exports = { + "AMS-Regular": { + "65": [0, 0.68889, 0, 0], + "66": [0, 0.68889, 0, 0], + "67": [0, 0.68889, 0, 0], + "68": [0, 0.68889, 0, 0], + "69": [0, 0.68889, 0, 0], + "70": [0, 0.68889, 0, 0], + "71": [0, 0.68889, 0, 0], + "72": [0, 0.68889, 0, 0], + "73": [0, 0.68889, 0, 0], + "74": [0.16667, 0.68889, 0, 0], + "75": [0, 0.68889, 0, 0], + "76": [0, 0.68889, 0, 0], + "77": [0, 0.68889, 0, 0], + "78": [0, 0.68889, 0, 0], + "79": [0.16667, 0.68889, 0, 0], + "80": [0, 0.68889, 0, 0], + "81": [0.16667, 0.68889, 0, 0], + "82": [0, 0.68889, 0, 0], + "83": [0, 0.68889, 0, 0], + "84": [0, 0.68889, 0, 0], + "85": [0, 0.68889, 0, 0], + "86": [0, 0.68889, 0, 0], + "87": [0, 0.68889, 0, 0], + "88": [0, 0.68889, 0, 0], + "89": [0, 0.68889, 0, 0], + "90": [0, 0.68889, 0, 0], + "107": [0, 0.68889, 0, 0], + "165": [0, 0.675, 0.025, 0], + "174": [0.15559, 0.69224, 0, 0], + "240": [0, 0.68889, 0, 0], + "295": [0, 0.68889, 0, 0], + "710": [0, 0.825, 0, 0], + "732": [0, 0.9, 0, 0], + "770": [0, 0.825, 0, 0], + "771": [0, 0.9, 0, 0], + "989": [0.08167, 0.58167, 0, 0], + "1008": [0, 0.43056, 0.04028, 0], + "8245": [0, 0.54986, 0, 0], + "8463": [0, 0.68889, 0, 0], + "8487": [0, 0.68889, 0, 0], + "8498": [0, 0.68889, 0, 0], + "8502": [0, 0.68889, 0, 0], + "8503": [0, 0.68889, 0, 0], + "8504": [0, 0.68889, 0, 0], + "8513": [0, 0.68889, 0, 0], + "8592": [-0.03598, 0.46402, 0, 0], + "8594": [-0.03598, 0.46402, 0, 0], + "8602": [-0.13313, 0.36687, 0, 0], + "8603": [-0.13313, 0.36687, 0, 0], + "8606": [0.01354, 0.52239, 0, 0], + "8608": [0.01354, 0.52239, 0, 0], + "8610": [0.01354, 0.52239, 0, 0], + "8611": [0.01354, 0.52239, 0, 0], + "8619": [0, 0.54986, 0, 0], + "8620": [0, 0.54986, 0, 0], + "8621": [-0.13313, 0.37788, 0, 0], + "8622": [-0.13313, 0.36687, 0, 0], + "8624": [0, 0.69224, 0, 0], + "8625": [0, 0.69224, 0, 0], + "8630": [0, 0.43056, 0, 0], + "8631": [0, 0.43056, 0, 0], + "8634": [0.08198, 0.58198, 0, 0], + "8635": [0.08198, 0.58198, 0, 0], + "8638": [0.19444, 0.69224, 0, 0], + "8639": [0.19444, 0.69224, 0, 0], + "8642": [0.19444, 0.69224, 0, 0], + "8643": [0.19444, 0.69224, 0, 0], + "8644": [0.1808, 0.675, 0, 0], + "8646": [0.1808, 0.675, 0, 0], + "8647": [0.1808, 0.675, 0, 0], + "8648": [0.19444, 0.69224, 0, 0], + "8649": [0.1808, 0.675, 0, 0], + "8650": [0.19444, 0.69224, 0, 0], + "8651": [0.01354, 0.52239, 0, 0], + "8652": [0.01354, 0.52239, 0, 0], + "8653": [-0.13313, 0.36687, 0, 0], + "8654": [-0.13313, 0.36687, 0, 0], + "8655": [-0.13313, 0.36687, 0, 0], + "8666": [0.13667, 0.63667, 0, 0], + "8667": [0.13667, 0.63667, 0, 0], + "8669": [-0.13313, 0.37788, 0, 0], + "8672": [-0.064, 0.437, 0, 0], + "8674": [-0.064, 0.437, 0, 0], + "8705": [0, 0.825, 0, 0], + "8708": [0, 0.68889, 0, 0], + "8709": [0.08167, 0.58167, 0, 0], + "8717": [0, 0.43056, 0, 0], + "8722": [-0.03598, 0.46402, 0, 0], + "8724": [0.08198, 0.69224, 0, 0], + "8726": [0.08167, 0.58167, 0, 0], + "8733": [0, 0.69224, 0, 0], + "8736": [0, 0.69224, 0, 0], + "8737": [0, 0.69224, 0, 0], + "8738": [0.03517, 0.52239, 0, 0], + "8739": [0.08167, 0.58167, 0, 0], + "8740": [0.25142, 0.74111, 0, 0], + "8741": [0.08167, 0.58167, 0, 0], + "8742": [0.25142, 0.74111, 0, 0], + "8756": [0, 0.69224, 0, 0], + "8757": [0, 0.69224, 0, 0], + "8764": [-0.13313, 0.36687, 0, 0], + "8765": [-0.13313, 0.37788, 0, 0], + "8769": [-0.13313, 0.36687, 0, 0], + "8770": [-0.03625, 0.46375, 0, 0], + "8774": [0.30274, 0.79383, 0, 0], + "8776": [-0.01688, 0.48312, 0, 0], + "8778": [0.08167, 0.58167, 0, 0], + "8782": [0.06062, 0.54986, 0, 0], + "8783": [0.06062, 0.54986, 0, 0], + "8785": [0.08198, 0.58198, 0, 0], + "8786": [0.08198, 0.58198, 0, 0], + "8787": [0.08198, 0.58198, 0, 0], + "8790": [0, 0.69224, 0, 0], + "8791": [0.22958, 0.72958, 0, 0], + "8796": [0.08198, 0.91667, 0, 0], + "8806": [0.25583, 0.75583, 0, 0], + "8807": [0.25583, 0.75583, 0, 0], + "8808": [0.25142, 0.75726, 0, 0], + "8809": [0.25142, 0.75726, 0, 0], + "8812": [0.25583, 0.75583, 0, 0], + "8814": [0.20576, 0.70576, 0, 0], + "8815": [0.20576, 0.70576, 0, 0], + "8816": [0.30274, 0.79383, 0, 0], + "8817": [0.30274, 0.79383, 0, 0], + "8818": [0.22958, 0.72958, 0, 0], + "8819": [0.22958, 0.72958, 0, 0], + "8822": [0.1808, 0.675, 0, 0], + "8823": [0.1808, 0.675, 0, 0], + "8828": [0.13667, 0.63667, 0, 0], + "8829": [0.13667, 0.63667, 0, 0], + "8830": [0.22958, 0.72958, 0, 0], + "8831": [0.22958, 0.72958, 0, 0], + "8832": [0.20576, 0.70576, 0, 0], + "8833": [0.20576, 0.70576, 0, 0], + "8840": [0.30274, 0.79383, 0, 0], + "8841": [0.30274, 0.79383, 0, 0], + "8842": [0.13597, 0.63597, 0, 0], + "8843": [0.13597, 0.63597, 0, 0], + "8847": [0.03517, 0.54986, 0, 0], + "8848": [0.03517, 0.54986, 0, 0], + "8858": [0.08198, 0.58198, 0, 0], + "8859": [0.08198, 0.58198, 0, 0], + "8861": [0.08198, 0.58198, 0, 0], + "8862": [0, 0.675, 0, 0], + "8863": [0, 0.675, 0, 0], + "8864": [0, 0.675, 0, 0], + "8865": [0, 0.675, 0, 0], + "8872": [0, 0.69224, 0, 0], + "8873": [0, 0.69224, 0, 0], + "8874": [0, 0.69224, 0, 0], + "8876": [0, 0.68889, 0, 0], + "8877": [0, 0.68889, 0, 0], + "8878": [0, 0.68889, 0, 0], + "8879": [0, 0.68889, 0, 0], + "8882": [0.03517, 0.54986, 0, 0], + "8883": [0.03517, 0.54986, 0, 0], + "8884": [0.13667, 0.63667, 0, 0], + "8885": [0.13667, 0.63667, 0, 0], + "8888": [0, 0.54986, 0, 0], + "8890": [0.19444, 0.43056, 0, 0], + "8891": [0.19444, 0.69224, 0, 0], + "8892": [0.19444, 0.69224, 0, 0], + "8901": [0, 0.54986, 0, 0], + "8903": [0.08167, 0.58167, 0, 0], + "8905": [0.08167, 0.58167, 0, 0], + "8906": [0.08167, 0.58167, 0, 0], + "8907": [0, 0.69224, 0, 0], + "8908": [0, 0.69224, 0, 0], + "8909": [-0.03598, 0.46402, 0, 0], + "8910": [0, 0.54986, 0, 0], + "8911": [0, 0.54986, 0, 0], + "8912": [0.03517, 0.54986, 0, 0], + "8913": [0.03517, 0.54986, 0, 0], + "8914": [0, 0.54986, 0, 0], + "8915": [0, 0.54986, 0, 0], + "8916": [0, 0.69224, 0, 0], + "8918": [0.0391, 0.5391, 0, 0], + "8919": [0.0391, 0.5391, 0, 0], + "8920": [0.03517, 0.54986, 0, 0], + "8921": [0.03517, 0.54986, 0, 0], + "8922": [0.38569, 0.88569, 0, 0], + "8923": [0.38569, 0.88569, 0, 0], + "8926": [0.13667, 0.63667, 0, 0], + "8927": [0.13667, 0.63667, 0, 0], + "8928": [0.30274, 0.79383, 0, 0], + "8929": [0.30274, 0.79383, 0, 0], + "8934": [0.23222, 0.74111, 0, 0], + "8935": [0.23222, 0.74111, 0, 0], + "8936": [0.23222, 0.74111, 0, 0], + "8937": [0.23222, 0.74111, 0, 0], + "8938": [0.20576, 0.70576, 0, 0], + "8939": [0.20576, 0.70576, 0, 0], + "8940": [0.30274, 0.79383, 0, 0], + "8941": [0.30274, 0.79383, 0, 0], + "8994": [0.19444, 0.69224, 0, 0], + "8995": [0.19444, 0.69224, 0, 0], + "9416": [0.15559, 0.69224, 0, 0], + "9484": [0, 0.69224, 0, 0], + "9488": [0, 0.69224, 0, 0], + "9492": [0, 0.37788, 0, 0], + "9496": [0, 0.37788, 0, 0], + "9585": [0.19444, 0.68889, 0, 0], + "9586": [0.19444, 0.74111, 0, 0], + "9632": [0, 0.675, 0, 0], + "9633": [0, 0.675, 0, 0], + "9650": [0, 0.54986, 0, 0], + "9651": [0, 0.54986, 0, 0], + "9654": [0.03517, 0.54986, 0, 0], + "9660": [0, 0.54986, 0, 0], + "9661": [0, 0.54986, 0, 0], + "9664": [0.03517, 0.54986, 0, 0], + "9674": [0.11111, 0.69224, 0, 0], + "9733": [0.19444, 0.69224, 0, 0], + "10003": [0, 0.69224, 0, 0], + "10016": [0, 0.69224, 0, 0], + "10731": [0.11111, 0.69224, 0, 0], + "10846": [0.19444, 0.75583, 0, 0], + "10877": [0.13667, 0.63667, 0, 0], + "10878": [0.13667, 0.63667, 0, 0], + "10885": [0.25583, 0.75583, 0, 0], + "10886": [0.25583, 0.75583, 0, 0], + "10887": [0.13597, 0.63597, 0, 0], + "10888": [0.13597, 0.63597, 0, 0], + "10889": [0.26167, 0.75726, 0, 0], + "10890": [0.26167, 0.75726, 0, 0], + "10891": [0.48256, 0.98256, 0, 0], + "10892": [0.48256, 0.98256, 0, 0], + "10901": [0.13667, 0.63667, 0, 0], + "10902": [0.13667, 0.63667, 0, 0], + "10933": [0.25142, 0.75726, 0, 0], + "10934": [0.25142, 0.75726, 0, 0], + "10935": [0.26167, 0.75726, 0, 0], + "10936": [0.26167, 0.75726, 0, 0], + "10937": [0.26167, 0.75726, 0, 0], + "10938": [0.26167, 0.75726, 0, 0], + "10949": [0.25583, 0.75583, 0, 0], + "10950": [0.25583, 0.75583, 0, 0], + "10955": [0.28481, 0.79383, 0, 0], + "10956": [0.28481, 0.79383, 0, 0], + "57350": [0.08167, 0.58167, 0, 0], + "57351": [0.08167, 0.58167, 0, 0], + "57352": [0.08167, 0.58167, 0, 0], + "57353": [0, 0.43056, 0.04028, 0], + "57356": [0.25142, 0.75726, 0, 0], + "57357": [0.25142, 0.75726, 0, 0], + "57358": [0.41951, 0.91951, 0, 0], + "57359": [0.30274, 0.79383, 0, 0], + "57360": [0.30274, 0.79383, 0, 0], + "57361": [0.41951, 0.91951, 0, 0], + "57366": [0.25142, 0.75726, 0, 0], + "57367": [0.25142, 0.75726, 0, 0], + "57368": [0.25142, 0.75726, 0, 0], + "57369": [0.25142, 0.75726, 0, 0], + "57370": [0.13597, 0.63597, 0, 0], + "57371": [0.13597, 0.63597, 0, 0], + }, + "Caligraphic-Regular": { + "48": [0, 0.43056, 0, 0], + "49": [0, 0.43056, 0, 0], + "50": [0, 0.43056, 0, 0], + "51": [0.19444, 0.43056, 0, 0], + "52": [0.19444, 0.43056, 0, 0], + "53": [0.19444, 0.43056, 0, 0], + "54": [0, 0.64444, 0, 0], + "55": [0.19444, 0.43056, 0, 0], + "56": [0, 0.64444, 0, 0], + "57": [0.19444, 0.43056, 0, 0], + "65": [0, 0.68333, 0, 0.19445], + "66": [0, 0.68333, 0.03041, 0.13889], + "67": [0, 0.68333, 0.05834, 0.13889], + "68": [0, 0.68333, 0.02778, 0.08334], + "69": [0, 0.68333, 0.08944, 0.11111], + "70": [0, 0.68333, 0.09931, 0.11111], + "71": [0.09722, 0.68333, 0.0593, 0.11111], + "72": [0, 0.68333, 0.00965, 0.11111], + "73": [0, 0.68333, 0.07382, 0], + "74": [0.09722, 0.68333, 0.18472, 0.16667], + "75": [0, 0.68333, 0.01445, 0.05556], + "76": [0, 0.68333, 0, 0.13889], + "77": [0, 0.68333, 0, 0.13889], + "78": [0, 0.68333, 0.14736, 0.08334], + "79": [0, 0.68333, 0.02778, 0.11111], + "80": [0, 0.68333, 0.08222, 0.08334], + "81": [0.09722, 0.68333, 0, 0.11111], + "82": [0, 0.68333, 0, 0.08334], + "83": [0, 0.68333, 0.075, 0.13889], + "84": [0, 0.68333, 0.25417, 0], + "85": [0, 0.68333, 0.09931, 0.08334], + "86": [0, 0.68333, 0.08222, 0], + "87": [0, 0.68333, 0.08222, 0.08334], + "88": [0, 0.68333, 0.14643, 0.13889], + "89": [0.09722, 0.68333, 0.08222, 0.08334], + "90": [0, 0.68333, 0.07944, 0.13889], + }, + "Fraktur-Regular": { + "33": [0, 0.69141, 0, 0], + "34": [0, 0.69141, 0, 0], + "38": [0, 0.69141, 0, 0], + "39": [0, 0.69141, 0, 0], + "40": [0.24982, 0.74947, 0, 0], + "41": [0.24982, 0.74947, 0, 0], + "42": [0, 0.62119, 0, 0], + "43": [0.08319, 0.58283, 0, 0], + "44": [0, 0.10803, 0, 0], + "45": [0.08319, 0.58283, 0, 0], + "46": [0, 0.10803, 0, 0], + "47": [0.24982, 0.74947, 0, 0], + "48": [0, 0.47534, 0, 0], + "49": [0, 0.47534, 0, 0], + "50": [0, 0.47534, 0, 0], + "51": [0.18906, 0.47534, 0, 0], + "52": [0.18906, 0.47534, 0, 0], + "53": [0.18906, 0.47534, 0, 0], + "54": [0, 0.69141, 0, 0], + "55": [0.18906, 0.47534, 0, 0], + "56": [0, 0.69141, 0, 0], + "57": [0.18906, 0.47534, 0, 0], + "58": [0, 0.47534, 0, 0], + "59": [0.12604, 0.47534, 0, 0], + "61": [-0.13099, 0.36866, 0, 0], + "63": [0, 0.69141, 0, 0], + "65": [0, 0.69141, 0, 0], + "66": [0, 0.69141, 0, 0], + "67": [0, 0.69141, 0, 0], + "68": [0, 0.69141, 0, 0], + "69": [0, 0.69141, 0, 0], + "70": [0.12604, 0.69141, 0, 0], + "71": [0, 0.69141, 0, 0], + "72": [0.06302, 0.69141, 0, 0], + "73": [0, 0.69141, 0, 0], + "74": [0.12604, 0.69141, 0, 0], + "75": [0, 0.69141, 0, 0], + "76": [0, 0.69141, 0, 0], + "77": [0, 0.69141, 0, 0], + "78": [0, 0.69141, 0, 0], + "79": [0, 0.69141, 0, 0], + "80": [0.18906, 0.69141, 0, 0], + "81": [0.03781, 0.69141, 0, 0], + "82": [0, 0.69141, 0, 0], + "83": [0, 0.69141, 0, 0], + "84": [0, 0.69141, 0, 0], + "85": [0, 0.69141, 0, 0], + "86": [0, 0.69141, 0, 0], + "87": [0, 0.69141, 0, 0], + "88": [0, 0.69141, 0, 0], + "89": [0.18906, 0.69141, 0, 0], + "90": [0.12604, 0.69141, 0, 0], + "91": [0.24982, 0.74947, 0, 0], + "93": [0.24982, 0.74947, 0, 0], + "94": [0, 0.69141, 0, 0], + "97": [0, 0.47534, 0, 0], + "98": [0, 0.69141, 0, 0], + "99": [0, 0.47534, 0, 0], + "100": [0, 0.62119, 0, 0], + "101": [0, 0.47534, 0, 0], + "102": [0.18906, 0.69141, 0, 0], + "103": [0.18906, 0.47534, 0, 0], + "104": [0.18906, 0.69141, 0, 0], + "105": [0, 0.69141, 0, 0], + "106": [0, 0.69141, 0, 0], + "107": [0, 0.69141, 0, 0], + "108": [0, 0.69141, 0, 0], + "109": [0, 0.47534, 0, 0], + "110": [0, 0.47534, 0, 0], + "111": [0, 0.47534, 0, 0], + "112": [0.18906, 0.52396, 0, 0], + "113": [0.18906, 0.47534, 0, 0], + "114": [0, 0.47534, 0, 0], + "115": [0, 0.47534, 0, 0], + "116": [0, 0.62119, 0, 0], + "117": [0, 0.47534, 0, 0], + "118": [0, 0.52396, 0, 0], + "119": [0, 0.52396, 0, 0], + "120": [0.18906, 0.47534, 0, 0], + "121": [0.18906, 0.47534, 0, 0], + "122": [0.18906, 0.47534, 0, 0], + "8216": [0, 0.69141, 0, 0], + "8217": [0, 0.69141, 0, 0], + "58112": [0, 0.62119, 0, 0], + "58113": [0, 0.62119, 0, 0], + "58114": [0.18906, 0.69141, 0, 0], + "58115": [0.18906, 0.69141, 0, 0], + "58116": [0.18906, 0.47534, 0, 0], + "58117": [0, 0.69141, 0, 0], + "58118": [0, 0.62119, 0, 0], + "58119": [0, 0.47534, 0, 0], + }, + "Main-Bold": { + "33": [0, 0.69444, 0, 0], + "34": [0, 0.69444, 0, 0], + "35": [0.19444, 0.69444, 0, 0], + "36": [0.05556, 0.75, 0, 0], + "37": [0.05556, 0.75, 0, 0], + "38": [0, 0.69444, 0, 0], + "39": [0, 0.69444, 0, 0], + "40": [0.25, 0.75, 0, 0], + "41": [0.25, 0.75, 0, 0], + "42": [0, 0.75, 0, 0], + "43": [0.13333, 0.63333, 0, 0], + "44": [0.19444, 0.15556, 0, 0], + "45": [0, 0.44444, 0, 0], + "46": [0, 0.15556, 0, 0], + "47": [0.25, 0.75, 0, 0], + "48": [0, 0.64444, 0, 0], + "49": [0, 0.64444, 0, 0], + "50": [0, 0.64444, 0, 0], + "51": [0, 0.64444, 0, 0], + "52": [0, 0.64444, 0, 0], + "53": [0, 0.64444, 0, 0], + "54": [0, 0.64444, 0, 0], + "55": [0, 0.64444, 0, 0], + "56": [0, 0.64444, 0, 0], + "57": [0, 0.64444, 0, 0], + "58": [0, 0.44444, 0, 0], + "59": [0.19444, 0.44444, 0, 0], + "60": [0.08556, 0.58556, 0, 0], + "61": [-0.10889, 0.39111, 0, 0], + "62": [0.08556, 0.58556, 0, 0], + "63": [0, 0.69444, 0, 0], + "64": [0, 0.69444, 0, 0], + "65": [0, 0.68611, 0, 0], + "66": [0, 0.68611, 0, 0], + "67": [0, 0.68611, 0, 0], + "68": [0, 0.68611, 0, 0], + "69": [0, 0.68611, 0, 0], + "70": [0, 0.68611, 0, 0], + "71": [0, 0.68611, 0, 0], + "72": [0, 0.68611, 0, 0], + "73": [0, 0.68611, 0, 0], + "74": [0, 0.68611, 0, 0], + "75": [0, 0.68611, 0, 0], + "76": [0, 0.68611, 0, 0], + "77": [0, 0.68611, 0, 0], + "78": [0, 0.68611, 0, 0], + "79": [0, 0.68611, 0, 0], + "80": [0, 0.68611, 0, 0], + "81": [0.19444, 0.68611, 0, 0], + "82": [0, 0.68611, 0, 0], + "83": [0, 0.68611, 0, 0], + "84": [0, 0.68611, 0, 0], + "85": [0, 0.68611, 0, 0], + "86": [0, 0.68611, 0.01597, 0], + "87": [0, 0.68611, 0.01597, 0], + "88": [0, 0.68611, 0, 0], + "89": [0, 0.68611, 0.02875, 0], + "90": [0, 0.68611, 0, 0], + "91": [0.25, 0.75, 0, 0], + "92": [0.25, 0.75, 0, 0], + "93": [0.25, 0.75, 0, 0], + "94": [0, 0.69444, 0, 0], + "95": [0.31, 0.13444, 0.03194, 0], + "96": [0, 0.69444, 0, 0], + "97": [0, 0.44444, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.44444, 0, 0], + "100": [0, 0.69444, 0, 0], + "101": [0, 0.44444, 0, 0], + "102": [0, 0.69444, 0.10903, 0], + "103": [0.19444, 0.44444, 0.01597, 0], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.69444, 0, 0], + "106": [0.19444, 0.69444, 0, 0], + "107": [0, 0.69444, 0, 0], + "108": [0, 0.69444, 0, 0], + "109": [0, 0.44444, 0, 0], + "110": [0, 0.44444, 0, 0], + "111": [0, 0.44444, 0, 0], + "112": [0.19444, 0.44444, 0, 0], + "113": [0.19444, 0.44444, 0, 0], + "114": [0, 0.44444, 0, 0], + "115": [0, 0.44444, 0, 0], + "116": [0, 0.63492, 0, 0], + "117": [0, 0.44444, 0, 0], + "118": [0, 0.44444, 0.01597, 0], + "119": [0, 0.44444, 0.01597, 0], + "120": [0, 0.44444, 0, 0], + "121": [0.19444, 0.44444, 0.01597, 0], + "122": [0, 0.44444, 0, 0], + "123": [0.25, 0.75, 0, 0], + "124": [0.25, 0.75, 0, 0], + "125": [0.25, 0.75, 0, 0], + "126": [0.35, 0.34444, 0, 0], + "168": [0, 0.69444, 0, 0], + "172": [0, 0.44444, 0, 0], + "175": [0, 0.59611, 0, 0], + "176": [0, 0.69444, 0, 0], + "177": [0.13333, 0.63333, 0, 0], + "180": [0, 0.69444, 0, 0], + "215": [0.13333, 0.63333, 0, 0], + "247": [0.13333, 0.63333, 0, 0], + "305": [0, 0.44444, 0, 0], + "567": [0.19444, 0.44444, 0, 0], + "710": [0, 0.69444, 0, 0], + "711": [0, 0.63194, 0, 0], + "713": [0, 0.59611, 0, 0], + "714": [0, 0.69444, 0, 0], + "715": [0, 0.69444, 0, 0], + "728": [0, 0.69444, 0, 0], + "729": [0, 0.69444, 0, 0], + "730": [0, 0.69444, 0, 0], + "732": [0, 0.69444, 0, 0], + "768": [0, 0.69444, 0, 0], + "769": [0, 0.69444, 0, 0], + "770": [0, 0.69444, 0, 0], + "771": [0, 0.69444, 0, 0], + "772": [0, 0.59611, 0, 0], + "774": [0, 0.69444, 0, 0], + "775": [0, 0.69444, 0, 0], + "776": [0, 0.69444, 0, 0], + "778": [0, 0.69444, 0, 0], + "779": [0, 0.69444, 0, 0], + "780": [0, 0.63194, 0, 0], + "824": [0.19444, 0.69444, 0, 0], + "915": [0, 0.68611, 0, 0], + "916": [0, 0.68611, 0, 0], + "920": [0, 0.68611, 0, 0], + "923": [0, 0.68611, 0, 0], + "926": [0, 0.68611, 0, 0], + "928": [0, 0.68611, 0, 0], + "931": [0, 0.68611, 0, 0], + "933": [0, 0.68611, 0, 0], + "934": [0, 0.68611, 0, 0], + "936": [0, 0.68611, 0, 0], + "937": [0, 0.68611, 0, 0], + "8211": [0, 0.44444, 0.03194, 0], + "8212": [0, 0.44444, 0.03194, 0], + "8216": [0, 0.69444, 0, 0], + "8217": [0, 0.69444, 0, 0], + "8220": [0, 0.69444, 0, 0], + "8221": [0, 0.69444, 0, 0], + "8224": [0.19444, 0.69444, 0, 0], + "8225": [0.19444, 0.69444, 0, 0], + "8242": [0, 0.55556, 0, 0], + "8407": [0, 0.72444, 0.15486, 0], + "8463": [0, 0.69444, 0, 0], + "8465": [0, 0.69444, 0, 0], + "8467": [0, 0.69444, 0, 0], + "8472": [0.19444, 0.44444, 0, 0], + "8476": [0, 0.69444, 0, 0], + "8501": [0, 0.69444, 0, 0], + "8592": [-0.10889, 0.39111, 0, 0], + "8593": [0.19444, 0.69444, 0, 0], + "8594": [-0.10889, 0.39111, 0, 0], + "8595": [0.19444, 0.69444, 0, 0], + "8596": [-0.10889, 0.39111, 0, 0], + "8597": [0.25, 0.75, 0, 0], + "8598": [0.19444, 0.69444, 0, 0], + "8599": [0.19444, 0.69444, 0, 0], + "8600": [0.19444, 0.69444, 0, 0], + "8601": [0.19444, 0.69444, 0, 0], + "8636": [-0.10889, 0.39111, 0, 0], + "8637": [-0.10889, 0.39111, 0, 0], + "8640": [-0.10889, 0.39111, 0, 0], + "8641": [-0.10889, 0.39111, 0, 0], + "8656": [-0.10889, 0.39111, 0, 0], + "8657": [0.19444, 0.69444, 0, 0], + "8658": [-0.10889, 0.39111, 0, 0], + "8659": [0.19444, 0.69444, 0, 0], + "8660": [-0.10889, 0.39111, 0, 0], + "8661": [0.25, 0.75, 0, 0], + "8704": [0, 0.69444, 0, 0], + "8706": [0, 0.69444, 0.06389, 0], + "8707": [0, 0.69444, 0, 0], + "8709": [0.05556, 0.75, 0, 0], + "8711": [0, 0.68611, 0, 0], + "8712": [0.08556, 0.58556, 0, 0], + "8715": [0.08556, 0.58556, 0, 0], + "8722": [0.13333, 0.63333, 0, 0], + "8723": [0.13333, 0.63333, 0, 0], + "8725": [0.25, 0.75, 0, 0], + "8726": [0.25, 0.75, 0, 0], + "8727": [-0.02778, 0.47222, 0, 0], + "8728": [-0.02639, 0.47361, 0, 0], + "8729": [-0.02639, 0.47361, 0, 0], + "8730": [0.18, 0.82, 0, 0], + "8733": [0, 0.44444, 0, 0], + "8734": [0, 0.44444, 0, 0], + "8736": [0, 0.69224, 0, 0], + "8739": [0.25, 0.75, 0, 0], + "8741": [0.25, 0.75, 0, 0], + "8743": [0, 0.55556, 0, 0], + "8744": [0, 0.55556, 0, 0], + "8745": [0, 0.55556, 0, 0], + "8746": [0, 0.55556, 0, 0], + "8747": [0.19444, 0.69444, 0.12778, 0], + "8764": [-0.10889, 0.39111, 0, 0], + "8768": [0.19444, 0.69444, 0, 0], + "8771": [0.00222, 0.50222, 0, 0], + "8776": [0.02444, 0.52444, 0, 0], + "8781": [0.00222, 0.50222, 0, 0], + "8801": [0.00222, 0.50222, 0, 0], + "8804": [0.19667, 0.69667, 0, 0], + "8805": [0.19667, 0.69667, 0, 0], + "8810": [0.08556, 0.58556, 0, 0], + "8811": [0.08556, 0.58556, 0, 0], + "8826": [0.08556, 0.58556, 0, 0], + "8827": [0.08556, 0.58556, 0, 0], + "8834": [0.08556, 0.58556, 0, 0], + "8835": [0.08556, 0.58556, 0, 0], + "8838": [0.19667, 0.69667, 0, 0], + "8839": [0.19667, 0.69667, 0, 0], + "8846": [0, 0.55556, 0, 0], + "8849": [0.19667, 0.69667, 0, 0], + "8850": [0.19667, 0.69667, 0, 0], + "8851": [0, 0.55556, 0, 0], + "8852": [0, 0.55556, 0, 0], + "8853": [0.13333, 0.63333, 0, 0], + "8854": [0.13333, 0.63333, 0, 0], + "8855": [0.13333, 0.63333, 0, 0], + "8856": [0.13333, 0.63333, 0, 0], + "8857": [0.13333, 0.63333, 0, 0], + "8866": [0, 0.69444, 0, 0], + "8867": [0, 0.69444, 0, 0], + "8868": [0, 0.69444, 0, 0], + "8869": [0, 0.69444, 0, 0], + "8900": [-0.02639, 0.47361, 0, 0], + "8901": [-0.02639, 0.47361, 0, 0], + "8902": [-0.02778, 0.47222, 0, 0], + "8968": [0.25, 0.75, 0, 0], + "8969": [0.25, 0.75, 0, 0], + "8970": [0.25, 0.75, 0, 0], + "8971": [0.25, 0.75, 0, 0], + "8994": [-0.13889, 0.36111, 0, 0], + "8995": [-0.13889, 0.36111, 0, 0], + "9651": [0.19444, 0.69444, 0, 0], + "9657": [-0.02778, 0.47222, 0, 0], + "9661": [0.19444, 0.69444, 0, 0], + "9667": [-0.02778, 0.47222, 0, 0], + "9711": [0.19444, 0.69444, 0, 0], + "9824": [0.12963, 0.69444, 0, 0], + "9825": [0.12963, 0.69444, 0, 0], + "9826": [0.12963, 0.69444, 0, 0], + "9827": [0.12963, 0.69444, 0, 0], + "9837": [0, 0.75, 0, 0], + "9838": [0.19444, 0.69444, 0, 0], + "9839": [0.19444, 0.69444, 0, 0], + "10216": [0.25, 0.75, 0, 0], + "10217": [0.25, 0.75, 0, 0], + "10815": [0, 0.68611, 0, 0], + "10927": [0.19667, 0.69667, 0, 0], + "10928": [0.19667, 0.69667, 0, 0], + }, + "Main-Italic": { + "33": [0, 0.69444, 0.12417, 0], + "34": [0, 0.69444, 0.06961, 0], + "35": [0.19444, 0.69444, 0.06616, 0], + "37": [0.05556, 0.75, 0.13639, 0], + "38": [0, 0.69444, 0.09694, 0], + "39": [0, 0.69444, 0.12417, 0], + "40": [0.25, 0.75, 0.16194, 0], + "41": [0.25, 0.75, 0.03694, 0], + "42": [0, 0.75, 0.14917, 0], + "43": [0.05667, 0.56167, 0.03694, 0], + "44": [0.19444, 0.10556, 0, 0], + "45": [0, 0.43056, 0.02826, 0], + "46": [0, 0.10556, 0, 0], + "47": [0.25, 0.75, 0.16194, 0], + "48": [0, 0.64444, 0.13556, 0], + "49": [0, 0.64444, 0.13556, 0], + "50": [0, 0.64444, 0.13556, 0], + "51": [0, 0.64444, 0.13556, 0], + "52": [0.19444, 0.64444, 0.13556, 0], + "53": [0, 0.64444, 0.13556, 0], + "54": [0, 0.64444, 0.13556, 0], + "55": [0.19444, 0.64444, 0.13556, 0], + "56": [0, 0.64444, 0.13556, 0], + "57": [0, 0.64444, 0.13556, 0], + "58": [0, 0.43056, 0.0582, 0], + "59": [0.19444, 0.43056, 0.0582, 0], + "61": [-0.13313, 0.36687, 0.06616, 0], + "63": [0, 0.69444, 0.1225, 0], + "64": [0, 0.69444, 0.09597, 0], + "65": [0, 0.68333, 0, 0], + "66": [0, 0.68333, 0.10257, 0], + "67": [0, 0.68333, 0.14528, 0], + "68": [0, 0.68333, 0.09403, 0], + "69": [0, 0.68333, 0.12028, 0], + "70": [0, 0.68333, 0.13305, 0], + "71": [0, 0.68333, 0.08722, 0], + "72": [0, 0.68333, 0.16389, 0], + "73": [0, 0.68333, 0.15806, 0], + "74": [0, 0.68333, 0.14028, 0], + "75": [0, 0.68333, 0.14528, 0], + "76": [0, 0.68333, 0, 0], + "77": [0, 0.68333, 0.16389, 0], + "78": [0, 0.68333, 0.16389, 0], + "79": [0, 0.68333, 0.09403, 0], + "80": [0, 0.68333, 0.10257, 0], + "81": [0.19444, 0.68333, 0.09403, 0], + "82": [0, 0.68333, 0.03868, 0], + "83": [0, 0.68333, 0.11972, 0], + "84": [0, 0.68333, 0.13305, 0], + "85": [0, 0.68333, 0.16389, 0], + "86": [0, 0.68333, 0.18361, 0], + "87": [0, 0.68333, 0.18361, 0], + "88": [0, 0.68333, 0.15806, 0], + "89": [0, 0.68333, 0.19383, 0], + "90": [0, 0.68333, 0.14528, 0], + "91": [0.25, 0.75, 0.1875, 0], + "93": [0.25, 0.75, 0.10528, 0], + "94": [0, 0.69444, 0.06646, 0], + "95": [0.31, 0.12056, 0.09208, 0], + "97": [0, 0.43056, 0.07671, 0], + "98": [0, 0.69444, 0.06312, 0], + "99": [0, 0.43056, 0.05653, 0], + "100": [0, 0.69444, 0.10333, 0], + "101": [0, 0.43056, 0.07514, 0], + "102": [0.19444, 0.69444, 0.21194, 0], + "103": [0.19444, 0.43056, 0.08847, 0], + "104": [0, 0.69444, 0.07671, 0], + "105": [0, 0.65536, 0.1019, 0], + "106": [0.19444, 0.65536, 0.14467, 0], + "107": [0, 0.69444, 0.10764, 0], + "108": [0, 0.69444, 0.10333, 0], + "109": [0, 0.43056, 0.07671, 0], + "110": [0, 0.43056, 0.07671, 0], + "111": [0, 0.43056, 0.06312, 0], + "112": [0.19444, 0.43056, 0.06312, 0], + "113": [0.19444, 0.43056, 0.08847, 0], + "114": [0, 0.43056, 0.10764, 0], + "115": [0, 0.43056, 0.08208, 0], + "116": [0, 0.61508, 0.09486, 0], + "117": [0, 0.43056, 0.07671, 0], + "118": [0, 0.43056, 0.10764, 0], + "119": [0, 0.43056, 0.10764, 0], + "120": [0, 0.43056, 0.12042, 0], + "121": [0.19444, 0.43056, 0.08847, 0], + "122": [0, 0.43056, 0.12292, 0], + "126": [0.35, 0.31786, 0.11585, 0], + "163": [0, 0.69444, 0, 0], + "305": [0, 0.43056, 0, 0.02778], + "567": [0.19444, 0.43056, 0, 0.08334], + "768": [0, 0.69444, 0, 0], + "769": [0, 0.69444, 0.09694, 0], + "770": [0, 0.69444, 0.06646, 0], + "771": [0, 0.66786, 0.11585, 0], + "772": [0, 0.56167, 0.10333, 0], + "774": [0, 0.69444, 0.10806, 0], + "775": [0, 0.66786, 0.11752, 0], + "776": [0, 0.66786, 0.10474, 0], + "778": [0, 0.69444, 0, 0], + "779": [0, 0.69444, 0.1225, 0], + "780": [0, 0.62847, 0.08295, 0], + "915": [0, 0.68333, 0.13305, 0], + "916": [0, 0.68333, 0, 0], + "920": [0, 0.68333, 0.09403, 0], + "923": [0, 0.68333, 0, 0], + "926": [0, 0.68333, 0.15294, 0], + "928": [0, 0.68333, 0.16389, 0], + "931": [0, 0.68333, 0.12028, 0], + "933": [0, 0.68333, 0.11111, 0], + "934": [0, 0.68333, 0.05986, 0], + "936": [0, 0.68333, 0.11111, 0], + "937": [0, 0.68333, 0.10257, 0], + "8211": [0, 0.43056, 0.09208, 0], + "8212": [0, 0.43056, 0.09208, 0], + "8216": [0, 0.69444, 0.12417, 0], + "8217": [0, 0.69444, 0.12417, 0], + "8220": [0, 0.69444, 0.1685, 0], + "8221": [0, 0.69444, 0.06961, 0], + "8463": [0, 0.68889, 0, 0], + }, + "Main-Regular": { + "32": [0, 0, 0, 0], + "33": [0, 0.69444, 0, 0], + "34": [0, 0.69444, 0, 0], + "35": [0.19444, 0.69444, 0, 0], + "36": [0.05556, 0.75, 0, 0], + "37": [0.05556, 0.75, 0, 0], + "38": [0, 0.69444, 0, 0], + "39": [0, 0.69444, 0, 0], + "40": [0.25, 0.75, 0, 0], + "41": [0.25, 0.75, 0, 0], + "42": [0, 0.75, 0, 0], + "43": [0.08333, 0.58333, 0, 0], + "44": [0.19444, 0.10556, 0, 0], + "45": [0, 0.43056, 0, 0], + "46": [0, 0.10556, 0, 0], + "47": [0.25, 0.75, 0, 0], + "48": [0, 0.64444, 0, 0], + "49": [0, 0.64444, 0, 0], + "50": [0, 0.64444, 0, 0], + "51": [0, 0.64444, 0, 0], + "52": [0, 0.64444, 0, 0], + "53": [0, 0.64444, 0, 0], + "54": [0, 0.64444, 0, 0], + "55": [0, 0.64444, 0, 0], + "56": [0, 0.64444, 0, 0], + "57": [0, 0.64444, 0, 0], + "58": [0, 0.43056, 0, 0], + "59": [0.19444, 0.43056, 0, 0], + "60": [0.0391, 0.5391, 0, 0], + "61": [-0.13313, 0.36687, 0, 0], + "62": [0.0391, 0.5391, 0, 0], + "63": [0, 0.69444, 0, 0], + "64": [0, 0.69444, 0, 0], + "65": [0, 0.68333, 0, 0], + "66": [0, 0.68333, 0, 0], + "67": [0, 0.68333, 0, 0], + "68": [0, 0.68333, 0, 0], + "69": [0, 0.68333, 0, 0], + "70": [0, 0.68333, 0, 0], + "71": [0, 0.68333, 0, 0], + "72": [0, 0.68333, 0, 0], + "73": [0, 0.68333, 0, 0], + "74": [0, 0.68333, 0, 0], + "75": [0, 0.68333, 0, 0], + "76": [0, 0.68333, 0, 0], + "77": [0, 0.68333, 0, 0], + "78": [0, 0.68333, 0, 0], + "79": [0, 0.68333, 0, 0], + "80": [0, 0.68333, 0, 0], + "81": [0.19444, 0.68333, 0, 0], + "82": [0, 0.68333, 0, 0], + "83": [0, 0.68333, 0, 0], + "84": [0, 0.68333, 0, 0], + "85": [0, 0.68333, 0, 0], + "86": [0, 0.68333, 0.01389, 0], + "87": [0, 0.68333, 0.01389, 0], + "88": [0, 0.68333, 0, 0], + "89": [0, 0.68333, 0.025, 0], + "90": [0, 0.68333, 0, 0], + "91": [0.25, 0.75, 0, 0], + "92": [0.25, 0.75, 0, 0], + "93": [0.25, 0.75, 0, 0], + "94": [0, 0.69444, 0, 0], + "95": [0.31, 0.12056, 0.02778, 0], + "96": [0, 0.69444, 0, 0], + "97": [0, 0.43056, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.43056, 0, 0], + "100": [0, 0.69444, 0, 0], + "101": [0, 0.43056, 0, 0], + "102": [0, 0.69444, 0.07778, 0], + "103": [0.19444, 0.43056, 0.01389, 0], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.66786, 0, 0], + "106": [0.19444, 0.66786, 0, 0], + "107": [0, 0.69444, 0, 0], + "108": [0, 0.69444, 0, 0], + "109": [0, 0.43056, 0, 0], + "110": [0, 0.43056, 0, 0], + "111": [0, 0.43056, 0, 0], + "112": [0.19444, 0.43056, 0, 0], + "113": [0.19444, 0.43056, 0, 0], + "114": [0, 0.43056, 0, 0], + "115": [0, 0.43056, 0, 0], + "116": [0, 0.61508, 0, 0], + "117": [0, 0.43056, 0, 0], + "118": [0, 0.43056, 0.01389, 0], + "119": [0, 0.43056, 0.01389, 0], + "120": [0, 0.43056, 0, 0], + "121": [0.19444, 0.43056, 0.01389, 0], + "122": [0, 0.43056, 0, 0], + "123": [0.25, 0.75, 0, 0], + "124": [0.25, 0.75, 0, 0], + "125": [0.25, 0.75, 0, 0], + "126": [0.35, 0.31786, 0, 0], + "160": [0, 0, 0, 0], + "168": [0, 0.66786, 0, 0], + "172": [0, 0.43056, 0, 0], + "175": [0, 0.56778, 0, 0], + "176": [0, 0.69444, 0, 0], + "177": [0.08333, 0.58333, 0, 0], + "180": [0, 0.69444, 0, 0], + "215": [0.08333, 0.58333, 0, 0], + "247": [0.08333, 0.58333, 0, 0], + "305": [0, 0.43056, 0, 0], + "567": [0.19444, 0.43056, 0, 0], + "710": [0, 0.69444, 0, 0], + "711": [0, 0.62847, 0, 0], + "713": [0, 0.56778, 0, 0], + "714": [0, 0.69444, 0, 0], + "715": [0, 0.69444, 0, 0], + "728": [0, 0.69444, 0, 0], + "729": [0, 0.66786, 0, 0], + "730": [0, 0.69444, 0, 0], + "732": [0, 0.66786, 0, 0], + "768": [0, 0.69444, 0, 0], + "769": [0, 0.69444, 0, 0], + "770": [0, 0.69444, 0, 0], + "771": [0, 0.66786, 0, 0], + "772": [0, 0.56778, 0, 0], + "774": [0, 0.69444, 0, 0], + "775": [0, 0.66786, 0, 0], + "776": [0, 0.66786, 0, 0], + "778": [0, 0.69444, 0, 0], + "779": [0, 0.69444, 0, 0], + "780": [0, 0.62847, 0, 0], + "824": [0.19444, 0.69444, 0, 0], + "915": [0, 0.68333, 0, 0], + "916": [0, 0.68333, 0, 0], + "920": [0, 0.68333, 0, 0], + "923": [0, 0.68333, 0, 0], + "926": [0, 0.68333, 0, 0], + "928": [0, 0.68333, 0, 0], + "931": [0, 0.68333, 0, 0], + "933": [0, 0.68333, 0, 0], + "934": [0, 0.68333, 0, 0], + "936": [0, 0.68333, 0, 0], + "937": [0, 0.68333, 0, 0], + "8211": [0, 0.43056, 0.02778, 0], + "8212": [0, 0.43056, 0.02778, 0], + "8216": [0, 0.69444, 0, 0], + "8217": [0, 0.69444, 0, 0], + "8220": [0, 0.69444, 0, 0], + "8221": [0, 0.69444, 0, 0], + "8224": [0.19444, 0.69444, 0, 0], + "8225": [0.19444, 0.69444, 0, 0], + "8230": [0, 0.12, 0, 0], + "8242": [0, 0.55556, 0, 0], + "8407": [0, 0.71444, 0.15382, 0], + "8463": [0, 0.68889, 0, 0], + "8465": [0, 0.69444, 0, 0], + "8467": [0, 0.69444, 0, 0.11111], + "8472": [0.19444, 0.43056, 0, 0.11111], + "8476": [0, 0.69444, 0, 0], + "8501": [0, 0.69444, 0, 0], + "8592": [-0.13313, 0.36687, 0, 0], + "8593": [0.19444, 0.69444, 0, 0], + "8594": [-0.13313, 0.36687, 0, 0], + "8595": [0.19444, 0.69444, 0, 0], + "8596": [-0.13313, 0.36687, 0, 0], + "8597": [0.25, 0.75, 0, 0], + "8598": [0.19444, 0.69444, 0, 0], + "8599": [0.19444, 0.69444, 0, 0], + "8600": [0.19444, 0.69444, 0, 0], + "8601": [0.19444, 0.69444, 0, 0], + "8614": [0.011, 0.511, 0, 0], + "8617": [0.011, 0.511, 0, 0], + "8618": [0.011, 0.511, 0, 0], + "8636": [-0.13313, 0.36687, 0, 0], + "8637": [-0.13313, 0.36687, 0, 0], + "8640": [-0.13313, 0.36687, 0, 0], + "8641": [-0.13313, 0.36687, 0, 0], + "8652": [0.011, 0.671, 0, 0], + "8656": [-0.13313, 0.36687, 0, 0], + "8657": [0.19444, 0.69444, 0, 0], + "8658": [-0.13313, 0.36687, 0, 0], + "8659": [0.19444, 0.69444, 0, 0], + "8660": [-0.13313, 0.36687, 0, 0], + "8661": [0.25, 0.75, 0, 0], + "8704": [0, 0.69444, 0, 0], + "8706": [0, 0.69444, 0.05556, 0.08334], + "8707": [0, 0.69444, 0, 0], + "8709": [0.05556, 0.75, 0, 0], + "8711": [0, 0.68333, 0, 0], + "8712": [0.0391, 0.5391, 0, 0], + "8715": [0.0391, 0.5391, 0, 0], + "8722": [0.08333, 0.58333, 0, 0], + "8723": [0.08333, 0.58333, 0, 0], + "8725": [0.25, 0.75, 0, 0], + "8726": [0.25, 0.75, 0, 0], + "8727": [-0.03472, 0.46528, 0, 0], + "8728": [-0.05555, 0.44445, 0, 0], + "8729": [-0.05555, 0.44445, 0, 0], + "8730": [0.2, 0.8, 0, 0], + "8733": [0, 0.43056, 0, 0], + "8734": [0, 0.43056, 0, 0], + "8736": [0, 0.69224, 0, 0], + "8739": [0.25, 0.75, 0, 0], + "8741": [0.25, 0.75, 0, 0], + "8743": [0, 0.55556, 0, 0], + "8744": [0, 0.55556, 0, 0], + "8745": [0, 0.55556, 0, 0], + "8746": [0, 0.55556, 0, 0], + "8747": [0.19444, 0.69444, 0.11111, 0], + "8764": [-0.13313, 0.36687, 0, 0], + "8768": [0.19444, 0.69444, 0, 0], + "8771": [-0.03625, 0.46375, 0, 0], + "8773": [-0.022, 0.589, 0, 0], + "8776": [-0.01688, 0.48312, 0, 0], + "8781": [-0.03625, 0.46375, 0, 0], + "8784": [-0.133, 0.67, 0, 0], + "8800": [0.215, 0.716, 0, 0], + "8801": [-0.03625, 0.46375, 0, 0], + "8804": [0.13597, 0.63597, 0, 0], + "8805": [0.13597, 0.63597, 0, 0], + "8810": [0.0391, 0.5391, 0, 0], + "8811": [0.0391, 0.5391, 0, 0], + "8826": [0.0391, 0.5391, 0, 0], + "8827": [0.0391, 0.5391, 0, 0], + "8834": [0.0391, 0.5391, 0, 0], + "8835": [0.0391, 0.5391, 0, 0], + "8838": [0.13597, 0.63597, 0, 0], + "8839": [0.13597, 0.63597, 0, 0], + "8846": [0, 0.55556, 0, 0], + "8849": [0.13597, 0.63597, 0, 0], + "8850": [0.13597, 0.63597, 0, 0], + "8851": [0, 0.55556, 0, 0], + "8852": [0, 0.55556, 0, 0], + "8853": [0.08333, 0.58333, 0, 0], + "8854": [0.08333, 0.58333, 0, 0], + "8855": [0.08333, 0.58333, 0, 0], + "8856": [0.08333, 0.58333, 0, 0], + "8857": [0.08333, 0.58333, 0, 0], + "8866": [0, 0.69444, 0, 0], + "8867": [0, 0.69444, 0, 0], + "8868": [0, 0.69444, 0, 0], + "8869": [0, 0.69444, 0, 0], + "8872": [0.249, 0.75, 0, 0], + "8900": [-0.05555, 0.44445, 0, 0], + "8901": [-0.05555, 0.44445, 0, 0], + "8902": [-0.03472, 0.46528, 0, 0], + "8904": [0.005, 0.505, 0, 0], + "8942": [0.03, 0.9, 0, 0], + "8943": [-0.19, 0.31, 0, 0], + "8945": [-0.1, 0.82, 0, 0], + "8968": [0.25, 0.75, 0, 0], + "8969": [0.25, 0.75, 0, 0], + "8970": [0.25, 0.75, 0, 0], + "8971": [0.25, 0.75, 0, 0], + "8994": [-0.14236, 0.35764, 0, 0], + "8995": [-0.14236, 0.35764, 0, 0], + "9136": [0.244, 0.744, 0, 0], + "9137": [0.244, 0.744, 0, 0], + "9651": [0.19444, 0.69444, 0, 0], + "9657": [-0.03472, 0.46528, 0, 0], + "9661": [0.19444, 0.69444, 0, 0], + "9667": [-0.03472, 0.46528, 0, 0], + "9711": [0.19444, 0.69444, 0, 0], + "9824": [0.12963, 0.69444, 0, 0], + "9825": [0.12963, 0.69444, 0, 0], + "9826": [0.12963, 0.69444, 0, 0], + "9827": [0.12963, 0.69444, 0, 0], + "9837": [0, 0.75, 0, 0], + "9838": [0.19444, 0.69444, 0, 0], + "9839": [0.19444, 0.69444, 0, 0], + "10216": [0.25, 0.75, 0, 0], + "10217": [0.25, 0.75, 0, 0], + "10222": [0.244, 0.744, 0, 0], + "10223": [0.244, 0.744, 0, 0], + "10229": [0.011, 0.511, 0, 0], + "10230": [0.011, 0.511, 0, 0], + "10231": [0.011, 0.511, 0, 0], + "10232": [0.024, 0.525, 0, 0], + "10233": [0.024, 0.525, 0, 0], + "10234": [0.024, 0.525, 0, 0], + "10236": [0.011, 0.511, 0, 0], + "10815": [0, 0.68333, 0, 0], + "10927": [0.13597, 0.63597, 0, 0], + "10928": [0.13597, 0.63597, 0, 0], + }, + "Math-BoldItalic": { + "47": [0.19444, 0.69444, 0, 0], + "65": [0, 0.68611, 0, 0], + "66": [0, 0.68611, 0.04835, 0], + "67": [0, 0.68611, 0.06979, 0], + "68": [0, 0.68611, 0.03194, 0], + "69": [0, 0.68611, 0.05451, 0], + "70": [0, 0.68611, 0.15972, 0], + "71": [0, 0.68611, 0, 0], + "72": [0, 0.68611, 0.08229, 0], + "73": [0, 0.68611, 0.07778, 0], + "74": [0, 0.68611, 0.10069, 0], + "75": [0, 0.68611, 0.06979, 0], + "76": [0, 0.68611, 0, 0], + "77": [0, 0.68611, 0.11424, 0], + "78": [0, 0.68611, 0.11424, 0], + "79": [0, 0.68611, 0.03194, 0], + "80": [0, 0.68611, 0.15972, 0], + "81": [0.19444, 0.68611, 0, 0], + "82": [0, 0.68611, 0.00421, 0], + "83": [0, 0.68611, 0.05382, 0], + "84": [0, 0.68611, 0.15972, 0], + "85": [0, 0.68611, 0.11424, 0], + "86": [0, 0.68611, 0.25555, 0], + "87": [0, 0.68611, 0.15972, 0], + "88": [0, 0.68611, 0.07778, 0], + "89": [0, 0.68611, 0.25555, 0], + "90": [0, 0.68611, 0.06979, 0], + "97": [0, 0.44444, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.44444, 0, 0], + "100": [0, 0.69444, 0, 0], + "101": [0, 0.44444, 0, 0], + "102": [0.19444, 0.69444, 0.11042, 0], + "103": [0.19444, 0.44444, 0.03704, 0], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.69326, 0, 0], + "106": [0.19444, 0.69326, 0.0622, 0], + "107": [0, 0.69444, 0.01852, 0], + "108": [0, 0.69444, 0.0088, 0], + "109": [0, 0.44444, 0, 0], + "110": [0, 0.44444, 0, 0], + "111": [0, 0.44444, 0, 0], + "112": [0.19444, 0.44444, 0, 0], + "113": [0.19444, 0.44444, 0.03704, 0], + "114": [0, 0.44444, 0.03194, 0], + "115": [0, 0.44444, 0, 0], + "116": [0, 0.63492, 0, 0], + "117": [0, 0.44444, 0, 0], + "118": [0, 0.44444, 0.03704, 0], + "119": [0, 0.44444, 0.02778, 0], + "120": [0, 0.44444, 0, 0], + "121": [0.19444, 0.44444, 0.03704, 0], + "122": [0, 0.44444, 0.04213, 0], + "915": [0, 0.68611, 0.15972, 0], + "916": [0, 0.68611, 0, 0], + "920": [0, 0.68611, 0.03194, 0], + "923": [0, 0.68611, 0, 0], + "926": [0, 0.68611, 0.07458, 0], + "928": [0, 0.68611, 0.08229, 0], + "931": [0, 0.68611, 0.05451, 0], + "933": [0, 0.68611, 0.15972, 0], + "934": [0, 0.68611, 0, 0], + "936": [0, 0.68611, 0.11653, 0], + "937": [0, 0.68611, 0.04835, 0], + "945": [0, 0.44444, 0, 0], + "946": [0.19444, 0.69444, 0.03403, 0], + "947": [0.19444, 0.44444, 0.06389, 0], + "948": [0, 0.69444, 0.03819, 0], + "949": [0, 0.44444, 0, 0], + "950": [0.19444, 0.69444, 0.06215, 0], + "951": [0.19444, 0.44444, 0.03704, 0], + "952": [0, 0.69444, 0.03194, 0], + "953": [0, 0.44444, 0, 0], + "954": [0, 0.44444, 0, 0], + "955": [0, 0.69444, 0, 0], + "956": [0.19444, 0.44444, 0, 0], + "957": [0, 0.44444, 0.06898, 0], + "958": [0.19444, 0.69444, 0.03021, 0], + "959": [0, 0.44444, 0, 0], + "960": [0, 0.44444, 0.03704, 0], + "961": [0.19444, 0.44444, 0, 0], + "962": [0.09722, 0.44444, 0.07917, 0], + "963": [0, 0.44444, 0.03704, 0], + "964": [0, 0.44444, 0.13472, 0], + "965": [0, 0.44444, 0.03704, 0], + "966": [0.19444, 0.44444, 0, 0], + "967": [0.19444, 0.44444, 0, 0], + "968": [0.19444, 0.69444, 0.03704, 0], + "969": [0, 0.44444, 0.03704, 0], + "977": [0, 0.69444, 0, 0], + "981": [0.19444, 0.69444, 0, 0], + "982": [0, 0.44444, 0.03194, 0], + "1009": [0.19444, 0.44444, 0, 0], + "1013": [0, 0.44444, 0, 0], + }, + "Math-Italic": { + "47": [0.19444, 0.69444, 0, 0], + "65": [0, 0.68333, 0, 0.13889], + "66": [0, 0.68333, 0.05017, 0.08334], + "67": [0, 0.68333, 0.07153, 0.08334], + "68": [0, 0.68333, 0.02778, 0.05556], + "69": [0, 0.68333, 0.05764, 0.08334], + "70": [0, 0.68333, 0.13889, 0.08334], + "71": [0, 0.68333, 0, 0.08334], + "72": [0, 0.68333, 0.08125, 0.05556], + "73": [0, 0.68333, 0.07847, 0.11111], + "74": [0, 0.68333, 0.09618, 0.16667], + "75": [0, 0.68333, 0.07153, 0.05556], + "76": [0, 0.68333, 0, 0.02778], + "77": [0, 0.68333, 0.10903, 0.08334], + "78": [0, 0.68333, 0.10903, 0.08334], + "79": [0, 0.68333, 0.02778, 0.08334], + "80": [0, 0.68333, 0.13889, 0.08334], + "81": [0.19444, 0.68333, 0, 0.08334], + "82": [0, 0.68333, 0.00773, 0.08334], + "83": [0, 0.68333, 0.05764, 0.08334], + "84": [0, 0.68333, 0.13889, 0.08334], + "85": [0, 0.68333, 0.10903, 0.02778], + "86": [0, 0.68333, 0.22222, 0], + "87": [0, 0.68333, 0.13889, 0], + "88": [0, 0.68333, 0.07847, 0.08334], + "89": [0, 0.68333, 0.22222, 0], + "90": [0, 0.68333, 0.07153, 0.08334], + "97": [0, 0.43056, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.43056, 0, 0.05556], + "100": [0, 0.69444, 0, 0.16667], + "101": [0, 0.43056, 0, 0.05556], + "102": [0.19444, 0.69444, 0.10764, 0.16667], + "103": [0.19444, 0.43056, 0.03588, 0.02778], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.65952, 0, 0], + "106": [0.19444, 0.65952, 0.05724, 0], + "107": [0, 0.69444, 0.03148, 0], + "108": [0, 0.69444, 0.01968, 0.08334], + "109": [0, 0.43056, 0, 0], + "110": [0, 0.43056, 0, 0], + "111": [0, 0.43056, 0, 0.05556], + "112": [0.19444, 0.43056, 0, 0.08334], + "113": [0.19444, 0.43056, 0.03588, 0.08334], + "114": [0, 0.43056, 0.02778, 0.05556], + "115": [0, 0.43056, 0, 0.05556], + "116": [0, 0.61508, 0, 0.08334], + "117": [0, 0.43056, 0, 0.02778], + "118": [0, 0.43056, 0.03588, 0.02778], + "119": [0, 0.43056, 0.02691, 0.08334], + "120": [0, 0.43056, 0, 0.02778], + "121": [0.19444, 0.43056, 0.03588, 0.05556], + "122": [0, 0.43056, 0.04398, 0.05556], + "915": [0, 0.68333, 0.13889, 0.08334], + "916": [0, 0.68333, 0, 0.16667], + "920": [0, 0.68333, 0.02778, 0.08334], + "923": [0, 0.68333, 0, 0.16667], + "926": [0, 0.68333, 0.07569, 0.08334], + "928": [0, 0.68333, 0.08125, 0.05556], + "931": [0, 0.68333, 0.05764, 0.08334], + "933": [0, 0.68333, 0.13889, 0.05556], + "934": [0, 0.68333, 0, 0.08334], + "936": [0, 0.68333, 0.11, 0.05556], + "937": [0, 0.68333, 0.05017, 0.08334], + "945": [0, 0.43056, 0.0037, 0.02778], + "946": [0.19444, 0.69444, 0.05278, 0.08334], + "947": [0.19444, 0.43056, 0.05556, 0], + "948": [0, 0.69444, 0.03785, 0.05556], + "949": [0, 0.43056, 0, 0.08334], + "950": [0.19444, 0.69444, 0.07378, 0.08334], + "951": [0.19444, 0.43056, 0.03588, 0.05556], + "952": [0, 0.69444, 0.02778, 0.08334], + "953": [0, 0.43056, 0, 0.05556], + "954": [0, 0.43056, 0, 0], + "955": [0, 0.69444, 0, 0], + "956": [0.19444, 0.43056, 0, 0.02778], + "957": [0, 0.43056, 0.06366, 0.02778], + "958": [0.19444, 0.69444, 0.04601, 0.11111], + "959": [0, 0.43056, 0, 0.05556], + "960": [0, 0.43056, 0.03588, 0], + "961": [0.19444, 0.43056, 0, 0.08334], + "962": [0.09722, 0.43056, 0.07986, 0.08334], + "963": [0, 0.43056, 0.03588, 0], + "964": [0, 0.43056, 0.1132, 0.02778], + "965": [0, 0.43056, 0.03588, 0.02778], + "966": [0.19444, 0.43056, 0, 0.08334], + "967": [0.19444, 0.43056, 0, 0.05556], + "968": [0.19444, 0.69444, 0.03588, 0.11111], + "969": [0, 0.43056, 0.03588, 0], + "977": [0, 0.69444, 0, 0.08334], + "981": [0.19444, 0.69444, 0, 0.08334], + "982": [0, 0.43056, 0.02778, 0], + "1009": [0.19444, 0.43056, 0, 0.08334], + "1013": [0, 0.43056, 0, 0.05556], + }, + "Math-Regular": { + "65": [0, 0.68333, 0, 0.13889], + "66": [0, 0.68333, 0.05017, 0.08334], + "67": [0, 0.68333, 0.07153, 0.08334], + "68": [0, 0.68333, 0.02778, 0.05556], + "69": [0, 0.68333, 0.05764, 0.08334], + "70": [0, 0.68333, 0.13889, 0.08334], + "71": [0, 0.68333, 0, 0.08334], + "72": [0, 0.68333, 0.08125, 0.05556], + "73": [0, 0.68333, 0.07847, 0.11111], + "74": [0, 0.68333, 0.09618, 0.16667], + "75": [0, 0.68333, 0.07153, 0.05556], + "76": [0, 0.68333, 0, 0.02778], + "77": [0, 0.68333, 0.10903, 0.08334], + "78": [0, 0.68333, 0.10903, 0.08334], + "79": [0, 0.68333, 0.02778, 0.08334], + "80": [0, 0.68333, 0.13889, 0.08334], + "81": [0.19444, 0.68333, 0, 0.08334], + "82": [0, 0.68333, 0.00773, 0.08334], + "83": [0, 0.68333, 0.05764, 0.08334], + "84": [0, 0.68333, 0.13889, 0.08334], + "85": [0, 0.68333, 0.10903, 0.02778], + "86": [0, 0.68333, 0.22222, 0], + "87": [0, 0.68333, 0.13889, 0], + "88": [0, 0.68333, 0.07847, 0.08334], + "89": [0, 0.68333, 0.22222, 0], + "90": [0, 0.68333, 0.07153, 0.08334], + "97": [0, 0.43056, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.43056, 0, 0.05556], + "100": [0, 0.69444, 0, 0.16667], + "101": [0, 0.43056, 0, 0.05556], + "102": [0.19444, 0.69444, 0.10764, 0.16667], + "103": [0.19444, 0.43056, 0.03588, 0.02778], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.65952, 0, 0], + "106": [0.19444, 0.65952, 0.05724, 0], + "107": [0, 0.69444, 0.03148, 0], + "108": [0, 0.69444, 0.01968, 0.08334], + "109": [0, 0.43056, 0, 0], + "110": [0, 0.43056, 0, 0], + "111": [0, 0.43056, 0, 0.05556], + "112": [0.19444, 0.43056, 0, 0.08334], + "113": [0.19444, 0.43056, 0.03588, 0.08334], + "114": [0, 0.43056, 0.02778, 0.05556], + "115": [0, 0.43056, 0, 0.05556], + "116": [0, 0.61508, 0, 0.08334], + "117": [0, 0.43056, 0, 0.02778], + "118": [0, 0.43056, 0.03588, 0.02778], + "119": [0, 0.43056, 0.02691, 0.08334], + "120": [0, 0.43056, 0, 0.02778], + "121": [0.19444, 0.43056, 0.03588, 0.05556], + "122": [0, 0.43056, 0.04398, 0.05556], + "915": [0, 0.68333, 0.13889, 0.08334], + "916": [0, 0.68333, 0, 0.16667], + "920": [0, 0.68333, 0.02778, 0.08334], + "923": [0, 0.68333, 0, 0.16667], + "926": [0, 0.68333, 0.07569, 0.08334], + "928": [0, 0.68333, 0.08125, 0.05556], + "931": [0, 0.68333, 0.05764, 0.08334], + "933": [0, 0.68333, 0.13889, 0.05556], + "934": [0, 0.68333, 0, 0.08334], + "936": [0, 0.68333, 0.11, 0.05556], + "937": [0, 0.68333, 0.05017, 0.08334], + "945": [0, 0.43056, 0.0037, 0.02778], + "946": [0.19444, 0.69444, 0.05278, 0.08334], + "947": [0.19444, 0.43056, 0.05556, 0], + "948": [0, 0.69444, 0.03785, 0.05556], + "949": [0, 0.43056, 0, 0.08334], + "950": [0.19444, 0.69444, 0.07378, 0.08334], + "951": [0.19444, 0.43056, 0.03588, 0.05556], + "952": [0, 0.69444, 0.02778, 0.08334], + "953": [0, 0.43056, 0, 0.05556], + "954": [0, 0.43056, 0, 0], + "955": [0, 0.69444, 0, 0], + "956": [0.19444, 0.43056, 0, 0.02778], + "957": [0, 0.43056, 0.06366, 0.02778], + "958": [0.19444, 0.69444, 0.04601, 0.11111], + "959": [0, 0.43056, 0, 0.05556], + "960": [0, 0.43056, 0.03588, 0], + "961": [0.19444, 0.43056, 0, 0.08334], + "962": [0.09722, 0.43056, 0.07986, 0.08334], + "963": [0, 0.43056, 0.03588, 0], + "964": [0, 0.43056, 0.1132, 0.02778], + "965": [0, 0.43056, 0.03588, 0.02778], + "966": [0.19444, 0.43056, 0, 0.08334], + "967": [0.19444, 0.43056, 0, 0.05556], + "968": [0.19444, 0.69444, 0.03588, 0.11111], + "969": [0, 0.43056, 0.03588, 0], + "977": [0, 0.69444, 0, 0.08334], + "981": [0.19444, 0.69444, 0, 0.08334], + "982": [0, 0.43056, 0.02778, 0], + "1009": [0.19444, 0.43056, 0, 0.08334], + "1013": [0, 0.43056, 0, 0.05556], + }, + "SansSerif-Regular": { + "33": [0, 0.69444, 0, 0], + "34": [0, 0.69444, 0, 0], + "35": [0.19444, 0.69444, 0, 0], + "36": [0.05556, 0.75, 0, 0], + "37": [0.05556, 0.75, 0, 0], + "38": [0, 0.69444, 0, 0], + "39": [0, 0.69444, 0, 0], + "40": [0.25, 0.75, 0, 0], + "41": [0.25, 0.75, 0, 0], + "42": [0, 0.75, 0, 0], + "43": [0.08333, 0.58333, 0, 0], + "44": [0.125, 0.08333, 0, 0], + "45": [0, 0.44444, 0, 0], + "46": [0, 0.08333, 0, 0], + "47": [0.25, 0.75, 0, 0], + "48": [0, 0.65556, 0, 0], + "49": [0, 0.65556, 0, 0], + "50": [0, 0.65556, 0, 0], + "51": [0, 0.65556, 0, 0], + "52": [0, 0.65556, 0, 0], + "53": [0, 0.65556, 0, 0], + "54": [0, 0.65556, 0, 0], + "55": [0, 0.65556, 0, 0], + "56": [0, 0.65556, 0, 0], + "57": [0, 0.65556, 0, 0], + "58": [0, 0.44444, 0, 0], + "59": [0.125, 0.44444, 0, 0], + "61": [-0.13, 0.37, 0, 0], + "63": [0, 0.69444, 0, 0], + "64": [0, 0.69444, 0, 0], + "65": [0, 0.69444, 0, 0], + "66": [0, 0.69444, 0, 0], + "67": [0, 0.69444, 0, 0], + "68": [0, 0.69444, 0, 0], + "69": [0, 0.69444, 0, 0], + "70": [0, 0.69444, 0, 0], + "71": [0, 0.69444, 0, 0], + "72": [0, 0.69444, 0, 0], + "73": [0, 0.69444, 0, 0], + "74": [0, 0.69444, 0, 0], + "75": [0, 0.69444, 0, 0], + "76": [0, 0.69444, 0, 0], + "77": [0, 0.69444, 0, 0], + "78": [0, 0.69444, 0, 0], + "79": [0, 0.69444, 0, 0], + "80": [0, 0.69444, 0, 0], + "81": [0.125, 0.69444, 0, 0], + "82": [0, 0.69444, 0, 0], + "83": [0, 0.69444, 0, 0], + "84": [0, 0.69444, 0, 0], + "85": [0, 0.69444, 0, 0], + "86": [0, 0.69444, 0.01389, 0], + "87": [0, 0.69444, 0.01389, 0], + "88": [0, 0.69444, 0, 0], + "89": [0, 0.69444, 0.025, 0], + "90": [0, 0.69444, 0, 0], + "91": [0.25, 0.75, 0, 0], + "93": [0.25, 0.75, 0, 0], + "94": [0, 0.69444, 0, 0], + "95": [0.35, 0.09444, 0.02778, 0], + "97": [0, 0.44444, 0, 0], + "98": [0, 0.69444, 0, 0], + "99": [0, 0.44444, 0, 0], + "100": [0, 0.69444, 0, 0], + "101": [0, 0.44444, 0, 0], + "102": [0, 0.69444, 0.06944, 0], + "103": [0.19444, 0.44444, 0.01389, 0], + "104": [0, 0.69444, 0, 0], + "105": [0, 0.67937, 0, 0], + "106": [0.19444, 0.67937, 0, 0], + "107": [0, 0.69444, 0, 0], + "108": [0, 0.69444, 0, 0], + "109": [0, 0.44444, 0, 0], + "110": [0, 0.44444, 0, 0], + "111": [0, 0.44444, 0, 0], + "112": [0.19444, 0.44444, 0, 0], + "113": [0.19444, 0.44444, 0, 0], + "114": [0, 0.44444, 0.01389, 0], + "115": [0, 0.44444, 0, 0], + "116": [0, 0.57143, 0, 0], + "117": [0, 0.44444, 0, 0], + "118": [0, 0.44444, 0.01389, 0], + "119": [0, 0.44444, 0.01389, 0], + "120": [0, 0.44444, 0, 0], + "121": [0.19444, 0.44444, 0.01389, 0], + "122": [0, 0.44444, 0, 0], + "126": [0.35, 0.32659, 0, 0], + "305": [0, 0.44444, 0, 0], + "567": [0.19444, 0.44444, 0, 0], + "768": [0, 0.69444, 0, 0], + "769": [0, 0.69444, 0, 0], + "770": [0, 0.69444, 0, 0], + "771": [0, 0.67659, 0, 0], + "772": [0, 0.60889, 0, 0], + "774": [0, 0.69444, 0, 0], + "775": [0, 0.67937, 0, 0], + "776": [0, 0.67937, 0, 0], + "778": [0, 0.69444, 0, 0], + "779": [0, 0.69444, 0, 0], + "780": [0, 0.63194, 0, 0], + "915": [0, 0.69444, 0, 0], + "916": [0, 0.69444, 0, 0], + "920": [0, 0.69444, 0, 0], + "923": [0, 0.69444, 0, 0], + "926": [0, 0.69444, 0, 0], + "928": [0, 0.69444, 0, 0], + "931": [0, 0.69444, 0, 0], + "933": [0, 0.69444, 0, 0], + "934": [0, 0.69444, 0, 0], + "936": [0, 0.69444, 0, 0], + "937": [0, 0.69444, 0, 0], + "8211": [0, 0.44444, 0.02778, 0], + "8212": [0, 0.44444, 0.02778, 0], + "8216": [0, 0.69444, 0, 0], + "8217": [0, 0.69444, 0, 0], + "8220": [0, 0.69444, 0, 0], + "8221": [0, 0.69444, 0, 0], + }, + "Script-Regular": { + "65": [0, 0.7, 0.22925, 0], + "66": [0, 0.7, 0.04087, 0], + "67": [0, 0.7, 0.1689, 0], + "68": [0, 0.7, 0.09371, 0], + "69": [0, 0.7, 0.18583, 0], + "70": [0, 0.7, 0.13634, 0], + "71": [0, 0.7, 0.17322, 0], + "72": [0, 0.7, 0.29694, 0], + "73": [0, 0.7, 0.19189, 0], + "74": [0.27778, 0.7, 0.19189, 0], + "75": [0, 0.7, 0.31259, 0], + "76": [0, 0.7, 0.19189, 0], + "77": [0, 0.7, 0.15981, 0], + "78": [0, 0.7, 0.3525, 0], + "79": [0, 0.7, 0.08078, 0], + "80": [0, 0.7, 0.08078, 0], + "81": [0, 0.7, 0.03305, 0], + "82": [0, 0.7, 0.06259, 0], + "83": [0, 0.7, 0.19189, 0], + "84": [0, 0.7, 0.29087, 0], + "85": [0, 0.7, 0.25815, 0], + "86": [0, 0.7, 0.27523, 0], + "87": [0, 0.7, 0.27523, 0], + "88": [0, 0.7, 0.26006, 0], + "89": [0, 0.7, 0.2939, 0], + "90": [0, 0.7, 0.24037, 0], + }, + "Size1-Regular": { + "40": [0.35001, 0.85, 0, 0], + "41": [0.35001, 0.85, 0, 0], + "47": [0.35001, 0.85, 0, 0], + "91": [0.35001, 0.85, 0, 0], + "92": [0.35001, 0.85, 0, 0], + "93": [0.35001, 0.85, 0, 0], + "123": [0.35001, 0.85, 0, 0], + "125": [0.35001, 0.85, 0, 0], + "710": [0, 0.72222, 0, 0], + "732": [0, 0.72222, 0, 0], + "770": [0, 0.72222, 0, 0], + "771": [0, 0.72222, 0, 0], + "8214": [-0.00099, 0.601, 0, 0], + "8593": [1e-05, 0.6, 0, 0], + "8595": [1e-05, 0.6, 0, 0], + "8657": [1e-05, 0.6, 0, 0], + "8659": [1e-05, 0.6, 0, 0], + "8719": [0.25001, 0.75, 0, 0], + "8720": [0.25001, 0.75, 0, 0], + "8721": [0.25001, 0.75, 0, 0], + "8730": [0.35001, 0.85, 0, 0], + "8739": [-0.00599, 0.606, 0, 0], + "8741": [-0.00599, 0.606, 0, 0], + "8747": [0.30612, 0.805, 0.19445, 0], + "8748": [0.306, 0.805, 0.19445, 0], + "8749": [0.306, 0.805, 0.19445, 0], + "8750": [0.30612, 0.805, 0.19445, 0], + "8896": [0.25001, 0.75, 0, 0], + "8897": [0.25001, 0.75, 0, 0], + "8898": [0.25001, 0.75, 0, 0], + "8899": [0.25001, 0.75, 0, 0], + "8968": [0.35001, 0.85, 0, 0], + "8969": [0.35001, 0.85, 0, 0], + "8970": [0.35001, 0.85, 0, 0], + "8971": [0.35001, 0.85, 0, 0], + "9168": [-0.00099, 0.601, 0, 0], + "10216": [0.35001, 0.85, 0, 0], + "10217": [0.35001, 0.85, 0, 0], + "10752": [0.25001, 0.75, 0, 0], + "10753": [0.25001, 0.75, 0, 0], + "10754": [0.25001, 0.75, 0, 0], + "10756": [0.25001, 0.75, 0, 0], + "10758": [0.25001, 0.75, 0, 0], + }, + "Size2-Regular": { + "40": [0.65002, 1.15, 0, 0], + "41": [0.65002, 1.15, 0, 0], + "47": [0.65002, 1.15, 0, 0], + "91": [0.65002, 1.15, 0, 0], + "92": [0.65002, 1.15, 0, 0], + "93": [0.65002, 1.15, 0, 0], + "123": [0.65002, 1.15, 0, 0], + "125": [0.65002, 1.15, 0, 0], + "710": [0, 0.75, 0, 0], + "732": [0, 0.75, 0, 0], + "770": [0, 0.75, 0, 0], + "771": [0, 0.75, 0, 0], + "8719": [0.55001, 1.05, 0, 0], + "8720": [0.55001, 1.05, 0, 0], + "8721": [0.55001, 1.05, 0, 0], + "8730": [0.65002, 1.15, 0, 0], + "8747": [0.86225, 1.36, 0.44445, 0], + "8748": [0.862, 1.36, 0.44445, 0], + "8749": [0.862, 1.36, 0.44445, 0], + "8750": [0.86225, 1.36, 0.44445, 0], + "8896": [0.55001, 1.05, 0, 0], + "8897": [0.55001, 1.05, 0, 0], + "8898": [0.55001, 1.05, 0, 0], + "8899": [0.55001, 1.05, 0, 0], + "8968": [0.65002, 1.15, 0, 0], + "8969": [0.65002, 1.15, 0, 0], + "8970": [0.65002, 1.15, 0, 0], + "8971": [0.65002, 1.15, 0, 0], + "10216": [0.65002, 1.15, 0, 0], + "10217": [0.65002, 1.15, 0, 0], + "10752": [0.55001, 1.05, 0, 0], + "10753": [0.55001, 1.05, 0, 0], + "10754": [0.55001, 1.05, 0, 0], + "10756": [0.55001, 1.05, 0, 0], + "10758": [0.55001, 1.05, 0, 0], + }, + "Size3-Regular": { + "40": [0.95003, 1.45, 0, 0], + "41": [0.95003, 1.45, 0, 0], + "47": [0.95003, 1.45, 0, 0], + "91": [0.95003, 1.45, 0, 0], + "92": [0.95003, 1.45, 0, 0], + "93": [0.95003, 1.45, 0, 0], + "123": [0.95003, 1.45, 0, 0], + "125": [0.95003, 1.45, 0, 0], + "710": [0, 0.75, 0, 0], + "732": [0, 0.75, 0, 0], + "770": [0, 0.75, 0, 0], + "771": [0, 0.75, 0, 0], + "8730": [0.95003, 1.45, 0, 0], + "8968": [0.95003, 1.45, 0, 0], + "8969": [0.95003, 1.45, 0, 0], + "8970": [0.95003, 1.45, 0, 0], + "8971": [0.95003, 1.45, 0, 0], + "10216": [0.95003, 1.45, 0, 0], + "10217": [0.95003, 1.45, 0, 0], + }, + "Size4-Regular": { + "40": [1.25003, 1.75, 0, 0], + "41": [1.25003, 1.75, 0, 0], + "47": [1.25003, 1.75, 0, 0], + "91": [1.25003, 1.75, 0, 0], + "92": [1.25003, 1.75, 0, 0], + "93": [1.25003, 1.75, 0, 0], + "123": [1.25003, 1.75, 0, 0], + "125": [1.25003, 1.75, 0, 0], + "710": [0, 0.825, 0, 0], + "732": [0, 0.825, 0, 0], + "770": [0, 0.825, 0, 0], + "771": [0, 0.825, 0, 0], + "8730": [1.25003, 1.75, 0, 0], + "8968": [1.25003, 1.75, 0, 0], + "8969": [1.25003, 1.75, 0, 0], + "8970": [1.25003, 1.75, 0, 0], + "8971": [1.25003, 1.75, 0, 0], + "9115": [0.64502, 1.155, 0, 0], + "9116": [1e-05, 0.6, 0, 0], + "9117": [0.64502, 1.155, 0, 0], + "9118": [0.64502, 1.155, 0, 0], + "9119": [1e-05, 0.6, 0, 0], + "9120": [0.64502, 1.155, 0, 0], + "9121": [0.64502, 1.155, 0, 0], + "9122": [-0.00099, 0.601, 0, 0], + "9123": [0.64502, 1.155, 0, 0], + "9124": [0.64502, 1.155, 0, 0], + "9125": [-0.00099, 0.601, 0, 0], + "9126": [0.64502, 1.155, 0, 0], + "9127": [1e-05, 0.9, 0, 0], + "9128": [0.65002, 1.15, 0, 0], + "9129": [0.90001, 0, 0, 0], + "9130": [0, 0.3, 0, 0], + "9131": [1e-05, 0.9, 0, 0], + "9132": [0.65002, 1.15, 0, 0], + "9133": [0.90001, 0, 0, 0], + "9143": [0.88502, 0.915, 0, 0], + "10216": [1.25003, 1.75, 0, 0], + "10217": [1.25003, 1.75, 0, 0], + "57344": [-0.00499, 0.605, 0, 0], + "57345": [-0.00499, 0.605, 0, 0], + "57680": [0, 0.12, 0, 0], + "57681": [0, 0.12, 0, 0], + "57682": [0, 0.12, 0, 0], + "57683": [0, 0.12, 0, 0], + }, + "Typewriter-Regular": { + "33": [0, 0.61111, 0, 0], + "34": [0, 0.61111, 0, 0], + "35": [0, 0.61111, 0, 0], + "36": [0.08333, 0.69444, 0, 0], + "37": [0.08333, 0.69444, 0, 0], + "38": [0, 0.61111, 0, 0], + "39": [0, 0.61111, 0, 0], + "40": [0.08333, 0.69444, 0, 0], + "41": [0.08333, 0.69444, 0, 0], + "42": [0, 0.52083, 0, 0], + "43": [-0.08056, 0.53055, 0, 0], + "44": [0.13889, 0.125, 0, 0], + "45": [-0.08056, 0.53055, 0, 0], + "46": [0, 0.125, 0, 0], + "47": [0.08333, 0.69444, 0, 0], + "48": [0, 0.61111, 0, 0], + "49": [0, 0.61111, 0, 0], + "50": [0, 0.61111, 0, 0], + "51": [0, 0.61111, 0, 0], + "52": [0, 0.61111, 0, 0], + "53": [0, 0.61111, 0, 0], + "54": [0, 0.61111, 0, 0], + "55": [0, 0.61111, 0, 0], + "56": [0, 0.61111, 0, 0], + "57": [0, 0.61111, 0, 0], + "58": [0, 0.43056, 0, 0], + "59": [0.13889, 0.43056, 0, 0], + "60": [-0.05556, 0.55556, 0, 0], + "61": [-0.19549, 0.41562, 0, 0], + "62": [-0.05556, 0.55556, 0, 0], + "63": [0, 0.61111, 0, 0], + "64": [0, 0.61111, 0, 0], + "65": [0, 0.61111, 0, 0], + "66": [0, 0.61111, 0, 0], + "67": [0, 0.61111, 0, 0], + "68": [0, 0.61111, 0, 0], + "69": [0, 0.61111, 0, 0], + "70": [0, 0.61111, 0, 0], + "71": [0, 0.61111, 0, 0], + "72": [0, 0.61111, 0, 0], + "73": [0, 0.61111, 0, 0], + "74": [0, 0.61111, 0, 0], + "75": [0, 0.61111, 0, 0], + "76": [0, 0.61111, 0, 0], + "77": [0, 0.61111, 0, 0], + "78": [0, 0.61111, 0, 0], + "79": [0, 0.61111, 0, 0], + "80": [0, 0.61111, 0, 0], + "81": [0.13889, 0.61111, 0, 0], + "82": [0, 0.61111, 0, 0], + "83": [0, 0.61111, 0, 0], + "84": [0, 0.61111, 0, 0], + "85": [0, 0.61111, 0, 0], + "86": [0, 0.61111, 0, 0], + "87": [0, 0.61111, 0, 0], + "88": [0, 0.61111, 0, 0], + "89": [0, 0.61111, 0, 0], + "90": [0, 0.61111, 0, 0], + "91": [0.08333, 0.69444, 0, 0], + "92": [0.08333, 0.69444, 0, 0], + "93": [0.08333, 0.69444, 0, 0], + "94": [0, 0.61111, 0, 0], + "95": [0.09514, 0, 0, 0], + "96": [0, 0.61111, 0, 0], + "97": [0, 0.43056, 0, 0], + "98": [0, 0.61111, 0, 0], + "99": [0, 0.43056, 0, 0], + "100": [0, 0.61111, 0, 0], + "101": [0, 0.43056, 0, 0], + "102": [0, 0.61111, 0, 0], + "103": [0.22222, 0.43056, 0, 0], + "104": [0, 0.61111, 0, 0], + "105": [0, 0.61111, 0, 0], + "106": [0.22222, 0.61111, 0, 0], + "107": [0, 0.61111, 0, 0], + "108": [0, 0.61111, 0, 0], + "109": [0, 0.43056, 0, 0], + "110": [0, 0.43056, 0, 0], + "111": [0, 0.43056, 0, 0], + "112": [0.22222, 0.43056, 0, 0], + "113": [0.22222, 0.43056, 0, 0], + "114": [0, 0.43056, 0, 0], + "115": [0, 0.43056, 0, 0], + "116": [0, 0.55358, 0, 0], + "117": [0, 0.43056, 0, 0], + "118": [0, 0.43056, 0, 0], + "119": [0, 0.43056, 0, 0], + "120": [0, 0.43056, 0, 0], + "121": [0.22222, 0.43056, 0, 0], + "122": [0, 0.43056, 0, 0], + "123": [0.08333, 0.69444, 0, 0], + "124": [0.08333, 0.69444, 0, 0], + "125": [0.08333, 0.69444, 0, 0], + "126": [0, 0.61111, 0, 0], + "127": [0, 0.61111, 0, 0], + "305": [0, 0.43056, 0, 0], + "567": [0.22222, 0.43056, 0, 0], + "768": [0, 0.61111, 0, 0], + "769": [0, 0.61111, 0, 0], + "770": [0, 0.61111, 0, 0], + "771": [0, 0.61111, 0, 0], + "772": [0, 0.56555, 0, 0], + "774": [0, 0.61111, 0, 0], + "776": [0, 0.61111, 0, 0], + "778": [0, 0.61111, 0, 0], + "780": [0, 0.56597, 0, 0], + "915": [0, 0.61111, 0, 0], + "916": [0, 0.61111, 0, 0], + "920": [0, 0.61111, 0, 0], + "923": [0, 0.61111, 0, 0], + "926": [0, 0.61111, 0, 0], + "928": [0, 0.61111, 0, 0], + "931": [0, 0.61111, 0, 0], + "933": [0, 0.61111, 0, 0], + "934": [0, 0.61111, 0, 0], + "936": [0, 0.61111, 0, 0], + "937": [0, 0.61111, 0, 0], + "2018": [0, 0.61111, 0, 0], + "2019": [0, 0.61111, 0, 0], + "8242": [0, 0.61111, 0, 0], + }, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/functions.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/functions.js new file mode 100644 index 000000000..03598eadf --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/functions.js @@ -0,0 +1,585 @@ +var utils = require("./utils"); +var ParseError = require("./ParseError"); + +/* This file contains a list of functions that we parse, identified by + * the calls to defineFunction. + * + * The first argument to defineFunction is a single name or a list of names. + * All functions named in such a list will share a single implementation. + * + * Each declared function can have associated properties, which + * include the following: + * + * - numArgs: The number of arguments the function takes. + * If this is the only property, it can be passed as a number + * instead of an element of a properties object. + * - argTypes: (optional) An array corresponding to each argument of the + * function, giving the type of argument that should be parsed. Its + * length should be equal to `numArgs + numOptionalArgs`. Valid + * types: + * - "size": A size-like thing, such as "1em" or "5ex" + * - "color": An html color, like "#abc" or "blue" + * - "original": The same type as the environment that the + * function being parsed is in (e.g. used for the + * bodies of functions like \color where the first + * argument is special and the second argument is + * parsed normally) + * Other possible types (probably shouldn't be used) + * - "text": Text-like (e.g. \text) + * - "math": Normal math + * If undefined, this will be treated as an appropriate length + * array of "original" strings + * - greediness: (optional) The greediness of the function to use ungrouped + * arguments. + * + * E.g. if you have an expression + * \sqrt \frac 1 2 + * since \frac has greediness=2 vs \sqrt's greediness=1, \frac + * will use the two arguments '1' and '2' as its two arguments, + * then that whole function will be used as the argument to + * \sqrt. On the other hand, the expressions + * \frac \frac 1 2 3 + * and + * \frac \sqrt 1 2 + * will fail because \frac and \frac have equal greediness + * and \sqrt has a lower greediness than \frac respectively. To + * make these parse, we would have to change them to: + * \frac {\frac 1 2} 3 + * and + * \frac {\sqrt 1} 2 + * + * The default value is `1` + * - allowedInText: (optional) Whether or not the function is allowed inside + * text mode (default false) + * - numOptionalArgs: (optional) The number of optional arguments the function + * should parse. If the optional arguments aren't found, + * `null` will be passed to the handler in their place. + * (default 0) + * + * The last argument is that implementation, the handler for the function(s). + * It is called to handle these functions and their arguments. + * It receives two arguments: + * - context contains information and references provided by the parser + * - args is an array of arguments obtained from TeX input + * The context contains the following properties: + * - funcName: the text (i.e. name) of the function, including \ + * - parser: the parser object + * - lexer: the lexer object + * - positions: the positions in the overall string of the function + * and the arguments. + * The latter three should only be used to produce error messages. + * + * The function should return an object with the following keys: + * - type: The type of element that this is. This is then used in + * buildHTML/buildMathML to determine which function + * should be called to build this node into a DOM node + * Any other data can be added to the object, which will be passed + * in to the function in buildHTML/buildMathML as `group.value`. + */ + +function defineFunction(names, props, handler) { + if (typeof names === "string") { + names = [names]; + } + if (typeof props === "number") { + props = { numArgs: props }; + } + // Set default values of functions + var data = { + numArgs: props.numArgs, + argTypes: props.argTypes, + greediness: (props.greediness === undefined) ? 1 : props.greediness, + allowedInText: !!props.allowedInText, + numOptionalArgs: props.numOptionalArgs || 0, + handler: handler, + }; + for (var i = 0; i < names.length; ++i) { + module.exports[names[i]] = data; + } +} + +// A normal square root +defineFunction("\\sqrt", { + numArgs: 1, + numOptionalArgs: 1, +}, function(context, args) { + var index = args[0]; + var body = args[1]; + return { + type: "sqrt", + body: body, + index: index, + }; +}); + +// Some non-mathy text +defineFunction(["\\text", "\\mbox", "\\hbox", "\\vbox"], { + numArgs: 1, + argTypes: ["text"], + greediness: 2, +}, function(context, args) { + var body = args[0]; + // Since the corresponding buildHTML/buildMathML function expects a + // list of elements, we normalize for different kinds of arguments + // TODO(emily): maybe this should be done somewhere else + var inner; + if (body.type === "ordgroup") { + inner = body.value; + } else { + inner = [body]; + } + + return { + type: "text", + body: inner, + }; +}); + +// A two-argument custom color +defineFunction("\\color", { + numArgs: 2, + allowedInText: true, + greediness: 3, + argTypes: ["color", "original"], +}, function(context, args) { + var color = args[0]; + var body = args[1]; + // Normalize the different kinds of bodies (see \text above) + var inner; + if (body.type === "ordgroup") { + inner = body.value; + } else { + inner = [body]; + } + + return { + type: "color", + color: color.value, + value: inner, + }; +}); + +// An overline +defineFunction("\\overline", { + numArgs: 1, +}, function(context, args) { + var body = args[0]; + return { + type: "overline", + body: body, + }; +}); + +// An underline +defineFunction("\\underline", { + numArgs: 1, +}, function(context, args) { + var body = args[0]; + return { + type: "underline", + body: body, + }; +}); + +// A box of the width and height +defineFunction("\\rule", { + numArgs: 2, + numOptionalArgs: 1, + argTypes: ["size", "size", "size"], +}, function(context, args) { + var shift = args[0]; + var width = args[1]; + var height = args[2]; + return { + type: "rule", + shift: shift && shift.value, + width: width.value, + height: height.value, + }; +}); + +// A KaTeX logo +defineFunction("\\KaTeX", { + numArgs: 0, +}, function(context) { + return { + type: "katex", + }; +}); + +defineFunction("\\phantom", { + numArgs: 1, +}, function(context, args) { + var body = args[0]; + var inner; + if (body.type === "ordgroup") { + inner = body.value; + } else { + inner = [body]; + } + + return { + type: "phantom", + value: inner, + }; +}); + +// Extra data needed for the delimiter handler down below +var delimiterSizes = { + "\\bigl" : {type: "open", size: 1}, + "\\Bigl" : {type: "open", size: 2}, + "\\biggl": {type: "open", size: 3}, + "\\Biggl": {type: "open", size: 4}, + "\\bigr" : {type: "close", size: 1}, + "\\Bigr" : {type: "close", size: 2}, + "\\biggr": {type: "close", size: 3}, + "\\Biggr": {type: "close", size: 4}, + "\\bigm" : {type: "rel", size: 1}, + "\\Bigm" : {type: "rel", size: 2}, + "\\biggm": {type: "rel", size: 3}, + "\\Biggm": {type: "rel", size: 4}, + "\\big" : {type: "textord", size: 1}, + "\\Big" : {type: "textord", size: 2}, + "\\bigg" : {type: "textord", size: 3}, + "\\Bigg" : {type: "textord", size: 4}, +}; + +var delimiters = [ + "(", ")", "[", "\\lbrack", "]", "\\rbrack", + "\\{", "\\lbrace", "\\}", "\\rbrace", + "\\lfloor", "\\rfloor", "\\lceil", "\\rceil", + "<", ">", "\\langle", "\\rangle", "\\lt", "\\gt", + "\\lvert", "\\rvert", "\\lVert", "\\rVert", + "\\lgroup", "\\rgroup", "\\lmoustache", "\\rmoustache", + "/", "\\backslash", + "|", "\\vert", "\\|", "\\Vert", + "\\uparrow", "\\Uparrow", + "\\downarrow", "\\Downarrow", + "\\updownarrow", "\\Updownarrow", + ".", +]; + +var fontAliases = { + "\\Bbb": "\\mathbb", + "\\bold": "\\mathbf", + "\\frak": "\\mathfrak", +}; + +// Single-argument color functions +defineFunction([ + "\\blue", "\\orange", "\\pink", "\\red", + "\\green", "\\gray", "\\purple", + "\\blueA", "\\blueB", "\\blueC", "\\blueD", "\\blueE", + "\\tealA", "\\tealB", "\\tealC", "\\tealD", "\\tealE", + "\\greenA", "\\greenB", "\\greenC", "\\greenD", "\\greenE", + "\\goldA", "\\goldB", "\\goldC", "\\goldD", "\\goldE", + "\\redA", "\\redB", "\\redC", "\\redD", "\\redE", + "\\maroonA", "\\maroonB", "\\maroonC", "\\maroonD", "\\maroonE", + "\\purpleA", "\\purpleB", "\\purpleC", "\\purpleD", "\\purpleE", + "\\mintA", "\\mintB", "\\mintC", + "\\grayA", "\\grayB", "\\grayC", "\\grayD", "\\grayE", + "\\grayF", "\\grayG", "\\grayH", "\\grayI", + "\\kaBlue", "\\kaGreen", +], { + numArgs: 1, + allowedInText: true, + greediness: 3, +}, function(context, args) { + var body = args[0]; + var atoms; + if (body.type === "ordgroup") { + atoms = body.value; + } else { + atoms = [body]; + } + + return { + type: "color", + color: "katex-" + context.funcName.slice(1), + value: atoms, + }; +}); + +// There are 2 flags for operators; whether they produce limits in +// displaystyle, and whether they are symbols and should grow in +// displaystyle. These four groups cover the four possible choices. + +// No limits, not symbols +defineFunction([ + "\\arcsin", "\\arccos", "\\arctan", "\\arg", "\\cos", "\\cosh", + "\\cot", "\\coth", "\\csc", "\\deg", "\\dim", "\\exp", "\\hom", + "\\ker", "\\lg", "\\ln", "\\log", "\\sec", "\\sin", "\\sinh", + "\\tan", "\\tanh", +], { + numArgs: 0, +}, function(context) { + return { + type: "op", + limits: false, + symbol: false, + body: context.funcName, + }; +}); + +// Limits, not symbols +defineFunction([ + "\\det", "\\gcd", "\\inf", "\\lim", "\\liminf", "\\limsup", "\\max", + "\\min", "\\Pr", "\\sup", +], { + numArgs: 0, +}, function(context) { + return { + type: "op", + limits: true, + symbol: false, + body: context.funcName, + }; +}); + +// No limits, symbols +defineFunction([ + "\\int", "\\iint", "\\iiint", "\\oint", +], { + numArgs: 0, +}, function(context) { + return { + type: "op", + limits: false, + symbol: true, + body: context.funcName, + }; +}); + +// Limits, symbols +defineFunction([ + "\\coprod", "\\bigvee", "\\bigwedge", "\\biguplus", "\\bigcap", + "\\bigcup", "\\intop", "\\prod", "\\sum", "\\bigotimes", + "\\bigoplus", "\\bigodot", "\\bigsqcup", "\\smallint", +], { + numArgs: 0, +}, function(context) { + return { + type: "op", + limits: true, + symbol: true, + body: context.funcName, + }; +}); + +// Fractions +defineFunction([ + "\\dfrac", "\\frac", "\\tfrac", + "\\dbinom", "\\binom", "\\tbinom", +], { + numArgs: 2, + greediness: 2, +}, function(context, args) { + var numer = args[0]; + var denom = args[1]; + var hasBarLine; + var leftDelim = null; + var rightDelim = null; + var size = "auto"; + + switch (context.funcName) { + case "\\dfrac": + case "\\frac": + case "\\tfrac": + hasBarLine = true; + break; + case "\\dbinom": + case "\\binom": + case "\\tbinom": + hasBarLine = false; + leftDelim = "("; + rightDelim = ")"; + break; + default: + throw new Error("Unrecognized genfrac command"); + } + + switch (context.funcName) { + case "\\dfrac": + case "\\dbinom": + size = "display"; + break; + case "\\tfrac": + case "\\tbinom": + size = "text"; + break; + } + + return { + type: "genfrac", + numer: numer, + denom: denom, + hasBarLine: hasBarLine, + leftDelim: leftDelim, + rightDelim: rightDelim, + size: size, + }; +}); + +// Left and right overlap functions +defineFunction(["\\llap", "\\rlap"], { + numArgs: 1, + allowedInText: true, +}, function(context, args) { + var body = args[0]; + return { + type: context.funcName.slice(1), + body: body, + }; +}); + +// Delimiter functions +defineFunction([ + "\\bigl", "\\Bigl", "\\biggl", "\\Biggl", + "\\bigr", "\\Bigr", "\\biggr", "\\Biggr", + "\\bigm", "\\Bigm", "\\biggm", "\\Biggm", + "\\big", "\\Big", "\\bigg", "\\Bigg", + "\\left", "\\right" +], { + numArgs: 1, +}, function(context, args) { + var delim = args[0]; + if (!utils.contains(delimiters, delim.value)) { + throw new ParseError( + "Invalid delimiter: '" + delim.value + "' after '" + + context.funcName + "'", + context.lexer, context.positions[1]); + } + + // \left and \right are caught somewhere in Parser.js, which is + // why this data doesn't match what is in buildHTML. + if (context.funcName === "\\left" || context.funcName === "\\right") { + return { + type: "leftright", + value: delim.value, + funcName: context.funcName + }; + } else { + return { + type: "delimsizing", + size: delimiterSizes[context.funcName].size, + delimType: delimiterSizes[context.funcName].type, + value: delim.value, + funcName: context.funcName + }; + } +}); + +// Sizing functions (handled in Parser.js explicitly, hence no handler) +defineFunction([ + "\\tiny", "\\scriptsize", "\\footnotesize", "\\small", + "\\normalsize", "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge", "\\textrm", "\\rm", "\\cal", "\\bf", "\\siptstyle", "\\boldmath", "\\it" +], 0, null); + +// Style changing functions (handled in Parser.js explicitly, hence no +// handler) +defineFunction([ + "\\displaystyle", "\\textstyle", "\\scriptstyle", + "\\scriptscriptstyle", +], 0, null); + +defineFunction([ + // styles + "\\mathrm", "\\mathit", "\\mathbf","\\mathop","\\stackrel", + + // families + "\\mathbb", "\\mathcal", "\\mathfrak", "\\mathscr", "\\mathsf", + "\\mathtt", + + "\\label", "\\comment", "\\hspace", "\\vspace", "\\atop", "\\fbox", "\\tag", "\\makebox", + "\\raisebox", "\\framebox", "\\circle", "\\line", "\\put", "\\vphantom", "\\textup", "\\noalign", + + // aliases + "\\Bbb", "\\bold", "\\frak", +], { + numArgs: 1, + greediness: 2, +}, function(context, args) { + var body = args[0]; + var func = context.funcName; + if (func in fontAliases) { + func = fontAliases[func]; + } + return { + type: "font", + font: func.slice(1), + body: body, + }; +}); + +// Accents +defineFunction([ + "\\acute", "\\grave", "\\ddot", "\\tilde", "\\bar", "\\breve", + "\\check", "\\hat", "\\vec", "\\dot", + // We don't support expanding accents yet + // "\\widetilde", "\\widehat" +], { + numArgs: 1, +}, function(context, args) { + var base = args[0]; + return { + type: "accent", + accent: context.funcName, + base: base, + }; +}); + +// Infix generalized fractions +defineFunction(["\\over", "\\choose"], { + numArgs: 0, +}, function(context) { + var replaceWith; + switch (context.funcName) { + case "\\over": + replaceWith = "\\frac"; + break; + case "\\choose": + replaceWith = "\\binom"; + break; + default: + throw new Error("Unrecognized infix genfrac command"); + } + return { + type: "infix", + replaceWith: replaceWith, + }; +}); + +// Row breaks for aligned data +defineFunction(["\\\\", "\\cr"], { + numArgs: 0, + numOptionalArgs: 1, + argTypes: ["size"], +}, function(context, args) { + var size = args[0]; + return { + type: "cr", + size: size, + }; +}); + +// Environment delimiters +defineFunction(["\\begin", "\\end"], { + numArgs: 1, + argTypes: ["text"], +}, function(context, args) { + var nameGroup = args[0]; + if (nameGroup.type !== "ordgroup") { + throw new ParseError( + "Invalid environment name", + context.lexer, context.positions[1]); + } + var name = ""; + for (var i = 0; i < nameGroup.value.length; ++i) { + name += nameGroup.value[i].value; + } + return { + type: "environment", + name: name, + namepos: context.positions[1], + }; +}); diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/mathMLTree.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/mathMLTree.js new file mode 100644 index 000000000..86e63562c --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/mathMLTree.js @@ -0,0 +1,102 @@ +/** + * These objects store data about MathML nodes. This is the MathML equivalent + * of the types in domTree.js. Since MathML handles its own rendering, and + * since we're mainly using MathML to improve accessibility, we don't manage + * any of the styling state that the plain DOM nodes do. + * + * The `toNode` and `toMarkup` functions work simlarly to how they do in + * domTree.js, creating namespaced DOM nodes and HTML text markup respectively. + */ + +var utils = require("./utils"); + +/** + * This node represents a general purpose MathML node of any type. The + * constructor requires the type of node to create (for example, `"mo"` or + * `"mspace"`, corresponding to `` and `` tags). + */ +function MathNode(type, children) { + this.type = type; + this.attributes = {}; + this.children = children || []; +} + +/** + * Sets an attribute on a MathML node. MathML depends on attributes to convey a + * semantic content, so this is used heavily. + */ +MathNode.prototype.setAttribute = function(name, value) { + this.attributes[name] = value; +}; + +/** + * Converts the math node into a MathML-namespaced DOM element. + */ +MathNode.prototype.toNode = function() { + var node = document.createElementNS( + "http://www.w3.org/1998/Math/MathML", this.type); + + for (var attr in this.attributes) { + if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { + node.setAttribute(attr, this.attributes[attr]); + } + } + + for (var i = 0; i < this.children.length; i++) { + node.appendChild(this.children[i].toNode()); + } + + return node; +}; + +/** + * Converts the math node into an HTML markup string. + */ +MathNode.prototype.toMarkup = function() { + var markup = "<" + this.type; + + // Add the attributes + for (var attr in this.attributes) { + if (Object.prototype.hasOwnProperty.call(this.attributes, attr)) { + markup += " " + attr + "=\""; + markup += utils.escape(this.attributes[attr]); + markup += "\""; + } + } + + markup += ">"; + + for (var i = 0; i < this.children.length; i++) { + markup += this.children[i].toMarkup(); + } + + markup += ""; + + return markup; +}; + +/** + * This node represents a piece of text. + */ +function TextNode(text) { + this.text = text; +} + +/** + * Converts the text node into a DOM text node. + */ +TextNode.prototype.toNode = function() { + return document.createTextNode(this.text); +}; + +/** + * Converts the text node into HTML markup (which is just the text itself). + */ +TextNode.prototype.toMarkup = function() { + return utils.escape(this.text); +}; + +module.exports = { + MathNode: MathNode, + TextNode: TextNode, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseData.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseData.js new file mode 100644 index 000000000..1075b82cb --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseData.js @@ -0,0 +1,12 @@ +/** + * The resulting parse tree nodes of the parse tree. + */ +function ParseNode(type, value, mode) { + this.type = type; + this.value = value; + this.mode = mode; +} + +module.exports = { + ParseNode: ParseNode, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseTree.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseTree.js new file mode 100644 index 000000000..3adba8248 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/parseTree.js @@ -0,0 +1,17 @@ +/** + * Provides a single function for parsing an expression using a Parser + * TODO(emily): Remove this + */ + +var Parser = require("./Parser"); + +/** + * Parses an expression using a Parser, then returns the parsed result. + */ +var parseTree = function(toParse, settings) { + var parser = new Parser(toParse, settings); + + return parser.parse(); +}; + +module.exports = parseTree; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/symbols.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/symbols.js new file mode 100644 index 000000000..9a2b130d1 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/symbols.js @@ -0,0 +1,687 @@ +/** + * This file holds a list of all no-argument functions and single-character + * symbols (like 'a' or ';'). + * + * For each of the symbols, there are three properties they can have: + * - font (required): the font to be used for this symbol. Either "main" (the + normal font), or "ams" (the ams fonts). + * - group (required): the ParseNode group type the symbol should have (i.e. + "textord", "mathord", etc). + See https://github.com/Khan/KaTeX/wiki/Examining-TeX#group-types + * - replace: the character that this symbol or function should be + * replaced with (i.e. "\phi" has a replace value of "\u03d5", the phi + * character in the main font). + * + * The outermost map in the table indicates what mode the symbols should be + * accepted in (e.g. "math" or "text"). + */ + +module.exports = { + math: {}, + text: {}, +}; + +function defineSymbol(mode, font, group, replace, name) { + module.exports[mode][name] = { + font: font, + group: group, + replace: replace, + }; +} + +// Some abbreviations for commonly used strings. +// This helps minify the code, and also spotting typos using jshint. + +// modes: +var math = "math"; +var text = "text"; + +// fonts: +var main = "main"; +var ams = "ams"; + +// groups: +var accent = "accent"; +var bin = "bin"; +var close = "close"; +var inner = "inner"; +var mathord = "mathord"; +var op = "op"; +var open = "open"; +var punct = "punct"; +var rel = "rel"; +var spacing = "spacing"; +var textord = "textord"; + +// Now comes the symbol table + +// Relation Symbols +defineSymbol(math, main, rel, "\u2261", "\\equiv"); +defineSymbol(math, main, rel, "\u227a", "\\prec"); +defineSymbol(math, main, rel, "\u227b", "\\succ"); +defineSymbol(math, main, rel, "\u223c", "\\sim"); +defineSymbol(math, main, rel, "\u22a5", "\\perp"); +defineSymbol(math, main, rel, "\u2aaf", "\\preceq"); +defineSymbol(math, main, rel, "\u2ab0", "\\succeq"); +defineSymbol(math, main, rel, "\u2243", "\\simeq"); +defineSymbol(math, main, rel, "\u2223", "\\mid"); +defineSymbol(math, main, rel, "\u226a", "\\ll"); +defineSymbol(math, main, rel, "\u226b", "\\gg"); +defineSymbol(math, main, rel, "\u224d", "\\asymp"); +defineSymbol(math, main, rel, "\u2225", "\\parallel"); +defineSymbol(math, main, rel, "\u22c8", "\\bowtie"); +defineSymbol(math, main, rel, "\u2323", "\\smile"); +defineSymbol(math, main, rel, "\u2291", "\\sqsubseteq"); +defineSymbol(math, main, rel, "\u2292", "\\sqsupseteq"); +defineSymbol(math, main, rel, "\u2250", "\\doteq"); +defineSymbol(math, main, rel, "\u2322", "\\frown"); +defineSymbol(math, main, rel, "\u220b", "\\ni"); +defineSymbol(math, main, rel, "\u221d", "\\propto"); +defineSymbol(math, main, rel, "\u22a2", "\\vdash"); +defineSymbol(math, main, rel, "\u22a3", "\\dashv"); +defineSymbol(math, main, rel, "\u220b", "\\owns"); + +defineSymbol(math, main, rel, "\u220b", "\\widehat"); +defineSymbol(math, main, rel, "\u220b", "\\widetilde"); +defineSymbol(math, main, rel, "\u220b", "\\sp"); +defineSymbol(math, main, rel, "\u220b", "\\quad"); +// defineSymbol(math, main, rel, "\u220b", "\\cr"); +defineSymbol(math, main, rel, "\u220b", "\\\\sim"); +defineSymbol(math, main, rel, "\u220b", "\\nonumber"); +defineSymbol(math, main, rel, "\u220b", "\\dots"); +defineSymbol(math, main, rel, "\u220b", "\\cases"); +defineSymbol(math, main, rel, "\u220b", "\\mit"); +defineSymbol(math, main, rel, "\u220b", "\\smallskip"); +defineSymbol(math, main, rel, "\u220b", "\\slash"); +defineSymbol(math, main, rel, "\u220b", "\\d"); +defineSymbol(math, main, rel, "\u220b", "\\c"); +defineSymbol(math, main, rel, "\u220b", "\\b"); +defineSymbol(math, main, rel, "\u220b", "\\M"); +defineSymbol(math, main, rel, "\u220b", "\\S"); +defineSymbol(math, main, rel, "\u220b", "\\("); +defineSymbol(math, main, rel, "\u220b", "\\)"); +// defineSymbol(math, main, rel, "\u220b", "\\Comp"); +defineSymbol(math, main, rel, "\u220b", "\\thinspace"); +defineSymbol(math, main, rel, "\u220b", "\\hskip"); +defineSymbol(math, main, rel, "\u220b", "\\tt"); +defineSymbol(math, main, rel, "\u220b", "\\not"); +defineSymbol(math, main, rel, "\u220b", "\\boldmathr"); +defineSymbol(math, main, rel, "\u220b", "\\overleftarrow"); +defineSymbol(math, main, rel, "\u220b", "\\overrightarrow"); +defineSymbol(math, main, rel, "\u220b", "\\intf"); +defineSymbol(math, main, rel, "\u220b", "\\sf"); +defineSymbol(math, main, rel, "\u220b", "\\textbf"); +defineSymbol(math, main, rel, "\u220b", "\\L"); +defineSymbol(math, main, rel, "\u220b", "\\pii"); +defineSymbol(math, main, rel, "\u220b", "\\unitlength"); +defineSymbol(math, main, rel, "\u220b", "\\arowtor5linv"); +defineSymbol(math, main, rel, "\u220b", "\\hline"); +defineSymbol(math, main, rel, "\u220b", "\\mathbin"); +defineSymbol(math, main, rel, "\u220b", "\\nc"); +defineSymbol(math, main, rel, "\u220b", "\\underbrace"); +defineSymbol(math, main, rel, "\u220b", "\\o"); +defineSymbol(math, main, rel, "\u220b", "\\a"); +defineSymbol(math, main, rel, "\u220b", "\\b"); +defineSymbol(math, main, rel, "\u220b", "\\c"); +defineSymbol(math, main, rel, "\u220b", "\\d"); +defineSymbol(math, main, rel, "\u220b", "\\e"); +defineSymbol(math, main, rel, "\u220b", "\\f"); +defineSymbol(math, main, rel, "\u220b", "\\g"); +defineSymbol(math, main, rel, "\u220b", "\\h"); +defineSymbol(math, main, rel, "\u220b", "\\i"); +defineSymbol(math, main, rel, "\u220b", "\\j"); +defineSymbol(math, main, rel, "\u220b", "\\k"); +defineSymbol(math, main, rel, "\u220b", "\\l"); +defineSymbol(math, main, rel, "\u220b", "\\m"); +defineSymbol(math, main, rel, "\u220b", "\\n"); +defineSymbol(math, main, rel, "\u220b", "\\o"); +// defineSymbol(math, main, rel, "\u220b", "\\wedgee"); +defineSymbol(math, main, rel, "\u220b", "\\sb"); +defineSymbol(math, main, rel, "\u220b", "\\do"); +defineSymbol(math, main, rel, "\u220b", "\\em"); +// defineSymbol(math, main, rel, "\u220b", "\\diamonda"); + + +defineSymbol(math, main, rel, "\u220b", "\\dint"); +defineSymbol(math, main, rel, "\u220b", "\\intd"); + + +// Punctuation +defineSymbol(math, main, punct, "\u002e", "\\ldotp"); +defineSymbol(math, main, punct, "\u22c5", "\\cdotp"); + +// Misc Symbols +defineSymbol(math, main, textord, "\u0023", "\\#"); +defineSymbol(math, main, textord, "\u0026", "\\&"); +defineSymbol(math, main, textord, "\u2135", "\\aleph"); +defineSymbol(math, main, textord, "\u2200", "\\forall"); +defineSymbol(math, main, textord, "\u210f", "\\hbar"); +defineSymbol(math, main, textord, "\u2203", "\\eixsts"); +defineSymbol(math, main, textord, "\u2207", "\\nabla"); +defineSymbol(math, main, textord, "\u266d", "\\flat"); +defineSymbol(math, main, textord, "\u2113", "\\ell"); +defineSymbol(math, main, textord, "\u266e", "\\natural"); +defineSymbol(math, main, textord, "\u2663", "\\clubsuit"); +defineSymbol(math, main, textord, "\u2118", "\\wp"); +defineSymbol(math, main, textord, "\u266f", "\\sharp"); +defineSymbol(math, main, textord, "\u2662", "\\diamondsuit"); +defineSymbol(math, main, textord, "\u211c", "\\Re"); +defineSymbol(math, main, textord, "\u2661", "\\heartsuit"); +defineSymbol(math, main, textord, "\u2111", "\\Im"); +defineSymbol(math, main, textord, "\u2660", "\\spadesuit"); + +// Math and Text +defineSymbol(math, main, textord, "\u2020", "\\dag"); +defineSymbol(math, main, textord, "\u2021", "\\ddag"); + +// Large Delimiters +defineSymbol(math, main, close, "\u23b1", "\\rmoustache"); +defineSymbol(math, main, open, "\u23b0", "\\lmoustache"); +defineSymbol(math, main, close, "\u27ef", "\\rgroup"); +defineSymbol(math, main, open, "\u27ee", "\\lgroup"); + +// Binary Operators +defineSymbol(math, main, bin, "\u2213", "\\mp"); +defineSymbol(math, main, bin, "\u2296", "\\ominus"); +defineSymbol(math, main, bin, "\u228e", "\\uplus"); +defineSymbol(math, main, bin, "\u2293", "\\sqcap"); +defineSymbol(math, main, bin, "\u2217", "\\ast"); +defineSymbol(math, main, bin, "\u2294", "\\sqcup"); +defineSymbol(math, main, bin, "\u25ef", "\\bigcirc"); +defineSymbol(math, main, bin, "\u2219", "\\bullet"); +defineSymbol(math, main, bin, "\u2021", "\\ddagger"); +defineSymbol(math, main, bin, "\u2240", "\\wr"); +defineSymbol(math, main, bin, "\u2a3f", "\\amalg"); + +// Arrow Symbols +defineSymbol(math, main, rel, "\u27f5", "\\longleftarrow"); +defineSymbol(math, main, rel, "\u21d0", "\\Leftarrow"); +defineSymbol(math, main, rel, "\u27f8", "\\Longleftarrow"); +defineSymbol(math, main, rel, "\u27f6", "\\longrightarrow"); +defineSymbol(math, main, rel, "\u21d2", "\\Rightarrow"); +defineSymbol(math, main, rel, "\u27f9", "\\Longrightarrow"); +defineSymbol(math, main, rel, "\u2194", "\\leftrightarrow"); +defineSymbol(math, main, rel, "\u27f7", "\\longleftrightarrow"); +defineSymbol(math, main, rel, "\u21d4", "\\Leftrightarrow"); +defineSymbol(math, main, rel, "\u27fa", "\\Longleftrightarrow"); +defineSymbol(math, main, rel, "\u21a6", "\\mapsto"); +defineSymbol(math, main, rel, "\u27fc", "\\longmapsto"); +defineSymbol(math, main, rel, "\u2197", "\\nearrow"); +defineSymbol(math, main, rel, "\u21a9", "\\hookleftarrow"); +defineSymbol(math, main, rel, "\u21aa", "\\hookrightarrow"); +defineSymbol(math, main, rel, "\u2198", "\\searrow"); +defineSymbol(math, main, rel, "\u21bc", "\\leftharpoonup"); +defineSymbol(math, main, rel, "\u21c0", "\\rightharpoonup"); +defineSymbol(math, main, rel, "\u2199", "\\swarrow"); +defineSymbol(math, main, rel, "\u21bd", "\\leftharpoondown"); +defineSymbol(math, main, rel, "\u21c1", "\\rightharpoondown"); +defineSymbol(math, main, rel, "\u2196", "\\nwarrow"); +defineSymbol(math, main, rel, "\u21cc", "\\rightleftharpoons"); + +// AMS Negated Binary Relations +defineSymbol(math, ams, rel, "\u226e", "\\nless"); +defineSymbol(math, ams, rel, "\ue010", "\\nleqslant"); +defineSymbol(math, ams, rel, "\ue011", "\\nleqq"); +defineSymbol(math, ams, rel, "\u2a87", "\\lneq"); +defineSymbol(math, ams, rel, "\u2268", "\\lneqq"); +defineSymbol(math, ams, rel, "\ue00c", "\\lvertneqq"); +defineSymbol(math, ams, rel, "\u22e6", "\\lnsim"); +defineSymbol(math, ams, rel, "\u2a89", "\\lnapprox"); +defineSymbol(math, ams, rel, "\u2280", "\\nprec"); +defineSymbol(math, ams, rel, "\u22e0", "\\npreceq"); +defineSymbol(math, ams, rel, "\u22e8", "\\precnsim"); +defineSymbol(math, ams, rel, "\u2ab9", "\\precnapprox"); +defineSymbol(math, ams, rel, "\u2241", "\\nsim"); +defineSymbol(math, ams, rel, "\ue006", "\\nshortmid"); +defineSymbol(math, ams, rel, "\u2224", "\\nmid"); +defineSymbol(math, ams, rel, "\u22ac", "\\nvdash"); +defineSymbol(math, ams, rel, "\u22ad", "\\nvDash"); +defineSymbol(math, ams, rel, "\u22ea", "\\ntriangleleft"); +defineSymbol(math, ams, rel, "\u22ec", "\\ntrianglelefteq"); +defineSymbol(math, ams, rel, "\u228a", "\\subsetneq"); +defineSymbol(math, ams, rel, "\ue01a", "\\varsubsetneq"); +defineSymbol(math, ams, rel, "\u2acb", "\\subsetneqq"); +defineSymbol(math, ams, rel, "\ue017", "\\varsubsetneqq"); +defineSymbol(math, ams, rel, "\u226f", "\\ngtr"); +defineSymbol(math, ams, rel, "\ue00f", "\\ngeqslant"); +defineSymbol(math, ams, rel, "\ue00e", "\\ngeqq"); +defineSymbol(math, ams, rel, "\u2a88", "\\gneq"); +defineSymbol(math, ams, rel, "\u2269", "\\gneqq"); +defineSymbol(math, ams, rel, "\ue00d", "\\gvertneqq"); +defineSymbol(math, ams, rel, "\u22e7", "\\gnsim"); +defineSymbol(math, ams, rel, "\u2a8a", "\\gnapprox"); +defineSymbol(math, ams, rel, "\u2281", "\\nsucc"); +defineSymbol(math, ams, rel, "\u22e1", "\\nsucceq"); +defineSymbol(math, ams, rel, "\u22e9", "\\succnsim"); +defineSymbol(math, ams, rel, "\u2aba", "\\succnapprox"); +defineSymbol(math, ams, rel, "\u2246", "\\ncong"); +defineSymbol(math, ams, rel, "\ue007", "\\nshortparallel"); +defineSymbol(math, ams, rel, "\u2226", "\\nparallel"); +defineSymbol(math, ams, rel, "\u22af", "\\nVDash"); +defineSymbol(math, ams, rel, "\u22eb", "\\ntriangleright"); +defineSymbol(math, ams, rel, "\u22ed", "\\ntrianglerighteq"); +defineSymbol(math, ams, rel, "\ue018", "\\nsupseteqq"); +defineSymbol(math, ams, rel, "\u228b", "\\supsetneq"); +defineSymbol(math, ams, rel, "\ue01b", "\\varsupsetneq"); +defineSymbol(math, ams, rel, "\u2acc", "\\supsetneqq"); +defineSymbol(math, ams, rel, "\ue019", "\\varsupsetneqq"); +defineSymbol(math, ams, rel, "\u22ae", "\\nVdash"); +defineSymbol(math, ams, rel, "\u2ab5", "\\precneqq"); +defineSymbol(math, ams, rel, "\u2ab6", "\\succneqq"); +defineSymbol(math, ams, rel, "\ue016", "\\nsubseteqq"); +defineSymbol(math, ams, bin, "\u22b4", "\\unlhd"); +defineSymbol(math, ams, bin, "\u22b5", "\\unrhd"); + +// AMS Negated Arrows +defineSymbol(math, ams, rel, "\u219a", "\\nleftarrow"); +defineSymbol(math, ams, rel, "\u219b", "\\nrightarrow"); +defineSymbol(math, ams, rel, "\u21cd", "\\nLeftarrow"); +defineSymbol(math, ams, rel, "\u21cf", "\\nRightarrow"); +defineSymbol(math, ams, rel, "\u21ae", "\\nleftrightarrow"); +defineSymbol(math, ams, rel, "\u21ce", "\\nLeftrightarrow"); + +// AMS Misc +defineSymbol(math, ams, rel, "\u25b3", "\\vartriangle"); +defineSymbol(math, ams, textord, "\u210f", "\\hslash"); +defineSymbol(math, ams, textord, "\u25bd", "\\triangledown"); +defineSymbol(math, ams, textord, "\u25ca", "\\lozenge"); +defineSymbol(math, ams, textord, "\u24c8", "\\circledS"); +defineSymbol(math, ams, textord, "\u00ae", "\\circledR"); +defineSymbol(math, ams, textord, "\u2221", "\\measuredangle"); +defineSymbol(math, ams, textord, "\u2204", "\\nexists"); +defineSymbol(math, ams, textord, "\u2127", "\\mho"); +defineSymbol(math, ams, textord, "\u2132", "\\Finv"); +defineSymbol(math, ams, textord, "\u2141", "\\Game"); +defineSymbol(math, ams, textord, "\u006b", "\\Bbbk"); +defineSymbol(math, ams, textord, "\u2035", "\\backprime"); +defineSymbol(math, ams, textord, "\u25b2", "\\blacktriangle"); +defineSymbol(math, ams, textord, "\u25bc", "\\blacktriangledown"); +defineSymbol(math, ams, textord, "\u25a0", "\\blacksquare"); +defineSymbol(math, ams, textord, "\u29eb", "\\blacklozenge"); +defineSymbol(math, ams, textord, "\u2605", "\\bigstar"); +defineSymbol(math, ams, textord, "\u2222", "\\sphericalangle"); +defineSymbol(math, ams, textord, "\u2201", "\\complement"); +defineSymbol(math, ams, textord, "\u00f0", "\\eth"); +defineSymbol(math, ams, textord, "\u2571", "\\diagup"); +defineSymbol(math, ams, textord, "\u2572", "\\diagdown"); +defineSymbol(math, ams, textord, "\u25a1", "\\square"); +defineSymbol(math, ams, textord, "\u25a1", "\\Box"); +defineSymbol(math, ams, textord, "\u25ca", "\\Diamond"); +defineSymbol(math, ams, textord, "\u00a5", "\\yen"); +defineSymbol(math, ams, textord, "\u2713", "\\checkmark"); + +// AMS Hebrew +defineSymbol(math, ams, textord, "\u2136", "\\beth"); +defineSymbol(math, ams, textord, "\u2138", "\\daleth"); +defineSymbol(math, ams, textord, "\u2137", "\\gimel"); + +// AMS Greek +defineSymbol(math, ams, textord, "\u03dd", "\\digamma"); +defineSymbol(math, ams, textord, "\u03f0", "\\varkappa"); + +// AMS Delimiters +defineSymbol(math, ams, open, "\u250c", "\\ulcorner"); +defineSymbol(math, ams, close, "\u2510", "\\urcorner"); +defineSymbol(math, ams, open, "\u2514", "\\llcorner"); +defineSymbol(math, ams, close, "\u2518", "\\lrcorner"); + +// AMS Binary Relations +defineSymbol(math, ams, rel, "\u2266", "\\leqq"); +defineSymbol(math, ams, rel, "\u2a7d", "\\leqslant"); +defineSymbol(math, ams, rel, "\u2a95", "\\eqslantless"); +defineSymbol(math, ams, rel, "\u2272", "\\lesssim"); +defineSymbol(math, ams, rel, "\u2a85", "\\lessapprox"); +defineSymbol(math, ams, rel, "\u224a", "\\approxeq"); +defineSymbol(math, ams, bin, "\u22d6", "\\lessdot"); +defineSymbol(math, ams, rel, "\u22d8", "\\lll"); +defineSymbol(math, ams, rel, "\u2276", "\\lessgtr"); +defineSymbol(math, ams, rel, "\u22da", "\\lesseqgtr"); +defineSymbol(math, ams, rel, "\u2a8b", "\\lesseqqgtr"); +defineSymbol(math, ams, rel, "\u2251", "\\doteqdot"); +defineSymbol(math, ams, rel, "\u2253", "\\risingdotseq"); +defineSymbol(math, ams, rel, "\u2252", "\\fallingdotseq"); +defineSymbol(math, ams, rel, "\u223d", "\\backsim"); +defineSymbol(math, ams, rel, "\u22cd", "\\backsimeq"); +defineSymbol(math, ams, rel, "\u2ac5", "\\subseteqq"); +defineSymbol(math, ams, rel, "\u22d0", "\\Subset"); +defineSymbol(math, ams, rel, "\u228f", "\\sqsubset"); +defineSymbol(math, ams, rel, "\u227c", "\\preccurlyeq"); +defineSymbol(math, ams, rel, "\u22de", "\\curlyeqprec"); +defineSymbol(math, ams, rel, "\u227e", "\\precsim"); +defineSymbol(math, ams, rel, "\u2ab7", "\\precapprox"); +defineSymbol(math, ams, rel, "\u22b2", "\\vartriangleleft"); +defineSymbol(math, ams, rel, "\u22b4", "\\trianglelefteq"); +defineSymbol(math, ams, rel, "\u22a8", "\\vDash"); +defineSymbol(math, ams, rel, "\u22aa", "\\Vvdash"); +defineSymbol(math, ams, rel, "\u2323", "\\smallsmile"); +defineSymbol(math, ams, rel, "\u2322", "\\smallfrown"); +defineSymbol(math, ams, rel, "\u224f", "\\bumpeq"); +defineSymbol(math, ams, rel, "\u224e", "\\Bumpeq"); +defineSymbol(math, ams, rel, "\u2267", "\\geqq"); +defineSymbol(math, ams, rel, "\u2a7e", "\\geqslant"); +defineSymbol(math, ams, rel, "\u2a96", "\\eqslantgtr"); +defineSymbol(math, ams, rel, "\u2273", "\\gtrsim"); +defineSymbol(math, ams, rel, "\u2a86", "\\gtrapprox"); +defineSymbol(math, ams, bin, "\u22d7", "\\gtrdot"); +defineSymbol(math, ams, rel, "\u22d9", "\\ggg"); +defineSymbol(math, ams, rel, "\u2277", "\\gtrless"); +defineSymbol(math, ams, rel, "\u22db", "\\gtreqless"); +defineSymbol(math, ams, rel, "\u2a8c", "\\gtreqqless"); +defineSymbol(math, ams, rel, "\u2256", "\\eqcirc"); +defineSymbol(math, ams, rel, "\u2257", "\\circeq"); +defineSymbol(math, ams, rel, "\u225c", "\\triangleq"); +defineSymbol(math, ams, rel, "\u223c", "\\thicksim"); +defineSymbol(math, ams, rel, "\u2248", "\\thickapprox"); +defineSymbol(math, ams, rel, "\u2ac6", "\\supseteqq"); +defineSymbol(math, ams, rel, "\u22d1", "\\Supset"); +defineSymbol(math, ams, rel, "\u2290", "\\sqsupset"); +defineSymbol(math, ams, rel, "\u227d", "\\succcurlyeq"); +defineSymbol(math, ams, rel, "\u22df", "\\curlyeqsucc"); +defineSymbol(math, ams, rel, "\u227f", "\\succsim"); +defineSymbol(math, ams, rel, "\u2ab8", "\\succapprox"); +defineSymbol(math, ams, rel, "\u22b3", "\\vartriangleright"); +defineSymbol(math, ams, rel, "\u22b5", "\\trianglerighteq"); +defineSymbol(math, ams, rel, "\u22a9", "\\Vdash"); +defineSymbol(math, ams, rel, "\u2223", "\\shortmid"); +defineSymbol(math, ams, rel, "\u2225", "\\shortparallel"); +defineSymbol(math, ams, rel, "\u226c", "\\between"); +defineSymbol(math, ams, rel, "\u22d4", "\\pitchfork"); +defineSymbol(math, ams, rel, "\u221d", "\\varpropto"); +defineSymbol(math, ams, rel, "\u25c0", "\\blacktriangleleft"); +defineSymbol(math, ams, rel, "\u2234", "\\therefore"); +defineSymbol(math, ams, rel, "\u220d", "\\backepsilon"); +defineSymbol(math, ams, rel, "\u25b6", "\\blacktriangleright"); +defineSymbol(math, ams, rel, "\u2235", "\\because"); +defineSymbol(math, ams, rel, "\u22d8", "\\llless"); +defineSymbol(math, ams, rel, "\u22d9", "\\gggtr"); +defineSymbol(math, ams, bin, "\u22b2", "\\lhd"); +defineSymbol(math, ams, bin, "\u22b3", "\\rhd"); +defineSymbol(math, ams, rel, "\u2242", "\\eqsim"); +defineSymbol(math, main, rel, "\u22c8", "\\Join"); +defineSymbol(math, ams, rel, "\u2251", "\\Doteq"); + +// AMS Binary Operators +defineSymbol(math, ams, bin, "\u2214", "\\dotplus"); +defineSymbol(math, ams, bin, "\u2216", "\\smallsetminus"); +defineSymbol(math, ams, bin, "\u22d2", "\\Cap"); +defineSymbol(math, ams, bin, "\u22d3", "\\Cup"); +defineSymbol(math, ams, bin, "\u2a5e", "\\doublebarwedge"); +defineSymbol(math, ams, bin, "\u229f", "\\boxminus"); +defineSymbol(math, ams, bin, "\u229e", "\\boxplus"); +defineSymbol(math, ams, bin, "\u22c7", "\\divideontimes"); +defineSymbol(math, ams, bin, "\u22c9", "\\ltimes"); +defineSymbol(math, ams, bin, "\u22ca", "\\rtimes"); +defineSymbol(math, ams, bin, "\u22cb", "\\leftthreetimes"); +defineSymbol(math, ams, bin, "\u22cc", "\\rightthreetimes"); +defineSymbol(math, ams, bin, "\u22cf", "\\curlywedge"); +defineSymbol(math, ams, bin, "\u22ce", "\\curlyvee"); +defineSymbol(math, ams, bin, "\u229d", "\\circleddash"); +defineSymbol(math, ams, bin, "\u229b", "\\circledast"); +defineSymbol(math, ams, bin, "\u22c5", "\\centerdot"); +defineSymbol(math, ams, bin, "\u22ba", "\\intercal"); +defineSymbol(math, ams, bin, "\u22d2", "\\doublecap"); +defineSymbol(math, ams, bin, "\u22d3", "\\doublecup"); +defineSymbol(math, ams, bin, "\u22a0", "\\boxtimes"); + +// AMS Arrows +defineSymbol(math, ams, rel, "\u21e2", "\\dashrightarrow"); +defineSymbol(math, ams, rel, "\u21e0", "\\dashleftarrow"); +defineSymbol(math, ams, rel, "\u21c7", "\\leftleftarrows"); +defineSymbol(math, ams, rel, "\u21c6", "\\leftrightarrows"); +defineSymbol(math, ams, rel, "\u21da", "\\Lleftarrow"); +defineSymbol(math, ams, rel, "\u219e", "\\twoheadleftarrow"); +defineSymbol(math, ams, rel, "\u21a2", "\\leftarrowtail"); +defineSymbol(math, ams, rel, "\u21ab", "\\looparrowleft"); +defineSymbol(math, ams, rel, "\u21cb", "\\leftrightharpoons"); +defineSymbol(math, ams, rel, "\u21b6", "\\curvearrowleft"); +defineSymbol(math, ams, rel, "\u21ba", "\\circlearrowleft"); +defineSymbol(math, ams, rel, "\u21b0", "\\Lsh"); +defineSymbol(math, ams, rel, "\u21c8", "\\upuparrows"); +defineSymbol(math, ams, rel, "\u21bf", "\\upharpoonleft"); +defineSymbol(math, ams, rel, "\u21c3", "\\downharpoonleft"); +defineSymbol(math, ams, rel, "\u22b8", "\\multimap"); +defineSymbol(math, ams, rel, "\u21ad", "\\leftrightsquigarrow"); +defineSymbol(math, ams, rel, "\u21c9", "\\rightrightarrows"); +defineSymbol(math, ams, rel, "\u21c4", "\\rightleftarrows"); +defineSymbol(math, ams, rel, "\u21a0", "\\twoheadrightarrow"); +defineSymbol(math, ams, rel, "\u21a3", "\\rightarrowtail"); +defineSymbol(math, ams, rel, "\u21ac", "\\looparrowright"); +defineSymbol(math, ams, rel, "\u21b7", "\\curvearrowright"); +defineSymbol(math, ams, rel, "\u21bb", "\\circlearrowright"); +defineSymbol(math, ams, rel, "\u21b1", "\\Rsh"); +defineSymbol(math, ams, rel, "\u21ca", "\\downdownarrows"); +defineSymbol(math, ams, rel, "\u21be", "\\upharpoonright"); +defineSymbol(math, ams, rel, "\u21c2", "\\downharpoonright"); +defineSymbol(math, ams, rel, "\u21dd", "\\rightsquigarrow"); +defineSymbol(math, ams, rel, "\u21dd", "\\leadsto"); +defineSymbol(math, ams, rel, "\u21db", "\\Rrightarrow"); +defineSymbol(math, ams, rel, "\u21be", "\\restriction"); + +defineSymbol(math, main, textord, "\u2018", "`"); +defineSymbol(math, main, textord, "$", "\\$"); +defineSymbol(math, main, textord, "%", "\\%"); +defineSymbol(math, main, textord, "_", "\\_"); +defineSymbol(math, main, textord, "\u2220", "\\angle"); +defineSymbol(math, main, textord, "\u221e", "\\infty"); +defineSymbol(math, main, textord, "\u2032", "\\prime"); +defineSymbol(math, main, textord, "\u25b3", "\\triangle"); +defineSymbol(math, main, textord, "\u0393", "\\Gamma"); +defineSymbol(math, main, textord, "\u0394", "\\Delta"); +defineSymbol(math, main, textord, "\u0398", "\\Theta"); +defineSymbol(math, main, textord, "\u039b", "\\Lambda"); +defineSymbol(math, main, textord, "\u039e", "\\Xi"); +defineSymbol(math, main, textord, "\u03a0", "\\Pi"); +defineSymbol(math, main, textord, "\u03a3", "\\Sigma"); +defineSymbol(math, main, textord, "\u03a5", "\\Upsilon"); +defineSymbol(math, main, textord, "\u03a6", "\\Phi"); +defineSymbol(math, main, textord, "\u03a8", "\\Psi"); +defineSymbol(math, main, textord, "\u03a9", "\\Omega"); +defineSymbol(math, main, textord, "\u00ac", "\\neg"); +defineSymbol(math, main, textord, "\u00ac", "\\lnot"); +defineSymbol(math, main, textord, "\u22a4", "\\top"); +defineSymbol(math, main, textord, "\u22a5", "\\bot"); +defineSymbol(math, main, textord, "\u2205", "\\emptyset"); +defineSymbol(math, ams, textord, "\u2205", "\\varnothing"); +defineSymbol(math, main, mathord, "\u03b1", "\\alpha"); +defineSymbol(math, main, mathord, "\u03b2", "\\beta"); +defineSymbol(math, main, mathord, "\u03b3", "\\gamma"); +defineSymbol(math, main, mathord, "\u03b4", "\\delta"); +defineSymbol(math, main, mathord, "\u03f5", "\\epsilon"); +defineSymbol(math, main, mathord, "\u03b6", "\\zeta"); +defineSymbol(math, main, mathord, "\u03b7", "\\eta"); +defineSymbol(math, main, mathord, "\u03b8", "\\theta"); +defineSymbol(math, main, mathord, "\u03b9", "\\iota"); +defineSymbol(math, main, mathord, "\u03ba", "\\kappa"); +defineSymbol(math, main, mathord, "\u03bb", "\\lambda"); +defineSymbol(math, main, mathord, "\u03bc", "\\mu"); +defineSymbol(math, main, mathord, "\u03bd", "\\nu"); +defineSymbol(math, main, mathord, "\u03be", "\\xi"); +defineSymbol(math, main, mathord, "o", "\\omicron"); +defineSymbol(math, main, mathord, "\u03c0", "\\pi"); +defineSymbol(math, main, mathord, "\u03c1", "\\rho"); +defineSymbol(math, main, mathord, "\u03c3", "\\sigma"); +defineSymbol(math, main, mathord, "\u03c4", "\\tau"); +defineSymbol(math, main, mathord, "\u03c5", "\\upsilon"); +defineSymbol(math, main, mathord, "\u03d5", "\\phi"); +defineSymbol(math, main, mathord, "\u03c7", "\\chi"); +defineSymbol(math, main, mathord, "\u03c8", "\\psi"); +defineSymbol(math, main, mathord, "\u03c9", "\\omega"); +defineSymbol(math, main, mathord, "\u03b5", "\\varepsilon"); +defineSymbol(math, main, mathord, "\u03d1", "\\vartheta"); +defineSymbol(math, main, mathord, "\u03d6", "\\varpi"); +defineSymbol(math, main, mathord, "\u03f1", "\\varrho"); +defineSymbol(math, main, mathord, "\u03c2", "\\varsigma"); +defineSymbol(math, main, mathord, "\u03c6", "\\varphi"); +defineSymbol(math, main, bin, "\u2217", "*"); +defineSymbol(math, main, bin, "+", "+"); +defineSymbol(math, main, bin, "\u2212", "-"); +defineSymbol(math, main, bin, "\u22c5", "\\cdot"); +defineSymbol(math, main, bin, "\u2218", "\\circ"); +defineSymbol(math, main, bin, "\u00f7", "\\div"); +defineSymbol(math, main, bin, "\u00b1", "\\pm"); +defineSymbol(math, main, bin, "\u00d7", "\\times"); +defineSymbol(math, main, bin, "\u2229", "\\cap"); +defineSymbol(math, main, bin, "\u222a", "\\cup"); +defineSymbol(math, main, bin, "\u2216", "\\setminus"); +defineSymbol(math, main, bin, "\u2227", "\\land"); +defineSymbol(math, main, bin, "\u2228", "\\lor"); +defineSymbol(math, main, bin, "\u2227", "\\wedge"); +defineSymbol(math, main, bin, "\u2228", "\\vee"); +defineSymbol(math, main, textord, "\u221a", "\\surd"); +defineSymbol(math, main, open, "(", "("); +defineSymbol(math, main, open, "[", "["); +defineSymbol(math, main, open, "\u27e8", "\\langle"); +defineSymbol(math, main, open, "\u2223", "\\lvert"); +defineSymbol(math, main, open, "\u2225", "\\lVert"); +defineSymbol(math, main, close, ")", ")"); +defineSymbol(math, main, close, "]", "]"); +defineSymbol(math, main, close, "?", "?"); +defineSymbol(math, main, close, "!", "!"); +defineSymbol(math, main, close, "\u27e9", "\\rangle"); +defineSymbol(math, main, close, "\u2223", "\\rvert"); +defineSymbol(math, main, close, "\u2225", "\\rVert"); +defineSymbol(math, main, rel, "=", "="); +defineSymbol(math, main, rel, "<", "<"); +defineSymbol(math, main, rel, ">", ">"); +defineSymbol(math, main, rel, ":", ":"); +defineSymbol(math, main, rel, "\u2248", "\\approx"); +defineSymbol(math, main, rel, "\u2245", "\\cong"); +defineSymbol(math, main, rel, "\u2265", "\\ge"); +defineSymbol(math, main, rel, "\u2265", "\\geq"); +defineSymbol(math, main, rel, "\u2190", "\\gets"); +defineSymbol(math, main, rel, ">", "\\gt"); +defineSymbol(math, main, rel, "\u2208", "\\in"); +defineSymbol(math, main, rel, "\u2209", "\\notin"); +defineSymbol(math, main, rel, "\u2282", "\\subset"); +defineSymbol(math, main, rel, "\u2283", "\\supset"); +defineSymbol(math, main, rel, "\u2286", "\\subseteq"); +defineSymbol(math, main, rel, "\u2287", "\\supseteq"); +defineSymbol(math, ams, rel, "\u2288", "\\nsubseteq"); +defineSymbol(math, ams, rel, "\u2289", "\\nsupseteq"); +defineSymbol(math, main, rel, "\u22a8", "\\models"); +defineSymbol(math, main, rel, "\u2190", "\\leftarrow"); +defineSymbol(math, main, rel, "\u2264", "\\le"); +defineSymbol(math, main, rel, "\u2264", "\\leq"); +defineSymbol(math, main, rel, "<", "\\lt"); +defineSymbol(math, main, rel, "\u2260", "\\ne"); +defineSymbol(math, main, rel, "\u2260", "\\neq"); +defineSymbol(math, main, rel, "\u2192", "\\rightarrow"); +defineSymbol(math, main, rel, "\u2192", "\\to"); +defineSymbol(math, ams, rel, "\u2271", "\\ngeq"); +defineSymbol(math, ams, rel, "\u2270", "\\nleq"); +defineSymbol(math, main, spacing, null, "\\!"); +defineSymbol(math, main, spacing, "\u00a0", "\\ "); +defineSymbol(math, main, spacing, "\u00a0", "~"); +defineSymbol(math, main, spacing, null, "\\,"); +defineSymbol(math, main, spacing, null, "\\:"); +defineSymbol(math, main, spacing, null, "\\;"); +defineSymbol(math, main, spacing, null, "\\enspace"); +defineSymbol(math, main, spacing, null, "\\qquad"); +defineSymbol(math, main, spacing, null, "\\quad"); +defineSymbol(math, main, spacing, "\u00a0", "\\space"); +defineSymbol(math, main, punct, ",", ","); +defineSymbol(math, main, punct, ";", ";"); +defineSymbol(math, main, punct, ":", "\\colon"); +defineSymbol(math, ams, bin, "\u22bc", "\\barwedge"); +defineSymbol(math, ams, bin, "\u22bb", "\\veebar"); +defineSymbol(math, main, bin, "\u2299", "\\odot"); +defineSymbol(math, main, bin, "\u2295", "\\oplus"); +defineSymbol(math, main, bin, "\u2297", "\\otimes"); +defineSymbol(math, main, textord, "\u2202", "\\partial"); +defineSymbol(math, main, bin, "\u2298", "\\oslash"); +defineSymbol(math, ams, bin, "\u229a", "\\circledcirc"); +defineSymbol(math, ams, bin, "\u22a1", "\\boxdot"); +defineSymbol(math, main, bin, "\u25b3", "\\bigtriangleup"); +defineSymbol(math, main, bin, "\u25bd", "\\bigtriangledown"); +defineSymbol(math, main, bin, "\u2020", "\\dagger"); +defineSymbol(math, main, bin, "\u22c4", "\\diamond"); +defineSymbol(math, main, bin, "\u22c6", "\\star"); +defineSymbol(math, main, bin, "\u25c3", "\\triangleleft"); +defineSymbol(math, main, bin, "\u25b9", "\\triangleright"); +defineSymbol(math, main, open, "{", "\\{"); +defineSymbol(math, main, close, "}", "\\}"); +defineSymbol(math, main, open, "{", "\\lbrace"); +defineSymbol(math, main, close, "}", "\\rbrace"); +defineSymbol(math, main, open, "[", "\\lbrack"); +defineSymbol(math, main, close, "]", "\\rbrack"); +defineSymbol(math, main, open, "\u230a", "\\lfloor"); +defineSymbol(math, main, close, "\u230b", "\\rfloor"); +defineSymbol(math, main, open, "\u2308", "\\lceil"); +defineSymbol(math, main, close, "\u2309", "\\rceil"); +defineSymbol(math, main, textord, "\\", "\\backslash"); +defineSymbol(math, main, textord, "\u2223", "|"); +defineSymbol(math, main, textord, "\u2223", "\\vert"); +defineSymbol(math, main, textord, "\u2225", "\\|"); +defineSymbol(math, main, textord, "\u2225", "\\Vert"); +defineSymbol(math, main, rel, "\u2191", "\\uparrow"); +defineSymbol(math, main, rel, "\u21d1", "\\Uparrow"); +defineSymbol(math, main, rel, "\u2193", "\\downarrow"); +defineSymbol(math, main, rel, "\u21d3", "\\Downarrow"); +defineSymbol(math, main, rel, "\u2195", "\\updownarrow"); +defineSymbol(math, main, rel, "\u21d5", "\\Updownarrow"); +defineSymbol(math, math, op, "\u2210", "\\coprod"); +defineSymbol(math, math, op, "\u22c1", "\\bigvee"); +defineSymbol(math, math, op, "\u22c0", "\\bigwedge"); +defineSymbol(math, math, op, "\u2a04", "\\biguplus"); +defineSymbol(math, math, op, "\u22c2", "\\bigcap"); +defineSymbol(math, math, op, "\u22c3", "\\bigcup"); +defineSymbol(math, math, op, "\u222b", "\\int"); +defineSymbol(math, math, op, "\u222b", "\\intop"); +defineSymbol(math, math, op, "\u222c", "\\iint"); +defineSymbol(math, math, op, "\u222d", "\\iiint"); +defineSymbol(math, math, op, "\u220f", "\\prod"); +defineSymbol(math, math, op, "\u2211", "\\sum"); + +defineSymbol(math, math, op, "\u2a02", "\\bigotimes"); +defineSymbol(math, math, op, "\u2a01", "\\bigoplus"); +defineSymbol(math, math, op, "\u2a00", "\\bigodot"); +defineSymbol(math, math, op, "\u222e", "\\oint"); +defineSymbol(math, math, op, "\u2a06", "\\bigsqcup"); +defineSymbol(math, math, op, "\u222b", "\\smallint"); +defineSymbol(math, main, inner, "\u2026", "\\ldots"); +defineSymbol(math, main, inner, "\u22ef", "\\cdots"); +defineSymbol(math, main, inner, "\u22f1", "\\ddots"); +defineSymbol(math, main, textord, "\u22ee", "\\vdots"); +defineSymbol(math, main, accent, "\u00b4", "\\acute"); +defineSymbol(math, main, accent, "\u0060", "\\grave"); +defineSymbol(math, main, accent, "\u00a8", "\\ddot"); +defineSymbol(math, main, accent, "\u007e", "\\tilde"); +defineSymbol(math, main, accent, "\u00af", "\\bar"); +defineSymbol(math, main, accent, "\u02d8", "\\breve"); +defineSymbol(math, main, accent, "\u02c7", "\\check"); +defineSymbol(math, main, accent, "\u005e", "\\hat"); +defineSymbol(math, main, accent, "\u20d7", "\\vec"); +defineSymbol(math, main, accent, "\u02d9", "\\dot"); +defineSymbol(math, main, mathord, "\u0131", "\\imath"); +defineSymbol(math, main, mathord, "\u0237", "\\jmath"); + + +defineSymbol(text, main, spacing, "\u00a0", "\\ "); +defineSymbol(text, main, spacing, "\u00a0", " "); +defineSymbol(text, main, spacing, "\u00a0", "~"); + +// There are lots of symbols which are the same, so we add them in afterwards. +var i; +var ch; + +// All of these are textords in math mode +var mathTextSymbols = "0123456789/@.\""; +for (i = 0; i < mathTextSymbols.length; i++) { + ch = mathTextSymbols.charAt(i); + defineSymbol(math, main, textord, ch, ch); +} + +// All of these are textords in text mode +var textSymbols = "0123456789`!@*()-=+[]'\";:?/.,"; +for (i = 0; i < textSymbols.length; i++) { + ch = textSymbols.charAt(i); + defineSymbol(text, main, textord, ch, ch); +} + +// All of these are textords in text mode, and mathords in math mode +var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +for (i = 0; i < letters.length; i++) { + ch = letters.charAt(i); + defineSymbol(math, main, mathord, ch, ch); + defineSymbol(text, main, textord, ch, ch); +} diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/utils.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/utils.js new file mode 100644 index 000000000..f9e57cc65 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/katex/src/utils.js @@ -0,0 +1,106 @@ +/** + * This file contains a list of utility functions which are useful in other + * files. + */ + +/** + * Provide an `indexOf` function which works in IE8, but defers to native if + * possible. + */ +var nativeIndexOf = Array.prototype.indexOf; +var indexOf = function(list, elem) { + if (list == null) { + return -1; + } + if (nativeIndexOf && list.indexOf === nativeIndexOf) { + return list.indexOf(elem); + } + var i = 0; + var l = list.length; + for (; i < l; i++) { + if (list[i] === elem) { + return i; + } + } + return -1; +}; + +/** + * Return whether an element is contained in a list + */ +var contains = function(list, elem) { + return indexOf(list, elem) !== -1; +}; + +/** + * Provide a default value if a setting is undefined + */ +var deflt = function(setting, defaultIfUndefined) { + return setting === undefined ? defaultIfUndefined : setting; +}; + +// hyphenate and escape adapted from Facebook's React under Apache 2 license + +var uppercase = /([A-Z])/g; +var hyphenate = function(str) { + return str.replace(uppercase, "-$1").toLowerCase(); +}; + +var ESCAPE_LOOKUP = { + "&": "&", + ">": ">", + "<": "<", + "\"": """, + "'": "'", +}; + +var ESCAPE_REGEX = /[&><"']/g; + +function escaper(match) { + return ESCAPE_LOOKUP[match]; +} + +/** + * Escapes text to prevent scripting attacks. + * + * @param {*} text Text value to escape. + * @return {string} An escaped string. + */ +function escape(text) { + return ("" + text).replace(ESCAPE_REGEX, escaper); +} + +/** + * A function to set the text content of a DOM element in all supported + * browsers. Note that we don't define this if there is no document. + */ +var setTextContent; +if (typeof document !== "undefined") { + var testNode = document.createElement("span"); + if ("textContent" in testNode) { + setTextContent = function(node, text) { + node.textContent = text; + }; + } else { + setTextContent = function(node, text) { + node.innerText = text; + }; + } +} + +/** + * A function to clear a node. + */ +function clearNode(node) { + setTextContent(node, ""); +} + +module.exports = { + contains: contains, + deflt: deflt, + escape: escape, + hyphenate: hyphenate, + indexOf: indexOf, + setTextContent: setTextContent, + clearNode: clearNode, +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/README.md b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/README.md new file mode 100644 index 000000000..69083d165 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/README.md @@ -0,0 +1 @@ +# match-at [![Build Status](https://travis-ci.org/spicyj/match-at.svg?branch=master)](https://travis-ci.org/spicyj/match-at) diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/index.js b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/index.js new file mode 100644 index 000000000..16da006f3 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/third_party/match-at/index.js @@ -0,0 +1,29 @@ +"use strict"; + +function getFlags(regex) { + var flags = ""; + + if (regex.ignoreCase) { + flags += "i"; + } + if (regex.multiline) { + flags += "m"; + } + if (regex.unicode) { + flags += "u"; + } + if (regex.dotAll) { + flags += "s"; + } + + return flags; +} + +module.exports = function matchAt(regex, input, position) { + if (!(regex instanceof RegExp)) { + throw new TypeError("Expected regex to be a RegExp"); + } + + var anchored = new RegExp("^(?:" + regex.source + ")", getFlags(regex)); + return anchored.exec(input.slice(position)); +}; diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/tokenize_latex.py b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/tokenize_latex.py new file mode 100644 index 000000000..7e44037f4 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/tokenize_latex/tokenize_latex.py @@ -0,0 +1,111 @@ +# taken and modified from https://github.com/harvardnlp/im2markup +# tokenize latex formulas +import os +import re +import subprocess +from datetime import datetime +from threading import Timer + + +def run_cmd(cmd, timeout_sec=30): + proc = subprocess.Popen(cmd, shell=True) + def kill_proc(p): return p.kill() + timer = Timer(timeout_sec, kill_proc, [proc]) + try: + timer.start() + stdout, stderr = proc.communicate() + finally: + timer.cancel() + + +def tokenize_latex(latex_code, latex_type="", middle_file=""): + if not latex_code: + return False, latex_code + if not latex_type: + latex_type = "tabular" if "tabular" in latex_code else "formula" + if not middle_file: + middle_file = "out-" + datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + ".txt" + temp_file = middle_file + '.tmp' + + if latex_type == "formula": + with open(temp_file, 'w') as f: + prepre = latex_code + # replace split, align with aligned + prepre = re.sub( + r'\\begin{(split|align|alignedat|alignat|eqnarray)\*?}(.+?)\\end{\1\*?}', + r'\\begin{aligned}\2\\end{aligned}', + prepre, + flags=re.S) + prepre = re.sub( + r'\\begin{(smallmatrix)\*?}(.+?)\\end{\1\*?}', + r'\\begin{matrix}\2\\end{matrix}', + prepre, + flags=re.S) + f.write(prepre) + + cmd = r"cat %s | node %s %s > %s " % (temp_file, os.path.join( + os.path.dirname(__file__), 'preprocess_formula.js'), 'normalize', middle_file) + ret = subprocess.call(cmd, shell=True) + os.remove(temp_file) + if ret != 0: + return False, latex_code + + operators = '\\s?'.join('|'.join(['arccos', 'arcsin', 'arctan', 'arg', 'cos', 'cosh', 'cot', 'coth', 'csc', 'deg', 'det', 'dim', 'exp', 'gcd', 'hom', 'inf', + 'injlim', 'ker', 'lg', 'lim', 'liminf', 'limsup', 'ln', 'log', 'max', 'min', 'Pr', 'projlim', 'sec', 'sin', 'sinh', 'sup', 'tan', 'tanh'])) + ops = re.compile(r'\\operatorname {(%s)}' % operators) + with open(middle_file, 'r') as fin: + for line in fin: + tokens = line.strip().split() + tokens_out = [] + for token in tokens: + tokens_out.append(token) + post = ' '.join(tokens_out) + # use \sin instead of \operatorname{sin} + names = ['\\' + x.replace(' ', '') for x in re.findall(ops, post)] + post = re.sub( + ops, lambda match: str( + names.pop(0)), post).replace( + r'\\ \end{array}', r'\end{array}') + os.remove(middle_file) + return True, post + + elif latex_type == "tabular": + latex_code = latex_code.replace("\\\\%", "\\\\ %") + latex_code = latex_code.replace("\\%", "") + latex_code = latex_code.split('%')[0] + latex_code = latex_code.replace("", "\\%") + if not "\\end{tabular}" in latex_code: + latex_code += "\\end{tabular}" + with open(middle_file, 'w') as f: + f.write(latex_code.replace('\r', ' ').replace('\n', ' ')) + cmd = "perl -pe 's|hskip(.*?)(cm\\|in\\|pt\\|mm\\|em)|hspace{\\1\\2}|g' %s > %s" % ( + middle_file, temp_file) + ret = subprocess.call(cmd, shell=True) + if ret != 0: + return False, latex_code + os.remove(middle_file) + cmd = r"cat %s | node %s %s > %s " % (temp_file, os.path.join( + os.path.dirname(__file__), 'preprocess_tabular.js'), 'tokenize', middle_file) + ret = subprocess.call(cmd, shell=True) + os.remove(temp_file) + if ret != 0: + return False, latex_code + with open(middle_file, 'r') as fin: + for line in fin: + tokens = line.strip().split() + tokens_out = [] + for token in tokens: + tokens_out.append(token) + post = ' '.join(tokens_out) + os.remove(middle_file) + return True, post + else: + print(f"latex type{latex_type} unrecognized.") + return False, latex_code + + +if __name__ == '__main__': + latex_code = open("2.txt", 'r').read().replace('\r', ' ') + print("=>", latex_code) + new_code = tokenize_latex(latex_code) + print("=>", new_code) diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/modules/visual_matcher.py b/vlmeval/dataset/MDPBench/metrics/cdm/modules/visual_matcher.py new file mode 100644 index 000000000..3ec8e259a --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/modules/visual_matcher.py @@ -0,0 +1,194 @@ +import time + +import numpy as np +from scipy.optimize import linear_sum_assignment +from scipy.spatial.distance import cdist + + +class SimpleAffineTransform: + """ + simple affine transform, only translation and scale. + """ + + def __init__(self, translation=(0, 0), scale=1.0): + self.translation = np.array(translation) + self.scale = scale + + def estimate(self, src, dst): + src_center = np.mean(src, axis=0) + dst_center = np.mean(dst, axis=0) + self.translation = dst_center - src_center + + src_dists = np.linalg.norm(src - src_center, axis=1) + dst_dists = np.linalg.norm(dst - dst_center, axis=1) + self.scale = np.mean(dst_dists) / (np.mean(src_dists) + 1e-10) + + def inverse(self): + inverse_transform = SimpleAffineTransform(-self.translation, 1.0 / self.scale) + return inverse_transform + + def __call__(self, coords): + return self.scale * (coords - np.mean(coords, axis=0)) + \ + np.mean(coords, axis=0) + self.translation + + def residuals(self, src, dst): + return np.sqrt(np.sum((self(src) - dst) ** 2, axis=1)) + + +def norm_coords(x, left, right): + if x < left: + return left + if x > right: + return right + return x + + +def norm_same_token(token): + special_map = { + "\\cdot": ".", + "\\mid": "|", + "\\to": "\\rightarrow", + "\\top": "T", + "\\Tilde": "\\tilde", + "\\cdots": "\\dots", + "\\prime": "'", + "\\ast": "*", + "\\left<": "\\langle", + "\\right>": "\\rangle" + } + if token in special_map.keys(): + token = special_map[token] + if token.startswith('\\left') or token.startswith('\\right'): + token = token.replace("\\left", "").replace("\\right", "") + if token.startswith('\\big') or token.startswith('\\Big'): + if "\\" in token[4:]: + token = "\\" + token[4:].split("\\")[-1] + else: + token = token[-1] + + if token in ['\\leq', '\\geq']: + return token[0:-1] + if token in ['\\lVert', '\\rVert', '\\Vert']: + return '\\|' + if token in ['\\lvert', '\\rvert', '\\vert']: + return '|' + if token.endswith("rightarrow"): + return "\\rightarrow" + if token.endswith("leftarrow"): + return "\\leftarrow" + if token.startswith('\\wide'): + return token.replace("wide", "") + if token.startswith('\\var'): + return token.replace("\\var", "") + return token + + +class HungarianMatcher: + def __init__( + self, + cost_token: float = 1, + cost_position: float = 0.05, + cost_order: float = 0.15, + ): + self.cost_token = cost_token + self.cost_position = cost_position + self.cost_order = cost_order + self.cost = {} + + def calculate_token_cost_old(self, box_gt, box_pred): + token_cost = np.ones((len(box_gt), len(box_pred))) + for i in range(token_cost.shape[0]): + box1 = box_gt[i] + for j in range(token_cost.shape[1]): + box2 = box_pred[j] + if box1['token'] == box2['token']: + token_cost[i, j] = 0 + elif norm_same_token(box1['token']) == norm_same_token(box2['token']): + token_cost[i, j] = 0.05 + return np.array(token_cost) + + def calculate_token_cost(self, box_gt, box_pred): + token2id = {} + for data in box_gt + box_pred: + if data['token'] not in token2id: + token2id[data['token']] = len(token2id) + num_classes = len(token2id) + + token2id_norm = {} + for data in box_gt + box_pred: + if norm_same_token(data['token']) not in token2id_norm: + token2id_norm[norm_same_token(data['token'])] = len(token2id_norm) + num_classes_norm = len(token2id_norm) + + gt_token_array = [] + norm_gt_token_array = [] + for data in box_gt: + gt_token_array.append(token2id[data['token']]) + norm_gt_token_array.append(token2id_norm[norm_same_token(data['token'])]) + + pred_token_logits = [] + norm_pred_token_logits = [] + for data in box_pred: + logits = [0] * num_classes + logits[token2id[data['token']]] = 1 + pred_token_logits.append(logits) + + logits_norm = [0] * num_classes_norm + logits_norm[token2id_norm[norm_same_token(data['token'])]] = 1 + norm_pred_token_logits.append(logits_norm) + + gt_token_array = np.array(gt_token_array) + pred_token_logits = np.array(pred_token_logits) + + norm_gt_token_array = np.array(norm_gt_token_array) + norm_pred_token_logits = np.array(norm_pred_token_logits) + + token_cost = 1.0 - pred_token_logits[:, gt_token_array] + norm_token_cost = 1.0 - norm_pred_token_logits[:, norm_gt_token_array] + + token_cost[np.logical_and(token_cost == 1, norm_token_cost == 0)] = 0.05 + return token_cost.T + + def box2array(self, box_list, size): + W, H = size + box_array = [] + for box in box_list: + x_min, y_min, x_max, y_max = box['bbox'] + box_array.append([x_min / W, y_min / H, x_max / W, y_max / H]) + return np.array(box_array) + + def order2array(self, box_list): + order_array = [] + for idx, box in enumerate(box_list): + order_array.append([idx / len(box_list)]) + return np.array(order_array) + + def calculate_l1_cost(self, gt_array, pred_array): + scale = gt_array.shape[-1] + l1_cost = cdist(gt_array, pred_array, 'minkowski', p=1) + return l1_cost / scale + + def __call__(self, box_gt, box_pred, gt_size, pred_size): + time.time() + gt_box_array = self.box2array(box_gt, gt_size) + pred_box_array = self.box2array(box_pred, pred_size) + gt_order_array = self.order2array(box_gt) + pred_order_array = self.order2array(box_pred) + + token_cost = self.calculate_token_cost(box_gt, box_pred) + position_cost = self.calculate_l1_cost(gt_box_array, pred_box_array) + order_cost = self.calculate_l1_cost(gt_order_array, pred_order_array) + + self.cost["token"] = token_cost + self.cost["position"] = position_cost + self.cost["order"] = order_cost + + cost = self.cost_token * token_cost + self.cost_position * \ + position_cost + self.cost_order * order_cost + cost[np.isnan(cost) | np.isinf(cost)] = 100 + indexes = linear_sum_assignment(cost) + matched_idxes = [] + for a, b in zip(*indexes): + matched_idxes.append((a, b)) + + return matched_idxes diff --git a/vlmeval/dataset/MDPBench/metrics/cdm/requirements.txt b/vlmeval/dataset/MDPBench/metrics/cdm/requirements.txt new file mode 100644 index 000000000..74edf2426 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm/requirements.txt @@ -0,0 +1,5 @@ +gradio==4.16.0 # optional +matplotlib +numpy<2.0.0 +scikit-image<=0.20.0 +tqdm diff --git a/vlmeval/dataset/MDPBench/metrics/cdm_metric.py b/vlmeval/dataset/MDPBench/metrics/cdm_metric.py new file mode 100644 index 000000000..728736105 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/cdm_metric.py @@ -0,0 +1,229 @@ +import json +import os +import shutil + +import numpy as np +from PIL import Image, ImageDraw +from skimage.measure import ransac + +from .cdm.modules.latex2bbox_color import latex2bbox_color +from .cdm.modules.visual_matcher import HungarianMatcher, SimpleAffineTransform + + +class CDM: + def __init__(self, output_root="./result"): + """ + Initialize the LaTeX formula evaluator. + + Args: + output_root (str): Root directory for saving intermediate and final results + """ + self.output_root = output_root + self.matcher = HungarianMatcher() + + # Evaluation parameters + self.max_iter = 3 + self.min_samples = 3 + self.residual_threshold = 25 + self.max_trials = 50 + + @staticmethod + def gen_color_list(num=10, gap=15): + """Generate a list of distinct colors for visualization""" + num += 1 + single_num = 255 // gap + 1 + max_num = single_num ** 3 + num = min(num, max_num) + color_list = [] + for idx in range(num): + R = idx // single_num**2 + GB = idx % single_num**2 + G = GB // single_num + B = GB % single_num + color_list.append((R * gap, G * gap, B * gap)) + return color_list[1:] + + @staticmethod + def update_inliers(ori_inliers, sub_inliers): + """Update inliers status based on new RANSAC results""" + inliers = np.copy(ori_inliers) + sub_idx = -1 + for idx in range(len(ori_inliers)): + if ori_inliers[idx] == False: + sub_idx += 1 + if sub_inliers[sub_idx] == True: + inliers[idx] = True + return inliers + + def _prepare_directories(self, img_id): + """Create necessary directories for output""" + os.makedirs(os.path.join(self.output_root, 'gt', 'bbox'), exist_ok=True) + os.makedirs(os.path.join(self.output_root, 'gt', 'vis'), exist_ok=True) + os.makedirs(os.path.join(self.output_root, 'pred', 'bbox'), exist_ok=True) + os.makedirs(os.path.join(self.output_root, 'pred', 'vis'), exist_ok=True) + os.makedirs(os.path.join(self.output_root, 'vis_match'), exist_ok=True) + + def _generate_bboxes(self, gt_latex, pred_latex, img_id): + """Generate bounding boxes for both GT and prediction""" + total_color_list = self.gen_color_list(num=5800) + + for subset, latex in zip(['gt', 'pred'], [gt_latex, pred_latex]): + output_path = os.path.join(self.output_root, subset) + temp_dir = os.path.join(self.output_root, f'temp_dir_{subset}_{img_id}') + os.makedirs(temp_dir, exist_ok=True) + + latex2bbox_color((latex, img_id, output_path, temp_dir, total_color_list)) + shutil.rmtree(temp_dir) + + def _load_bboxes(self, img_id): + """Load generated bounding boxes from files""" + gt_box_path = os.path.join(self.output_root, 'gt', 'bbox', f"{img_id}.jsonl") + pred_box_path = os.path.join(self.output_root, 'pred', 'bbox', f"{img_id}.jsonl") + + with open(gt_box_path, 'r') as f: + box_gt = [json.loads(line) for line in f if json.loads(line)['bbox']] + + with open(pred_box_path, 'r') as f: + box_pred = [json.loads(line) for line in f if json.loads(line)['bbox']] + + return box_gt, box_pred + + def _load_images(self, img_id): + """Load visualization images""" + gt_img_path = os.path.join(self.output_root, 'gt', 'vis', f"{img_id}_base.png") + pred_img_path = os.path.join(self.output_root, 'pred', 'vis', f"{img_id}_base.png") + return Image.open(gt_img_path), Image.open(pred_img_path) + + def _match_boxes(self, box_gt, box_pred, img_gt, img_pred): + """Perform box matching using Hungarian algorithm and RANSAC""" + matched_idxes = self.matcher(box_gt, box_pred, img_gt.size, img_pred.size) + + # Prepare matching points + src, dst = [], [] + for (idx1, idx2) in matched_idxes: + x1min, y1min, x1max, y1max = box_gt[idx1]['bbox'] + x2min, y2min, x2max, y2max = box_pred[idx2]['bbox'] + src.append([float((y1min + y1max) / 2), float((x1min + x1max) / 2)]) + dst.append([float((y2min + y2max) / 2), float((x2min + x2max) / 2)]) + + src = np.array(src) + dst = np.array(dst) + + # Apply RANSAC filtering + if src.shape[0] <= self.min_samples: + inliers = np.array([True for _ in matched_idxes]) + else: + inliers = np.array([False for _ in matched_idxes]) + for i in range(self.max_iter): + if src[inliers == False].shape[0] <= self.min_samples: + break + try: + model, inliers_1 = ransac( + (src[inliers == False], dst[inliers == False]), + SimpleAffineTransform, + min_samples=self.min_samples, + residual_threshold=self.residual_threshold, + max_trials=self.max_trials, + random_state=42, + ) + except TypeError: + # Older scikit-image versions do not support `random_state`. + model, inliers_1 = ransac( + (src[inliers == False], dst[inliers == False]), + SimpleAffineTransform, + min_samples=self.min_samples, + residual_threshold=self.residual_threshold, + max_trials=self.max_trials, + ) + if inliers_1 is not None and inliers_1.any(): + inliers = self.update_inliers(inliers, inliers_1) + else: + break + if len(inliers[inliers == True]) >= len(matched_idxes): + break + + # Filter token mismatches + for idx, (a, b) in enumerate(matched_idxes): + if inliers[idx] == True and self.matcher.cost['token'][a, b] == 1: + inliers[idx] = False + + return matched_idxes, inliers + + def _calculate_metrics(self, box_gt, box_pred, inliers): + """Calculate evaluation metrics""" + final_match_num = len(inliers[inliers == True]) + recall = round(final_match_num / len(box_gt), 3) + precision = round(final_match_num / len(box_pred), 3) + F1_score = round(2 * final_match_num / (len(box_gt) + len(box_pred)), 3) + return recall, precision, F1_score + + def _visualize_matches(self, img_gt, img_pred, box_gt, box_pred, + matched_idxes, inliers, img_id): + """Generate and save visualization of matches""" + gap = 5 + W1, H1 = img_gt.size + W2, H2 = img_pred.size + H = H1 + H2 + gap + W = max(W1, W2) + + # Create base visualization + vis_img = Image.new('RGB', (W, H), (255, 255, 255)) + vis_img.paste(img_gt, (0, 0)) + vis_img.paste(Image.new('RGB', (W, gap), (120, 120, 120)), (0, H1)) + vis_img.paste(img_pred, (0, H1 + gap)) + + # Create match visualization + match_img = vis_img.copy() + match_draw = ImageDraw.Draw(match_img) + + gt_matched_idx = {a: flag for (a, b), flag in zip(matched_idxes, inliers)} + pred_matched_idx = {b: flag for (a, b), flag in zip(matched_idxes, inliers)} + + # Draw GT boxes + for idx, box in enumerate(box_gt): + color = "green" if idx in gt_matched_idx and gt_matched_idx[idx] == True else "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, y_min - 1, x_max + 1, y_max + 1], + fill=None, outline=color, width=2) + + # Draw prediction boxes + for idx, box in enumerate(box_pred): + color = "green" if idx in pred_matched_idx and pred_matched_idx[idx] == True else "red" + x_min, y_min, x_max, y_max = box['bbox'] + match_draw.rectangle([x_min - 1, y_min - 1 + H1 + gap, x_max + 1, + y_max + 1 + H1 + gap], fill=None, outline=color, width=2) + + # Save visualizations + vis_img.save(os.path.join(self.output_root, 'vis_match', f"{img_id}_base.png")) + match_img.save(os.path.join(self.output_root, 'vis_match', f"{img_id}.png")) + + def evaluate(self, gt_latex, pred_latex, img_id): + """ + Evaluate a single LaTeX formula pair (ground truth vs prediction) + + Args: + gt_latex (str): Ground truth LaTeX formula + pred_latex (str): Predicted LaTeX formula + img_id (str): Unique identifier for this evaluation + + Returns: + dict: Evaluation metrics (recall, precision, F1_score) + """ + + try: + self._prepare_directories(img_id) + self._generate_bboxes(gt_latex, pred_latex, img_id) + box_gt, box_pred = self._load_bboxes(img_id) + img_gt, img_pred = self._load_images(img_id) + matched_idxes, inliers = self._match_boxes(box_gt, box_pred, img_gt, img_pred) + except BaseException: + return {"recall": 0, "precision": 0, "F1_score": 0} + + recall, precision, F1_score = self._calculate_metrics(box_gt, box_pred, inliers) + self._visualize_matches(img_gt, img_pred, box_gt, box_pred, matched_idxes, inliers, img_id) + + return { + "recall": recall, + "precision": precision, + "F1_score": F1_score, + } diff --git a/vlmeval/dataset/MDPBench/metrics/show_result.py b/vlmeval/dataset/MDPBench/metrics/show_result.py new file mode 100644 index 000000000..68da5ee43 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/show_result.py @@ -0,0 +1,185 @@ +import os +from collections import defaultdict + +import pandas as pd +from tabulate import tabulate + + +def show_result(results): + for metric_name in results.keys(): + print(f'{metric_name}:') + score_table = [[k, v] for k, v in results[metric_name].items()] + print(tabulate(score_table)) + print('=' * 100) + + +def sort_nested_dict(d): + # If it's a dictionary, recursively sort it + if isinstance(d, dict): + # Sort the current dictionary + sorted_dict = {k: sort_nested_dict(v) for k, v in sorted(d.items())} + return sorted_dict + # If not a dictionary, return directly + return d + + +def get_full_labels_results(samples): + if not samples: + return {} + label_group_dict = defaultdict(lambda: defaultdict(list)) + for sample in samples: + label_list = [] + if not sample.get("gt_attribute"): + continue + for anno in sample["gt_attribute"]: + for k, v in anno.items(): + label_list.append(k + ": " + str(v)) + # Currently if there are merged cases, calculate based on the set of all + # labels involved after merging + for label_name in list(set(label_list)): + for metric, score in sample['metric'].items(): + label_group_dict[label_name][metric].append(score) + + print('----Anno Attribute---------------') + result = {} + result['sample_count'] = {} + for attribute in label_group_dict.keys(): + for metric, scores in label_group_dict[attribute].items(): + mean_score = sum(scores) / len(scores) + if not result.get(metric): + result[metric] = {} + result[metric][attribute] = mean_score + result['sample_count'][attribute] = len(scores) + result = sort_nested_dict(result) + show_result(result) + return result + +# def get_page_split(samples, page_info): # Sample level metric +# if not page_info: +# return {} +# page_split_dict = defaultdict(lambda: defaultdict(list)) +# for sample in samples: +# img_name = sample['img_id'] if sample['img_id'].endswith('.jpg') else '_'.join(sample['img_id'].split('_')[:-1]) +# page_info_s = page_info[img_name] +# if not sample.get('metric'): +# continue +# for metric, score in sample['metric'].items(): +# for k,v in page_info_s.items(): +# if isinstance(v, list): # special issue +# for special_issue in v: +# if 'table' not in special_issue: # Table-related special fields have duplicates +# page_split_dict[metric][special_issue].append(score) +# else: +# page_split_dict[metric][k+": "+str(v)].append(score) + +# print('----Page Attribute---------------') +# result = {} +# result['sample_count'] = {} +# for metric in page_split_dict.keys(): +# for attribute, scores in page_split_dict[metric].items(): +# mean_score = sum(scores) / len(scores) +# if not result.get(metric): +# result[metric] = {} +# result[metric][attribute] = mean_score +# result['sample_count'][attribute] = len(scores) +# result = sort_nested_dict(result) +# show_result(result) +# return result + + +def get_page_split(samples, page_info): # Page level metric + if not page_info: + return {} + result_list = defaultdict(list) + for sample in samples: + # Try to find the base image name from the sample ID (which might be a variant name) + # The logic here needs to be robust to handle names like + # 'en_book_1_indoor.md' mapping back to 'en_book_1.png' + + current_id = sample['img_id'] + + # Find the matching key in page_info + # page_info keys are like 'en_book_1.png' + # current_id could be 'en_book_1_indoor_...' + + matched_key = None + # First try direct match + if current_id in page_info: + matched_key = current_id + else: + # Try to match by prefix + # We look for a key in page_info such that current_id starts with key_without_extension + for key in page_info.keys(): + key_no_ext = os.path.splitext(key)[0] + # Check if current_id starts with key_no_ext AND followed by _ or end of string + if current_id == key_no_ext or current_id.startswith(key_no_ext + '_'): + matched_key = key + break + + if matched_key: + img_name = matched_key + page_info_s = page_info[img_name] + else: + # Fallback to original logic if not found (though likely to fail if not found above) + img_name = sample['img_id'] if sample['img_id'].endswith('.jpg') or sample['img_id'].endswith( + '.png') else '_'.join(sample['img_id'].split('_')[:-1]) + if img_name not in page_info: + print(f"Warning: Could not find page info for {sample['img_id']}") + continue + page_info_s = page_info[img_name] + + if not sample.get('metric'): + continue + + for metric, score in sample['metric'].items(): + gt = sample['norm_gt'] if sample.get('norm_gt') else sample['gt'] + pred = sample['norm_pred'] if sample.get('norm_pred') else sample['pred'] + result_list[metric].append({ + 'image_name': img_name, + 'metric': metric, + 'attribute': 'ALL', + 'score': score, + 'upper_len': max(len(gt), len(pred)) + }) + for k, v in page_info_s.items(): + if isinstance(v, list): # special issue + for special_issue in v: + if 'table' not in special_issue: # Table-related special fields have duplicates + result_list[metric].append({ + 'image_name': img_name, + 'metric': metric, + 'attribute': special_issue, + 'score': score, + 'upper_len': max(len(gt), len(pred)) + }) + else: + result_list[metric].append({ + 'image_name': img_name, + 'metric': metric, + 'attribute': k + ": " + str(v), + 'score': score, + 'upper_len': max(len(gt), len(pred)) + }) + + # Page level logic, accumulation is only done within pages, and mean + # operation is performed between pages + result = {} + if result_list.get('Edit_dist'): # 只有Edit_dist需要进行page level的计算 + df = pd.DataFrame(result_list['Edit_dist']) + up_total_avg = df.groupby(["image_name", "attribute"]).apply(lambda x: (x["score"] * x['upper_len']).sum() / x['upper_len'].sum( + )).groupby('attribute').mean() # At page level, accumulate edits, denominator is sum of max(gt, pred) from each sample + # up_total_avg = df.groupby(["attribute"]).apply(lambda x: + # (x["score"]*x['upper_len']).sum() / x['upper_len'].sum()) # whole_level + result['Edit_dist'] = up_total_avg.to_dict() + for metric in result_list.keys(): + if metric == 'Edit_dist': + continue + df = pd.DataFrame(result_list[metric]) + page_avg = df.groupby(["image_name", "attribute"]).apply( + lambda x: x["score"].mean()).groupby('attribute').mean() # 页面内部平均以后,再页面间的平均 + result[metric] = page_avg.to_dict() + + result = sort_nested_dict(result) + # print('----Page Attribute---------------') + show_result(result) + return result diff --git a/vlmeval/dataset/MDPBench/metrics/table_metric.py b/vlmeval/dataset/MDPBench/metrics/table_metric.py new file mode 100644 index 000000000..4185ec9a1 --- /dev/null +++ b/vlmeval/dataset/MDPBench/metrics/table_metric.py @@ -0,0 +1,174 @@ +# Copyright 2020 IBM +# Author: peter.zhong@au1.ibm.com +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the Apache 2.0 License. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Apache 2.0 License for more details. + +from collections import deque + +import Levenshtein +# import rapidfuzz.distance as distance +from apted import APTED, Config +from apted.helpers import Tree +from lxml import etree, html +# from parallel import parallel_process +from tqdm import tqdm + + +class TableTree(Tree): + def __init__(self, tag, colspan=None, rowspan=None, content=None, *children): + self.tag = tag + self.colspan = colspan + self.rowspan = rowspan + self.content = content + self.children = list(children) + + def bracket(self): + """Show tree using brackets notation""" + if self.tag == 'td': + result = '"tag": %s, "colspan": %d, "rowspan": %d, "text": %s' % \ + (self.tag, self.colspan, self.rowspan, self.content) + else: + result = '"tag": %s' % self.tag + for child in self.children: + result += child.bracket() + return "{{{}}}".format(result) + + +class CustomConfig(Config): + @staticmethod + def maximum(*sequences): + """Get maximum possible value + """ + return max(map(len, sequences)) + + def normalized_distance(self, *sequences): + """Get distance from 0 to 1 + """ + return float(Levenshtein.distance(*sequences)) / self.maximum(*sequences) + + def rename(self, node1, node2): + """Compares attributes of trees""" + if (node1.tag != node2.tag) or (node1.colspan + != node2.colspan) or (node1.rowspan != node2.rowspan): + return 1. + if node1.tag == 'td': + if node1.content or node2.content: + return self.normalized_distance(node1.content, node2.content) + return 0. + + +class TEDS(object): + ''' Tree Edit Distance basead Similarity + ''' + + def __init__(self, structure_only=False, n_jobs=1, ignore_nodes=None): + assert isinstance( + n_jobs, int) and ( + n_jobs >= 1), 'n_jobs must be an integer greather than 1' + self.structure_only = structure_only + self.n_jobs = n_jobs + self.ignore_nodes = ignore_nodes + self.__tokens__ = [] + + def tokenize(self, node): + ''' Tokenizes table cells + ''' + self.__tokens__.append('<%s>' % node.tag) + if node.text is not None: + self.__tokens__ += list(node.text) + for n in node.getchildren(): + self.tokenize(n) + if node.tag != 'unk': + self.__tokens__.append('' % node.tag) + if node.tag != 'td' and node.tail is not None: + self.__tokens__ += list(node.tail) + + def load_html_tree(self, node, parent=None): + ''' Converts HTML tree to the format required by apted + ''' + global __tokens__ + if node.tag == 'td': + if self.structure_only: + cell = [] + else: + self.__tokens__ = [] + self.tokenize(node) + cell = self.__tokens__[1:-1].copy() + new_node = TableTree(node.tag, + int(node.attrib.get('colspan', '1')), + int(node.attrib.get('rowspan', '1')), + cell, *deque()) + else: + new_node = TableTree(node.tag, None, None, None, *deque()) + if parent is not None: + parent.children.append(new_node) + if node.tag != 'td': + for n in node.getchildren(): + self.load_html_tree(n, new_node) + if parent is None: + return new_node + + def evaluate(self, pred, true): + ''' Computes TEDS score between the prediction and the ground truth of a + given sample + ''' + if (not pred) or (not true): + return 0.0 + parser = html.HTMLParser(remove_comments=True, encoding='utf-8') + pred = html.fromstring(pred, parser=parser) + true = html.fromstring(true, parser=parser) + if pred.xpath('body/table') and true.xpath('body/table'): + pred = pred.xpath('body/table')[0] + true = true.xpath('body/table')[0] + if self.ignore_nodes: + etree.strip_tags(pred, *self.ignore_nodes) + etree.strip_tags(true, *self.ignore_nodes) + n_nodes_pred = len(pred.xpath(".//*")) + n_nodes_true = len(true.xpath(".//*")) + n_nodes = max(n_nodes_pred, n_nodes_true) + tree_pred = self.load_html_tree(pred) + tree_true = self.load_html_tree(true) + distance = APTED(tree_pred, tree_true, CustomConfig()).compute_edit_distance() + return 1.0 - (float(distance) / n_nodes) + else: + return 0.0 + + def batch_evaluate(self, pred_json, true_json): + ''' Computes TEDS score between the prediction and the ground truth of + a batch of samples + @params pred_json: {'FILENAME': 'HTML CODE', ...} + @params true_json: {'FILENAME': {'html': 'HTML CODE'}, ...} + @output: {'FILENAME': 'TEDS SCORE', ...} + ''' + samples = true_json.keys() + # if self.n_jobs == 1: + scores = [ + self.evaluate( + pred_json.get( + filename, + ''), + true_json[filename]['html']) for filename in tqdm(samples)] + # else: + # inputs = [{'pred': pred_json.get(filename, ''), 'true': true_json[filename]['html']} for filename in samples] + # scores = parallel_process(inputs, self.evaluate, use_kwargs=True, n_jobs=self.n_jobs, front_num=1) + scores = dict(zip(samples, scores)) + return scores + + +if __name__ == '__main__': + import json + import pprint + with open('sample_pred.json') as fp: + pred_json = json.load(fp) + with open('sample_gt.json') as fp: + true_json = json.load(fp) + teds = TEDS(n_jobs=4) + scores = teds.batch_evaluate(pred_json, true_json) + pp = pprint.PrettyPrinter() + pp.pprint(scores) diff --git a/vlmeval/dataset/MDPBench/registry/__init__.py b/vlmeval/dataset/MDPBench/registry/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/vlmeval/dataset/MDPBench/registry/registry.py b/vlmeval/dataset/MDPBench/registry/registry.py new file mode 100644 index 000000000..2f7902094 --- /dev/null +++ b/vlmeval/dataset/MDPBench/registry/registry.py @@ -0,0 +1,25 @@ +class Registry: + def __init__(self): + self._registry = {} + + def register(self, name): + def decorator(item): + if name in self._registry: + raise ValueError(f"Item {name} already registered.") + self._registry[name] = item + return item + return decorator + + def get(self, name): + if name not in self._registry: + raise ValueError(f"Item {name} not found in registry.") + return self._registry[name] + + def list_items(self): + return list(self._registry.keys()) + + +# Create global registries for tasks and models +EVAL_TASK_REGISTRY = Registry() +METRIC_REGISTRY = Registry() +DATASET_REGISTRY = Registry() diff --git a/vlmeval/dataset/MDPBench/requirements.txt b/vlmeval/dataset/MDPBench/requirements.txt new file mode 100644 index 000000000..93f7546c5 --- /dev/null +++ b/vlmeval/dataset/MDPBench/requirements.txt @@ -0,0 +1,20 @@ +apted +beautifulsoup4 +distance +evaluate +func_timeout +loguru +lxml +matplotlib +mmeval +numpy +opencv-python-headless +pandas +Pillow +pylatexenc +python-Levenshtein +PyYAML +scikit-image +scipy +tabulate +tqdm diff --git a/vlmeval/dataset/MDPBench/utils/__init__.py b/vlmeval/dataset/MDPBench/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/vlmeval/dataset/MDPBench/utils/data_preprocess.py b/vlmeval/dataset/MDPBench/utils/data_preprocess.py new file mode 100644 index 000000000..09d919c64 --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/data_preprocess.py @@ -0,0 +1,488 @@ +import html +import os +import re +import shutil +import subprocess +import unicodedata +import uuid + +from bs4 import BeautifulSoup +from pylatexenc.latex2text import LatexNodes2Text + + +def remove_markdown_fences(content): + content = re.sub(r'^```markdown\n?', '', content, flags=re.MULTILINE) + content = re.sub(r'^```html\n?', '', content, flags=re.MULTILINE) + content = re.sub(r'^```latex\n?', '', content, flags=re.MULTILINE) + content = re.sub(r'```\n?$', '', content, flags=re.MULTILINE) + return content + +# Standardize all consecutive characters + + +def replace_repeated_chars(input_str): + # Replace more than 4 consecutive underscores with 4 underscores + input_str = re.sub(r'_{4,}', '____', input_str) + # Replace more than 4 consecutive spaces with 4 spaces + input_str = re.sub(r' {4,}', ' ', input_str) + return input_str + # return re.sub(r'([^a-zA-Z0-9])\1{10,}', r'\1\1\1\1', input_str) # For + # other consecutive symbols (except numbers and letters), replace more + # than 10 occurrences with 4 + +# Special Unicode handling + + +def fullwidth_to_halfwidth(s): + result = [] + for char in s: + code = ord(char) + # Convert full-width space to half-width space + if code == 0x3000: + code = 0x0020 + # Convert other full-width characters to half-width + elif 0xFF01 <= code <= 0xFF5E: + code -= 0xFEE0 + result.append(chr(code)) + return ''.join(result) + + +def find_special_unicode(s): + special_chars = {} + for char in s: + if ord(char) > 127: # Non-ASCII characters + # unicode_name = unicodedata.name(char, None) + unicode_name = unicodedata.category(char) + special_chars[char] = f'U+{ord(char):04X} ({unicode_name})' + return special_chars + +# # Define dictionary for Unicode character replacements +# unicode_replacements = { +# "\u00A9": r"$\copyright$", # Copyright symbol © to latex +# "\u00AE": r"$^\circledR$", # Registered trademark ® to latex +# "\u2122": r"$^\text{TM}$", # Trademark ™ to latex +# "\u2018": "'", # Left single quote to straight quote +# "\u2019": "'", # Right single quote to straight quote +# "\u201C": "\"", # Left double quote to straight quote +# "\u201D": "\"", # Right double quote to straight quote +# "\u2013": "-", # En dash to hyphen +# "\u2014": "-", # Em dash to hyphen +# "\u2026": "...", # Unicode ellipsis to three dots +# "\u2103": r"$\textdegree C$", # ℃ +# "\u03B1": r"$\alpha$", # α +# "\u03B2": r"$\beta$", # β +# "\u03A3": r"$\Sigma$", # Σ +# } + +# # Use regex to replace Unicode characters +# def replace_unicode(match): +# char = match.group(0) +# return unicode_replacements.get(char, char) + + +inline_reg = re.compile( + r'\$(.*?)\$|' + r'\\\((.*?)\\\)', +) + + +def textblock2unicode(text): + inline_matches = inline_reg.finditer(text) + removal_positions = [] + for match in inline_matches: + position = [match.start(), match.end()] + content = match.group(1) if match.group(1) is not None else match.group(2) + # print('-------- content-------', content) + # Remove escape characters \ + clean_content = re.sub(r'\\([\\_&%^])', '', content) + + try: + if any(char in clean_content for char in r'\^_'): + if clean_content.endswith('\\'): + clean_content += ' ' + # inline_array.append(match.group(0)) + unicode_content = LatexNodes2Text().latex_to_text(clean_content) + removal_positions.append((position[0], position[1], unicode_content)) + except BaseException: + continue + + # Remove inline formulas from original text + for start, end, unicode_content in sorted(removal_positions, reverse=True): + text = text[:start] + unicode_content.strip() + text[end:] + + return text + + +def normalized_formula(text): + # Normalize math formulas before matching + filter_list = ['\\mathbf', '\\mathrm', '\\mathnormal', '\\mathit', '\\mathbb', '\\mathcal', '\\mathscr', '\\mathfrak', '\\mathsf', '\\mathtt', + '\\textbf', '\\text', '\\boldmath', '\\boldsymbol', '\\operatorname', '\\bm', + '\\symbfit', '\\mathbfcal', '\\symbf', '\\scriptscriptstyle', '\\notag', + '\\setlength', '\\coloneqq', '\\space', '\\thickspace', '\\thinspace', '\\medspace', '\\nobreakspace', '\\negmedspace', + '\\quad', '\\qquad', '\\enspace', '\\substackw', ' ', '$$', '\\left', '\\right', '\\displaystyle', '\\text'] + # '\\left', '\\right', '{', '}', ' '] + + # delimiter_filter + text = text.strip().strip('$').strip('\n') + pattern = re.compile(r"\\\[(.+?)(?]*>(.*)
' + tables = re.findall(pattern, table_res, re.DOTALL | re.IGNORECASE) + table_res = ''.join(tables) + # table_res = re.sub('','',table_res) + table_res = re.sub('( style=".*?")', "", table_res) + table_res = re.sub('( height=".*?")', "", table_res) + table_res = re.sub('( width=".*?")', "", table_res) + table_res = re.sub('( align=".*?")', "", table_res) + table_res = re.sub('( class=".*?")', "", table_res) + table_res = re.sub('', "", table_res) + + table_res = re.sub(r'\s+', " ", table_res) + table_res_no_space = '' + \ + table_res.replace(' ', '') + '
' + # table_res_no_space = re.sub(' (style=".*?")',"",table_res_no_space) + # table_res_no_space = re.sub(r'[ ]', " ", table_res_no_space) + table_res_no_space = re.sub('colspan="', ' colspan="', table_res_no_space) + table_res_no_space = re.sub('rowspan="', ' rowspan="', table_res_no_space) + table_res_no_space = re.sub('border="', ' border="', table_res_no_space) + + table_res = '' + table_res + '
' + # table_flow.append(table_res) + # table_flow_no_space.append(table_res_no_space) + + return table_res, table_res_no_space + + def clean_table(input_str, flag=True): + if flag: + input_str = input_str.replace('', '').replace('', '') + input_str = input_str.replace('', '').replace('', '') + input_str = input_str.replace('', '').replace('', '') + input_str = input_str.replace('
', '').replace('
', '') + input_str = input_str.replace('

', '').replace('

', '') + input_str = input_str.replace('', '') + input_str = re.sub('.*?', '', input_str) + return input_str + + norm_text, _ = process_table_html(text) + norm_text = clean_table(norm_text) + return norm_text + + +def normalized_latex_table(text): + def latex_template(latex_code): + template = r''' + \documentclass[border=20pt]{article} + \usepackage{subcaption} + \usepackage{url} + \usepackage{graphicx} + \usepackage{caption} + \usepackage{multirow} + \usepackage{booktabs} + \usepackage{color} + \usepackage{colortbl} + \usepackage{xcolor,soul,framed} + \usepackage{fontspec} + \usepackage{amsmath,amssymb,mathtools,bm,mathrsfs,textcomp} + \setlength{\parindent}{0pt}''' + \ + r''' + \begin{document} + ''' + \ + latex_code + \ + r''' + \end{document}''' + + return template + + def process_table_latex(latex_code): + SPECIAL_STRINGS = [ + ['\\\\vspace\\{.*?\\}', ''], + ['\\\\hspace\\{.*?\\}', ''], + ['\\\\rule\\{.*?\\}\\{.*?\\}', ''], + ['\\\\addlinespace\\[.*?\\]', ''], + ['\\\\addlinespace', ''], + ['\\\\renewcommand\\{\\\\arraystretch\\}\\{.*?\\}', ''], + ['\\\\arraystretch\\{.*?\\}', ''], + ['\\\\(row|column)?colors?\\{[^}]*\\}(\\{[^}]*\\}){0,2}', ''], + ['\\\\color\\{.*?\\}', ''], + ['\\\\textcolor\\{.*?\\}', ''], + ['\\\\rowcolor(\\[.*?\\])?\\{.*?\\}', ''], + ['\\\\columncolor(\\[.*?\\])?\\{.*?\\}', ''], + ['\\\\cellcolor(\\[.*?\\])?\\{.*?\\}', ''], + ['\\\\colorbox\\{.*?\\}', ''], + ['\\\\(tiny|scriptsize|footnotesize|small|normalsize|large|Large|LARGE|huge|Huge)', ''], + [r'\s+', ' '], + ['\\\\centering', ''], + ['\\\\begin\\{table\\}\\[.*?\\]', '\\\\begin{table}'], + ['\t', ''], + ['@{}', ''], + ['\\\\toprule(\\[.*?\\])?', '\\\\hline'], + ['\\\\bottomrule(\\[.*?\\])?', '\\\\hline'], + ['\\\\midrule(\\[.*?\\])?', '\\\\hline'], + ['p\\{[^}]*\\}', 'l'], + ['m\\{[^}]*\\}', 'c'], + ['\\\\scalebox\\{[^}]*\\}\\{([^}]*)\\}', '\\1'], + ['\\\\textbf\\{([^}]*)\\}', '\\1'], + ['\\\\textit\\{([^}]*)\\}', '\\1'], + ['\\\\cmidrule(\\[.*?\\])?\\(.*?\\)\\{([0-9]-[0-9])\\}', '\\\\cline{\\2}'], + ['\\\\hline', ''], + [r'\\multicolumn\{1\}\{[^}]*\}\{((?:[^{}]|(?:\{[^{}]*\}))*)\}', r'\1'] + ] + pattern = r'\\begin\{tabular\}.*\\end\{tabular\}' # 注意这里不用 .*? + matches = re.findall(pattern, latex_code, re.DOTALL) + latex_code = ' '.join(matches) + + for special_str in SPECIAL_STRINGS: + latex_code = re.sub(fr'{special_str[0]}', fr'{special_str[1]}', latex_code) + + return latex_code + + def convert_latex_to_html(latex_content, cache_dir='./temp'): + if not os.path.exists(cache_dir): + os.makedirs(cache_dir) + + uuid_str = str(uuid.uuid1()) + with open(f'{cache_dir}/{uuid_str}.tex', 'w') as f: + f.write(latex_template(latex_content)) + + cmd = ['latexmlc', '--quiet', '--nocomments', f'--log={cache_dir}/{uuid_str}.log', + f'{cache_dir}/{uuid_str}.tex', f'--dest={cache_dir}/{uuid_str}.html'] + try: + subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + with open(f'{cache_dir}/{uuid_str}.html', 'r') as f: + html_content = f.read() + + pattern = r']*>(.*)' + tables = re.findall(pattern, html_content, re.DOTALL | re.IGNORECASE) + tables = [f'{table}
' for table in tables] + html_content = '\n'.join(tables) + + except Exception: + html_content = '' + + shutil.rmtree(cache_dir) + return html_content + + html_text = convert_latex_to_html(text) + normlized_tables = normalized_html_table(html_text) + return normlized_tables + + +def normalized_table(text, format='html'): + if format not in ['html', 'latex']: + raise ValueError('Invalid format: {}'.format(format)) + else: + return globals()['normalized_{}_table'.format(format)](text) + + +def textblock_with_norm_formula(text): + inline_matches = inline_reg.finditer(text) + removal_positions = [] + for match in inline_matches: + position = [match.start(), match.end()] + content = match.group(1) if match.group(1) is not None else match.group(2) + # print('-------- content-------', content) + + norm_content = normalized_formula(content) + removal_positions.append((position[0], position[1], norm_content)) + + # Remove inline formulas from original text + for start, end, norm_content in sorted(removal_positions, reverse=True): + text = text[:start] + norm_content.strip() + text[end:] + + return text + +# def inline_filter_unicode(text): +# # Ensure text is string type +# if not isinstance(text, str): +# text = str(text) + +# # Convert LaTeX content to Unicode representation +# text = LatexNodes2Text().latex_to_text(text) + +# inline_array = [] +# inline_matches = inline_reg.finditer(text) + +# for match in inline_matches: +# position = [match.start(), match.end()] +# content = match.group(1) if match.group(1) is not None else match.group(2) + +# # Remove escape characters \ +# clean_content = re.sub(r'\\([\\_&%^])', '', content) + +# if any(char in clean_content for char in r'\^_'): +# # inline_array.append(match.group(0)) +# inline_array.append({ +# 'category_type': 'equation_inline', +# 'position': position, +# 'content': match.group(0), +# }) +# text = text.replace(match.group(0), '') +# # print('-----Found inline formula: ', match.group(0)) +# else: +# text = text.replace(match.group(0), content) +# # # Add to inline_array +# # inline_array.append({ +# # 'category_type': 'equation_inline', +# # 'position': position, +# # 'content': content, +# # }) + +# # # Remove matched formula from original text, can choose to replace with spaces or remove directly +# # text = text[:position[0]] + ' '*(position[1]-position[0]) + text[position[1]:] + +# return text, inline_array + + +def inline_filter_unicode(text): + # Ensure text is string type + if not isinstance(text, str): + text = str(text) + + # Replace inline formula boundary markers + # print('--------text-------',text) + placeholder = '__INLINE_FORMULA_BOUNDARY__' + text_copy = text.replace( + '$', placeholder).replace( + '\\(', placeholder).replace( + '\\)', placeholder) + # print('--------text_copy-------',text_copy) + # Convert LaTeX content to Unicode representation + text_copy = LatexNodes2Text().latex_to_text(text_copy) + # print('--------text_copy---unicode----',text_copy) + # Restore boundary markers + text_copy = text_copy.replace(placeholder, '$') + + inline_array = [] + inline_matches = inline_reg.finditer(text_copy) + # Record positions of inline formulas to be removed + removal_positions = [] + + for match in inline_matches: + position = [match.start(), match.end()] + content = match.group(1) if match.group(1) is not None else match.group(2) + print('-------- content-------', content) + # Remove escape characters \ + clean_content = re.sub(r'\\([\\_&%^])', '', content) + + if any(char in clean_content for char in r'\^_'): + # inline_array.append(match.group(0)) + inline_array.append({ + 'category_type': 'equation_inline', + 'position': position, + 'content': content, + }) + removal_positions.append((position[0], position[1])) + + # Remove inline formulas from original text + for start, end in sorted(removal_positions, reverse=True): + text = text[:start] + text[end:] + + return text, inline_array + + +def inline_filter(text): + # Ensure text is string type + if not isinstance(text, str): + text = str(text) + + inline_array = [] + inline_matches = inline_reg.finditer(text) + + for match in inline_matches: + position = [match.start(), match.end()] + content = match.group(1) if match.group(1) is not None else match.group(2) + # print('inline_content: ', content) + + # Remove escape characters \ + clean_content = re.sub(r'\\([\\_&%^])', '', content) + + if any(char in clean_content for char in r'\^_'): + # inline_array.append(match.group(0)) + inline_array.append({ + 'category_type': 'equation_inline', + 'position': position, + 'content': match.group(0), + }) + text = text.replace(match.group(0), '') + # print('-----Found inline formula: ', match.group(0)) + else: + text = text.replace(match.group(0), content) + + return text, inline_array + +# Text OCR quality check processing: + + +def clean_string(input_string): + # Use regex to keep Chinese characters, English letters and numbers + # input_string = input_string.replace('\\t', '').replace('\\n', '').replace('\t', '').replace('\n', '').replace('/t', '').replace('/n', '') + input_string = input_string.replace( + '\\t', + '').replace( + '\\n', + '').replace( + '\t', + '').replace( + '\n', + '').replace( + '/t', + '').replace( + '/n', + '') + cleaned_string = re.sub(r'[^\w\u4e00-\u9fff]', '', input_string) # 只保留中英文和数字 + return cleaned_string diff --git a/vlmeval/dataset/MDPBench/utils/extract.py b/vlmeval/dataset/MDPBench/utils/extract.py new file mode 100644 index 000000000..f84756ad8 --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/extract.py @@ -0,0 +1,590 @@ +import re +from collections import defaultdict + +from pylatexenc.latexwalker import (LatexCharsNode, LatexEnvironmentNode, LatexGroupNode, + LatexMacroNode, LatexSpecialsNode) + +from ..utils.data_preprocess import remove_markdown_fences, replace_repeated_chars +# from modules.table_utils import convert_markdown_to_html #end +from .table_utils import convert_markdown_to_html + + +def extract_tabular(text): + begin_pattern = r'\\begin{tabular}' + end_pattern = r'\\end{tabular}' + + tabulars = [] + positions = [] + current_pos = 0 + stack = [] + + while current_pos < len(text): + begin_match = re.search(begin_pattern, text[current_pos:]) + end_match = re.search(end_pattern, text[current_pos:]) + + if not begin_match and not end_match: + break + + if begin_match and (not end_match or begin_match.start() < end_match.start()): + stack.append(current_pos + begin_match.start()) + current_pos += begin_match.start() + len(end_pattern) + elif end_match: + if stack: + start_pos = stack.pop() + if not stack: + end_pos = current_pos + end_match.start() + len(end_pattern) + tabular_code = text[start_pos:end_pos] + tabulars.append(tabular_code) + positions.append((start_pos, end_pos)) + current_pos += end_match.start() + len(end_pattern) + else: + current_pos += 1 + + if stack: + new_start = stack[0] + len(begin_pattern) + new_tabulars, new_positions = extract_tabular(text[new_start:]) + new_positions = [(start + new_start, end + new_start) for start, end in new_positions] + tabulars.extend(new_tabulars) + positions.extend(new_positions) + + return tabulars, positions + + +# math reg + # r'\\begin{equation\*?}(.*?)\\end{equation\*?}|' + # r'\\begin{align\*?}(.*?)\\end{align\*?}|' + # r'\\begin{gather\*?}(.*?)\\end{gather\*?}|' +display_reg = re.compile( + # r'\\begin{equation\*?}(.*?)\\end{equation\*?}|' + # r'\\begin{align\*?}(.*?)\\end{align\*?}|' + # r'\\begin{gather\*?}(.*?)\\end{gather\*?}|' + # r'\\begin{array\*?}(.*?)\\end{array\*?}|' + r'\$\$(.*?)\$\$|' + r'\\\[(.*?)\\\]|' + r'\$(.*?)\$|' + r'\\\((.*?)\\\)', + re.DOTALL +) + +# inline_reg = re.compile( +# r'(?)', + re.DOTALL +) + +# title +title_reg = re.compile( + r'^\s*#.*$', + re.MULTILINE) + +# img +img_pattern = r'!\[.*?\]\(.*?\)' + +# code block +code_block_reg = re.compile( + r'```(\w+)\n(.*?)```', + re.DOTALL +) + + +def md_tex_filter(content): + ''' + Input: 1 page md or tex content - String + Output: text, display, inline, table, title, code - list + ''' + content = re.sub(img_pattern, '', content) # remove image + content = remove_markdown_fences(content) # remove markdown fences + content = replace_repeated_chars(content) # replace all consecutive characters + content = content.replace( + '', + '').replace( + '', + '').replace( + '', + '').replace( + '', + '') + + # # 使用正则表达式对unicode进行替换 + # special_unicode = ''.join(unicode_replacements.keys()) + # content = re.sub(f'[{special_unicode}]', replace_unicode, content) + + # content = fullwidth_to_halfwidth(content) # fullwidth to halfwidth, + # TODO: GT also needs this operation + + # # pylatexenc's unicode to latex + # content = unicode_to_latex(content, unknown_char_warning=False) + # markdown_table_content[i, j] = LatexNodes2Text().latex_to_text(content_str) + # content_ori = copy.deepcopy(content) + + # print('--------------After pre_process: \n', content) + + pred_all = [] + # deal with inline formula + # content_new, inline_array = inline_filter_unicode(content) + # #print('------------inline_array----------------',inline_array) + # for inline_item in inline_array: + # inline_item['content'] = inline_to_unicode(inline_item['content']) + # #print('------------inline_array_unicode----------------',inline_item['content']) + # pred_all.append({ + # 'category_type': 'text_all', + # 'position': inline_item['position'], + # 'content': inline_item['content'], + # 'fine_category_type': 'equation_inline' + # }) + + # extract latex table + latex_table_array, table_positions = extract_tex_table(content) + for latex_table, position in zip(latex_table_array, table_positions): + position = [position[0], position[0] + len(latex_table)] # !!! + pred_all.append({ + 'category_type': 'latex_table', + 'position': position, + 'content': latex_table + }) + # replace latex table with space + content = content[:position[0]] + ' ' * (position[1] - position[0]) + content[position[1]:] + + # print('--------After latex table: \n', content) + # print('-------latex_table_array: \n', latex_table_array) + + # extract html table + html_table_array, table_positions = extract_html_table(content) + for html_table, position in zip(html_table_array, table_positions): + position = [position[0], position[0] + len(html_table)] + pred_all.append({ + 'category_type': 'html_table', + 'position': position, + 'content': html_table + }) + # replace html table with space + content = content[:position[0]] + ' ' * (position[1] - position[0]) + content[position[1]:] + # html_table_array = [] + # html_table_matches = html_table_reg.finditer(content) + # if html_table_matches: + # for match in html_table_matches: + # matched = match.group(0) + # position = [match.start(), match.end()] + # html_table_array.append(matched.strip()) + # # content = content.replace(matched, ' '*len(matched)) # replace html table with space + # content = content[:position[0]] + ' '*(position[1]-position[0]) + content[position[1]:] # replace html table with space + # pred_all.append({ + # 'category_type': 'html_table', + # 'position': position, + # 'content': matched.strip() + # }) + + # print('--------------After html table: \n', content) + # # extract tables in latex and html + # table_array = [] + # table_matches = table_reg.finditer(content) + # tables = "" + # for match in table_matches: + # matched = match.group(0) + # if matched: + # tables += matched + # tables += "\n\n" + # table_array.append(matched) + # content = content.replace(matched, '') + + # extract interline formula + display_matches = display_reg.finditer(content) + for match in display_matches: + matched = match.group(0) + if matched: + # single_line = ''.join(matched.split()) + single_line = ' '.join(matched.strip().split('\n')) + position = [match.start(), match.end()] + # replace $$ with \[\] + dollar_pattern = re.compile(r'\$\$(.*?)\$\$|\$(.*?)\$|\\\((.*?)\\\)', re.DOTALL) + sub_match = dollar_pattern.search(single_line) + if sub_match is None: + # pass + content = content[:position[0]] + ' ' * \ + (position[1] - position[0]) + content[position[1]:] + pred_all.append({ + 'category_type': 'equation_isolated', + 'position': position, + 'content': single_line + }) + elif sub_match.group(1): + single_line = re.sub(dollar_pattern, r'\\[\1\\]', single_line) + # replace equation with space + content = content[:position[0]] + ' ' * \ + (position[1] - position[0]) + content[position[1]:] + pred_all.append({ + 'category_type': 'equation_isolated', + 'position': position, + 'content': single_line + }) + else: + # start, end = match.span() + # char_before = content_copy[start-1] if start > 0 else '\n' + # char_after = content_copy[end] if end < len(content_copy) else '\n' + # if char_before == '\n' or char_after == '\n': + # single_line = re.sub(dollar_pattern, r'\\[\2\3\\]', single_line) + # pred_all.append({ + # 'category_type': 'equation_isolated', + # 'position': position, + # 'content': single_line, + # 'fine_category_type': 'equation_inline' + # }) + single_line = re.sub(dollar_pattern, r'\\[\2\3\\]', single_line) + pred_all.append({ + 'category_type': 'equation_isolated', + 'position': position, + 'content': single_line, + 'fine_category_type': 'equation_inline' + }) + # single_line = re.sub(dollar_pattern, r'\\[\1\2\3\\]', single_line) + # print('single_line: ', single_line) + # content = content.replace(matched, ' '*len(matched)) + # pred_all.append({ + # 'category_type': 'equation_isolated', + # 'position': position, + # 'content': single_line + # }) + # print('-----Found display formula: ', matched) + + # print('-------------After display: \n', content) + # extract md table with || + md_table_mathces = md_table_reg.findall(content + '\n') + if len(md_table_mathces) >= 2: + # print("md table found!") + # print("content:", content) + content = convert_markdown_to_html(content) + # print('----------content after converting md table to html:', content) + html_table_matches = html_table_reg.finditer(content) + if html_table_matches: + for match in html_table_matches: + matched = match.group(0) + position = [match.start(), match.end()] + # content = content.replace(match, '') + # print('content after removing the md table:', content) + # replace md table with space + content = content[:position[0]] + ' ' * \ + (position[1] - position[0]) + content[position[1]:] + pred_all.append({ + 'category_type': 'html_table', + 'position': position, + 'content': matched.strip(), + 'fine_category_type': 'md2html_table' + }) + # print('---------After md table: \n', content) + + # extract code blocks + code_matches = code_block_reg.finditer(content) + if code_matches: + for match in code_matches: + position = [match.start(), match.end()] + language = match.group(1) + code = match.group(2).strip() + # content = content.replace(match.group(0), '') + # replace code block with space + content = content[:position[0]] + ' ' * \ + (position[1] - position[0]) + content[position[1]:] + pred_all.append({ + 'category_type': 'text_all', + 'position': position, + 'content': code, + 'language': language, + 'fine_category_type': 'code' + }) + + # print('-------After code block: \n', content) + + # # Extract titles: Do not extract titles, as some models do not wrap code blocks, causing all comments to be treated as titles + # title_matches = title_reg.finditer(content) + # if title_matches: + # for match in title_matches: + # position = [match.start(), match.end()] + # matched = match.group(0) + # matched = matched.replace("#", "").strip() + # # content = content.replace(match, '') + # # print('content after removing the titles:', content) + # if matched: + # # print('Add title: ', matched) + # content = content[:position[0]] + ' '*(position[1]-position[0]) + content[position[1]:] + # pred_all.append({ + # 'category_type': 'text_all', + # 'position': position, + # 'content': matched, + # 'fine_category_type': 'title' + # }) + + # print('----------After title: \n', content) + + # # Delete extracted content + # extracted_position = [_['position'] for _ in pred_all] + # for start, end in sorted(extracted_position, reverse=True): + # content = content[:start] + content[end:] + + # print('----------After delete extracted: \n', content) + + # Remove latex style + content = re.sub(r'\\title\{(.*?)\}', r'\1', content) + content = re.sub(r'\\title\s*\{\s*(.*?)\s*\}', r'\1', content, flags=re.DOTALL) + content = re.sub(r'\\text\s*\{\s*(.*?)\s*\}', r'\1', content, flags=re.DOTALL) + content = re.sub(r'\\section\*?\{(.*?)\}', r'\1', content) + content = re.sub(r'\\section\*?\{\s*(.*?)\s*\}', r'\1', content, flags=re.DOTALL) + + # extract texts + res = content.split('\n\n') + if len(res) == 1: + # some models do not use double newlines, so use single newlines to split + res = content.split('\n') + + content_position = 0 + for text in res: + position = [content_position, content_position + len(text)] + content_position += len(text) + text = text.strip() + text = text.strip('\n') + # print('ori_text: ', text) + # avoid some single newline content with many spaces + text = '\n'.join([_.strip() for _ in text.split('\n') if _.strip()]) + # print('after strip text: ', text) + + if text: # Check if the stripped text is not empty + if text.startswith(''): + pred_all.append({ + 'category_type': 'html_table', + 'position': position, + 'content': text, + }) + # elif text.startswith('#') and '\n' not in text: + # text = text.replace('#', '').strip() + # if text: + # # print('Add title: ', matched) + # pred_all.append({ + # 'category_type': 'text_all', + # 'position': position, + # 'content': text, + # 'fine_category_type': 'title' + # }) + elif text.startswith('$') and text.endswith('$'): + if text.replace('$', '').strip(): + pred_all.append({ + 'category_type': 'equation_isolated', + 'position': position, + 'content': text.strip(), + }) + else: + text = text.strip() + if text: + pred_all.append({ + 'category_type': 'text_all', + 'position': position, + 'content': text, + 'fine_category_type': 'text_block' + }) + # if '$' in text: + # for formula in re.findall(r'\$(.*?)\$', text): + # formula_array.append(formula) + + pred_dataset = defaultdict(list) + pred_all = sorted(pred_all, key=lambda x: x['position'][0]) + for item in pred_all: + pred_dataset[item['category_type']].append(item) + # pdb.set_trace() + return pred_dataset + + +# def replace_or_extract(match): +# content = match.group(1) if match.group(1) is not None else match.group(2) + +# if any(char in content for char in r'\^_'): +# inline_array.append(match.group(0)) +# return '' +# else: +# return content + +# extract inline math equations in text +# def inline_filter(text): + +# inline_array = [] +# inline_matches = inline_reg.finditer(text) +# for match in inline_matches: +# content = match.group(1) if match.group(1) is not None else match.group(2) + +# # remove \\, \_, \&, \%, \^ +# clean_content = re.sub(r'\\([\\_&%^])', '', content) + +# if any(char in clean_content for char in r'\^_'): +# inline_array.append(match.group(0)) +# text = text.replace(match.group(0), '') +# else: +# text = text.replace(match.group(0), content) + +# return text, inline_array + +# def extract_tex_table(content): +# tables = [] +# positions = [] + +# walker = LatexWalker(content) +# nodes, _, _ = walker.get_latex_nodes() +# if nodes is None: +# return tables, positions + +# for node in nodes: +# if isinstance(node, LatexEnvironmentNode) and ( +# node.environmentname == 'tabular' or node.environmentname == 'table'): +# # table_latex = extract_node_content(node) +# table_latex = content[node.pos:node.pos_end] +# tables.append(table_latex) +# start_pos = node.pos +# end_pos = get_node_end_pos(node) +# positions.append((start_pos, end_pos)) + +# return tables, positions + +def extract_tex_table(content): + tables = [] + tables_positions = [] + + pattern = r'\\begin{table}(.*?)\\end{table}' + for match in re.finditer(pattern, content, re.DOTALL): + start_pos = match.start() + end_pos = match.end() + table_content = match.group(0) + tables.append(table_content) + tables_positions.append((start_pos, end_pos)) + content = content[:start_pos] + ' ' * (end_pos - start_pos) + content[end_pos:] + + tabulars, tabular_positions = extract_tabular(content) + all_tables = tables + tabulars + all_positions = tables_positions + tabular_positions + + all_result = sorted([[pos, table]for pos, table in zip( + all_positions, all_tables)], key=lambda x: x[0][0]) + all_tables = [x[1] for x in all_result] + all_positions = [x[0] for x in all_result] + + return all_tables, all_positions + +# def extract_html_table(content): +# soup = BeautifulSoup(content, 'html.parser') +# all_tables = soup.find_all('table') +# tables = [] +# positions = [] + +# for table in all_tables: +# if table.find_parent('table') is None: +# table_str = str(table) +# start_pos = content.find(table_str) +# end_pos = start_pos + len(table_str) + +# tables.append(table_str) +# positions.append((start_pos, end_pos)) +# return tables, positions + + +def extract_html_table(text): + begin_pattern = r']*)>' + end_pattern = r'' + + tabulars = [] + positions = [] + current_pos = 0 + stack = [] + + while current_pos < len(text): + begin_match = re.search(begin_pattern, text[current_pos:]) + end_match = re.search(end_pattern, text[current_pos:]) + + if not begin_match and not end_match: + break + + if begin_match and (not end_match or begin_match.start() < end_match.start()): + stack.append(current_pos + begin_match.start()) + current_pos += begin_match.start() + len(end_pattern) + elif end_match: + if stack: + start_pos = stack.pop() + if not stack: + end_pos = current_pos + end_match.start() + len(end_pattern) + tabular_code = text[start_pos:end_pos] + tabulars.append(tabular_code) + positions.append((start_pos, end_pos)) + current_pos += end_match.start() + len(end_pattern) + else: + current_pos += 1 + + if stack: + new_start = stack[0] + len(begin_pattern) + new_tabulars, new_positions = extract_html_table(text[new_start:]) + new_positions = [(start + new_start, end + new_start) for start, end in new_positions] + tabulars.extend(new_tabulars) + positions.extend(new_positions) + + return tabulars, positions + + +def extract_node_content(node): + """ Recursively extract content from LatexEnvironmentNode and rebuild LaTeX table representation """ + if isinstance(node, LatexCharsNode): + return node.chars # Use chars attribute + elif isinstance(node, LatexGroupNode): + return "{" + "".join(extract_node_content(n) for n in node.nodelist) + "}" + elif isinstance(node, LatexMacroNode): + # Extract macro command and its arguments + macro_content = "\\" + node.macroname + if node.nodeargs: + macro_content += "".join([extract_node_content(arg) for arg in node.nodeargs]) + return macro_content + elif isinstance(node, LatexEnvironmentNode): + # Extract environment, preserve environment name and arguments + content = "\\begin{" + node.environmentname + "}" + if node.nodeargd and node.nodeargd.argnlist: + # content += "".join("{" + extract_node_content(arg) + "}" for arg in node.nodeargd) + # content += "".join("{" + extract_node_content(node.nodeargd) + "}") + content += "{" + extract_node_content(node.nodeargd.argnlist[0]) + "}" + if node.nodelist: + content += "".join(extract_node_content(n) for n in node.nodelist) + content += "\\end{" + node.environmentname + "}" + return content + elif isinstance(node, LatexSpecialsNode): # Changed to LatexSpecialsNode + return node.specials_chars + else: + return "" + + +def get_node_end_pos(node): + """Recursively determine the end position of a node""" + if hasattr(node, 'nodelist') and node.nodelist: + # If the node has child nodes, recursively find the end position of the last child node + return get_node_end_pos(node.nodelist[-1]) + elif hasattr(node, 'pos_end'): + # If the node has pos_end attribute, return it directly + return node.pos_end + else: + # If there are no child nodes, assume the node ends at the last character of its content + return node.pos + len(str(node)) + + +def remove_tex_table(content): + tables, positions = extract_tex_table(content) + + # Delete in reverse order by position to avoid affecting unprocessed start positions + for start, end in sorted(positions, reverse=True): + content = content[:start] + content[end:] # Remove table content + + return content diff --git a/vlmeval/dataset/MDPBench/utils/match.py b/vlmeval/dataset/MDPBench/utils/match.py new file mode 100644 index 000000000..13fdfd865 --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/match.py @@ -0,0 +1,321 @@ +import re +from copy import deepcopy + +import Levenshtein +import numpy as np +from bs4 import BeautifulSoup +from scipy.optimize import linear_sum_assignment + +from .data_preprocess import clean_string, normalized_formula, textblock2unicode + + +def get_pred_category_type(pred_idx, pred_items): + if pred_items[pred_idx].get('fine_category_type'): + pred_pred_category_type = pred_items[pred_idx]['fine_category_type'] + else: + pred_pred_category_type = pred_items[pred_idx]['category_type'] + return pred_pred_category_type + + +def compute_edit_distance_matrix_new(gt_lines, matched_lines): + try: + distance_matrix = np.zeros((len(gt_lines), len(matched_lines))) + for i, gt_line in enumerate(gt_lines): + for j, matched_line in enumerate(matched_lines): + if len(gt_line) == 0 and len(matched_line) == 0: + distance_matrix[i][j] = 0 + else: + distance_matrix[i][j] = Levenshtein.distance( + gt_line, matched_line) / max(len(matched_line), len(gt_line)) + return distance_matrix + except ZeroDivisionError: + raise + + +# 混合匹配here 0403 +def get_gt_pred_lines(gt_mix, pred_dataset_mix, line_type): + + norm_html_lines, gt_lines, pred_lines, norm_gt_lines, norm_pred_lines, gt_cat_list = [], [], [], [], [], [] + if line_type in ['html_table', 'latex_table']: + for item in gt_mix: + if item.get('fine_category_type'): + gt_cat_list.append(item['fine_category_type']) + else: + gt_cat_list.append(item['category_type']) + if item.get('content'): + gt_lines.append(str(item['content'])) + norm_html_lines.append(str(item['content'])) + elif line_type == 'text': + gt_lines.append(str(item['text'])) + elif line_type == 'html_table': + gt_lines.append(str(item['html'])) + elif line_type == 'formula': + gt_lines.append(str(item['latex'])) + elif line_type == 'latex_table': + gt_lines.append(str(item['latex'])) + norm_html_lines.append(str(item['html'])) + + pred_lines = [str(item['content']) for item in pred_dataset_mix] + if line_type == 'formula': + norm_gt_lines = [normalized_formula(_) for _ in gt_lines] + norm_pred_lines = [normalized_formula(_) for _ in pred_lines] + elif line_type == 'text': + norm_gt_lines = [clean_string(textblock2unicode(_)) for _ in gt_lines] + norm_pred_lines = [clean_string(textblock2unicode(_)) for _ in pred_lines] + else: + norm_gt_lines = gt_lines + norm_pred_lines = pred_lines + if line_type == 'latex_table': + gt_lines = norm_html_lines + + else: + for item in pred_dataset_mix: + # text + if item['category_type'] == 'text_all': + pred_lines.append(str(item['content'])) + norm_pred_lines.append(clean_string(textblock2unicode(str(item['content'])))) + # formula + elif item['category_type'] == 'equation_isolated': + pred_lines.append(str(item['content'])) + norm_pred_lines.append(normalized_formula(str(item['content']))) + # table + else: + pred_lines.append(str(item['content'])) + norm_pred_lines.append(str(item['content'])) + + for item in gt_mix: + if item.get('content'): + gt_lines.append(str(item['content'])) + if item['category_type'] == 'text_all': + norm_gt_lines.append(clean_string(textblock2unicode(str(item['content'])))) + else: + norm_gt_lines.append(item['content']) + + norm_html_lines.append(str(item['content'])) + + if item.get('fine_category_type'): + gt_cat_list.append(item['fine_category_type']) + else: + gt_cat_list.append(item['category_type']) + # text + elif item['category_type'] in ['text_block', 'title', 'code_txt', 'code_txt_caption', 'reference', 'equation_caption', 'figure_caption', 'figure_footnote', 'table_caption', 'table_footnote', 'code_algorithm', 'code_algorithm_caption', 'header', 'footer', 'page_footnote', 'page_number']: + gt_lines.append(str(item['text'])) + norm_gt_lines.append(clean_string(textblock2unicode(str(item['text'])))) + + if item.get('fine_category_type'): + gt_cat_list.append(item['fine_category_type']) + else: + gt_cat_list.append(item['category_type']) + + # formula + elif item['category_type'] == 'equation_isolated': + gt_lines.append(str(item['latex'])) + norm_gt_lines.append(normalized_formula(str(item['latex']))) + + if item.get('fine_category_type'): + gt_cat_list.append(item['fine_category_type']) + else: + gt_cat_list.append(item['category_type']) + # table + # elif item['category_type'] == 'table': + # gt_lines.append(str(item['html'])) + # norm_gt_lines.append(str(item['html'])) + + # if item.get('fine_category_type'): + # gt_cat_list.append(item['fine_category_type']) + # else: + # gt_cat_list.append(item['category_type']) + + filtered_lists = [(a, b, c) for a, b, c in zip( + gt_lines, norm_gt_lines, gt_cat_list) if a and b] + + # decompress to three lists + if filtered_lists: + gt_lines_c, norm_gt_lines_c, gt_cat_list_c = zip(*filtered_lists) + + # convert to lists + gt_lines_c = list(gt_lines_c) + norm_gt_lines_c = list(norm_gt_lines_c) + gt_cat_list_c = list(gt_cat_list_c) + else: + gt_lines_c = [] + norm_gt_lines_c = [] + gt_cat_list_c = [] + + # pred's empty values + filtered_lists = [(a, b) for a, b in zip(pred_lines, norm_pred_lines) if a and b] + + # decompress to two lists + if filtered_lists: + pred_lines_c, norm_pred_lines_c = zip(*filtered_lists) + + # convert to lists + pred_lines_c = list(pred_lines_c) + norm_pred_lines_c = list(norm_pred_lines_c) + else: + pred_lines_c = [] + norm_pred_lines_c = [] + + return gt_lines_c, norm_gt_lines_c, gt_cat_list_c, pred_lines_c, norm_pred_lines_c, gt_mix, pred_dataset_mix + + +def match_gt2pred_simple(gt_items, pred_items, line_type, img_name): + + gt_lines, norm_gt_lines, gt_cat_list, pred_lines, norm_pred_lines, gt_items, pred_items = get_gt_pred_lines( + gt_items, pred_items, line_type) + match_list = [] + + if not norm_gt_lines: # not matched pred should be concatenate + pred_idx_list = range(len(norm_pred_lines)) + match_list.append({ + 'gt_idx': [""], + 'gt': "", + 'pred_idx': pred_idx_list, + 'pred': ''.join(pred_lines[_] for _ in pred_idx_list), + 'gt_position': [""], + # get the first pred's position + 'pred_position': pred_items[pred_idx_list[0]]['position'][0], + 'norm_gt': "", + 'norm_pred': ''.join(norm_pred_lines[_] for _ in pred_idx_list), + 'gt_category_type': "", + # get the first pred's category + 'pred_category_type': get_pred_category_type(pred_idx_list[0], pred_items), + 'gt_attribute': [{}], + 'edit': 1, + 'img_id': img_name + }) + return match_list, None + elif not norm_pred_lines: # not matched gt should be separated + for gt_idx in range(len(norm_gt_lines)): + match_list.append({ + 'gt_idx': [gt_idx], + 'gt': gt_lines[gt_idx], + 'pred_idx': [""], + 'pred': "", + 'gt_position': [gt_items[gt_idx].get('order') if gt_items[gt_idx].get('order') else gt_items[gt_idx].get('position', [""])[0]], + 'pred_position': "", + 'norm_gt': norm_gt_lines[gt_idx], + 'norm_pred': "", + 'gt_category_type': gt_cat_list[gt_idx], + 'pred_category_type': "", + 'gt_attribute': [gt_items[gt_idx].get("attribute", {})], + 'edit': 1, + 'img_id': img_name + }) + return match_list, None + + cost_matrix = compute_edit_distance_matrix_new(norm_gt_lines, norm_pred_lines) + + row_ind, col_ind = linear_sum_assignment(cost_matrix) + + for gt_idx in range(len(norm_gt_lines)): + if gt_idx in row_ind: + row_i = list(row_ind).index(gt_idx) + pred_idx = int(col_ind[row_i]) + pred_line = pred_lines[pred_idx] + norm_pred_line = norm_pred_lines[pred_idx] + edit = cost_matrix[gt_idx][pred_idx] + else: + pred_idx = "" + pred_line = "" + norm_pred_line = "" + edit = 1 + + match_list.append({ + 'gt_idx': [gt_idx], + 'gt': gt_lines[gt_idx], + 'norm_gt': norm_gt_lines[gt_idx], + 'gt_category_type': gt_cat_list[gt_idx], + 'gt_position': [gt_items[gt_idx].get('order') if gt_items[gt_idx].get('order') else gt_items[gt_idx].get('position', [""])[0]], + 'gt_attribute': [gt_items[gt_idx].get("attribute", {})], + 'pred_idx': [pred_idx], + 'pred': pred_line, + 'norm_pred': norm_pred_line, + 'pred_category_type': get_pred_category_type(pred_idx, pred_items) if pred_idx else "", + 'pred_position': pred_items[pred_idx]['position'][0] if pred_idx else "", + 'edit': edit, + 'img_id': img_name + }) + + pred_idx_list = [pred_idx for pred_idx in range( + len(norm_pred_lines)) if pred_idx not in col_ind] # get not matched preds + if pred_idx_list: + if line_type in ['html_table', 'latex_table']: + unmatch_table_pred = [] + for i in pred_idx_list: + original_item = pred_items[i] + soup = BeautifulSoup(original_item.get('content'), 'html.parser') + text_block = [re.sub(r'\$\\cdot\$', '', item.string).strip() + for item in soup.findAll('td') if item.string] + for concatenate_text in text_block: + new_item = deepcopy(original_item) + new_item['content'] = concatenate_text + new_item['category_type'] = 'text_all' + unmatch_table_pred.append(new_item) + return match_list, unmatch_table_pred + + else: + match_list.append({ + 'gt_idx': [""], + 'gt': "", + 'pred_idx': pred_idx_list, + 'pred': ''.join(pred_lines[_] for _ in pred_idx_list), + 'gt_position': [""], + # get the first pred's position + 'pred_position': pred_items[pred_idx_list[0]]['position'][0], + 'norm_gt': "", + 'norm_pred': ''.join(norm_pred_lines[_] for _ in pred_idx_list), + 'gt_category_type': "", + # get the first pred's category + 'pred_category_type': get_pred_category_type(pred_idx_list[0], pred_items), + 'gt_attribute': [{}], + 'edit': 1, + 'img_id': img_name + }) + return match_list, None + + +def match_gt2pred_no_split(gt_items, pred_items, line_type, img_name): + # directly concatenate gt and pred by position + gt_lines, norm_gt_lines, gt_cat_list, pred_lines, norm_pred_lines = get_gt_pred_lines( + gt_items, pred_items) + gt_line_with_position = [] + for gt_line, norm_gt_line, gt_item in zip(gt_lines, norm_gt_lines, gt_items): + gt_position = gt_item['order'] if gt_item.get( + 'order') else gt_item.get('position', [""])[0] + if gt_position: + gt_line_with_position.append((gt_position, gt_line, norm_gt_line)) + sorted_gt_lines = sorted(gt_line_with_position, key=lambda x: x[0]) + gt = '\n\n'.join([_[1] for _ in sorted_gt_lines]) + norm_gt = '\n\n'.join([_[2] for _ in sorted_gt_lines]) + pred_line_with_position = [ + (pred_item['position'], + pred_line, + pred_norm_line) for pred_line, + pred_norm_line, + pred_item in zip( + pred_lines, + norm_pred_lines, + pred_items)] + sorted_pred_lines = sorted(pred_line_with_position, key=lambda x: x[0]) + pred = '\n\n'.join([_[1] for _ in sorted_pred_lines]) + norm_pred = '\n\n'.join([_[2] for _ in sorted_pred_lines]) + # edit = Levenshtein.distance(norm_gt, norm_pred)/max(len(norm_gt), len(norm_pred)) + if norm_gt or norm_pred: + return [{ + 'gt_idx': [0], + 'gt': gt, + 'norm_gt': norm_gt, + 'gt_category_type': "text_merge", + 'gt_position': [""], + 'gt_attribute': [{}], + 'pred_idx': [0], + 'pred': pred, + 'norm_pred': norm_pred, + 'pred_category_type': "text_merge", + 'pred_position': "", + # 'edit': edit, + 'img_id': img_name + }] + else: + return [] diff --git a/vlmeval/dataset/MDPBench/utils/match_quick.py b/vlmeval/dataset/MDPBench/utils/match_quick.py new file mode 100644 index 000000000..b6c311536 --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/match_quick.py @@ -0,0 +1,1284 @@ +import copy +import re +from collections import Counter, defaultdict +from copy import deepcopy +from typing import Any, Dict, List + +# from rapidfuzz.distance import Levenshtein +import Levenshtein +import numpy as np +from Levenshtein import distance as Levenshtein_distance +from scipy.optimize import linear_sum_assignment + +from ..utils.match import (compute_edit_distance_matrix_new, get_gt_pred_lines, + get_pred_category_type) + +# ARRAY_RE = re.compile( +# r'\\begin\{array\}\{[^}]*\}(.*?)\\end\{array\}', re.S +# ) + +# def split_gt_equation_arrays(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]: +# """ +# 拆分带 \\begin{array} … \\end{array} 的 GT 字典条目。 + +# - 仅针对 category_type == 'equation_isolated' 且 latex 含 array。 +# - 每行公式拆出一个新条目: +# * 更新 'latex' +# * 若存在 line_with_spans,则同步替换其内部 latex +# * 'order' 由 7 --> 7.1, 7.2, … +# """ +# output = [] + +# for item in data: +# # 只处理满足条件的字典 +# if (item.get("category_type") == "equation_isolated" and +# "\\begin{array" in item.get("latex", "")): + +# # 抽取 array 内部内容 +# match = ARRAY_RE.search(item["latex"]) +# if match: +# body = match.group(1) # 去掉 array 外壳 +# # 按 LaTeX 行分隔符 \\\\ 拆分 +# lines = [ln.strip() for ln in re.split(r'\\\\', body) if ln.strip()] + +# base_order = float(item["order"]) # 7 -> 7.0,可兼容 float/int + +# for idx, line in enumerate(lines, start=1): +# new_item = deepcopy(item) +# new_item["latex"] = f"\\[{line}\\]" +# new_item["order"] = round(base_order + idx / 10, 1) +# output.append(new_item) +# continue # 跳过把原 item 加入 +# # 其它情况不修改 +# output.append(item) + +# return output + +# def _wrap(line: str) -> str: +# """给单行公式重新包 \\[ ... \\]""" +# return f"\\[{line.strip()}\\]" + +# def split_equation_arrays(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]: +# """ +# 处理 category_type == 'equation_isolated' 且含 \\begin{array} … 的条目: +# * 拆分多行公式 +# * 重新包装 content +# * **重计算 position / positions** +# """ +# out: List[Dict[str, Any]] = [] + +# for item in data: +# if (item.get("category_type") == "equation_isolated" and +# "\\begin{array" in item.get("content", "")): + +# content = item["content"] +# m = ARRAY_RE.search(content) +# if not m: +# out.append(item) +# continue + +# body = m.group(1) +# lines = [ln.strip() for ln in re.split(r'\\\\', body) if ln.strip()] + +# # 全局起始字符索引 +# pos_key = "position" if "position" in item else "positions" +# global_start = item[pos_key][0] + +# # array 正文在原 content 内的起点 +# body_start_in_content = m.start(1) + +# search_from = 0 # 在 body 中的游标 +# for ln in lines: +# # 在 body 中找到当前行的偏移 +# idx_in_body = body.find(ln, search_from) +# if idx_in_body == -1: +# # 不太可能发生;保守处理 +# idx_in_body = search_from +# search_from = idx_in_body + len(ln) # 更新游标 + +# # 计算全局索引 +# line_start_global = global_start + body_start_in_content + idx_in_body +# line_end_global = line_start_global + len(ln) - 1 + +# new_item = deepcopy(item) +# new_item["content"] = _wrap(ln) +# new_item[pos_key] = [line_start_global, line_end_global] + +# out.append(new_item) + +# # 拆分完成,不保留原条目 +# continue + +# # 其它条目直接加入 +# out.append(item) + +# return out + +ARRAY_RE = re.compile( + r'\\begin\{array\}\{(?P[^}]*)\}(?P.*?)\\end\{array\}', + re.S +) + + +def is_all_l(spec: str) -> bool: + """检查是否为单列array格式,用于排除矩阵等多列格式。这个函数只拆分单列的array""" + spec = re.sub(r'\s+|\|', '', spec) # 删空白与竖线 + spec = re.sub(r'@{[^}]*}', '', spec) # 删 @{…} 修饰 + spec = re.sub(r'!{[^}]*}', '', spec) # 删 !{…} 修饰 + # 检查是否为单列基本对齐格式:l, c, r + return bool(spec) and len(spec) == 1 and spec in {'l', 'c', 'r'} + +# def is_all_l(spec: str) -> bool: +# """忽略空格 / 竖线 / @{…} 之后,判断列格式是否只剩基本对齐格式。这个函数会将多行多列的array按行拆分""" +# spec = re.sub(r'\s+|\|', '', spec) # 删空白与竖线 +# spec = re.sub(r'@{[^}]*}', '', spec) # 删 @{…} 修饰 +# spec = re.sub(r'!{[^}]*}', '', spec) # 删 !{…} 修饰 +# # 检查是否只包含基本对齐格式:l, c, r +# return bool(spec) and set(spec) <= {'l', 'c', 'r'} + + +def split_gt_equation_arrays(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + """ + 拆分带 \\begin{array} … \\end{array} 的 GT 字典条目。 + + - 仅针对 category_type == 'equation_isolated' 且 latex 含 array。 + - 每行公式拆出一个新条目: + * 更新 'latex' + * 若存在 line_with_spans,则同步替换其内部 latex + * 'order' 由 7 --> 7.1, 7.2, … + """ + output = [] + + for item in data: + # 只处理满足条件的字典 + if (item.get("category_type") == "equation_isolated" + and "\\begin{array" in item.get("latex", "")): + + # 抽取 array 内部内容 + match = ARRAY_RE.search(item["latex"]) + if match: + + spec = match.group("spec") + if not is_all_l(spec): + # 若列里混有 r / c / p{…} 等,直接保留原条目 + output.append(item) + continue + + body = match.group("body") + # body = match.group(1) # 去掉 array 外壳 + # 按 LaTeX 行分隔符 \\\\ 拆分 + lines = [ln.strip() for ln in re.split(r'\\\\', body) if ln.strip()] + + base_order = float(item["order"]) # 7 -> 7.0,可兼容 float/int + + for idx, line in enumerate(lines, start=1): + new_item = deepcopy(item) + new_item["latex"] = f"\\[{line}\\]" + new_item["order"] = round(base_order + idx / 10, 1) + output.append(new_item) + continue # 跳过把原 item 加入 + # 其它情况不修改 + output.append(item) + + return output + + +def _wrap(line: str) -> str: + """给单行公式重新包 \\[ ... \\]""" + return f"\\[{line.strip()}\\]" + + +def split_equation_arrays(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + """ + 处理 category_type == 'equation_isolated' 且含 \\begin{array} … 的条目: + * 拆分多行公式 + * 重新包装 content + * **重计算 position / positions** + """ + out: List[Dict[str, Any]] = [] + + for item in data: + if (item.get("category_type") == "equation_isolated" + and "\\begin{array" in item.get("content", "")): + + content = item["content"] + m = ARRAY_RE.search(content) + if not m: + out.append(item) + continue + + if not is_all_l(m.group('spec')): + out.append(item) + continue + + # body = m.group(1) + body = m.group('body') + lines = [ln.strip() for ln in re.split(r'\\\\', body) if ln.strip()] + + # 全局起始字符索引 + pos_key = "position" if "position" in item else "positions" + global_start = item[pos_key][0] + + # array 正文在原 content 内的起点 + # body_start_in_content = m.start(1) + body_start_in_content = m.start('body') + + search_from = 0 # 在 body 中的游标 + for ln in lines: + # 在 body 中找到当前行的偏移 + idx_in_body = body.find(ln, search_from) + if idx_in_body == -1: + # 不太可能发生;保守处理 + idx_in_body = search_from + search_from = idx_in_body + len(ln) # 更新游标 + + # 计算全局索引 + line_start_global = global_start + body_start_in_content + idx_in_body + line_end_global = line_start_global + len(ln) - 1 + + new_item = deepcopy(item) + new_item["content"] = _wrap(ln) + new_item[pos_key] = [line_start_global, line_end_global] + + out.append(new_item) + + # 拆分完成,不保留原条目 + continue + + # 其它条目直接加入 + out.append(item) + + return out + + +def sort_by_position_skip_inline(items: List[Dict[str, Any]]) -> List[Dict[str, Any]]: + """ + 先按 position[0] 从小到大排序; + 若 fine_category_type == 'equation_inline',则统一放到最后, + 并保持它们在原列表中的相对顺序(稳定排序)。 + """ + # enumerate 保留原始顺序索引,用于 equation_inline “并列时” 的稳定性 + return sorted( + enumerate(items), + key=lambda pair: ( + pair[1].get('fine_category_type') == 'equation_inline', # False < True + pair[1]['position'][0], # 位置起点 + pair[0] # 原序号,确保稳定 + ) + ) + + +def match_gt2pred_quick(gt_items, pred_items, line_type, img_name): + + gt_items = split_gt_equation_arrays(gt_items) + + # pred_items = sorted(pred_items, key=lambda x: x['position'][0]) + pred_items = [pair[1] for pair in sort_by_position_skip_inline(pred_items)] + + pred_items = split_equation_arrays(pred_items) + + # gt_lines, norm_gt_lines, gt_cat_list, pred_lines, norm_pred_lines= get_gt_pred_lines(gt_items, pred_items, line_type) + gt_lines, norm_gt_lines, gt_cat_list, pred_lines, norm_pred_lines, gt_items, pred_items = get_gt_pred_lines( + gt_items, pred_items, None) + set(range(len(norm_gt_lines))) + set(range(len(norm_pred_lines))) + + if not norm_gt_lines: + match_list = [] + for pred_idx in range(len(norm_pred_lines)): + match_list.append({ + 'gt_idx': [""], + 'gt': "", + 'pred_idx': [pred_idx], + 'pred': pred_lines[pred_idx], + 'gt_position': [""], + 'pred_position': pred_items[pred_idx]['position'][0], + 'norm_gt': "", + 'norm_pred': norm_pred_lines[pred_idx], + 'gt_category_type': "", + 'pred_category_type': get_pred_category_type(pred_idx, pred_items), + 'gt_attribute': [{}], + 'edit': 1, + 'img_id': img_name + }) + return match_list + elif not norm_pred_lines: + match_list = [] + for gt_idx in range(len(norm_gt_lines)): + match_list.append({ + 'gt_idx': [gt_idx], + 'gt': gt_lines[gt_idx], + 'pred_idx': [""], + 'pred': "", + 'gt_position': [gt_items[gt_idx].get('order') if gt_items[gt_idx].get('order') else gt_items[gt_idx].get('position', [""])[0]], + 'pred_position': "", + 'norm_gt': norm_gt_lines[gt_idx], + 'norm_pred': "", + 'gt_category_type': gt_cat_list[gt_idx], + 'pred_category_type': "", + 'gt_attribute': [gt_items[gt_idx].get("attribute", {})], + 'edit': 1, + 'img_id': img_name + }) + return match_list + elif len(norm_gt_lines) == 1 and len(norm_pred_lines) == 1: + edit_distance = Levenshtein_distance(norm_gt_lines[0], norm_pred_lines[0]) + normalized_edit_distance = edit_distance / \ + max(len(norm_gt_lines[0]), len(norm_pred_lines[0])) + return [{ + 'gt_idx': [0], + 'gt': gt_lines[0], + 'pred_idx': [0], + 'pred': pred_lines[0], + 'gt_position': [gt_items[0].get('order') if gt_items[0].get('order') else gt_items[0].get('position', [""])[0]], + 'pred_position': pred_items[0]['position'][0], + 'norm_gt': norm_gt_lines[0], + 'norm_pred': norm_pred_lines[0], + 'gt_category_type': gt_cat_list[0], + 'pred_category_type': get_pred_category_type(0, pred_items), + 'gt_attribute': [gt_items[0].get("attribute", {})], + 'edit': normalized_edit_distance, + 'img_id': img_name + }] + + # match category ignore first + ignores = ['figure_caption', 'figure_footnote', 'table_caption', 'table_footnote', 'code_algorithm', + 'code_algorithm_caption', 'header', 'footer', 'page_footnote', 'page_number', 'equation_caption'] + + ignore_gt_lines = [] + ignores_ori_gt_lines = [] + ignores_gt_items = [] + ignore_gt_idxs = [] + ignores_gt_cat_list = [] + + no_ignores_gt_lines = [] + no_ignores_ori_gt_lines = [] + no_ignores_gt_idxs = [] + no_ignores_gt_items = [] + no_ignores_gt_cat_list = [] + + for i, line in enumerate(norm_gt_lines): + if gt_cat_list[i] in ignores: + ignore_gt_lines.append(line) + ignores_ori_gt_lines.append(gt_lines[i]) + ignores_gt_items.append(gt_items[i]) + ignore_gt_idxs.append(i) + ignores_gt_cat_list.append(gt_cat_list[i]) + else: + no_ignores_gt_lines.append(line) + no_ignores_ori_gt_lines.append(gt_lines[i]) + no_ignores_gt_items.append(gt_items[i]) + no_ignores_gt_cat_list.append(gt_cat_list[i]) + no_ignores_gt_idxs.append(i) + + # print("-------------ignore_gt_lines-------------------") + # for idx, line in zip(ignore_idx,ignore_gt_lines): + # print(f"{gt_cat_list[idx]}: {line}") + + # print("-------------no_ignores_gt_lines-------------------") + # for line in no_ignores_gt_lines: + # print(line) + + ignore_pred_idxs = [] + ignore_pred_lines = [] + ignores_pred_items = [] + ignores_ori_pred_lines = [] + + merged_ignore_results = [] + + if len(ignore_gt_lines) > 0: + + ignore_matches_dict = {} + + ignore_matrix = compute_edit_distance_matrix_new(ignore_gt_lines, norm_pred_lines) + # print("-------------ignore_matrix-------------") + # print(ignore_matrix) + + ignores_gt_indices = set(range(len(ignore_gt_lines))) + ignores_pred_indices = set(range(len(ignore_pred_lines))) + + ignore_matches = np.argwhere(ignore_matrix < 0.25) + # print("-------------ignore_matches-------------") + # print(ignore_matches) + if len(ignore_matches) > 0: + ignore_pred_idxs = [_[1] for _ in ignore_matches] + [ignore_gt_idxs[_[0]] for _ in ignore_matches] + # print("-------------ignore_pred_idxs-------------") + # print(ignore_pred_idxs) + # print("-------------ignore_gt_matched_idxs-------------") + # print(ignore_gt_matched_idxs) + + for i in ignore_pred_idxs: + ignore_pred_lines.append(norm_pred_lines[i]) + ignores_ori_pred_lines.append(pred_lines[i]) + ignores_pred_items.append(pred_items[i]) + # print("-------------ignore_pred_lines-------------") + # for i in ignore_pred_lines: + # print(i) + + ignores_gt_indices = set(range(len(ignore_gt_lines))) + ignores_pred_indices = set(range(len(ignore_pred_lines))) + + for idx, i in enumerate(ignore_matches): + ignore_matches_dict[i[0]] = { + 'pred_indices': [idx], + 'edit_distance': ignore_matrix[i[0]][i[1]] + } + # print("-------------ignore_matches_dict-------------") + # print(ignore_matches_dict) + + ignore_final_matches = merge_matches(ignore_matches_dict, {}) + # print("-------------ignore_final_matches-------------") + # print(ignore_final_matches) + + recalculate_edit_distances(ignore_final_matches, {}, ignore_gt_lines, ignore_pred_lines) + # print("-------------recalculate_ignore_final_matches-------------") + # print(ignore_final_matches) + + converted_ignore_results = convert_final_matches( + ignore_final_matches, ignore_gt_lines, ignore_pred_lines) + # print("-------------converted_ignore_results-------------") + # for i in converted_ignore_results: + # print(i) + + merged_ignore_results = merge_duplicates_add_unmatched( + converted_ignore_results, + ignore_gt_lines, + ignore_pred_lines, + ignores_ori_gt_lines, + ignores_ori_pred_lines, + ignores_gt_indices, + ignores_pred_indices) + + for entry in merged_ignore_results: + entry['gt_idx'] = [entry['gt_idx']] if not isinstance( + entry['gt_idx'], list) else entry['gt_idx'] + entry['pred_idx'] = [entry['pred_idx']] if not isinstance( + entry['pred_idx'], list) else entry['pred_idx'] + entry['gt_position'] = [ + ignores_gt_items[_].get('order') if ignores_gt_items[_].get('order') else ignores_gt_items[_].get( + 'position', [""])[0] for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [""] + entry['pred_position'] = ignores_pred_items[entry['pred_idx'] + [0]]['position'][0] if entry['pred_idx'] != [""] else "" + entry['gt'] = ''.join([ignores_ori_gt_lines[_] + for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + entry['pred'] = ''.join([ignores_ori_pred_lines[_] + for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + entry['norm_gt'] = ''.join([ignore_gt_lines[_] + for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + entry['norm_pred'] = ''.join( + [ignore_pred_lines[_] for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + + if entry['gt_idx'] != [""]: + ignore_type = [ + 'figure_caption', + 'figure_footnote', + 'table_caption', + 'table_footnote', + 'code_algorithm', + 'code_algorithm_caption', + 'header', + 'footer', + 'page_footnote', + 'page_number', + 'equation_caption'] + gt_cagegory_clean = [ignores_gt_cat_list[_] + for _ in entry['gt_idx'] if ignores_gt_cat_list[_] not in ignore_type] + if gt_cagegory_clean: + entry['gt_category_type'] = Counter(gt_cagegory_clean).most_common(1)[0][0] + else: + entry['gt_category_type'] = Counter( + [ignores_gt_cat_list[_] for _ in entry['gt_idx']]).most_common(1)[0][0] + else: + entry['gt_category_type'] = "" + entry['pred_category_type'] = get_pred_category_type( + entry['pred_idx'][0], ignores_pred_items) if entry['pred_idx'] != [""] else "" + if entry['pred_category_type'] == 'equation_inline': + merged_ignore_results.remove(entry) + entry['pred_category_type'] = get_pred_category_type( + entry['pred_idx'][0], ignores_pred_items) if entry['pred_idx'] != [""] else "" + entry['gt_attribute'] = [ + ignores_gt_items[_].get( + "attribute", + {}) for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [ + {}] + entry['img_id'] = img_name + + for entry in merged_ignore_results: + if isinstance(entry['gt_idx'], list) and entry['gt_idx'] != [""]: + gt_idx = [] + for i in entry['gt_idx']: + gt_idx.append(ignore_gt_idxs[i]) + entry['gt_idx'] = gt_idx + if isinstance(entry['pred_idx'], list) and entry['pred_idx'] != [""]: + pred_idx = [] + for i in entry['pred_idx']: + pred_idx.append(int(ignore_pred_idxs[i])) + entry['pred_idx'] = pred_idx + + # print("-------------merged_ignore_results-------------") + # for i in merged_ignore_results: + # print(i) + + no_ignores_pred_lines = [] + no_ignores_ori_pred_lines = [] + no_ignores_pred_indices = [] + no_ignores_pred_items = [] + no_ignore_pred_idxs = [] + + for idx, line in enumerate(norm_pred_lines): + if not idx in ignore_pred_idxs: + no_ignores_pred_lines.append(line) + no_ignores_ori_pred_lines.append(pred_lines[idx]) + # no_ignores_pred_indices.append(idx) + no_ignores_pred_items.append(pred_items[idx]) + no_ignore_pred_idxs.append(idx) + + # initialize new indices for lines without ignore categories + no_ignores_gt_indices = set(range(len(no_ignores_gt_lines))) + no_ignores_pred_indices = set(range(len(no_ignores_pred_lines))) + + # exclude ignore categories + cost_matrix = compute_edit_distance_matrix_new(no_ignores_gt_lines, no_ignores_pred_lines) + # print("-------------cost matrix-------------") + # print(cost_matrix) + + matched_col_idx, row_ind, cost_list = cal_final_match( + cost_matrix, no_ignores_gt_lines, no_ignores_pred_lines) + # print("-------------matched_col_idx-------------") + # print(matched_col_idx) + + # print("-------------gt_row_ind-------------") + # print(row_ind) + + # print("-------------cost_list-------------") + # print(cost_list) + + gt_lens_dict, pred_lens_dict = initialize_indices(no_ignores_gt_lines, no_ignores_pred_lines) + # print("-------------gt_lens_dict-------------") + # print(gt_lens_dict) + + # print("-------------pred_lens_dict-------------") + # print(pred_lens_dict) + + matches, unmatched_gt_indices, unmatched_pred_indices = process_matches( + matched_col_idx, row_ind, cost_list, no_ignores_gt_lines, no_ignores_pred_lines, no_ignores_ori_pred_lines) + + # print("-------------matches-------------") + # print(matches) + + # print("-------------unmatched_gt_indices-------------") + # print(unmatched_gt_indices) + + # print("-------------unmatched_pred_indices-------------") + # print(unmatched_pred_indices) + + matching_dict = fuzzy_match_unmatched_items( + unmatched_gt_indices, no_ignores_gt_lines, no_ignores_pred_lines) + # print("-------------matching_dict-------------") + # print(matching_dict) + + final_matches = merge_matches(matches, matching_dict) + # print("-------------final_matches-------------") + # print(final_matches) + + recalculate_edit_distances( + final_matches, + gt_lens_dict, + no_ignores_gt_lines, + no_ignores_pred_lines) + # print("-------------recalculate_edit_distances-------------") + # print(final_matches) + + converted_results = convert_final_matches( + final_matches, no_ignores_gt_lines, no_ignores_pred_lines) + # print("-------------converted_results-------------") + # print(converted_results) + + merged_results = merge_duplicates_add_unmatched( + converted_results, + no_ignores_gt_lines, + no_ignores_pred_lines, + no_ignores_ori_gt_lines, + no_ignores_ori_pred_lines, + no_ignores_gt_indices, + no_ignores_pred_indices) + + for entry in merged_results: + if entry['gt_idx'] != [""]: + ignore_type = [ + 'figure_caption', + 'figure_footnote', + 'table_caption', + 'table_footnote', + 'code_algorithm', + 'code_algorithm_caption', + 'header', + 'footer', + 'page_footnote', + 'page_number', + 'equation_caption'] + gt_cagegory_clean = [no_ignores_gt_cat_list[_] + for _ in entry['gt_idx'] if no_ignores_gt_cat_list[_] not in ignore_type] + if gt_cagegory_clean: + entry['gt_category_type'] = Counter(gt_cagegory_clean).most_common(1)[0][0] + else: + entry['gt_category_type'] = Counter( + [no_ignores_gt_cat_list[_] for _ in entry['gt_idx']]).most_common(1)[0][0] + else: + entry['gt_category_type'] = "" + entry['pred_category_type'] = get_pred_category_type( + entry['pred_idx'][0], no_ignores_pred_items) if entry['pred_idx'] != [""] else "" + if entry['pred_category_type'] == 'equation_inline': + merged_results.remove(entry) + + entry['gt_idx'] = [entry['gt_idx']] if not isinstance( + entry['gt_idx'], list) else entry['gt_idx'] + entry['pred_idx'] = [entry['pred_idx']] if not isinstance( + entry['pred_idx'], list) else entry['pred_idx'] + entry['gt_position'] = [ + no_ignores_gt_items[_].get('order') if no_ignores_gt_items[_].get('order') else no_ignores_gt_items[_].get( + 'position', [""])[0] for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [""] + entry['pred_position'] = no_ignores_pred_items[entry['pred_idx'] + [0]]['position'][0] if entry['pred_idx'] != [""] else "" + # 0507 多行公式拼接修改 + if entry['gt_category_type'] == 'equation_isolated' and len(entry['gt_idx']) > 1: + mutli_formula = ' \\\\ '.join(['{' + + no_ignores_ori_gt_lines[_].strip('$$').strip('\n') + + '}' for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + mutli_formula = '\\begin{array}{l} ' + mutli_formula + ' \\end{array}' + entry['gt'] = mutli_formula + else: + entry['gt'] = ''.join([no_ignores_ori_gt_lines[_] + for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + + entry['pred_category_type'] = get_pred_category_type( + entry['pred_idx'][0], no_ignores_pred_items) if entry['pred_idx'] != [""] else "" + entry['gt_attribute'] = [ + no_ignores_gt_items[_].get( + "attribute", + {}) for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [ + {}] + entry['img_id'] = img_name + + # 0724 多行公式拼接修改pred + if 'equation' in entry['pred_category_type'] and len(entry['pred_idx']) > 1: + mutli_formula = ' \\\\ '.join(['{' + + no_ignores_ori_pred_lines[_].strip('$$').strip('\n') + + '}' for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + mutli_formula = '\\begin{array}{l} ' + mutli_formula + ' \\end{array}' + entry['pred'] = mutli_formula + else: + entry['pred'] = ''.join([no_ignores_ori_pred_lines[_] + for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + + entry['norm_gt'] = ''.join([no_ignores_gt_lines[_] + for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + entry['norm_pred'] = ''.join([no_ignores_pred_lines[_] + for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + + # print("-------------merged_results-------------") + # for i in merged_results: + # print(i) + for entry in merged_results: + if isinstance(entry['gt_idx'], list) and entry['gt_idx'] != [""]: + gt_idx = [] + for i in entry['gt_idx']: + gt_idx.append(no_ignores_gt_idxs[i]) + entry['gt_idx'] = gt_idx + if isinstance(entry['pred_idx'], list) and entry['pred_idx'] != [""]: + pred_idx = [] + for i in entry['pred_idx']: + pred_idx.append(int(no_ignore_pred_idxs[i])) + entry['pred_idx'] = pred_idx + + if len(merged_ignore_results) > 0: + merged_results.extend(merged_ignore_results) + # for i in merged_ignore_results: + # merged_results.append(i) + + return merged_results + + # cost_matrix = compute_edit_distance_matrix_new(norm_gt_lines, norm_pred_lines) + + # matched_col_idx, row_ind, cost_list = cal_final_match(cost_matrix, norm_gt_lines, norm_pred_lines) + + # gt_lens_dict, pred_lens_dict = initialize_indices(norm_gt_lines, norm_pred_lines) + + # matches, unmatched_gt_indices, unmatched_pred_indices = process_matches(matched_col_idx, row_ind, cost_list, norm_gt_lines, norm_pred_lines, pred_lines) + + # matching_dict = fuzzy_match_unmatched_items(unmatched_gt_indices, norm_gt_lines, norm_pred_lines) + + # final_matches = merge_matches(matches, matching_dict) + + # recalculate_edit_distances(final_matches, gt_lens_dict, norm_gt_lines, norm_pred_lines) + + # converted_results = convert_final_matches(final_matches, norm_gt_lines, norm_pred_lines) + + # merged_results = merge_duplicates_add_unmatched(converted_results, norm_gt_lines, norm_pred_lines, gt_lines, pred_lines, all_gt_indices, all_pred_indices) + + # for entry in merged_results: + # entry['gt_idx'] = [entry['gt_idx']] if not isinstance(entry['gt_idx'], list) else entry['gt_idx'] + # entry['pred_idx'] = [entry['pred_idx']] if not isinstance(entry['pred_idx'], list) else entry['pred_idx'] + # entry['gt_position'] = [gt_items[_].get('order') if gt_items[_].get('order') else gt_items[_].get('position', [""])[0] for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [""] + # entry['pred_position'] = pred_items[entry['pred_idx'][0]]['position'][0] if entry['pred_idx'] != [""] else "" + # entry['gt'] = ''.join([gt_lines[_] for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + # entry['pred'] = ''.join([pred_lines[_] for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + # entry['norm_gt'] = ''.join([norm_gt_lines[_] for _ in entry['gt_idx']]) if entry['gt_idx'] != [""] else "" + # entry['norm_pred'] = ''.join([norm_pred_lines[_] for _ in entry['pred_idx']]) if entry['pred_idx'] != [""] else "" + + # if entry['gt_idx'] != [""]: + # ignore_type = ['figure_caption', 'figure_footnote', 'table_caption', 'table_footnote', 'code_algorithm', 'code_algorithm_caption', 'header', 'footer', 'page_footnote', 'page_number', 'equation_caption'] + # gt_cagegory_clean = [gt_cat_list[_] for _ in entry['gt_idx'] if gt_cat_list[_] not in ignore_type] + # if gt_cagegory_clean: + # entry['gt_category_type'] = Counter(gt_cagegory_clean).most_common(1)[0][0] + # else: + # entry['gt_category_type'] = Counter([gt_cat_list[_] for _ in entry['gt_idx']]).most_common(1)[0][0] + # else: + # entry['gt_category_type'] = "" + # entry['pred_category_type'] = get_pred_category_type(entry['pred_idx'][0], pred_items) if entry['pred_idx'] != [""] else "" + # entry['gt_attribute'] = [gt_items[_].get("attribute", {}) for _ in entry['gt_idx']] if entry['gt_idx'] != [""] else [{}] + # entry['img_id'] = img_name + + # return merged_results + + +def merge_duplicates_add_unmatched(converted_results, norm_gt_lines, + norm_pred_lines, gt_lines, pred_lines, all_gt_indices, all_pred_indices): + merged_results = [] + processed_pred = set() + processed_gt = set() + + for entry in converted_results: + pred_idx = tuple( + entry['pred_idx']) if isinstance( + entry['pred_idx'], + list) else ( + entry['pred_idx'], + ) + if pred_idx not in processed_pred and pred_idx != ("",): + merged_entry = { + 'gt_idx': [entry['gt_idx']], + 'gt': entry['gt'], + 'pred_idx': entry['pred_idx'], + 'pred': entry['pred'], + 'edit': entry['edit'] + } + for other_entry in converted_results: + other_pred_idx = tuple( + other_entry['pred_idx']) if isinstance( + other_entry['pred_idx'], + list) else ( + other_entry['pred_idx'], + ) + if other_pred_idx == pred_idx and other_entry is not entry: + merged_entry['gt_idx'].append(other_entry['gt_idx']) + merged_entry['gt'] += other_entry['gt'] + processed_gt.add(other_entry['gt_idx']) + merged_results.append(merged_entry) + processed_pred.add(pred_idx) + processed_gt.add(entry['gt_idx']) + + # for entry in converted_results: + # if entry['gt_idx'] not in processed_gt: + # merged_results.append(entry) + + for gt_idx in range(len(norm_gt_lines)): + if gt_idx not in processed_gt: + merged_results.append({ + 'gt_idx': [gt_idx], + 'gt': gt_lines[gt_idx], + 'pred_idx': [""], + 'pred': "", + 'edit': 1 + }) + return merged_results + + +def formula_format(formula_matches, img_name): + return [ + { + "gt": item["gt"], + "pred": item["pred"], + "img_id": f"{img_name}_{i}" + } + for i, item in enumerate(formula_matches) + ] + + +def merge_lists_with_sublists(main_list, sub_lists): + main_list_final = list(copy.deepcopy(main_list)) + for sub_list in sub_lists: + pop_idx = main_list_final.index(sub_list[0]) + for _ in sub_list: + main_list_final.pop(pop_idx) + main_list_final.insert(pop_idx, sub_list) + return main_list_final + + +def sub_pred_fuzzy_matching(gt, pred): + + min_d = float('inf') + # pos = -1 + + gt_len = len(gt) + pred_len = len(pred) + + if gt_len >= pred_len and pred_len > 0: + for i in range(gt_len - pred_len + 1): + sub = gt[i:i + pred_len] + dist = Levenshtein_distance(sub, pred) / pred_len + if dist < min_d: + min_d = dist + + return min_d + else: + return False + + +def sub_gt_fuzzy_matching(pred, gt): + + min_d = float('inf') + pos = "" + matched_sub = "" + gt_len = len(gt) + pred_len = len(pred) + + if pred_len >= gt_len and gt_len > 0: + for i in range(pred_len - gt_len + 1): + sub = pred[i:i + gt_len] + dist = Levenshtein.distance(sub, gt) / gt_len + if dist < min_d: + min_d = dist + pos = i + matched_sub = sub + return min_d, pos, gt_len, matched_sub + else: + return 1, "", gt_len, "" + + +def get_final_subset(subset_certain, subset_certain_cost): + if not subset_certain or not subset_certain_cost: + return [] + + subset_turple = sorted([(a, b) for a, b in zip( + subset_certain, subset_certain_cost)], key=lambda x: x[0][0]) + + group_list = defaultdict(list) + group_idx = 0 + group_list[group_idx].append(subset_turple[0]) + + for item in subset_turple[1:]: + overlap_flag = False + for subset in group_list[group_idx]: + for idx in item[0]: + if idx in subset[0]: + overlap_flag = True + break + if overlap_flag: + break + if overlap_flag: + group_list[group_idx].append(item) + else: + group_idx += 1 + group_list[group_idx].append(item) + + final_subset = [] + for _, group in group_list.items(): + if len(group) == 1: + final_subset.append(group[0][0]) + else: + path_dict = defaultdict(list) + path_idx = 0 + path_dict[path_idx].append(group[0]) + + for subset in group[1:]: + new_path = True + for path_idx_s, path_items in path_dict.items(): + is_dup = False + is_same = False + for path_item in path_items: + if path_item[0] == subset[0]: + is_dup = True + is_same = True + if path_item[1] > subset[1]: + path_dict[path_idx_s].pop(path_dict[path_idx_s].index(path_item)) + path_dict[path_idx_s].append(subset) + else: + for num_1 in path_item[0]: + for num_2 in subset[0]: + if num_1 == num_2: + is_dup = True + if not is_dup: + path_dict[path_idx_s].append(subset) + new_path = False + if is_same: + new_path = False + if new_path: + path_idx = len(path_dict.keys()) + path_dict[path_idx].append(subset) + + saved_cost = float('inf') + saved_subset = [] + for path_idx, path in path_dict.items(): + avg_cost = sum([i[1] for i in path]) / len(path) + if avg_cost < saved_cost: + saved_subset = [i[0] for i in path] + saved_cost = avg_cost + + final_subset.extend(saved_subset) + + return final_subset + + +def judge_pred_merge(gt_list, pred_list, threshold=0.6): + if len(pred_list) == 1: + return False, False + + cur_pred = ' '.join(pred_list[:-1]) + merged_pred = ' '.join(pred_list) + + cur_dist = Levenshtein.distance(gt_list[0], cur_pred) / max(len(gt_list[0]), len(cur_pred)) + merged_dist = Levenshtein.distance( + gt_list[0], merged_pred) / max(len(gt_list[0]), len(merged_pred)) + + if merged_dist > cur_dist: + return False, False + + cur_fuzzy_dists = [sub_pred_fuzzy_matching(gt_list[0], cur_pred) + for cur_pred in pred_list[:-1]] + if any(dist == False or dist > threshold for dist in cur_fuzzy_dists): + return False, False + + add_fuzzy_dist = sub_pred_fuzzy_matching(gt_list[0], pred_list[-1]) + if add_fuzzy_dist == False: + return False, False + + merged_pred_flag = add_fuzzy_dist < threshold + continue_flag = len(merged_pred) <= len(gt_list[0]) + + return merged_pred_flag, continue_flag + + +def deal_with_truncated(cost_matrix, norm_gt_lines, norm_pred_lines): + matched_first = np.argwhere(cost_matrix < 0.25) + masked_gt_idx = [i[0] for i in matched_first] + unmasked_gt_idx = [i for i in range(cost_matrix.shape[0]) if i not in masked_gt_idx] + masked_pred_idx = [i[1] for i in matched_first] + unmasked_pred_idx = [i for i in range(cost_matrix.shape[1]) if i not in masked_pred_idx] + + merges_gt_dict = {} + + for gt_idx in unmasked_gt_idx: + check_merge_subset = [] + merged_dist = [] + + for pred_idx in unmasked_pred_idx: + step = 1 + merged_pred = [norm_pred_lines[pred_idx]] + + while True: + if pred_idx + step in masked_pred_idx or pred_idx + step >= len(norm_pred_lines): + break + else: + merged_pred.append(norm_pred_lines[pred_idx + step]) + merged_pred_flag, continue_flag = judge_pred_merge( + [norm_gt_lines[gt_idx]], merged_pred) + if not merged_pred_flag: + break + else: + step += 1 + if not continue_flag: + break + + check_merge_subset.append(list(range(pred_idx, pred_idx + step))) + matched_line = ' '.join([norm_pred_lines[i] for i in range(pred_idx, pred_idx + step)]) + dist = Levenshtein_distance( + norm_gt_lines[gt_idx], matched_line) / max(len(matched_line), len(norm_gt_lines[gt_idx])) + merged_dist.append(dist) + + if not merged_dist: + subset_certain = [] + min_cost_idx = "" + min_cost = float('inf') + else: + min_cost = min(merged_dist) + min_cost_idx = merged_dist.index(min_cost) + subset_certain = check_merge_subset[min_cost_idx] + + merges_gt_dict[gt_idx] = { + 'merge_subset': check_merge_subset, + 'merged_cost': merged_dist, + 'min_cost_idx': min_cost_idx, + 'subset_certain': subset_certain, + 'min_cost': min_cost + } + + subset_certain = [merges_gt_dict[gt_idx]['subset_certain'] + for gt_idx in unmasked_gt_idx if merges_gt_dict[gt_idx]['subset_certain']] + subset_certain_cost = [merges_gt_dict[gt_idx]['min_cost'] + for gt_idx in unmasked_gt_idx if merges_gt_dict[gt_idx]['subset_certain']] + + subset_certain_final = get_final_subset(subset_certain, subset_certain_cost) + + if not subset_certain_final: + return cost_matrix, norm_pred_lines, range(len(norm_pred_lines)) + + final_pred_idx_list = merge_lists_with_sublists( + range(len(norm_pred_lines)), subset_certain_final) + final_norm_pred_lines = [' '.join(norm_pred_lines[idx_list[0]:idx_list[-1] + 1]) if isinstance( + idx_list, list) else norm_pred_lines[idx_list] for idx_list in final_pred_idx_list] + + new_cost_matrix = compute_edit_distance_matrix_new(norm_gt_lines, final_norm_pred_lines) + + return new_cost_matrix, final_norm_pred_lines, final_pred_idx_list + + +def cal_move_dist(gt, pred): + assert len(gt) == len(pred), 'Not right length' + step = 0 + for i, gt_c in enumerate(gt): + if gt_c != pred[i]: + step += abs(i - pred.index(gt_c)) + pred[i], pred[pred.index(gt_c)] = pred[pred.index(gt_c)], pred[i] + return step / len(gt) + + +def cal_final_match(cost_matrix, norm_gt_lines, norm_pred_lines): + # min_indice = cost_matrix.argmax(axis=1) + + new_cost_matrix, final_norm_pred_lines, final_pred_idx_list = deal_with_truncated( + cost_matrix, norm_gt_lines, norm_pred_lines) + + row_ind, col_ind = linear_sum_assignment(new_cost_matrix) + + cost_list = [new_cost_matrix[r][c] for r, c in zip(row_ind, col_ind)] + matched_col_idx = [final_pred_idx_list[i] for i in col_ind] + + return matched_col_idx, row_ind, cost_list + + +def initialize_indices(norm_gt_lines, norm_pred_lines): + gt_lens_dict = {idx: len(gt_line) for idx, gt_line in enumerate(norm_gt_lines)} + pred_lens_dict = {idx: len(pred_line) for idx, pred_line in enumerate(norm_pred_lines)} + return gt_lens_dict, pred_lens_dict + + +def process_matches(matched_col_idx, row_ind, cost_list, + norm_gt_lines, norm_pred_lines, pred_lines): + matches = {} + unmatched_gt_indices = [] + unmatched_pred_indices = [] + + for i in range(len(norm_gt_lines)): + if i in row_ind: + idx = list(row_ind).index(i) + pred_idx = matched_col_idx[idx] + + if pred_idx is None or (isinstance(pred_idx, list) and None in pred_idx): + unmatched_pred_indices.append(pred_idx) + continue + + if isinstance(pred_idx, list): + pred_line = ' | '.join(norm_pred_lines[pred_idx[0]:pred_idx[-1] + 1]) + ori_pred_line = ' | '.join(pred_lines[pred_idx[0]:pred_idx[-1] + 1]) + matched_pred_indices_range = list(range(pred_idx[0], pred_idx[-1] + 1)) + else: + norm_pred_lines[pred_idx] + pred_lines[pred_idx] + matched_pred_indices_range = [pred_idx] + + edit = cost_list[idx] + + if edit > 0.7: + unmatched_pred_indices.extend(matched_pred_indices_range) + unmatched_gt_indices.append(i) + else: + matches[i] = { + 'pred_indices': matched_pred_indices_range, + 'edit_distance': edit, + } + for matched_pred_idx in matched_pred_indices_range: + if matched_pred_idx in unmatched_pred_indices: + unmatched_pred_indices.remove(matched_pred_idx) + else: + unmatched_gt_indices.append(i) + + return matches, unmatched_gt_indices, unmatched_pred_indices + + +def fuzzy_match_unmatched_items(unmatched_gt_indices, norm_gt_lines, norm_pred_lines): + matching_dict = {} + + for pred_idx, pred_content in enumerate(norm_pred_lines): + if isinstance(pred_idx, list): + continue + + matching_indices = [] + + for unmatched_gt_idx in unmatched_gt_indices: + gt_content = norm_gt_lines[unmatched_gt_idx] + cur_fuzzy_dist_unmatch, cur_pos, gt_lens, matched_field = sub_gt_fuzzy_matching( + pred_content, gt_content) + if cur_fuzzy_dist_unmatch < 0.4: + matching_indices.append(unmatched_gt_idx) + + if matching_indices: + matching_dict[pred_idx] = matching_indices + + return matching_dict + + +def merge_matches(matches, matching_dict): + final_matches = {} + processed_gt_indices = set() + + for gt_idx, match_info in matches.items(): + pred_indices = match_info['pred_indices'] + edit_distance = match_info['edit_distance'] + + pred_key = tuple(sorted(pred_indices)) + + if pred_key in final_matches: + if gt_idx not in processed_gt_indices: + final_matches[pred_key]['gt_indices'].append(gt_idx) + processed_gt_indices.add(gt_idx) + else: + final_matches[pred_key] = { + 'gt_indices': [gt_idx], + 'edit_distance': edit_distance + } + processed_gt_indices.add(gt_idx) + + for pred_idx, gt_indices in matching_dict.items(): + pred_key = ( + pred_idx, + ) if not isinstance( + pred_idx, + (list, + tuple)) else tuple( + sorted(pred_idx)) + + if pred_key in final_matches: + for gt_idx in gt_indices: + if gt_idx not in processed_gt_indices: + final_matches[pred_key]['gt_indices'].append(gt_idx) + processed_gt_indices.add(gt_idx) + else: + final_matches[pred_key] = { + 'gt_indices': [gt_idx for gt_idx in gt_indices if gt_idx not in processed_gt_indices], + 'edit_distance': None + } + processed_gt_indices.update(final_matches[pred_key]['gt_indices']) + + return final_matches + + +def recalculate_edit_distances(final_matches, gt_lens_dict, norm_gt_lines, norm_pred_lines): + for pred_key, info in final_matches.items(): + gt_indices = sorted(set(info['gt_indices'])) + + if not gt_indices: + info['edit_distance'] = 1 + continue + + if len(gt_indices) > 1: + merged_gt_content = ''.join(norm_gt_lines[gt_idx] for gt_idx in gt_indices) + pred_content = norm_pred_lines[pred_key[0]] if isinstance(pred_key[0], int) else '' + + try: + edit_distance = Levenshtein_distance(merged_gt_content, pred_content) + normalized_edit_distance = edit_distance / \ + max(len(merged_gt_content), len(pred_content)) + except ZeroDivisionError: + normalized_edit_distance = 1 + + info['edit_distance'] = normalized_edit_distance + else: + gt_idx = gt_indices[0] + pred_content = ' '.join(norm_pred_lines[pred_idx] + for pred_idx in pred_key if isinstance(pred_idx, int)) + + try: + edit_distance = Levenshtein_distance(norm_gt_lines[gt_idx], pred_content) + normalized_edit_distance = edit_distance / \ + max(len(norm_gt_lines[gt_idx]), len(pred_content)) + except ZeroDivisionError: + normalized_edit_distance = 1 + + info['edit_distance'] = normalized_edit_distance + info['pred_content'] = pred_content + + +def convert_final_matches(final_matches, norm_gt_lines, norm_pred_lines): + converted_results = [] + + all_gt_indices = set(range(len(norm_gt_lines))) + all_pred_indices = set(range(len(norm_pred_lines))) + + for pred_key, info in final_matches.items(): + pred_content = ' '.join(norm_pred_lines[pred_idx] + for pred_idx in pred_key if isinstance(pred_idx, int)) + + for gt_idx in sorted(set(info['gt_indices'])): + result_entry = { + 'gt_idx': int(gt_idx), + 'gt': norm_gt_lines[gt_idx], + 'pred_idx': list(pred_key), + 'pred': pred_content, + 'edit': info['edit_distance'] + } + converted_results.append(result_entry) + + matched_gt_indices = set().union(*[set(info['gt_indices']) for info in final_matches.values()]) + unmatched_gt_indices = all_gt_indices - matched_gt_indices + matched_pred_indices = set(idx for pred_key in final_matches.keys() + for idx in pred_key if isinstance(idx, int)) + unmatched_pred_indices = all_pred_indices - matched_pred_indices + + if unmatched_pred_indices: + if unmatched_gt_indices: + distance_matrix = [ + # [Levenshtein_distance(norm_gt_lines[gt_idx], norm_pred_lines[pred_idx]) for pred_idx in unmatched_pred_indices] + [Levenshtein_distance(norm_gt_lines[gt_idx], + norm_pred_lines[pred_idx]) / max(len(norm_gt_lines[gt_idx]), + len(norm_pred_lines[pred_idx])) for pred_idx in unmatched_pred_indices] + for gt_idx in unmatched_gt_indices + ] + + row_ind, col_ind = linear_sum_assignment(distance_matrix) + + for i, j in zip(row_ind, col_ind): + gt_idx = list(unmatched_gt_indices)[i] + pred_idx = list(unmatched_pred_indices)[j] + result_entry = { + 'gt_idx': int(gt_idx), + 'gt': norm_gt_lines[gt_idx], + 'pred_idx': [pred_idx], + 'pred': norm_pred_lines[pred_idx], + 'edit': 1 + } + converted_results.append(result_entry) + + matched_gt_indices.update(list(unmatched_gt_indices)[i] for i in row_ind) + else: + result_entry = { + 'gt_idx': "", + 'gt': '', + 'pred_idx': list(unmatched_pred_indices), + 'pred': ' '.join(norm_pred_lines[pred_idx] for pred_idx in unmatched_pred_indices), + 'edit': 1 + } + converted_results.append(result_entry) + else: + for gt_idx in unmatched_gt_indices: + result_entry = { + 'gt_idx': int(gt_idx), + 'gt': norm_gt_lines[gt_idx], + 'pred_idx': "", + 'pred': '', + 'edit': 1 + } + converted_results.append(result_entry) + + return converted_results diff --git a/vlmeval/dataset/MDPBench/utils/read_files.py b/vlmeval/dataset/MDPBench/utils/read_files.py new file mode 100644 index 000000000..7fe711d01 --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/read_files.py @@ -0,0 +1,22 @@ +import json + + +def read_md_file(filepath): + with open(filepath, 'r', encoding='utf-8') as file: + content = file.read() + + return content + + +def save_paired_result(preds, gts, save_path): + save_result = [] + formula_id = 0 + for gt, pred in zip(gts, preds): + save_result.append({ + "gt": gt, + "pred": pred, + "img_id": formula_id + }) + formula_id += 1 + with open(save_path, 'w', encoding='utf-8') as f: + json.dump(save_result, f, indent=4, ensure_ascii=False) diff --git a/vlmeval/dataset/MDPBench/utils/table_utils.py b/vlmeval/dataset/MDPBench/utils/table_utils.py new file mode 100644 index 000000000..ce91e2d4e --- /dev/null +++ b/vlmeval/dataset/MDPBench/utils/table_utils.py @@ -0,0 +1,252 @@ +import os +import re + +import matplotlib.font_manager as fm +import matplotlib.pyplot as plt +import numpy as np + +font = fm.FontProperties(fname=r'font/SimHei.ttf') + + +def print_aligned_dict(data): + # Find the maximum length of all keys + max_key_length = max(len(key) for key in data['testcase1']) + + # Print header + print(f"{' ' * (max_key_length + 4)}", end="") + for key in data: + print(f"{key:>{max_key_length}}", end="") + print() + + # Print dictionary content + for subkey in data['testcase1']: + print(f"{subkey:<{max_key_length + 4}}", end="") + for key in data: + print(f"{data[key][subkey]:>{max_key_length}}", end="") + print() + + +def create_dict_from_folders(directory): + body = {} + for folder_name in os.listdir(directory): + folder_path = os.path.join(directory, folder_name) + if os.path.isdir(folder_path): + body[folder_name] = {} + return body + + +def create_radar_chart(df, title, filename): + labels = df.columns + + # Calculate angles + angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist() + angles += angles[:1] + + # Initialize radar chart + fig, ax = plt.subplots(figsize=(10, 6), subplot_kw=dict(polar=True), dpi=200) + # ax.spines['polar'].set_visible(False) + + # Draw radar chart for each dataset + for index, row in df.iterrows(): + values = row.tolist() + values += values[:1] + ax.fill(angles, values, alpha=0.1) + ax.plot(angles, values, label=index) + + # Add percentage labels next to each data point + for angle, value in zip(angles, values): + ax.text( + angle, + value, + '{:.1%}'.format(value), + ha='center', + va='center', + fontsize=7, + alpha=0.7) + + # Set labels + ax.set_yticklabels([]) + ax.set_xticks(angles[:-1]) + ax.set_xticklabels(labels, fontproperties=font) + ax.spines['polar'].set_visible(False) # Hide the outermost circle + ax.grid(False) + for j in np.arange(0, 1.2, 0.2): + ax.plot(angles, len(values) * [j], '-.', lw=0.5, color='black', alpha=0.5) + for j in range(len(values)): + ax.plot([angles[j], angles[j]], [0, 1], '-.', lw=0.5, color='black', alpha=0.5) + + # Add title and legend + plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1)) + + ax.tick_params(pad=30) + ax.set_theta_zero_location('N') + # Save chart to file + plt.savefig(filename) + +# The function is from https://github.com/intsig-textin/markdown_tester + + +def markdown_to_html(markdown_table): + rows = [row.strip() for row in markdown_table.strip().split('\n')] + len(rows[0].split('|')) - 2 + + html_table = '\n \n \n' + + header_cells = [cell.strip() for cell in rows[0].split('|')[1:-1]] + for cell in header_cells: + html_table += f' \n' + html_table += ' \n \n \n' + + for row in rows[2:]: + cells = [cell.strip() for cell in row.split('|')[1:-1]] + html_table += ' \n' + for cell in cells: + html_table += f' \n' + html_table += ' \n' + + html_table += ' \n
{cell}
{cell}
\n' + return html_table + + +def convert_table_str(s): + s = re.sub(r'', '', s) + s = re.sub(r'', '', s) + # s = re.sub(r'
',lambda x:f'',s) + # s = re.sub(r'',lambda x:f'',s) + res = '' + res += '\n\n' + temp_item = '' + for c in s: + temp_item += c + if c == '>' and not re.search(r'\$', temp_item): + res += temp_item + '\n' + temp_item = '' + return res + '\n' + + +def merge_table(md): + table_temp = '' + for line in md: + table_temp += line + return convert_table_str(table_temp) + + +def find_md_table_mode(line): + if re.search(r'-*?:', line) or re.search(r'---', line) or re.search(r':-*?', line): + return True + return False + + +def delete_table_and_body(input_list): + res = [] + for line in input_list: + if not re.search(r'', line): + res.append(line) + return res + + +def merge_tables(input_str): + # Delete HTML comments + input_str = re.sub(r'', '', input_str) + + # Use regex to find each block + table_blocks = re.findall(r'
[\s\S]*?
', input_str) + + # Process each block, replace ') + final_tr = delete_table_and_body(block_lines) + if len(final_tr) > 2: + # Ignore
with + output_lines = [] + for block in table_blocks: + block_lines = block.split('\n') + for i, line in enumerate(block_lines): + if '' in line: + block_lines[i] = line.replace('', '').replace('', '
and
tags, keep only table content + output_lines.extend(final_tr) + + # Rejoin the processed strings + merged_output = '\n{}\n
'.format('\n'.join(output_lines)) + + return "\n\n" + merged_output + "\n\n" + + +def replace_table_with_placeholder(input_string): + lines = input_string.split('\n') + output_lines = [] + + in_table_block = False + temp_block = "" + last_line = "" + + for idx, line in enumerate(lines): + # if not in_org_table: + # if "" not in last_line and in_table_block == False and temp_block != "": + # output_lines.append(merge_tables(temp_block)) + # temp_block = "" + if "
" in line: + # if "
" not in last_line: + temp_block += "\n" + last_line + if "
" in last_line: + if "" not in line: + in_table_block = False + output_lines.append(merge_tables(temp_block)) + temp_block = "" + else: + output_lines.append(last_line) + + last_line = line + # else: + # org_table_list.append(line) + # if "" in last_line: + temp_block += "\n" + last_line + output_lines.append(merge_tables(temp_block)) + else: + output_lines.append(last_line) + # if "
" in last_line: + # output_lines.append(merge_tables(temp_block)) + + return '\n'.join(output_lines) + + +def convert_table(input_str): + # Replace + output_str = input_str.replace("
", "
") + + # Replace
+ output_str = output_str.replace("", "") + + return output_str + + +def convert_markdown_to_html(markdown_content): + # Define a regex pattern to find Markdown tables with newlines + markdown_content = markdown_content.replace('\r', '') + '\n' + pattern = re.compile(r'\|\s*.*?\s*\|\n', re.DOTALL) + + # Find all matches in the Markdown content + matches = pattern.findall(markdown_content) + + for match in matches: + html_table = markdown_to_html(match) + markdown_content = markdown_content.replace( + match, html_table, 1) # Only replace the first occurrence + + res_html = convert_table(replace_table_with_placeholder(markdown_content)) + + return res_html diff --git a/vlmeval/dataset/__init__.py b/vlmeval/dataset/__init__.py index 0714e7a1b..e03863734 100644 --- a/vlmeval/dataset/__init__.py +++ b/vlmeval/dataset/__init__.py @@ -68,6 +68,7 @@ from .m4bench import M4Bench from .macbench import MaCBench from .matbench import MATBench +from .MDPBench.mdpbench import MDPBench from .medq_deg_bench import MedQDEGBenchDataset from .medqbench_caption import MedqbenchCaptionDataset from .medqbench_mcq import MedqbenchMCQDataset @@ -284,7 +285,7 @@ def evaluate(self, eval_file, **judge_kwargs): HRBenchDataset, CRPE, MathVerse, NaturalBenchDataset, MIABench, OlympiadBench, SeePhys, WildVision, MMMath, QSpatial, Dynamath, GSM8KVDataset, MMGenBench, VizWiz, # noqa: E501 MMNIAH, CMMMU, VLRewardBench, WeMath, LogicVista, MMMUProDataset, - CreationMMBenchDataset, HLEDataset, ImageShortQADataset, MMAlignBench, OmniDocBench, + CreationMMBenchDataset, HLEDataset, ImageShortQADataset, MMAlignBench, OmniDocBench, MDPBench, VLM2Bench, VMCBenchDataset, EMMADataset, MME_CoT, MOAT, MedXpertQA_MM_test, LEGO, MMSci_Captioning, Physics_yale, ScreenSpot_Pro, ScreenSpot, VenusBench_GD, ScreenSpotV2, OSWorld_G, VBGD, MMIFEval, Spatial457, VisuLogic, CVBench, PathVQA_VAL,