Datasets:
Tasks:
Automatic Speech Recognition
Modalities:
Text
Languages:
Chinese
Size:
10K<n<100K
ArXiv:
License:
Upload clean_data.py
Browse files- sentence_data/clean_data.py +64 -0
sentence_data/clean_data.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 把该py文件的 ./transcript 文件夹下面的所有文件依次取出来,
|
2 |
+
# 看看是否包含()这个符号,付过包含,打印出来,并且把()和他里面的内容删除,把处理好的文本放回原文件之中
|
3 |
+
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
|
7 |
+
def clean_line(line):
|
8 |
+
result = [] # 用于存储清洗后的字符
|
9 |
+
in_brackets = False # 标记是否在括号内
|
10 |
+
|
11 |
+
for char in line:
|
12 |
+
if char == '(':
|
13 |
+
# 遇到左括号,标记为在括号内
|
14 |
+
in_brackets = True
|
15 |
+
elif char == ')':
|
16 |
+
# 遇到右括号,标记为不在括号内
|
17 |
+
in_brackets = False
|
18 |
+
elif not in_brackets:
|
19 |
+
# 如果不在括号内,保留字符
|
20 |
+
result.append(char)
|
21 |
+
|
22 |
+
# 将字符列表拼接成字符串并返回
|
23 |
+
return ''.join(result)
|
24 |
+
|
25 |
+
|
26 |
+
def process_files(folder_path):
|
27 |
+
"""
|
28 |
+
遍历指定文件夹中的所有文件,逐行清洗包含括号的文本,并将结果写回原文件。
|
29 |
+
"""
|
30 |
+
# 检查文件夹是否存在
|
31 |
+
if not os.path.exists(folder_path):
|
32 |
+
print(f"文件夹 {folder_path} 不存在!")
|
33 |
+
return
|
34 |
+
|
35 |
+
# 遍历文件夹中的所有文件
|
36 |
+
for file_name in os.listdir(folder_path):
|
37 |
+
file_path = os.path.join(folder_path, file_name)
|
38 |
+
|
39 |
+
# 确保是文件而不是子文件夹
|
40 |
+
if os.path.isfile(file_path):
|
41 |
+
try:
|
42 |
+
# 创建临时文件
|
43 |
+
temp_file_path = file_path + ".tmp"
|
44 |
+
with open(file_path, 'r', encoding='utf-8') as infile, open(temp_file_path, 'w', encoding='utf-8') as outfile:
|
45 |
+
# 逐行读取文件
|
46 |
+
for line in infile:
|
47 |
+
# 清洗当前行
|
48 |
+
cleaned_line = clean_line(line)
|
49 |
+
# 写入清洗后的行到临时文件
|
50 |
+
outfile.write(cleaned_line)
|
51 |
+
|
52 |
+
# 替换原文件
|
53 |
+
os.replace(temp_file_path, file_path)
|
54 |
+
print(f"已清洗并保存到: {file_name}")
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
print(f"处理文件 {file_name} 时出错: {e}")
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
# 指定文件夹路径
|
61 |
+
transcript_folder = "./transcript"
|
62 |
+
|
63 |
+
# 调用函数处理文件
|
64 |
+
process_files(transcript_folder)
|