rrutmann commited on
Commit
8c0a27e
·
verified ·
1 Parent(s): 12b6e90

Upload merge_texts_and_scores.py

Browse files
Files changed (1) hide show
  1. merge_texts_and_scores.py +56 -0
merge_texts_and_scores.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import json
3
+ from pathlib import Path
4
+
5
+ import pandas as pd
6
+
7
+
8
+ def read_jsonl_into_df(
9
+ path_to_jsonl: Path,
10
+ ) -> pd.DataFrame:
11
+ """
12
+ Read a JSONL file into a pandas DataFrame.
13
+
14
+ Args:
15
+ path_to_jsonl (Path): Path to the JSONL file.
16
+
17
+ Returns:
18
+ pd.DataFrame: DataFrame containing the data from the JSONL file.
19
+ """
20
+ with open(path_to_jsonl, "r", encoding="utf-8") as f:
21
+ lines = [json.loads(line) for line in f if line.strip() if line != "\n"]
22
+ return pd.DataFrame(lines)
23
+
24
+
25
+ def merge_texts_and_scores(
26
+ path_to_text_jsonl: Path,
27
+ path_to_scores_jsonl: Path,
28
+ path_to_output_jsonl: Path,
29
+ ) -> None:
30
+ """
31
+ Merge texts and scores from two JSONL files into a single JSONL file.
32
+
33
+ Args:
34
+ path_to_text_jsonl (Path): Path to the JSONL file containing texts.
35
+ path_to_scores_jsonl (Path): Path to the JSONL file containing scores.
36
+ path_to_output_jsonl (Path): Path to the output JSONL file.
37
+ """
38
+ texts_df = read_jsonl_into_df(path_to_text_jsonl)
39
+ scores_df = read_jsonl_into_df(path_to_scores_jsonl)
40
+
41
+ # Merge the DataFrames on the "id" column
42
+ merged_df = pd.merge(texts_df, scores_df, on="id", how="inner")
43
+
44
+ # Write the merged DataFrame to the output JSONL file
45
+ with open(path_to_output_jsonl, "w", encoding="utf-8") as output_file:
46
+ for _, row in merged_df.iterrows():
47
+ output_file.write(json.dumps(row.to_dict()) + "\n")
48
+
49
+
50
+ if __name__ == "__main__":
51
+ # Example usage
52
+ merge_texts_and_scores(
53
+ path_to_text_jsonl=Path("/path/to/fineweb_2_500k_both_deduplicated/mlt_Latn_sampled_500k.jsonl"),
54
+ path_to_scores_jsonl=Path("/path/to/annotations/adult_content/mlt_Latn_sampled_500k_gemma-3-27b-it_aggregated_scores_majority.jsonl"),
55
+ path_to_output_jsonl=Path("/path/to/output_dir/mlt_Latn_sampled_500k_gemma-3-27b-it_aggregated_scores_majority_merged.jsonl")
56
+ )