forked from alessiodipasquale/Wikidata_WLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountSomevaluesAsserted.py
86 lines (71 loc) · 2.58 KB
/
countSomevaluesAsserted.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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')
errors = 0
directory = './'
normalRankCount = 0
preferredRankCount = 0
deprecatedRankCount = 0
claimsTotalNumber = 0
totalStatements = 0
noResponseCounter = 0
def isAnotherNormal(this,list):
for elem in list:
if(elem['rank'] == 'normal' and elem != this):
return True
def isAnotherDeprecated(this,list):
for elem in list:
if(elem['rank'] == 'deprecated' and elem != this):
return True
def isAnotherPreferred(this,list):
for elem in list:
if(elem['rank'] == 'preferred' and elem != this):
return True
notAssertedCount = 0
assertedCount = 0
claimsTotalNumber = 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):
claimsTotalNumber = 0
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]
for elem in statements:
claimsTotalNumber +=1
totalStatements += 1
mainsnak = elem['mainsnak']
if elem['rank'] == 'normal' and mainsnak['snaktype']=='somevalue':
if (isAnotherPreferred(elem, statements)):
notAssertedCount+=1
else:
assertedCount+=1
if elem['rank'] == 'preferred' and mainsnak['snaktype']=='somevalue':
assertedCount+=1
if elem['rank'] == 'deprecated' and mainsnak['snaktype']=='somevalue':
notAssertedCount+=1
pbar.update(1)
except KeyError:
print('Key Error')
errors += 1
except JSONDecodeError as err:
print('Json Decode Error')
errors += 1
outString = {
'asserted': assertedCount,
'notAsserted': notAssertedCount,
'count':totalStatements
}
json_string = json.dumps(outString)
with open(os.getenv('OUTPUT_DIR')+'/somevaluesAsserted.json','w') as output:
output.write(json_string)
print("Errors: "+str(errors))