forked from alessiodipasquale/Wikidata_WLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountProperties.py
36 lines (30 loc) · 1.19 KB
/
countProperties.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
import os
from tqdm import tqdm
from dotenv import load_dotenv
load_dotenv()
dir_path = os.getenv('INPUT_DIR')
properties = {}
pbar = tqdm(total=len([entry for entry in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, entry))]))
for file in os.listdir(dir_path):
try:
with open(dir_path+file,'r') as f:
data = json.load(f)
entities = data['entities']
for key in entities:
el = entities[key]
for claimsId in el['claims']:
statements = el['claims'][claimsId]
if(claimsId not in properties.keys()):
toChange = {claimsId: len(statements)}
properties.update(toChange)
else:
toChange = {claimsId: properties.get(claimsId)+len(statements)}
properties.update(toChange)
except KeyError:
print('Key Error')
except json.JSONDecodeError as err:
print('Json Decode Error')
pbar.update(1)
with open(os.getenv('OUTPUT_DIR')+'/propertiesCount.json','w') as output:
output.write(json.dumps(properties, indent = 4))