File size: 1,070 Bytes
cd769ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc1cb55
8df80a6
cd769ac
bc1cb55
 
cd769ac
bc1cb55
cd769ac
 
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
import gradio as gr
import pandas as pd
import re

def check_spacing_csv(csv_file):
    # 讀取 CSV 檔案
    df = pd.read_csv(csv_file.name)

    errors = []

    # 逐行檢查每一行的內容
    for index, row in df.iterrows():
        row_text = ' '.join(row.astype(str).values.flatten())
        
        # 使用正則表達式來查找$符號兩側的中文字之間沒有空格的地方
        pattern = r'[\u4e00-\u9fa5]\$[\u4e00-\u9fa5]'
        matches = re.finditer(pattern, row_text)
        
        for match in matches:
            errors.append(f"第 {index + 1} 行,錯誤位置:{match.start()},內容:{match.group()}")

    if errors:
        return "\n".join(errors)
    else:
        return "未發現錯誤"

# 使用 Gradio 來建立介面
interface = gr.Interface(
    fn=check_spacing_csv,
    inputs=gr.File(file_types=['.csv']), 
    outputs="text",
    title="CSV 中文校對系統",
    description="上傳一個CSV檔案,系統會檢查$符號前後的中文字是否有空格"
)

# 啟動 Gradio 介面
interface.launch()