-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_utils.py
284 lines (243 loc) · 7.69 KB
/
parse_utils.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
import nltk
import string
import itertools
from collections import defaultdict
import unicodedata
from utils import *
import nltk.data
PUNCT = set(string.punctuation.replace('-', '').replace('+', '')
.replace('[', '').replace(']', '').replace('(', '').replace(')', ''))
# Pubmed helper methods
sent_detector = None
def cleanAbstract(paper):
"""returns cleaned and normalized pubmed abstract text"""
article = paper.get('MedlineCitation', {}).get('Article', {})
abstract = article.get('Abstract', {}).get('AbstractText', '')
if isinstance(abstract, list):
abstract = ''.join(abstract)
if isinstance(abstract, unicode):
abstract = unicodedata.normalize(
'NFKD', abstract).encode('ascii', 'ignore')
return abstract
def cleanJournal(paper):
"""returns the cleaned journal of a pubmed paper"""
journal = paper.get('MedlineCitation', {}).get(
'Article', {}).get('Journal', {}).get('Title', '')
if isinstance(journal, list):
journal = ''.join(journal)
if isinstance(journal, unicode):
journal = unicodedata.normalize(
'NFKD', journal).encode('ascii', 'ignore')
return journal.lower()
def cleanTitle(paper):
"""returns the cleaned title of a pubmed paper"""
article = paper.get('MedlineCitation', {}).get('Article', {})
title = article.get('ArticleTitle', {})
if isinstance(title, list):
title = ''.join(title)
if isinstance(title, unicode):
title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore')
return title.lower()
# Chemical db helper methods
def grab_names(names_entry):
"""Returns all names and synonyms of a chemical"""
names = []
names += [x.strip() for x in names_entry.get('brenda', [])]
names += [x.strip() for x in names_entry.get('synonyms', [])]
names += [x.strip() for x in flatten_list([x['values'] for x in
names_entry.get('pubchem', [])
if 'values' in x])]
names += [x.strip() for x in names_entry.get('genbank', [])]
names += [x.strip() for x in names_entry.get('pubmed', [])]
return names
# String helper methods
def isNumber(s):
"""returns true if string s is a number"""
try:
float(s)
return True
except ValueError:
return False
# Sentence helper methods
def getPos(sents, strip=True):
"""returns tuple of tagged tokens, dictionary of POS tags to words"""
if strip:
sents = [stripPunct(s) for s in sents]
toks = [nltk.word_tokenize(sent) for sent in sents]
tagged = [nltk.pos_tag(tok) for tok in toks]
flat = sum(tagged, [])
posDict = defaultdict(list)
for wd, pos in flat:
posDict[pos].append(wd)
return tagged, posDict
# Paragraph helper methods
def stripPunct(text):
"""Returns string w/o punctuations"""
newtext = ''
for ch in text:
if ch not in PUNCT:
newtext += ch
elif ch == '/' or ch == '-' or ch == ':':
newtext += ' '
return newtext
def getMispelled(dic, text):
"""returns a list of mispelled words from a paragraph"""
return [wd for wd in stripPunct(text).split(' ')
if wd != '' and not dic.check(wd)]
def nextLetter(text, index):
"""return next letter that comes after index. helper for splitSentences"""
index += 1
next = ''
while index < len(text) and text[index] == ' ':
index += 1
next = text[index]
return next
def splitSentences(text):
"""
Splits a paragraph into sentences.
Deal with cases like:
The isopenicillin N synthase (cyclase) of Streptomyces lactamdurans (syn.
Nocardia lactamdurans) has been purified to near homogeneity as judged by
SDS-PAGE and isoelectric focusing.
Some ambiguity when sentence ends in things like "L." referring to a
chemical name
"""
split = []
parens = False
brackets = False
part = ''
for index in range(len(text)):
i = text[index]
if i == '[':
brackets = True
elif i == ']':
brackets = False
elif i == '(':
parens = True
elif i == ')':
parens = False
elif i == '.' and not parens and not brackets:
# split the sentence if period does not come after a single letter
# or if the next word after the period is capitalized.
if text[index - 2] != ' ' or nextLetter(text, index).isupper():
split.append(part)
part = ''
continue
part = part + i
# remove leading space
for i in range(len(split)):
if split[i].startswith(' '):
split[i] = split[i][1:]
return split
def getTree(chunker, tagged):
"""returns a tree given list of tagged words using the chunker"""
if tagged == []:
return None
(words, tags) = zip(*tagged)
chunks = chunker.tag(tags)
wtc = itertools.izip(words, chunks)
lines = [' '.join([w, t, c]) for (w, (t, c)) in wtc if c]
tree = nltk.chunk.conllstr2tree('\n'.join(lines))
return tree
def shake(tree, fruit):
"""returns a list of all branches of the tree with the POS tag type"""
leaves = []
for subtree in tree.subtrees(filter=lambda t: t.node == fruit):
leaves.append(subtree.leaves())
return leaves
ABERRANT_PLURAL_MAP = {
'appendix': 'appendices',
'barracks': 'barracks',
'cactus': 'cacti',
'child': 'children',
'criterion': 'criteria',
'deer': 'deer',
'echo': 'echoes',
'elf': 'elves',
'embargo': 'embargoes',
'focus': 'foci',
'fungus': 'fungi',
'goose': 'geese',
'hero': 'heroes',
'hoof': 'hooves',
'index': 'indices',
'knife': 'knives',
'leaf': 'leaves',
'life': 'lives',
'man': 'men',
'mouse': 'mice',
'nucleus': 'nuclei',
'person': 'people',
'phenomenon': 'phenomena',
'potato': 'potatoes',
'self': 'selves',
'syllabus': 'syllabi',
'tomato': 'tomatoes',
'torpedo': 'torpedoes',
'veto': 'vetoes',
'woman': 'women',
}
VOWELS = set('aeiou')
def pluralize(singular):
"""
from http://code.activestate.com/recipes/577781-pluralize-word-convert-singular-word-to-its-plural/
Return plural form of given lowercase singular word (English only). Based on
ActiveState recipe http://code.activestate.com/recipes/413172/
>>> pluralize('')
''
>>> pluralize('goose')
'geese'
>>> pluralize('dolly')
'dollies'
>>> pluralize('genius')
'genii'
>>> pluralize('jones')
'joneses'
>>> pluralize('pass')
'passes'
>>> pluralize('zero')
'zeros'
>>> pluralize('casino')
'casinos'
>>> pluralize('hero')
'heroes'
>>> pluralize('church')
'churches'
>>> pluralize('x')
'xs'
>>> pluralize('car')
'cars'
"""
if not singular:
return ''
plural = ABERRANT_PLURAL_MAP.get(singular)
if plural:
return plural
root = singular
try:
if singular[-1] == 'y' and singular[-2] not in VOWELS:
root = singular[:-1]
suffix = 'ies'
elif singular[-1] == 's':
if singular[-2] in VOWELS:
if singular[-3:] == 'ius':
root = singular[:-2]
suffix = 'i'
else:
root = singular[:-1]
suffix = 'ses'
else:
suffix = 'es'
elif singular[-2:] in ('ch', 'sh'):
suffix = 'es'
else:
suffix = 's'
except IndexError:
suffix = 's'
plural = root + suffix
return plural
def main():
"""Put test code here"""
pass
if __name__ == '__main__':
main()