Skip to main content

Automatic data filtering library specialized in Korean data purification

Project description

Purism: Automatic data filtering library specialized in Korean data purification

Purify system

View in Korean

Summary

This repository is an automatic data filtering library specialized in Korean data purification.

How to use

Installation

Installation using pip

pip install purism

Quickstart

C4 dataset has "Clean" in its name, but it is NOT clean at all (especially the Korean subset). This is a code that performs additional filtering on the C4 dataset using this library.

from datasets import load_dataset
from collections import Counter
from tqdm.auto import tqdm
from purism import PurifyConfig, UnicodeCleaner, UICleaner, TextCleaner, LengthFilter, HarmfulWordsFilter, SpamWordsFilter, SignAbuseFilter, PIIFilter, LanguageFilter, DedupFilter

take_count = 40000
batch_size = 64

print("=" * 100)
ds = load_dataset(
    "allenai/c4",
    "ko",
    split="train",
    streaming=True
).take(take_count)

ds_sample = []

for text in tqdm(ds, desc="Extracting texts", total=take_count):
    ds_sample.append(text["text"])

normalizers = [
    UnicodeCleaner("NFC"),
    UICleaner(),
    TextCleaner()
]

multi_filters = [
    LengthFilter(),
    HarmfulWordsFilter(),
    SpamWordsFilter(),
    SignAbuseFilter(),
    PIIFilter()
]

batch_filters = [
    LanguageFilter(),
    DedupFilter()
]

counter = Counter()
filtered_all = 0
n_passed = 0
passed = []
n_filtered = 0
filtered = []
reason = []
purifier = PurifyConfig(normalizers, multi_filters, batch_filters, batch_size)

print("=" * 100)
result = purifier.parallel_purify(ds_sample, -1)

for text in result:
    if text["passed"]:
        counter["Passed"] += 1
        if n_passed < 10:
            passed.append(text["text"])
            n_passed += 1
    else:
        filtered_all += 1
        counter[text["filtered_by"]] += 1
        if n_filtered < 10:
            filtered.append(text["text"])
            reason.append(text["filtered_by"])
            n_filtered += 1

print("=" * 100)
print("Purification complete!")
print("=" * 100)
print("<|Filtering statistics|>")
print(" ")
print(f"Passed: {counter["Passed"]:,} ({counter["Passed"] / take_count * 100:.3f}%)")
print(f"LengthFilter: {counter["LengthFilter"]:,} ({counter["LengthFilter"] / take_count * 100:.3f}%)")
print(f"HarmfulWordsFilter: {counter["HarmfulWordsFilter"]:,} ({counter["HarmfulWordsFilter"] / take_count * 100:.3f}%)")
print(f"SpamWordsFilter: {counter["SpamWordsFilter"]:,} ({counter["SpamWordsFilter"] / take_count * 100:.3f}%)")
print(f"SignAbuseFilter: {counter["SignAbuseFilter"]:,} ({counter["SignAbuseFilter"] / take_count * 100:.3f}%)")
print(f"PIIFilter: {counter["PIIFilter"]:,} ({counter["PIIFilter"] / take_count * 100:.3f}%)")
print(f"LanguageFilter: {counter["LanguageFilter"]:,} ({counter["LanguageFilter"] / take_count * 100:.3f}%)")
print(f"DedupFilter: {counter["DedupFilter"]:,} ({counter["DedupFilter"] / take_count * 100:.3f}%)")
print(f"Total number of filtered texts: {filtered_all:,} ({filtered_all / take_count * 100:.3f}%)")
print("=" * 100)
print("<|Passed Samples|>")
print(" ")

for i, text in enumerate(passed):
    print("=" * 100)
    print(f"Sample {i + 1}")
    print(f"{text[i]}")

print("=" * 100)
print("<|Filtered Samples|>")
print(" ")

for i, text in enumerate(filtered):
    print("=" * 100)
    print(f"Sample {i + 1} (Filtered by {reason[i]})")
    print(f"{text[i]}")

print("=" * 100)

If you look at the results after running this code, you can see that there are many filtered texts.

API

More features can be found on this page.

Limitations

  • This library can accurately filter only Korean text. Modification of the source code is required to use other languages.
  • This library is not always accurate. It can filter out non-harmful text, but may fail to filter out some harmful text.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

purism-2.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

purism-2.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file purism-2.1.0.tar.gz.

File metadata

  • Download URL: purism-2.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for purism-2.1.0.tar.gz
Algorithm Hash digest
SHA256 5a84b19b44155c3104112b983a2b1ced7d75058698a2e190afee0266e945aa14
MD5 d2a7bed30404732f9ee04e34805522ac
BLAKE2b-256 d9f0495c5e63ddd7902f2a85a4d7d4db0ea53484ed6217f96b03cdaf0496b7e2

See more details on using hashes here.

File details

Details for the file purism-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: purism-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for purism-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3dfe4ccf84fab53cb9a21bfead2e17fdc5b8a76fba341e5e80329564af141c59
MD5 fb0b7c8a787674eb0cdaea8920291ed9
BLAKE2b-256 a21d3122ada23a02ee053c47f646764b5c5c3cd5c4651bc204494089e8a848ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page