feat(pre_process_file):解决hxops上传excel行截断的问题,由markdown换为命中更好的key=value的方式;dify同步custom;行间\n\n,数据行单独成块;
This commit is contained in:
1 parent
a18a07ef1a
commit
67d08924a4
25 files changed
+655
-25
No files matched your search
@@ -7,6 +7,9 @@ COND_KWS = ["条件", "判定", "if", "IF"]
|
||||
ACT_KWS = ["处理", "动作", "措施", "结果", "输出"]
|
||||
OWN_KWS = ["责任", "负责人", "部门", "岗位"]
|
||||
|
||||
# Wide tables use row text blocks so Dify custom segmentation stays row-aligned.
|
||||
WIDE_TABLE_COLS = 8
|
||||
|
||||
|
||||
def _find_col(headers: list[str], kws: list[str]) -> int | None:
|
||||
for i, h in enumerate(headers):
|
||||
@@ -16,40 +19,73 @@ def _find_col(headers: list[str], kws: list[str]) -> int | None:
|
||||
return None
|
||||
|
||||
|
||||
def _to_table_md(headers: list[str], rows: list[list[str]]) -> str:
|
||||
def _to_table_md_loose(headers: list[str], rows: list[list[str]]) -> str:
|
||||
"""Markdown header block plus one pipe row per chunk, separated by blank lines."""
|
||||
hs = [h or "-" for h in headers]
|
||||
out = ["| " + " | ".join(hs) + " |", "| " + " | ".join(["---"] * len(hs)) + " |"]
|
||||
for r in rows:
|
||||
vals = [r[i] if i < len(r) else "" for i in range(len(hs))]
|
||||
out.append("| " + " | ".join(vals) + " |")
|
||||
return "\n".join(out)
|
||||
header_block = "\n".join(
|
||||
[
|
||||
"| " + " | ".join(hs) + " |",
|
||||
"| " + " | ".join(["---"] * len(hs)) + " |",
|
||||
]
|
||||
)
|
||||
data_blocks: list[str] = []
|
||||
for row in rows:
|
||||
vals = [row[i] if i < len(row) else "" for i in range(len(hs))]
|
||||
data_blocks.append("| " + " | ".join(vals) + " |")
|
||||
if not data_blocks:
|
||||
return header_block
|
||||
return header_block + "\n\n" + "\n\n".join(data_blocks)
|
||||
|
||||
|
||||
def _to_row_text_md(headers: list[str], rows: list[list[str]]) -> str:
|
||||
"""Wide-table fallback: one bullet per row for stable chunk boundaries."""
|
||||
hs = [h or "-" for h in headers]
|
||||
blocks: list[str] = []
|
||||
for row in rows:
|
||||
pairs = []
|
||||
for i, header in enumerate(hs):
|
||||
val = row[i] if i < len(row) else ""
|
||||
pairs.append(f"{header}={val}")
|
||||
blocks.append("- 【行】" + ";".join(pairs) + "。")
|
||||
return "\n\n".join(blocks)
|
||||
|
||||
|
||||
def run(input_data: list[dict] | dict, context: Context) -> str:
|
||||
tables = input_data if isinstance(input_data, list) else [input_data]
|
||||
parts: list[str] = []
|
||||
|
||||
for t in tables:
|
||||
headers = [str(x).strip() for x in t.get("headers", [])]
|
||||
rows = [[str(c).strip() for c in row] for row in t.get("rows", [])]
|
||||
parts.append(f"## Sheet: {t.get('sheet', 'Sheet1')}")
|
||||
for table in tables:
|
||||
headers = [str(x).strip() for x in table.get("headers", [])]
|
||||
rows = [[str(c).strip() for c in row] for row in table.get("rows", [])]
|
||||
sheet_blocks: list[str] = [f"## Sheet: {table.get('sheet', 'Sheet1')}"]
|
||||
|
||||
cidx = _find_col(headers, COND_KWS)
|
||||
aidx = _find_col(headers, ACT_KWS)
|
||||
oidx = _find_col(headers, OWN_KWS)
|
||||
if cidx is not None and aidx is not None:
|
||||
cond_idx = _find_col(headers, COND_KWS)
|
||||
act_idx = _find_col(headers, ACT_KWS)
|
||||
owner_idx = _find_col(headers, OWN_KWS)
|
||||
if cond_idx is not None and act_idx is not None:
|
||||
bullets: list[str] = []
|
||||
for row in rows:
|
||||
cond = row[cidx] if cidx < len(row) else "满足条件"
|
||||
act = row[aidx] if aidx < len(row) else "执行处理"
|
||||
cond = row[cond_idx] if cond_idx < len(row) else "满足条件"
|
||||
act = row[act_idx] if act_idx < len(row) else "执行处理"
|
||||
owner = (
|
||||
row[oidx] if oidx is not None and oidx < len(row) else "相关责任方"
|
||||
row[owner_idx]
|
||||
if owner_idx is not None and owner_idx < len(row)
|
||||
else "相关责任方"
|
||||
)
|
||||
parts.append(
|
||||
bullets.append(
|
||||
f"- 当 {cond or '满足条件'} 时,{owner or '相关责任方'} 执行 {act or '执行处理'}。"
|
||||
)
|
||||
if bullets:
|
||||
sheet_blocks.append("\n\n".join(bullets))
|
||||
elif len(headers) > WIDE_TABLE_COLS:
|
||||
content = _to_row_text_md(headers, rows)
|
||||
if content:
|
||||
sheet_blocks.append(content)
|
||||
else:
|
||||
parts.append(_to_table_md(headers, rows))
|
||||
content = _to_table_md_loose(headers, rows)
|
||||
if content:
|
||||
sheet_blocks.append(content)
|
||||
|
||||
parts.append("")
|
||||
parts.append("\n\n".join(sheet_blocks))
|
||||
|
||||
return "\n".join(parts).strip() + "\n"
|
||||
return "\n\n".join(parts).strip() + "\n"
|
||||
Reference in new issue
Block a user