-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
426 lines (383 loc) · 14.4 KB
/
model.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
import types
import re
from itertools import izip_longest
from util import normalize
def is_initial(name):
if name.endswith('.'):
return True
if len(name) == 1:
return True
if name.isupper() and len(name) <= 3:
return True
return False
class Author(object):
def __init__(self, surname, names=None, unparsed_text=None):
if not isinstance(surname, types.StringTypes):
raise TypeError('surname must be string')
self.surname = surname
if names == None:
self.names = []
elif isinstance(names, types.StringTypes):
self.names = [names]
else:
self.names = list(names)
self.unparsed_text = unparsed_text
def __unicode__(self):
return u' '.join(self.names + [self.surname])
def __str__(self):
return self.__unicode__().encode('UTF-8')
def __repr__(self):
r = 'Author({!r}'.format(self.surname)
if len(self.names) > 0:
r += ', names={!r}'.format(self.names)
if self.unparsed_text:
r += ', unparsed_text={!r}'.format(self.unparsed_text)
r += ')'
return r
def to_dict(self):
return {'names': self.names, 'surname': self.surname,
'unparsed_text': self.unparsed_text}
@classmethod
def from_dict(cls, d):
return cls(d['surname'], names=d['names'], unparsed_text=d.get('unparsed_text'))
def __eq__(self, other):
if not isinstance(other, Author):
return NotImplemented
if normalize(self.surname) != normalize(other.surname):
return False
if len(self.names) > 0 and len(other.names) > 0:
if is_initial(self.names[0]) or is_initial(other.names[0]):
return normalize(self.names[0][0]) == normalize(other.names[0][0])
else:
return normalize(self.names[0]) == normalize(other.names[0])
return True
def __hash__(self):
"""Hash musi zavisiet len na priezvisku, lebo to v niektorych pripadoch staci na to,
aby sa dva Author objekty rovnali
"""
return hash(normalize(self.surname))
@property
def formatted_surname(self):
if self.surname.isupper():
parts = self.surname.split()
new_parts = []
for part in parts:
if part in [u'DE', u'VON']:
new_parts.append(part.lower())
elif part.startswith(u'MC'):
new_parts.append(u'Mc' + part[2:].title())
elif self.surname.startswith(u'MAC'):
new_parts.append(u'Mac' + part[3:].title())
else:
new_parts.append(part.title())
return u' '.join(new_parts)
return self.surname
@property
def short_name(self):
return u', '.join([self.formatted_surname, u' '.join(name[0].upper() + u'.' for name in self.names)])
@classmethod
def parse_sn_first(cls, fullname):
if ',' in fullname:
parts = re.split(r'[,]+', fullname, maxsplit=1)
surname = parts[0].strip()
names = parts[1]
else:
parts = re.split(r'[ ]+', fullname)
surname_parts = []
surname_next = False
consumed = False
for i in range(len(parts)):
if parts[i].endswith('.'):
names = u' '.join(parts[i:])
break
if parts[i].lower() in [u'de', u'von']:
surname_next = True
surname_parts.append(parts[i])
continue
if surname_next:
surname_parts.append(parts[i])
surname_next = False
continue
if i == 0 or (not consumed and i != len(parts) -1 ):
surname_parts.append(parts[i])
consumed = True
continue
names = u' '.join(parts[i:])
break
else:
names = ''
surname = u' '.join(surname_parts)
if len(names) > 0:
name_parts = re.split(r'([. -]+)', names)
names = []
for name, separator in izip_longest(name_parts[::2], name_parts[1::2], fillvalue=''):
if len(name) == 0:
continue
initial = '.' in separator
# Osetrime inicialky, ak su napriklad Smith, JD
if not surname.isupper() and name.isupper() and len(name) <= 3:
for char in name:
names.append(char + '.')
else:
names.append(name + ('.' if initial else ''))
else:
names = None
return cls(surname, names, unparsed_text=fullname)
@classmethod
def parse_sn_first_list(cls, names, separator=u';'):
return [cls.parse_sn_first(x.strip()) for x in names.split(separator)]
class TaggedValue(object):
def __init__(self, value, type=None, description=None):
"""Reprezentuje hodnotu s typom
value = string identifikator
type = typ identifikatora (ISBN, ISSN, WOK, ...)
description = pridany popis identifikatora, napriklad ak je viac roznych ISBN pre hardcover a paperback
verzie, da sa to popisat v tejto poznamke
"""
self.value = value
self.type = type
self.description = description
def __unicode__(self):
r = u''
if self.type:
r += u'{}:'.format(self.type)
r += self.value
if self.description:
r += u'({})'.format(self.description)
return r
def __str__(self):
return self.__unicode__().encode('UTF-8')
def __repr__(self):
r = '{}({!r}'.format(type(self).__name__, self.value)
if self.type:
r += ', type={!r}'.format(self.type)
if self.description:
r += ', description={!r}'.format(self.description)
r += ')'
return r
def to_dict(self):
return {'value': self.value, 'type': self.type, 'description': self.description}
def __eq__(self, other):
return self.type == other.type and self.value == other.value
def __hash__(self):
return hash((self.type, self.value))
@classmethod
def from_dict(cls, d):
return cls(d['value'], type=d['type'], description=d['description'])
@staticmethod
def find_by_type(iterable, type):
return filter(lambda x: x.type == type, iterable)
class Identifier(TaggedValue):
"""Reprezentuje unikatny identifikator
value = string identifikator
type = typ identifikatora (ISBN, ISSN, WOK, ...)
description = pridany popis identifikatora, napriklad ak je viac roznych ISBN pre hardcover a paperback
verzie, da sa to popisat v tejto poznamke
"""
pass
class URL(TaggedValue):
pass
class Index(TaggedValue):
pass
class Publication(object):
def __init__(self, title, authors, year, published_in=None, pages=None, volume=None, series=None, issue=None, special_issue=None, supplement=None, source_urls=None, cite_urls=None, identifiers=None, errors=None, authors_incomplete=False, indexes=None, times_cited=None, article_no=None, publisher=None, publisher_city=None, edition=None):
"""Reprezentuje jednu publikaciu
title = nazov publikacie
authors = zoznam autorov publikacie
year = rok vydania publikacie
published_in = nazov casopisu/konferencie etc.
pages = strany, na ktorych sa publikacia nachadza vramci published_in
volume = volume casopisu
series = nazov serie knih, kam patri published_in
source_urls = adresy na zdrojove datbazy na webe
cite_urls = adresy na zoznam citacii na webe
identifiers = identifikatory tejto publikacie
authors_incomplete = zoznam autorov nie je uplny, obsahuje len par z nich
indexes = v ktorych citacnych indexoch sa publikacia nachadza
times_cited = pocet dokumentov citujucich tuto publikaciu
article_no = Article Number
"""
self.title = title
self.authors = authors
self.authors_incomplete = authors_incomplete
self.year = year
self.published_in = published_in
self.pages = pages
self.volume = volume
self.series = series
self.issue = issue
self.special_issue = special_issue
self.supplement = supplement
self.times_cited = times_cited
self.article_no = article_no
self.publisher = publisher
self.publisher_city = publisher_city
self.edition = edition
if source_urls == None:
self.source_urls = []
else:
self.source_urls = list(source_urls)
if cite_urls == None:
self.cite_urls = []
else:
self.cite_urls = list(cite_urls)
if identifiers == None:
self.identifiers = []
else:
self.identifiers = list(identifiers)
if indexes == None:
self.indexes = []
else:
self.indexes = list(indexes)
if errors == None:
self.errors = []
else:
self.errors = list(errors)
def __unicode__(self):
authors = u', '.join(unicode(x) for x in self.authors)
if self.authors_incomplete:
authors += u' et al.'
source_urls = u' '.join(unicode(x) for x in self.source_urls)
cite_urls = u' '.join(unicode(x) for x in self.cite_urls)
identifiers = u' '.join(unicode(x) for x in self.identifiers)
indexes = u' '.join(unicode(x) for x in self.indexes)
r = u'{}\n'.format(self.title)
r += u' Publication year: {}\n'.format(self.year)
r += u' Authors: {}\n'.format(authors)
if self.published_in:
r += u' Published in: {}\n'.format(self.published_in)
if self.pages:
r += u' Pages: {}\n'.format(self.pages)
if self.article_no:
r += u' Article No.: {}\n'.format(self.article_no)
if self.issue:
r += u' Issue: {}\n'.format(self.issue)
if self.special_issue:
r += u' Special issue: {}\n'.format(self.special_issue)
if self.supplement:
r += u' Supplement: {}\n'.format(self.supplement)
if self.volume:
r += u' Volume: {}\n'.format(self.volume)
if self.series:
r += u' Series: {}\n'.format(self.series)
if self.publisher:
r += u' Publisher: {}\n'.format(self.publisher)
if self.publisher_city:
r += u' Publisher city: {}\n'.format(self.publisher_city)
if self.edition:
r += u' Edition: {}\n'.format(self.edition)
r += u' Source URLs: {}\n'.format(source_urls)
r += u' Citation list URLs: {}\n'.format(cite_urls)
r += u' Identifiers: {}\n'.format(identifiers)
r += u' Indexes: {}\n'.format(indexes)
if self.times_cited:
r += u' Times cited: {}\n'.format(self.times_cited)
if len(self.errors) > 0:
errors = u' '.join(unicode(x) for x in self.errors)
r += u' Errors: {}\n'.format(errors)
return r
def __str__(self):
return self.__unicode__().encode('UTF-8')
def __repr__(self):
return self.repr()
def repr(self, pretty=False):
def reprlist(l):
if pretty:
return '[' + ''.join('\n ' + repr(x) + ', ' for x in l) + '\n]'
else:
return repr(l)
if pretty:
nl = '\n'
else:
nl = ''
r = 'Publication({!r}, {}{}, {}{!r}'.format(self.title, nl, reprlist(self.authors), nl, self.year)
if self.published_in:
r += ', {}published_in={!r}'.format(nl, self.published_in)
if self.pages:
r += ', {}pages={!r}'.format(nl, self.pages)
if self.volume:
r += ', {}volume={!r}'.format(nl, self.volume)
if self.series:
r += ', {}series={!r}'.format(nl, self.series)
if self.issue:
r += ', {}issue={!r}'.format(nl, self.issue)
if self.special_issue:
r += ', {}special_issue={!r}'.format(nl, self.special_issue)
if self.supplement:
r += ', {}supplement={!r}'.format(nl, self.supplement)
if len(self.source_urls) > 0:
r += ', {}source_urls={}'.format(nl, reprlist(self.source_urls))
if len(self.cite_urls) > 0:
r += ', {}cite_urls={}'.format(nl, reprlist(self.cite_urls))
if len(self.identifiers) > 0:
r += ', {}identifiers={}'.format(nl, reprlist(self.identifiers))
if len(self.errors) > 0:
r += ', {}errors={}'.format(nl, reprlist(self.errors))
if self.authors_incomplete:
r += ', {}authors_incomplete=True'.format(nl)
if len(self.indexes) > 0:
r += ', {}indexes={}'.format(nl, reprlist(self.indexes))
if self.times_cited != None:
r += ', {}times_cited={}'.format(nl, self.times_cited)
if self.article_no:
r += ', {}article_no={!r}'.format(nl, self.article_no)
if self.publisher:
r += ', {}publisher={!r}'.format(nl, self.publisher)
if self.publisher_city:
r += ', {}publisher_city={!r}'.format(nl, self.publisher_city)
if self.edition:
r += ', {}edition={!r}'.format(nl, self.edition)
r += nl + ')'
return r
def to_dict(self):
def dictify(l):
return [x.to_dict() for x in l]
return {
'title': self.title, 'authors': dictify(self.authors), 'year': self.year,
'published_in': self.published_in, 'pages': self.pages, 'volume': self.volume,
'series': self.series, 'issue': self.issue, 'special_issue': self.special_issue,
'supplement': self.supplement, 'source_urls': dictify(self.source_urls),
'cite_urls': dictify(self.cite_urls), 'identifiers': dictify(self.identifiers),
'errors': self.errors, 'authors_incomplete': self.authors_incomplete,
'indexes': dictify(self.indexes), 'times_cited': self.times_cited,
'article_no': self.article_no, 'publisher': self.publisher, 'publisher_city': self.publisher_city,
'edition': self.edition
}
@classmethod
def from_dict(cls, d):
return Publication(
d['title'], [Author.from_dict(x) for x in d['authors']], d['year'],
published_in=d['published_in'], pages=d['pages'], volume=d['volume'],
series=d['series'], issue=d['issue'], special_issue=d['special_issue'],
supplement=d['supplement'], source_urls=[URL.from_dict(x) for x in d['source_urls']],
cite_urls=[URL.from_dict(x) for x in d['cite_urls']],
identifiers=[Identifier.from_dict(x) for x in d['identifiers']],
errors=d['errors'], authors_incomplete=d['authors_incomplete'],
indexes=[Index.from_dict(x) for x in d['indexes']], times_cited=d['times_cited'],
article_no=d['article_no'], publisher=d.get('publisher'), publisher_city=d.get('publisher_city'),
edition=d.get('edition')
)
def __eq__(self, other):
if self.year != other.year:
return False
if not self.authors_incomplete and not other.authors_incomplete and self.authors != other.authors:
return False
if normalize(self.title) != normalize(other.title):
return False
if normalize(self.article_no) != normalize(other.article_no):
return False
if normalize(self.pages) != normalize(other.pages):
return False
if normalize(self.volume) != normalize(other.volume):
return False
if normalize(self.issue) != normalize(other.issue):
return False
if normalize(self.published_in) != normalize(other.published_in):
return False
return True
def in_index(self, *values):
for index in self.indexes:
if index.value in values:
return True
return False