forked from alessiodipasquale/Wikidata_WLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountRanking.py
58 lines (52 loc) · 1.79 KB
/
countRanking.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
51
52
53
54
55
56
57
58
from html import entities
import json
import os
from json.decoder import JSONDecodeError
from tqdm import tqdm
from dotenv import load_dotenv
load_dotenv()
dir_path = os.getenv('INPUT_DIR')
directory = './'
normalRankCount = 0
preferredRankCount = 0
deprecatedRankCount = 0
claimsTotalNumber = 0
totalStatements = 0
errorCounter = 0
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']:
statement = el['claims'][claimsId]
for elem in statement:
claimsTotalNumber +=1
totalStatements += 1
if elem['rank'] == 'normal':
normalRankCount +=1
if elem['rank'] == 'preferred':
preferredRankCount +=1
if elem['rank'] == 'deprecated':
deprecatedRankCount +=1
pbar.update(1)
except KeyError:
print('KeyError')
errorCounter+=1
except JSONDecodeError as err:
print('JSONDecodeError')
errorCounter+=1
outString = {
'normal': normalRankCount,
'preferred': preferredRankCount,
'deprecated':deprecatedRankCount,
'count':claimsTotalNumber
}
json_string = json.dumps(outString)
with open(os.getenv('OUTPUT_DIR')+'/ranking.json','w') as output:
output.write(json_string)
print('Total Statements: '+str(totalStatements))
print('Errors: '+str(errorCounter))