-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseJson.py
More file actions
74 lines (59 loc) · 1.87 KB
/
parseJson.py
File metadata and controls
74 lines (59 loc) · 1.87 KB
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
70
71
72
73
74
import json
import os
from dataclasses import dataclass
from typing import List
import pandas as pd
from defs import etl_gliner # Add import for etl_gliner function
@dataclass
class NodeEntry:
filename: str
node_name: str
index: int
text: str
def parse_json_file(file_path: str) -> List[NodeEntry]:
"""
Parse a JSON file and extract structured information from it.
Args:
file_path: Path to the JSON file to parse
Returns:
List of NodeEntry objects containing the extracted information
"""
# Get the filename from the path
filename = os.path.basename(file_path)
# Read and parse the JSON file
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
results = []
# Iterate through each node (key) in the JSON
for node_name, entries in data.items():
# Skip if the value is not a list/array
if not isinstance(entries, list):
continue
# Process each entry in the node's array
for index, text in enumerate(entries):
entry = NodeEntry(
filename=filename,
node_name=node_name,
index=index,
text=text
)
results.append(entry)
return results
# Example usage
if __name__ == "__main__":
# Replace with your actual file path
file_path = "./secret/idea_2409.09239v1.json"
entries = parse_json_file(file_path)
for entry in entries:
print("-" * 80)
print(f"Text: {entry.text}")
results = etl_gliner.process(entry.text)
df = pd.DataFrame(results)
print(df)
# Print the parsed entries
# for entry in entries:
# print(f"File: {entry.filename}")
# print(f"Node: {entry.node_name}")
# print(f"Index: {entry.index}")
# print(f"Text: {entry.text}")
# print("-" * 80)