File size: 1,900 Bytes
d78207e |
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 |
import polars as pl
import sys
import json
from tqdm import tqdm
labelmap = {"E": 1.0, "S": 0.1, "C": 0.01, "I": 0.0}
split = sys.argv[3]
products = (
pl.read_parquet(sys.argv[1])
.filter((pl.col("product_locale") == "us"))
.with_columns(
pl.concat_str(
[
pl.col("product_title"),
pl.col("product_description"),
pl.col("product_bullet_point"),
pl.col("product_brand"),
pl.col("product_color"),
],
separator=" ",
ignore_nulls=True,
).alias("text")
)
.select(pl.col("product_id", "text"))
)
examples = (
pl.read_parquet(sys.argv[2])
.filter(
(pl.col("product_locale") == "us")
& (pl.col("small_version") == 1)
& (pl.col("split") == split)
)
.with_columns(
pl.col("esci_label").replace(labelmap).alias("score").cast(pl.Float64)
)
.select(pl.col("query_id", "query", "product_id", "score"))
)
merged = examples.join(products, on="product_id", how="left")
print(merged)
result = merged.group_by("query_id").agg(
pl.first("query"), pl.col("text"), pl.col("score")
)
def save_json(df: pl.DataFrame, path: str):
with open(path, "w") as f:
for row in tqdm(result.to_dicts(), desc=f"saving {path}"):
query = row["query"]
pos = []
neg = []
negscore = []
for doc, score in zip(row["text"], row["score"]):
if score == 1.0:
pos.append(doc)
else:
neg.append(doc)
negscore.append(score)
for p in pos:
line = json.dumps(
{"query": query, "pos": p, "neg": neg, "negscore": negscore}
)
f.write(line + "\n")
save_json(result, f"{split}.jsonl")
|