donfu
commited on
Commit
•
1283c69
1
Parent(s):
7f84d9c
Add trim and combine script
Browse files- combine.py +32 -0
- stackexchange.parquet +3 -0
combine.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# Combine (and shorten) parquet files into a single file
|
3 |
+
|
4 |
+
import os, glob, sys, re
|
5 |
+
import pandas as pd
|
6 |
+
from merge_parquets import merge_parquet_dir
|
7 |
+
|
8 |
+
MAX_LENGTH = 1000 # max length of question or answer
|
9 |
+
|
10 |
+
for file in glob.glob("full/*.parquet"):
|
11 |
+
df = pd.read_parquet(file)
|
12 |
+
before = len(df)
|
13 |
+
df = df[df["INSTRUCTION"].str.len() < MAX_LENGTH]
|
14 |
+
df = df[df["RESPONSE"].str.len() < MAX_LENGTH]
|
15 |
+
df["METADATA"] = df["METADATA"].apply(
|
16 |
+
lambda meta: {
|
17 |
+
"tags": meta["tags"],
|
18 |
+
"answer_score": int(meta["answer_score"])
|
19 |
+
if "answer_score" in meta and meta["answer_score"]
|
20 |
+
else 0,
|
21 |
+
"question_score": int(meta["question_score"])
|
22 |
+
if "question_score" in meta and meta["question_score"]
|
23 |
+
else 0,
|
24 |
+
}
|
25 |
+
)
|
26 |
+
df.to_parquet(file)
|
27 |
+
after = len(df)
|
28 |
+
print(
|
29 |
+
f"Shortened {file} from {before} to {after} rows ({100 * after / before:.2f})"
|
30 |
+
)
|
31 |
+
|
32 |
+
merge_parquet_dir("full", "stackexchange.parquet")
|
stackexchange.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:145bf165604c0a58bfc16688c90e898bedaeb4878afbb60361bcc3cc579405ae
|
3 |
+
size 3713863154
|