处理 jsonl 文件 Args: input_path: 输入文件路径 output_path: 输出文件路径(可选)。如果不提供,将自动生成为 input_path_processed.jsonl verbose: 是否打印详细信息 Returns: Dict[str, int]: 处理统计信息
(
self,
input_path: Union[str, Path],
output_path: Optional[Union[str, Path]] = None,
verbose: bool = True
)
| 168 | return results |
| 169 | |
| 170 | def process( |
| 171 | self, |
| 172 | input_path: Union[str, Path], |
| 173 | output_path: Optional[Union[str, Path]] = None, |
| 174 | verbose: bool = True |
| 175 | ) -> Dict[str, int]: |
| 176 | """ |
| 177 | 处理 jsonl 文件 |
| 178 | |
| 179 | Args: |
| 180 | input_path: 输入文件路径 |
| 181 | output_path: 输出文件路径(可选)。如果不提供,将自动生成为 input_path_processed.jsonl |
| 182 | verbose: 是否打印详细信息 |
| 183 | |
| 184 | Returns: |
| 185 | Dict[str, int]: 处理统计信息 |
| 186 | """ |
| 187 | input_path = Path(input_path) |
| 188 | |
| 189 | # 如果未提供输出路径,自动生成 |
| 190 | if output_path is None: |
| 191 | output_path = input_path.parent / f"{input_path.stem}_processed{input_path.suffix}" |
| 192 | else: |
| 193 | output_path = Path(output_path) |
| 194 | |
| 195 | # 确保输出目录存在 |
| 196 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 197 | |
| 198 | # 重置统计信息 |
| 199 | self.stats = defaultdict(int) |
| 200 | |
| 201 | if verbose: |
| 202 | print(f"📖 正在读取输入文件: {input_path}") |
| 203 | |
| 204 | # 处理数据 |
| 205 | with jsonlines.open(input_path) as reader, \ |
| 206 | jsonlines.open(output_path, mode='w') as writer: |
| 207 | |
| 208 | for line in reader: |
| 209 | self.stats['total_input'] += 1 |
| 210 | |
| 211 | # 应用过滤器 |
| 212 | if not self._apply_filters(line): |
| 213 | self.stats['total_filtered'] += 1 |
| 214 | continue |
| 215 | |
| 216 | # 应用转换器 |
| 217 | transformed_items = self._apply_transformers(line) |
| 218 | |
| 219 | # 写入结果 |
| 220 | for item in transformed_items: |
| 221 | writer.write(item) |
| 222 | self.stats['total_output'] += 1 |
| 223 | |
| 224 | if verbose: |
| 225 | self._print_stats() |
| 226 | print(f"✅ 处理完成,结果已保存到: {output_path}") |
| 227 |
no test coverage detected