This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
commentReintegration.py
317 lines (294 loc) · 18.6 KB
/
commentReintegration.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import os
import sys
import re
import difflib
def is_unfinished_str(term):
return term.count('"') % 2 == 1
def remove_empty_str(lis):
new_list = []
for i in lis:
if len(i) > 0:
new_list.append(i)
return new_list
def term_present(sentence):
if 'Grammar' in sentence or 'Entry:' in sentence or 'Element:' in sentence or 'Abstract:' in sentence or 'Group' in sentence:
return True
return False
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def keyTerms(sentence):
sentence = sentence.replace('Coding', 'concept')
sentence = sentence.replace('CodeableConcept', 'concept')
sentence = sentence.replace('EntryElement:', 'Entry:')
sentence = sentence.replace('Abstract Element:', 'Abstract:')
sentence = sentence.replace('Based on:', 'Parent:')
sentence = sentence.replace('value is type', 'only')
sentence = sentence.replace('is type', 'substitute')
sentence = sentence.replace('DataElement 5.0', 'DataElement 6.0')
while(sentence.find('ref(') != -1):
m = sentence.find('ref(')
index = m + 4
term = ''
while sentence[index] != ")":
term += sentence[index]
index += 1
sentence = sentence[:m] + term + sentence[index + 1:]
if "should be from" in sentence:
new_sentence = sentence.split(" should be from ")
term = new_sentence[0] + " from " + new_sentence[1] + " (preferred)"
sentence = term[:]
elif "could be from" in sentence:
new_sentence = sentence.split("could be from")
term = new_sentence[0] + "from " + new_sentence[1] + " (example)"
sentence = term[:]
elif "from" in sentence and "if covered" in sentence:
sentence = sentence.replace("if covered", "(extensible)")
elif "from" in sentence and 'VS' in sentence:
sentence = sentence + " (required)"
return sentence
if __name__ == "__main__":
path_name = sys.argv[1]
cimpl_6_path = sys.argv[2]
comment_hash = dict()
replacements = dict()
file_elements = dict()
#Get all of the lines associated with a particular namespace.
for file in files(path_name):
if '.txt' in file and not '_vs' in file and not '_map' in file and not '.html' in file and not '.json' in file and not 'ig-' in file:
file_name = path_name + file
comment_hash[file] = []
with open(file_name, 'r') as f:
for line in f:
comment_hash[file].append(line)
particular_comments = []
file_elements[file] = dict()
current_element = 'DataElement 6.0'
file_elements[file]['DataElement 6.0'] = []
next_line = ''
line = 0
while line < len(comment_hash[file]) - 1:
if '//' in comment_hash[file][line] and not '://' in comment_hash[file][line]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][line])]
extraction = slash_indices[0]
text = comment_hash[file][line][:extraction]
comment = comment_hash[file][line][extraction:]
if len(text.rstrip().strip()) > 0:
if is_unfinished_str(text):
for s in range(line -1, -1,-1):
if is_unfinished_str(comment_hash[file][s]):
next_line = comment_hash[file][s]
break
else:
next_line = text
else:
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][l][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
#next_line = comment_hash[file][line + 1]
if 'Element:' in next_line:
current_element = next_line.split(":")[1].rstrip().strip()
file_elements[file][current_element] = []
file_elements[file][current_element].append((next_line, comment))
elif '//' in comment_hash[file][line] and '://' in comment_hash[file][line]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][line])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][line])])
if len(http_indices) == len(slash_indices):
#next_line = comment_hash[file][line + 1]
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][line][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
else:
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
text = comment_hash[file][line][:slash_indices[i]]
comment = comment_hash[file][line][slash_indices[i]:]
if len(text.rstrip().strip()) > 0:
next_line = text
else:
#next_line = comment_hash[file][line + 1]
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][line][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
if 'Element:' in next_line:
current_element = prev_line.split(":")[1].rstrip().strip()
if not current_element in file_elements[file]:
file_elements[file][current_element] = []
file_elements[file][current_element].append((next_line, comment))
break
elif '/*' in comment_hash[file][line]:
long_comment = comment_hash[file][line]
while not '*/' in comment_hash[file][line]:
line += 1
long_comment += comment_hash[file][line]
if line == len(comment_hash[file]) - 1:
next_line = '\n'
else:
#next_line = comment_hash[file][line + 1]
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][l][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
file_elements[file][current_element].append((next_line, long_comment))
elif 'Element:' in comment_hash[file][line]:
current_element = comment_hash[file][line].split(":")[1].rstrip().strip()
if not current_element in file_elements[file]:
file_elements[file][current_element] = []
next_line = comment_hash[file][line + 1]
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][line][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
else:
if len(comment_hash[file][line].strip()) > 0:
for l in range(line + 1, len(comment_hash[file])):
if not comment_hash[file][l].rstrip().strip().startswith('//'):
next_line = comment_hash[file][l]
if '//' in comment_hash[file][l] and not '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
extraction = slash_indices[0]
next_line = comment_hash[file][l][:extraction]
elif '//' in comment_hash[file][l] and '://' in comment_hash[file][l]:
slash_indices = [m.start() for m in re.finditer('//', comment_hash[file][l])]
http_indices = set([m.start() for m in re.finditer('://', comment_hash[file][l])])
if len(http_indices) != len(slash_indices):
for i in range(0, len(slash_indices)):
if not slash_indices[i] - 1 in http_indices:
next_line = comment_hash[file][l][:slash_indices[i]]
break
break
#next_line = comment_hash[file][line + 1]
line += 1
output_lines = []
new_cimpl_hash = dict()
for file in files(cimpl_6_path):
if '.txt' in file and not '_vs' in file and not '_map' in file and not '.html' in file and not '.json' in file and not 'ig-' in file:
file_name = cimpl_6_path + file
with open(file_name, 'r') as f:
all_lines = []
namespace = ''
new_cimpl_hash[file] = []
for line in f:
new_cimpl_hash[file].append(line)
output_dir = 'CommentReintegration/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
added_comment_hash = dict()
for k in new_cimpl_hash:
all_terms = []
current_term = ''
comments = []
added_comment_hash[k] = dict()
for a in range(0, len(new_cimpl_hash[k])):
if a == 0:
initial_comments = file_elements[k]['DataElement 6.0']
for c in range(0, len(initial_comments)):
if len(initial_comments[c][0]) == 0:
all_terms.append(initial_comments[c][1])
if 'Grammar:' in new_cimpl_hash[k][a] or 'Entry:' in new_cimpl_hash[k][a] or 'Element:' in new_cimpl_hash[k][a] or 'Abstract:' in new_cimpl_hash[k][a] or 'Group:' in new_cimpl_hash[k][a]:
if len(current_term) > 0:
for t in range(0, len(file_elements[k][current_term])):
if not t in added_comment_hash[k][current_term]:
if not file_elements[k][current_term][t][1].strip().startswith('/*'):
all_terms.append((' '*19) + file_elements[k][current_term][t][1])
else:
all_terms.append(file_elements[k][current_term][t][1])
current_term = new_cimpl_hash[k][a].split(":")[1].rstrip().strip()
added_comment_hash[k][current_term] = []
if current_term in file_elements[k]:
comments = file_elements[k][current_term]
if len(new_cimpl_hash[k][a].rstrip().strip()) > 0:
added_comments = []
for b in range(0, len(comments)):
if len(comments[b][0]) == 0 or comments[b][0] == '\n':
continue
if b in added_comment_hash[k][current_term]:
continue
five_line = set(remove_empty_str(keyTerms(comments[b][0]).rstrip().strip().replace('\t',' ').split(" ")))
six_line = set(remove_empty_str(new_cimpl_hash[k][a].rstrip().strip().replace('\t','').split(" ")))
new_five_set = five_line - six_line
new_six_set = six_line - five_line
if len(new_five_set) <= 1 and len(new_six_set) <= 1:
if not comments[b][1].strip().startswith('/*'):
all_terms.append((' '*19) + comments[b][1])
else:
all_terms.append(comments[b][1])
added_comment_hash[k][current_term].append(b)
all_terms.append(new_cimpl_hash[k][a])
if a == len(new_cimpl_hash[k]) - 1:
if len(current_term) > 0:
for t in range(0, len(file_elements[k][current_term])):
if not t in added_comment_hash[k][current_term]:
if not file_elements[k][current_term][t][1].strip().startswith('/*'):
all_terms.append((' '*19) + file_elements[k][current_term][t][1])
else:
all_terms.append(file_elements[k][current_term][t][1])
file_name = output_dir + k
a = open(file_name, 'w')
for t in all_terms:
a.write(t)
a.close()