add Userwarning (#2)
Browse files
JGLUE.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
|
|
4 |
from typing import Dict, List, Optional, Union
|
5 |
|
6 |
import datasets as ds
|
@@ -327,8 +328,38 @@ def preprocess_for_marc_ja(
|
|
327 |
filter_review_id_list_paths: Dict[str, str],
|
328 |
label_conv_review_id_list_paths: Dict[str, str],
|
329 |
) -> Dict[str, pd.DataFrame]:
|
330 |
-
|
331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
from tqdm import tqdm
|
333 |
|
334 |
df = pd.read_csv(data_file_path, delimiter="\t")
|
@@ -350,11 +381,7 @@ def preprocess_for_marc_ja(
|
|
350 |
|
351 |
# remove html tags from the text
|
352 |
tqdm.pandas(dynamic_ncols=True, desc="Remove html tags from the text")
|
353 |
-
df = df.assign(
|
354 |
-
text=df["text"].progress_apply(
|
355 |
-
lambda text: BeautifulSoup(text, "html.parser").get_text()
|
356 |
-
)
|
357 |
-
)
|
358 |
|
359 |
# filter by ascii rate
|
360 |
tqdm.pandas(dynamic_ncols=True, desc="Filter by ascii rate")
|
@@ -364,7 +391,7 @@ def preprocess_for_marc_ja(
|
|
364 |
df = df[df["text"].str.len() <= config.max_char_length]
|
365 |
|
366 |
if config.is_han_to_zen:
|
367 |
-
df = df.assign(text=df["text"].apply(
|
368 |
|
369 |
df = df[["text", "label", "review_id"]]
|
370 |
df = df.rename(columns={"text": "sentence"})
|
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
4 |
+
import warnings
|
5 |
from typing import Dict, List, Optional, Union
|
6 |
|
7 |
import datasets as ds
|
|
|
328 |
filter_review_id_list_paths: Dict[str, str],
|
329 |
label_conv_review_id_list_paths: Dict[str, str],
|
330 |
) -> Dict[str, pd.DataFrame]:
|
331 |
+
try:
|
332 |
+
import mojimoji
|
333 |
+
|
334 |
+
def han_to_zen(text: str) -> str:
|
335 |
+
return mojimoji.han_to_zen(text)
|
336 |
+
|
337 |
+
except ImportError:
|
338 |
+
warnings.warn(
|
339 |
+
"can't import `mojimoji`, failing back to method that do nothing. "
|
340 |
+
"We recommend running `pip install mojimoji` to reproduce the original preprocessing.",
|
341 |
+
UserWarning,
|
342 |
+
)
|
343 |
+
|
344 |
+
def han_to_zen(text: str) -> str:
|
345 |
+
return text
|
346 |
+
|
347 |
+
try:
|
348 |
+
from bs4 import BeautifulSoup
|
349 |
+
|
350 |
+
def cleanup_text(text: str) -> str:
|
351 |
+
return BeautifulSoup(text, "html.parser").get_text()
|
352 |
+
|
353 |
+
except ImportError:
|
354 |
+
warnings.warn(
|
355 |
+
"can't import `beautifulsoup4`, failing back to method that do nothing."
|
356 |
+
"We recommend running `pip install beautifulsoup4` to reproduce the original preprocessing.",
|
357 |
+
UserWarning,
|
358 |
+
)
|
359 |
+
|
360 |
+
def cleanup_text(text: str) -> str:
|
361 |
+
return text
|
362 |
+
|
363 |
from tqdm import tqdm
|
364 |
|
365 |
df = pd.read_csv(data_file_path, delimiter="\t")
|
|
|
381 |
|
382 |
# remove html tags from the text
|
383 |
tqdm.pandas(dynamic_ncols=True, desc="Remove html tags from the text")
|
384 |
+
df = df.assign(text=df["text"].progress_apply(cleanup_text))
|
|
|
|
|
|
|
|
|
385 |
|
386 |
# filter by ascii rate
|
387 |
tqdm.pandas(dynamic_ncols=True, desc="Filter by ascii rate")
|
|
|
391 |
df = df[df["text"].str.len() <= config.max_char_length]
|
392 |
|
393 |
if config.is_han_to_zen:
|
394 |
+
df = df.assign(text=df["text"].apply(han_to_zen))
|
395 |
|
396 |
df = df[["text", "label", "review_id"]]
|
397 |
df = df.rename(columns={"text": "sentence"})
|