-
-
Save anishthite/8a7db95ae62166d9d0f6e31113e6744a to your computer and use it in GitHub Desktop.
profanity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import lm_dataformat as lmd | |
| from glob import glob | |
| import os | |
| import json | |
| import collections | |
| from tqdm import tqdm | |
| import re | |
| from best_download import download_file | |
| import fasttext | |
| import zstandard | |
| import multiprocessing as mp | |
| from profanity_check import predict | |
| in_path = 'pile' | |
| out_path = 'pile_analysis' | |
| def profanity(doc): | |
| words = re.split(r'\s+', doc) | |
| pred = predict(words) | |
| return { | |
| 'num_profane': sum(pred), | |
| 'num_ngrams': len(pred), | |
| 'num_bytes': len(doc.encode('utf-8')) | |
| } | |
| def writef(f, lines): | |
| with open(f, 'wb') as fh: | |
| cctx = zstandard.ZstdCompressor(level=3, threads=8) | |
| compressor = cctx.stream_writer(fh) | |
| for line in tqdm(lines): | |
| compressor.write(line) | |
| compressor.flush(zstandard.FLUSH_FRAME) | |
| def analyze(ob): | |
| doc, meta = ob | |
| res = { | |
| 'pile_set_name': meta['pile_set_name'] | |
| } | |
| for metric in metrics: | |
| res = {**res, **metric(doc)} | |
| return json.dumps(res).encode('utf-8') | |
| metrics = [ | |
| profanity | |
| ] | |
| pool = mp.Pool(24) | |
| for f in tqdm(sorted(glob(in_path + '/*'))): | |
| if os.path.exists(out_path + '/analysis_' + f.split('/')[-1]): continue | |
| def meta_items(): | |
| rdr = lmd.Reader(f) | |
| return pool.imap(analyze, rdr.stream_data(get_meta=True)) | |
| writef(out_path + '/tmp_analysis_' + f.split('/')[-1], meta_items()) | |
| os.rename(out_path + '/tmp_analysis_' + f.split('/')[-1], out_path + '/analysis_' + f.split('/')[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment