File size: 1,198 Bytes
ac8b55a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import gradio as gr
import pandas as pd
import re
def check_spacing(csv_file):
# 讀取 CSV 檔案
df = pd.read_csv(csv_file.name)
# 將所有的內容合併成一個大的字串進行檢查
text = ' '.join(df.astype(str).values.flatten())
# 使用正則表達式來查找$符號兩側的中文字之間沒有空格的地方
pattern = r'[\u4e00-\u9fa5]\$[\u4e00-\u9fa5]'
matches = re.finditer(pattern, text)
# 收集所有錯誤的地方
errors = []
for match in matches:
errors.append((match.start(), match.group()))
# 如果有錯誤,列出所有錯誤
if errors:
error_list = []
for error in errors:
error_list.append(f"錯誤位置:{error[0]},內容:{error[1]}")
return "\n".join(error_list)
else:
return "未發現錯誤"
# 使用 Gradio 來建立介面
interface = gr.Interface(
fn=check_spacing,
inputs=gr.File(file_types=['.csv', '.xls', '.pdf']),
outputs="text",
title="中文校對系統",
description="上傳一個CSV、XLS 或 PDF 檔案,系統會檢查$符號前後的中文字是否有空格"
)
# 啟動 Gradio 介面
interface.launch()
|