forked from mdrumond/pc-chair-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconflict.py
242 lines (190 loc) · 6.79 KB
/
conflict.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
from base import Person, Institutions
import re
def parse_line(line):
"""
Line formats supported:
name [-;, ] [(]institution[)] [-;, ] reason (promptly ignored)
"""
original_line = line
blacklist = ['institution', 'advisor', 'other', 'students', 'coauthors', 'conflict',
'collaborators', 'coi', 'paper', 'institutional', 'student', 'co-authors']
institution_indication = ['university', 'institute', 'college']
line = line.strip('"')
name_regex = "\s*\w+\-?'?\w*\.?\&?\s*"
g = re.match("\s*\d*\s*\-?\.?(.*)", line)
line = g.group(1)
g = re.match(name_regex, line, re.IGNORECASE)
names = ""
# Match all names
while g:
s, e = g.span()
name = line[s:e].strip()
if name.lower() in blacklist:
raise ValueError("Bad name in string: %s, line: %s" % (name, original_line))
names += name + " "
line = line[e:]
g = re.match(name_regex, line)
if names.strip().lower() == 'none':
return None, None
if re.match("\s*[,;:\-\(]", line, re.IGNORECASE):
# This is a name, make sure there is no warning:
#for i in institution_indication:
# if i.lower() in names.lower():
# raise ValueError("Conflict looks like university: %s, line: %s" % (name, original_line))
return names.strip(), None
else:
return None, names.strip()
raise ValueError("Can't parse line: %s" % original_line)
class ConflictSet(object):
def __init__(self, value=None):
self._d = []
self._reasons = []
if value:
self._d.append(value)
self._reasons.append("")
def __contains__(self, item):
for i in self._d:
if i.match(item):
return True
return False
def match(self, item):
for i in self._d:
if i.match(item):
return "%s" % (str(item))
return False
def intersects_with(self, other):
intersection = ConflictSet()
intersection_str = []
for i in self._d:
if i in other:
intersection.add(i)
intersection_str.append(other.match(i))
return zip(intersection, intersection_str)
def merge(self, other):
for c, r in zip(other._d, other._reasons):
self.add(c, r)
def add(self, item, reason=""):
if not item:
return
if item not in self:
self._d.append(item)
self._reasons.append(reason)
def __iter__(self):
for d in self._d:
yield d
def __str__(self):
if not self._d:
return ""
s = ""
for i,r in zip(self._d, self._reasons):
if r:
s += ("- %s; %s\n" % (str(i), r))
else:
s += ("- %s\n" % str(i))
return s
def str_no_linebreaks(self):
if not self._d:
return ""
s = ""
for i, r in zip(self._d, self._reasons):
# if r:
# s += ("(%s: %s);" % (str(i), r))
# else:
s += ("(%s);" % str(i))
return s
def __bool__(self):
return True if self._d else False
class BaseConflicts(object):
def __init__(self, institution, collaborators_str=""):
""" Parse collaborators string """
self.collabs = ConflictSet()
self.institutions = ConflictSet()
self.insts = institution
self.bad_data = False
lines = collaborators_str.strip().splitlines()
for line in lines:
name, institution = parse_line(line)
if institution:
self.add_institution(institution)
elif name:
self.add_co_author(name)
def __iter__(self):
for d in self.collabs:
yield d
def add_institution(self, inst):
if isinstance(inst, BaseConflicts):
raise ValueError("Trying to add a conflict list to a conflict list")
if isinstance(inst, ConflictSet):
raise ValueError("Trying to add a conflict list to a conflict list")
self.institutions.add(self.insts.get_inst(inst))
def add_co_author(self, a):
if isinstance(a, ConflictSet):
raise ValueError("Trying to add a conflict list to a conflict list")
if isinstance(a, BaseConflicts):
raise ValueError("Trying to add a conflict list to a conflict list")
if isinstance(a, Person):
p = a
else:
p = Person(a)
self.collabs.add(p)
def __inst_conflicts(self, other):
return self.institutions.intersects_with(other.institutions)
def __collab_conflicts(self, other):
return self.collabs.intersects_with(other.collabs)
def find_institution_conflicts(self, other):
out = BaseConflicts(self.insts)
for i in self.__inst_conflicts(other):
inst, r = i
out.institutions.add(inst, r)
return out
def find_collab_conflicts(self, other):
out = BaseConflicts(self.insts)
for i in self.__collab_conflicts(other):
(c, r) = i
out.collabs.add(c, r)
return out
def find_conflicts(self, other):
collabs = self.find_collab_conflicts(other)
insts = self.find_institution_conflicts(other)
collabs.merge_inst_conflicts(insts)
return collabs
def merge_inst_conflicts(self, other):
self.institutions.merge(other.institutions)
def merge_collab_conflicts(self, other):
self.collabs.merge(other.collabs)
def match_institution(self, inst):
return self.institutions.match(inst)
def match_co_author(self, co_author):
return self.collabs.match(co_author)
def compare_co_authors(self, other):
"""
Returns:
(in_this_but_not_other, in_other_but_not_this)
"""
in_this = []
in_other = []
for coa in other.collabs:
if coa not in self.collabs:
in_other.append(coa)
for coa in self.collabs:
if coa not in other.collabs:
in_this.append(coa)
return in_this, in_other
def __bool__(self):
return True if self.collabs or self.institutions else False
def __str__(self):
s= ""
if self.institutions:
s+= "Institutions:\n"
s+= str(self.institutions)
if self.collabs:
s+= "Collaborators:\n"
s+= str(self.collabs)
return s
def str_no_linebreaks(self):
s = ""
if self.institutions:
s += "instituitons:[%s];" % self.institutions.str_no_linebreaks()
if self.collabs:
s += "collaborators:[%s]" % self.collabs.str_no_linebreaks()
return s