-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPapers.py
166 lines (147 loc) · 6.28 KB
/
Papers.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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
# Information about papers
import Wiley
import Springer
class PaperLibrary(object):
"""
Keeps track of all the new papers/reference, and their match in CUL entries, if any.
"""
def __init__(self):
self.byTitleLower = {}
self.byDoi = {}
self.by1stAuthorLastNameLower = {}
# Google truncates titles, but this lib expects full paper titles.
# Therefore we hack it.
self.titleLenCaches = {}
return(None)
def getAllMatchupsGroupedByTitle(self):
"""
Returns list of all matchups, grouped and indexed by lower case title.
"""
return(self.byTitleLower)
def getByDoi(self, doi):
return(self.byDoi.get(doi))
def addPaper(self, paper):
"""
Add a paper to the library
"""
titleLower = paper.getTitleLower()
if titleLower not in self.byTitleLower:
# Google is a special case, as they truncate titles. The paper library
# is not set up for that.
if type(paper).__name__ == "GSPaper" and paper.titleIsTruncated():
# see if we have already set up a cache for this length
truncLen = len(paper.title)
if truncLen not in self.titleLenCaches:
print(" Creating new cache for length: " + str(truncLen))
self.titleLenCaches[truncLen] = {}
for lowerTitle, paperList in self.byLowerTitle.items():
truncLowerTitle = lowerTitle[:min(truncLen, len(lowerTitle))]
self.titleLenCaches[truncLen][truncLowerTitle] = papersList
if titleLower not in self.titleLenCaches[truncLen]:
# Longer vesrion of paper does not exist. Add to cache and to overall list.
self.byTitleLower[titleLower] = []
self.titleLenCaches[truncLen][titleLower] = self.byTitleLower[titleLower]
else:
self.byTitleLower[titleLower] = []
# add this to any cached entries as well
for length in self.titleLenCaches:
self.titleLenCaches[length][titleLower] = self.byTitleLower[titleLower]
self.byTitleLower[titleLower].append(paper)
else:
self.byTitleLower[titleLower].append(paper)
if paper.doi:
if paper.doi not in self.byDoi:
self.byDoi[paper.doi] = []
self.byDoi[paper.doi].append(paper)
firstAuthorLower = paper.getFirstAuthorLastNameLower()
if firstAuthorLower not in self.by1stAuthorLastNameLower:
self.by1stAuthorLastNameLower[firstAuthorLower] = []
self.by1stAuthorLastNameLower[firstAuthorLower].append(paper)
return(None)
def verifyConsistentDois(self):
"""
Confirm that any papers we think are the same, either have the same DOI, or
don't have a DOI.
"""
for lowerTitle, papersWithTitle in self.byTitleLower.items():
doi = None
for paper in papersWithTitle:
if paper.doi:
if not doi:
doi = paper.doi
elif doi != paper.doi:
print("Papers with same title, don't have same DOIs:<br />")
print(" Title: " + paper.title + "<br />")
print(" Conflicting DOIs: " + doi + ", " + paper.doi + "<br />")
def verifyConsistent1stAuthor(self):
"""
Verify that any papers that we think are the same, either have the same
first author last name, or no author specified.
"""
for lowerTitle, papersWithTitle in self.byTitleLower.items():
author1 = None
for paper in papersWithTitle:
firstAuthorForThisPaper = paper.getFirstAuthorLastNameLower()
if firstAuthorForThisPaper:
if not author1:
author1 = firstAuthorForThisPaper
elif author1 != firstAuthorForThisPaper:
print("Papers with same title, don't have same first authors: <br />")
print(" Title: " + paper.title + "<br />")
print(" Conflicting authors: <br />")
print(u" Author A: '" + author1 + u"' <br />")
print(u" Author B: '" + firstAuthorForThisPaper + u"' <br />")
def getDoiFromPaperList(paperList):
"""
List is assumed to have been pre-verified to have consistent DOIs
"""
for paper in paperList:
if paper.doi:
return(paper.doi)
return(None)
def getDoiUrlFromPaperList(paperList):
"""
List is assumed to have been pre-verified to have consistent DOIs
"""
for paper in paperList:
if paper.doiUrl:
return(paper.doiUrl)
return(None)
def getUrlFromPaperList(paperList):
"""
Extract a URL from paper list. Favor DOI URLs, and then fallback to others
if needed.
List is assumed to have been pre-verified to have consistent DOIs
"""
doiUrl = getDoiUrlFromPaperList(paperList)
if not doiUrl:
for paper in paperList:
if paper.url:
return(paper.url)
return(doiUrl)
def getHopkinsUrlFromPaperList(paperList):
"""
Extract a Hopkins specific URL from paper list.
Not all sources have this.
"""
hopkinsUrl = None
doiUrl = getDoiUrlFromPaperList(paperList)
if doiUrl:
urlParts = doiUrl.split("/")
hopkinsUrl = "/".join(urlParts[0:3]) + ".proxy1.library.jhu.edu/" + "/".join(urlParts[3:])
else:
for paper in paperList:
if paper.hopkinsUrl:
return(paper.hopkinsUrl)
elif Wiley.isWileyUrl(paper.url):
# Some wiley comes from other searches.
return(Wiley.createHopkinsUrl(paper.url))
elif Springer.isSpringerUrl(paper.url):
return(Springer.createHopkinsUrl(paper.url))
elif paper.url and not hopkinsUrl:
urlParts = paper.url.split("/")
hopkinsUrl = "/".join(urlParts[0:3]) + ".proxy1.library.jhu.edu/" + "/".join(urlParts[3:])
return(hopkinsUrl)