-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_io.py
36 lines (29 loc) · 919 Bytes
/
file_io.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
import json
from pathlib import Path
from typing import List, Dict, Union
import pandas as pd
def read_lm_kbc_jsonl(file_path: Union[str, Path]) -> List[Dict]:
"""
Reads a LM-KBC jsonl file and returns a list of dictionaries.
Args:
file_path: path to the jsonl file
Returns:
list of dictionaries, each possibly has the following keys:
- "SubjectEntity": str
- "Relation": str
- "ObjectEntities":
None or List[List[str]] (can be omitted for the test input)
"""
rows = []
with open(file_path, "r") as f:
for line in f:
row = json.loads(line)
rows.append(row)
return rows
def read_lm_kbc_jsonl_to_df(file_path: Union[str, Path]) -> pd.DataFrame:
"""
Reads a LM-KBC jsonl file and returns a dataframe.
"""
rows = read_lm_kbc_jsonl(file_path)
df = pd.DataFrame(rows)
return df