File size: 1,204 Bytes
c5adbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import pandas as pd

folder_path = "双语数据\史记\七十列传"

def get_files(folder_path):
    subfolders = [f.path for f in os.scandir(folder_path) if f.is_dir()]
    print(subfolders)
    dataset = []
    
    source_file = "source.txt"
    target_file = "target.txt"
    for x in subfolders:
        with open(os.path.join(x,source_file) , "r", encoding="utf-8") as f:
            source_content = f.read()
        with open(os.path.join(x,target_file), "r", encoding="utf-8") as f:
            target_content = f.read()

        source_content = source_content.split("\n")
        target_content = target_content.split("\n")

        for i in range(len(source_content)):
            dataset.append([source_content[i], target_content[i]])
    return dataset
dataset = get_files(folder_path)

# add one column "instruction" with the content "请把古文翻译成现代汉语" to the dataset
df = pd.DataFrame(dataset, columns=["source", "target"])
df["instruction"] = "请把现代汉语翻译成古文"

df.rename(columns={"source": "output", "target": "input"}, inplace=True)

print(len(df))

df.to_json("dataset.jsonl", orient="records", lines=True, force_ascii=False)