File size: 8,997 Bytes
85d7559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7998590
85d7559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7998590
85d7559
 
 
 
 
 
7998590
85d7559
 
 
 
 
7998590
85d7559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7998590
85d7559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
This code is licensed under CC-BY-4.0 from the original work by shunk031.
The code is adapted from https://huggingface.co/datasets/shunk031/JGLUE/blob/main/JGLUE.py
with minor modifications to the code structure.

This codebase provides pre-processing functionality for the MARC-ja dataset in the Japanese GLUE benchmark.
The original code can be found at https://github.com/yahoojapan/JGLUE/blob/main/preprocess/marc-ja/scripts/marc-ja.py.
"""

import random
import warnings
from typing import Dict, List, Optional, Union
import string

import datasets as ds
import pandas as pd


class MarcJaConfig(ds.BuilderConfig):
    def __init__(
        self,
        name: str = "MARC-ja",
        is_han_to_zen: bool = False,
        max_instance_num: Optional[int] = None,
        max_char_length: int = 500,
        remove_netural: bool = True,
        train_ratio: float = 0.94,
        val_ratio: float = 0.03,
        test_ratio: float = 0.03,
        output_testset: bool = False,
        filter_review_id_list_valid: bool = True,
        label_conv_review_id_list_valid: bool = True,
        version: Optional[Union[ds.utils.Version, str]] = ds.utils.Version("0.0.0"),
        data_dir: Optional[str] = None,
        data_files: Optional[ds.data_files.DataFilesDict] = None,
        description: Optional[str] = None,
    ) -> None:
        super().__init__(
            name=name,
            version=version,
            data_dir=data_dir,
            data_files=data_files,
            description=description,
        )
        if train_ratio + val_ratio + test_ratio != 1.0:
            raise ValueError(
                "train_ratio + val_ratio + test_ratio should be 1.0, "
                f"but got {train_ratio} + {val_ratio} + {test_ratio} = {train_ratio + val_ratio + test_ratio}"
            )

        self.train_ratio = train_ratio
        self.val_ratio = val_ratio
        self.test_ratio = test_ratio

        self.is_han_to_zen = is_han_to_zen
        self.max_instance_num = max_instance_num
        self.max_char_length = max_char_length
        self.remove_netural = remove_netural
        self.output_testset = output_testset

        self.filter_review_id_list_valid = filter_review_id_list_valid
        self.label_conv_review_id_list_valid = label_conv_review_id_list_valid


def get_label(rating: int, remove_netural: bool = False) -> Optional[str]:
    if rating >= 4:
        return "positive"
    elif rating <= 2:
        return "negative"
    else:
        if remove_netural:
            return None
        else:
            return "neutral"


def is_filtered_by_ascii_rate(text: str, threshold: float = 0.9) -> bool:
    ascii_letters = set(string.printable)
    rate = sum(c in ascii_letters for c in text) / len(text)
    return rate >= threshold


def shuffle_dataframe(df: pd.DataFrame) -> pd.DataFrame:
    instances = df.to_dict(orient="records")
    random.seed(1)
    random.shuffle(instances)
    return pd.DataFrame(instances)


def get_filter_review_id_list(
    filter_review_id_list_paths: Dict[str, str],
) -> Dict[str, List[str]]:
    filter_review_id_list_valid = filter_review_id_list_paths.get("valid")
    filter_review_id_list_test = filter_review_id_list_paths.get("test")

    filter_review_id_list = {}

    if filter_review_id_list_valid is not None:
        with open(filter_review_id_list_valid, "r") as rf:
            filter_review_id_list["valid"] = [line.rstrip() for line in rf]

    if filter_review_id_list_test is not None:
        with open(filter_review_id_list_test, "r") as rf:
            filter_review_id_list["test"] = [line.rstrip() for line in rf]

    return filter_review_id_list


def get_label_conv_review_id_list(
    label_conv_review_id_list_paths: Dict[str, str],
) -> Dict[str, Dict[str, str]]:
    import csv

    label_conv_review_id_list_valid = label_conv_review_id_list_paths.get("valid")
    label_conv_review_id_list_test = label_conv_review_id_list_paths.get("test")

    label_conv_review_id_list: Dict[str, Dict[str, str]] = {}

    if label_conv_review_id_list_valid is not None:
        with open(label_conv_review_id_list_valid, "r") as rf:
            label_conv_review_id_list["valid"] = {row[0]: row[1] for row in csv.reader(rf)}

    if label_conv_review_id_list_test is not None:
        with open(label_conv_review_id_list_test, "r") as rf:
            label_conv_review_id_list["test"] = {row[0]: row[1] for row in csv.reader(rf)}

    return label_conv_review_id_list


def output_data(
    df: pd.DataFrame,
    train_ratio: float,
    val_ratio: float,
    test_ratio: float,
    output_testset: bool,
    filter_review_id_list_paths: Dict[str, str],
    label_conv_review_id_list_paths: Dict[str, str],
) -> Dict[str, pd.DataFrame]:
    instance_num = len(df)
    split_dfs: Dict[str, pd.DataFrame] = {}
    length1 = int(instance_num * train_ratio)
    split_dfs["train"] = df.iloc[:length1]

    length2 = int(instance_num * (train_ratio + val_ratio))
    split_dfs["valid"] = df.iloc[length1:length2]
    split_dfs["test"] = df.iloc[length2:]

    filter_review_id_list = get_filter_review_id_list(
        filter_review_id_list_paths=filter_review_id_list_paths,
    )
    label_conv_review_id_list = get_label_conv_review_id_list(
        label_conv_review_id_list_paths=label_conv_review_id_list_paths,
    )

    for eval_type in ("valid", "test"):
        if filter_review_id_list.get(eval_type):
            df = split_dfs[eval_type]
            df = df[~df["review_id"].isin(filter_review_id_list[eval_type])]
            split_dfs[eval_type] = df

    for eval_type in ("valid", "test"):
        if label_conv_review_id_list.get(eval_type):
            df = split_dfs[eval_type]
            df = df.assign(converted_label=df["review_id"].map(label_conv_review_id_list["valid"]))
            df = df.assign(
                label=df[["label", "converted_label"]].apply(
                    lambda xs: xs["label"] if pd.isnull(xs["converted_label"]) else xs["converted_label"],
                    axis=1,
                )
            )
            df = df.drop(columns=["converted_label"])
            split_dfs[eval_type] = df

    return {
        "train": split_dfs["train"],
        "valid": split_dfs["valid"],
    }


def preprocess_marc_ja(
    config: MarcJaConfig,
    data_file_path: str,
    filter_review_id_list_paths: Dict[str, str],
    label_conv_review_id_list_paths: Dict[str, str],
) -> Dict[str, pd.DataFrame]:
    try:
        import mojimoji

        def han_to_zen(text: str) -> str:
            return mojimoji.han_to_zen(text)

    except ImportError:
        warnings.warn(
            "can't import `mojimoji`, failing back to method that do nothing. "
            "We recommend running `pip install mojimoji` to reproduce the original preprocessing.",
            UserWarning,
        )

        def han_to_zen(text: str) -> str:
            return text

    try:
        from bs4 import BeautifulSoup

        def cleanup_text(text: str) -> str:
            return BeautifulSoup(text, "html.parser").get_text()

    except ImportError:
        warnings.warn(
            "can't import `beautifulsoup4`, failing back to method that do nothing."
            "We recommend running `pip install beautifulsoup4` to reproduce the original preprocessing.",
            UserWarning,
        )

        def cleanup_text(text: str) -> str:
            return text

    from tqdm import tqdm

    df = pd.read_csv(data_file_path, delimiter="\t")
    df = df[["review_body", "star_rating", "review_id"]]

    # rename columns
    df = df.rename(columns={"review_body": "text", "star_rating": "rating"})

    # convert the rating to label
    tqdm.pandas(dynamic_ncols=True, desc="Convert the rating to the label")
    df = df.assign(label=df["rating"].progress_apply(lambda rating: get_label(rating, config.remove_netural)))

    # remove rows where the label is None
    df = df[~df["label"].isnull()]

    # remove html tags from the text
    tqdm.pandas(dynamic_ncols=True, desc="Remove html tags from the text")
    df = df.assign(text=df["text"].progress_apply(cleanup_text))

    # filter by ascii rate
    tqdm.pandas(dynamic_ncols=True, desc="Filter by ascii rate")
    df = df[~df["text"].progress_apply(is_filtered_by_ascii_rate)]

    if config.max_char_length is not None:
        df = df[df["text"].str.len() <= config.max_char_length]

    if config.is_han_to_zen:
        df = df.assign(text=df["text"].apply(han_to_zen))

    df = df[["text", "label", "review_id"]]
    df = df.rename(columns={"text": "sentence"})

    # shuffle dataset
    df = shuffle_dataframe(df)

    split_dfs = output_data(
        df=df,
        train_ratio=config.train_ratio,
        val_ratio=config.val_ratio,
        test_ratio=config.test_ratio,
        output_testset=config.output_testset,
        filter_review_id_list_paths=filter_review_id_list_paths,
        label_conv_review_id_list_paths=label_conv_review_id_list_paths,
    )
    return split_dfs