-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
50 lines (41 loc) · 1.3 KB
/
data.py
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
import os
import json
import argparse
import pandas as pd
def parse(language):
"""
Parse the data from a JSONL file for a given language and convert it into a CSV file.
Args:
language (str): Language for which the data needs to be parsed.
Returns:
None
Raises:
FileNotFoundError: If the data file for the given language is not found.
"""
file_path = f"data/{language}.jsonl"
if not os.path.isfile(file_path):
raise FileNotFoundError(f"Missing data file: {file_path}")
dataset = dict(
id=list(),
tweet=list()
)
raw_tweets_file = open(file_path, "r")
json_data = [json.loads(line) for line in raw_tweets_file]
for data in json_data:
data = data["data"]
for datapoint in data:
dataset["id"] += [datapoint["id"]]
dataset["tweet"] += [datapoint["text"]]
csv_file = f"data/{language}.csv"
pd.DataFrame.from_dict(dataset).to_csv(csv_file, index=False)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--language",
type=str,
default="IT",
choices=["IT", "EN", "DE", "FR", "ES", "PT", "NO"],
help="The language of the experiment"
)
args = parser.parse_args()
parse(args.language)