OTSL 是 Optimized Table Structure Language(优化表格结构语言)的缩写,是一种用于描述表格结构的标记语言。
OTSL (Optimized Table Structure Language) is a specialized language designed to represent and process table structures efficiently, particularly in the context of machine learning and table recognition tasks. It was introduced to address the limitations of traditional table encoding methods, such as those based on HTML, by offering a more compact and optimized representation.OTSL(优化表结构语言)是一种专门的语言,旨在有效地表示和处理表结构,特别是在机器学习和表识别任务的上下文中。引入它是为了通过提供更紧凑和优化的表示来解决传统表格编码方法(例如基于 HTML 的编码方法)的局限性。 Key Features of OTSL 的主要特点
Minimized Vocabulary: OTSL uses a significantly reduced set of tokens (e.g., only 5 tokens compared to HTML's 28+), making it lightweight and efficient.最小化词汇量:OTSL 使用的标记集显着减少(例如,与 HTML 的 5+ 相比,只有 28 个标记),使其轻量级且高效。 Compact Sequence Representation: It shortens the sequence length required to encode tables, often reducing it by half compared to HTML-based methods.紧凑的序列表示:它缩短了对表进行编码所需的序列长度,与基于 HTML 的方法相比,通常将其减少一半。 Preservation of Table Structure: Despite its compactness, OTSL retains the essential two-dimensional structure of tables, including complex features like merged cells or spanning rows/columns.表结构的保留:尽管 OTSL 结构紧凑,但它保留了表的基本二维结构,包括合并单元格或跨行/列等复杂功能。 Optimized for Machine Learning: Its linear format is tailored for sequence models, enabling faster and more accurate table recognition and processing.针对机器学习进行了优化:其线性格式专为序列模型量身定制,可实现更快、更准确的表格识别和处理。
Applications应用
Table Recognition: Used in systems like IBM's DocLing TableFormer for extracting and understanding table data from documents.表识别:用于 IBM 的 DocLing TableFormer 等系统,用于从文档中提取和理解表数据。 Data Processing: Facilitates efficient handling of tabular data in AI models, especially for tasks like document analysis, information retrieval, and natural language processing.数据处理:促进 AI 模型中表格数据的高效处理,特别是对于文档分析、信息检索和自然语言处理等任务。
Advantages优势
Efficiency: Reduces computational overhead by minimizing token usage and sequence length.效率:通过最大限度地减少令牌使用和序列长度来减少计算开销。 Flexibility: Supports complex table structures without sacrificing simplicity.灵活性:支持复杂的表结构,同时又不牺牲简单性。 Scalability: Ideal for large-scale document processing tasks.可扩展性:非常适合大规模文档处理任务。
OTSL represents a significant step forward in table structure recognition, offering a streamlined and effective solution for modern AI applications.OTSL 代表了表结构识别向前迈出的重要一步,为现代人工智能应用提供了简化且有效的解决方案。
OTSL 使用 6个核心标签 来描述表格的单元格和结构:
| 标签 | 含义 | 说明 |
|---|---|---|
<fcel> |
Full Cell | 完整单元格(有内容) |
<ecel> |
Empty Cell | 空单元格(无内容) |
<nl> |
New Line | 换行(新行开始) |
| 标签 | 含义 | 说明 |
|---|---|---|
<lcel> |
Left Cell | 左侧合并单元格(与左侧单元格合并) |
<ucel> |
Upper Cell | 上侧合并单元格(与上侧单元格合并) |
<xcel> |
Cross Cell | 交叉合并单元格(同时与左侧和上侧合并) |
<fcel>交易日期<fcel>交易时间<fcel>交易摘要<fcel>交易金额<fcel>本次余额<fcel>对手信息<fcel>日志号<fcel>交易渠道<nl>
<fcel>20240223<fcel>034026<fcel>宝付<fcel>-15.34<fcel>25755.99<fcel>广西广投资产管理股份有限公司广西广投资产管理股份有限公司<fcel>149887431<fcel>电子商务<nl>
...
转换为表格:
| 交易日期 | 交易时间 | 交易摘要 | 交易金额 | 本次余额 | 对手信息 | 日志号 | 交易渠道 |
|---|---|---|---|---|---|---|---|
| 20240223 | 034026 | 宝付 | -15.34 | 25755.99 | 广西广投资产管理股份有限公司 | 149887431 | 电子商务 |
| 20240223 | 034031 | 转支 | -245.85 | 25510.14 | 江苏苏宁银行股份有限公司 | 149896732 | (空) |
| ... | ... | ... | ... | ... | ... | ... | ... |
graph LR
A[原始表格图像] --> B[视觉识别模型]
B --> C[生成OTSL字符串]
C --> D[OTSL解析器]
D --> E[HTML表格]
E --> F[Markdown表格]
从您提供的代码中可以看到 OTSL 的处理流程:
pipeline.py)# 在 get_layout_parsing_results 中
text_prompt = "Table Recognition:" # 提示 VLM 识别表格
vl_rec_result = vl_rec_model.predict(...) # 模型输出 OTSL 字符串
result_str = vl_rec_result.get("result", "")
uilts.py)def otsl_pad_to_sqr_v2(otsl_str: str) -> str:
"""将 OTSL 填充为矩形(每行单元格数相同)"""
# 示例:
# 输入: '<fcel>A<fcel>B<nl><fcel>C<nl>'
# 输出: '<fcel>A<fcel>B<nl><fcel>C<ecel><nl>'
# ↑ 自动补齐空单元格
uilts.py)def convert_otsl_to_html(otsl_content: str):
"""
OTSL → HTML 转换流程:
1. 提取标签和文本
2. 解析为 TableCell 对象
3. 生成 HTML <table>
"""
otsl_content = otsl_pad_to_sqr_v2(otsl_content) # 标准化
tokens, mixed_texts = otsl_extract_tokens_and_text(otsl_content)
table_cells, split_row_tokens = otsl_parse_texts(mixed_texts, tokens)
table_data = TableData(
num_rows=len(split_row_tokens),
num_cols=max(len(row) for row in split_row_tokens),
table_cells=table_cells,
)
return export_to_html(table_data)
pipeline.py)if block_label == "table":
html_str = convert_otsl_to_html(result_str) # OTSL → HTML
if html_str != "":
result_str = html_str
uilts.py)在表格中嵌入图像:
def tokenize_figure_of_table(table_block_img, table_box, figures):
"""
将表格中的图像替换为 Token(如 [F1], [F2])
避免 VLM 混淆图像和表格结构
"""
token_str = "[F" + str(random_map[figure_id]) + "]"
table_block_img = paint_token(table_block_img, draw_box, token_str)
token_map[token_str] = f'<img src="{figure["path"]}" >'
uilts.py)def truncate_repetitive_content(content: str):
"""
检测并截断重复内容:
- 字符级重复: 'ababab' → 'ab'
- 短语级重复: '错误错误错误...' → '错误'
- 行级重复: 同一行重复多次
"""
| 交易日期 | 交易时间 | 金额 |
|----------|----------|---------|
| 20240223 | 034026 | -15.34 |
| 20240223 | 034031 | -245.85 |
<fcel>交易日期<fcel>交易时间<fcel>金额<nl>
<fcel>20240223<fcel>034026<fcel>-15.34<nl>
<fcel>20240223<fcel>034031<fcel>-245.85<nl>
<table>
<tr><th>交易日期</th><th>交易时间</th><th>金额</th></tr>
<tr><td>20240223</td><td>034026</td><td>-15.34</td></tr>
<tr><td>20240223</td><td>034031</td><td>-245.85</td></tr>
</table>
这就是 OTSL 在 PaddleOCR-VL 中的完整应用流程!🎯