自学内容网 自学内容网

JSONL 文件的检查和修订器

下面是一个JSONL 文件的检查和修订器,代码如下:

import json
import tkinter as tk
from tkinter import filedialog, messagebox

def check_jsonl_file(input_file, log_file, output_file=None):
    errors = []
    valid_lines = []

    with open(input_file, 'r', encoding='utf-8') as infile, open(log_file, 'w', encoding='utf-8') as logfile:
        for line_number, line in enumerate(infile, start=1):
            try:
                data = json.loads(line)
                if not (
                    "question" in data and
                    "xihe_answers" in data and
                    "ling_answers" in data and
                    isinstance(data["question"], str) and
                    isinstance(data["xihe_answers"], list) and
                    isinstance(data["ling_answers"], list) and
                    all(isinstance(answer, str) for answer in data["xihe_answers"]) and
                    all(isinstance(answer, str) for answer in data["ling_answers"])
                ):
                    errors.append((line_number, line))
                    logfile.write(f"第 {line_number} 行错误: {line}")
                else:
                    valid_lines.append(line)
            except json.JSONDecodeError as e:
                errors.append((line_number, line))
                logfile.write(f"第 {line_number} 行无效 JSON: {line} - {e}\n")

    if output_file:
        with open(output_file, 'w', encoding='utf-8') as outfile:
            for line in valid_lines:
                outfile.write(line)
        messagebox.showinfo("提示", f"检查完成。有效行已保存到 {output_file}。日志已保存到 {log_file}")
    else:
        messagebox.showinfo("提示", f"检查完成。日志已保存到 {log_file}")

def select_file():
    file_path = filedialog.askopenfilename(filetypes=[("JSONL 文件", "*.jsonl")])
    if file_path:
        file_entry.delete(0, tk.END)
        file_entry.insert(0, file_path)

def select_output_file():
    file_path = filedialog.asksaveasfilename(defaultextension=".jsonl", filetypes=[("JSONL 文件", "*.jsonl")])
    if file_path:
        output_file_entry.delete(0, tk.END)
        output_file_entry.insert(0, file_path)

def run_check():
    input_file = file_entry.get()
    output_file = output_file_entry.get()
    if not input_file:
        messagebox.showwarning("警告", "请选择要检查的输入文件。")
        return

    log_file = "error_log.txt"
    check_jsonl_file(input_file, log_file, output_file)

# 创建主窗口
root = tk.Tk()
root.title("JSONL 文件检查器")

# 创建并放置控件
file_label = tk.Label(root, text="选择 JSONL 文件:")
file_label.pack(pady=5)

file_entry = tk.Entry(root, width=50)
file_entry.pack(pady=5)

browse_button = tk.Button(root, text="浏览", command=select_file)
browse_button.pack(pady=5)

output_label = tk.Label(root, text="选择输出文件 (可选):")
output_label.pack(pady=5)

output_file_entry = tk.Entry(root, width=50)
output_file_entry.pack(pady=5)

output_browse_button = tk.Button(root, text="浏览", command=select_output_file)
output_browse_button.pack(pady=5)

check_button = tk.Button(root, text="检查文件", command=run_check)
check_button.pack(pady=20)

# 运行应用程序
root.mainloop()

使用说明:
运行程序后,点击“浏览”按钮选择要检查的 JSONL 文件。
可以选择一个输出文件来保存修订后的数据(可选)。
点击“检查文件”按钮开始检查文件。
检查完成后,会显示一个消息框,告知检查结果和日志文件的位置。如果指定了输出文件,还会告知输出文件的位置。

为了确保 JSONL 数据集是正确的,我们需要明确数据集中每个字段的预期格式和内容。我提供的代码,数据集中的每一行应该是一个 JSON 对象,并且必须包含以下字段:

question:问题,类型为字符串。
xihe_answers:希和答案列表,类型为字符串列表。
ling_answers:灵答案列表,类型为字符串列表。
每个字段的具体要求如下:

question:必须是一个非空字符串。
xihe_answers:必须是一个列表,列表中的每个元素都是一个非空字符串。
ling_answers:必须是一个列表,列表中的每个元素都是一个非空字符串。
正确的数据集示例
以下是一个符合上述要求的正确 JSONL 数据集示例:
jsonl
{“question”: “什么是人工智能?”, “xihe_answers”: [“人工智能是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。”], “ling_answers”: [“人工智能是一种使计算机能够执行通常需要人类智能的任务的技术。”]}
{“question”: “机器学习是什么?”, “xihe_answers”: [“机器学习是人工智能的一个分支,它使计算机能够在不进行明确编程的情况下从数据中学习。”], “ling_answers”: [“机器学习是一种通过数据训练模型,使其能够进行预测或决策的技术。”]}
{“question”: “深度学习与机器学习有什么区别?”, “xihe_answers”: [“深度学习是机器学习的一个子领域,它使用深层神经网络来学习数据的复杂模式。”], “ling_answers”: [“深度学习是机器学习的一种高级形式,它利用多层神经网络来处理复杂的任务。”]}
希望对您数据集处理有帮助。


原文地址:https://blog.csdn.net/weixin_54366286/article/details/142831522

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!