-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_making.py
131 lines (108 loc) · 4.45 KB
/
json_making.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 17:22:04 2019
@author: 726094
"""
import json
import pandas as pd
import re
import os
from fuzzywuzzy import fuzz
def getjsonFile():
# citation_raw_file_folder = 'C:/Users/726094/Desktop/analysis/json_building/'
citation_ml_output_csv = 'outputs/result_citation.csv'
input_path = 'input/'
fn = citation_ml_output_csv
data = pd.read_csv(fn)
print('')
print('Data dimensions: ',data.shape)
filenames = data['FileName'].unique()
citationsar = []
json_obj = {}
json_object = []
for ii in filenames:
print(ii)
citations = data.loc[data['FileName'] == ii]['Citation']
citationsar.extend(citations)
start_and_len = []
snl = {}
ctr = 00
for i in range(len(citationsar)):
fname = data.iloc[i]['FileName']
fname = fname.replace('xml.xml', 'txt')
myfile = open(input_path + fname).read()
# myfile = re.sub(r'[^\x00-\x7F]+|\x0c',' ', myfile) # remove all non-XML-compatible characters
# citationsar[i] = re.sub(r'[^\x00-\x7F]+|\x0c',' ', citationsar[i])
startid = myfile.find(citationsar[i], 0, len(myfile))
length = len(citationsar[i])
if(startid != -1 and length < 150):
snl = {
'start_id': startid,
'len': length,
'citation': citationsar[i],
'filename': data.iloc[i]['FileName'],
'subtype': data.iloc[i]['SubType'],
'anaphoric': {
'status': data.iloc[i]['Anaphoric'],
'co-ref': str(data.iloc[i]['CoReference'])
}
}
if(i == 0):
start_and_len.append(snl)
else:
fuzzr = 0
for obj in range(len(start_and_len)):
fuzzr = (fuzz.ratio(snl['citation'],start_and_len[obj]['citation']))
if fuzzr > 90:
break
if fuzzr < 90:
start_and_len.append(snl)
else:
# print('Duplicate')
strd = (start_and_len[len(start_and_len)-1]['start_id']+20)
startid = myfile.find(citationsar[i], strd, len(myfile)) # Searching for the string after the last valid citation
# print(citationsar[i])
# print("Dup: ",startid)
snl = {
'start_id': startid,
'len': length,
'citation': citationsar[i],
'filename': data.iloc[i]['FileName'],
'subtype': data.iloc[i]['SubType'],
'anaphoric': {
'status': data.iloc[i]['Anaphoric'],
'co-ref': str(data.iloc[i]['CoReference'])
}
}
if(startid != -1):
start_and_len.append(snl)
else:
data.drop(i,inplace=False)
i=i+1
for i in range(len(start_and_len)):
# print(start_and_len[i]['start_id'], start_and_len[i]['len'])
# print(start_and_len[i]['citation'])
json_obj = {
"filename": start_and_len[i]['filename'],
"subtype": start_and_len[i]['subtype'],
"citation_id": ctr,
"citation_textstartid": start_and_len[i]['start_id'],
"citation_textlength": start_and_len[i]['len'],
"anaphoric": {
"status": start_and_len[i]['anaphoric']['status'],
"co-ref": start_and_len[i]['anaphoric']['co-ref']
},
"name": start_and_len[i]['citation']
}
ctr+=1
# print(json_obj)
json_object.append(json_obj)
# json_object.remove({})
final = {
"citation": json_object
}
# fnm = fn.replace('.csv','_')
with open('outputs/result_citation_data.json', 'w') as outfile:
json.dump(final, outfile)
print('Output file: result_citation_data.json')
print("Intermediate file - JSON created successfully")