File size: 2,024 Bytes
0be65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import json
from pathlib import Path

import pandas as pd

from project_settings import project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--data_dir",
        default="examples/preprocess/data/tweets_with_emoji",
        type=str,
    )
    parser.add_argument(
        "--output_file",
        default="data/tweets_with_emoji.jsonl",
        type=str,
    )
    args = parser.parse_args()
    return args


def print_examples():
    args = get_args()

    data_dir = project_path / args.data_dir

    print("| 样本数量 | 类别 | 例句1 | 例句2 |")
    print("|  ---  |  ---  |  ---  |  ---  |")
    for filename in data_dir.glob("*.csv"):
        df = pd.read_csv(filename)
        length = len(df)
        category = filename.stem
        text_list = list()
        for i, row in df.iterrows():
            row = dict(row)
            text = row["Text"]
            text = str(text).strip()

            text = text.replace("\n", " <br> ")
            text = text.replace("\r", " <br> ")
            text_list.append(text)

            if i >= 2:
                break

        row = "| {} | {} | {} | {} |".format(length, category, text_list[0], text_list[1])
        print(row)

    return


def main():
    args = get_args()

    data_dir = project_path / args.data_dir
    output_file = project_path / args.output_file

    with open(output_file, "w", encoding="utf-8") as fout:
        for filename in data_dir.glob("*.csv"):
            df = pd.read_csv(filename)

            for i, row in df.iterrows():
                row = dict(row)
                text = row["Text"]

                row_ = {
                    "text": text,
                    "category": filename.stem
                }
                row_ = json.dumps(row_, ensure_ascii=False)
                row_ = "{}\n".format(row_)
                fout.write(row_)

    return


if __name__ == '__main__':
    print_examples()