-
Notifications
You must be signed in to change notification settings - Fork 1
/
sd_rank_from.py
296 lines (263 loc) · 10.3 KB
/
sd_rank_from.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
# See sd_run.py for status and copyright release information
import re
import pywikibot
from sd_functions import find_between, in_category
# Get rank from categories
def rank_from_category(page):
match_cat_dict = {
'subspecies': 'Subspecies',
'subgenera': 'Subgenus',
'genera': 'Genus',
'subfamilies': 'Subfamily',
'families': 'Family',
'superfamilies': 'Superfamily',
'orders': 'Order',
'suborders': 'Suborder',
'phyla': 'Phylum',
}
rank = None
for key, val in match_cat_dict.items():
if in_category(page, key):
rank = val
break
# Exceptions for multiple matches
if rank == 'Genus' and in_category(page, 'subgenera'):
rank = 'Subgenus'
if rank == 'Family' and in_category(page, 'subfamilies'):
rank = 'Subfamily'
if rank == 'Family' and in_category(page, 'superfamilies'):
rank = 'Superfamily'
if rank == 'Order' and in_category(page, 'suborder'):
rank = 'Suborder'
return rank
# Get rank from lead
def rank_from_lead(title_nobra, lead_txt, name_singular, verbose_stage):
regex_start1 = "(is\sa|is\san|was\sa|was\san)\s" # eg "is a genus ..."
# Regex2 covers eg "is a" + <maximum of 30 chars not including 'in the'> + "genus"
# Need to ensure that "is an order of fungi in the class Tremellomycetes" maps to 'Order' not Class'
# Tempered greedy token - http://www.rexegg.com/regex-quantifiers.html#tempered_greed
regex_start2 = "(is\sa|are\sa|was\sa|were\sa)(?:(?!\sin\sthe).){0,50}\s"
lead_dict1 = { # Partial strings to ensure unique matches
'Subgenus': regex_start1 + 'subgenu',
'Genus': regex_start1 + 'genus',
'Superfamily': regex_start1 + 'superfami',
'Family': regex_start1 + 'famil',
'Subfamily': regex_start1 + 'subfami',
'Tribe': regex_start1 + 'tribe',
'Subtribe': regex_start1 + 'subtrib',
'Class': regex_start1 + 'class',
'Subclass': regex_start1 + 'subclas',
'Order': regex_start1 + 'order',
'Suborder': regex_start1 + 'suborde',
'Infraorder': regex_start1 + 'infraorde',
'Clade': regex_start1 + 'clade',
'Variety': regex_start1 + 'variet',
'Species': regex_start1 + 'species',
'Informal group': regex_start1 + 'informal group',
'Phylum': regex_start1 + 'phylum',
'Subphylum': regex_start1 + 'subphylu',
}
lead_dict2 = { # Partial strings to ensure unique matches
'Subgenus': regex_start2 + 'subgenu',
'Genus': regex_start2 + 'genus',
'Superfamily': regex_start2 + 'superfami',
'Family': regex_start2 + 'famil',
'Subfamily': regex_start2 + 'subfami',
'Tribe': regex_start2 + 'tribe',
'Subtribe': regex_start2 + 'subtrib',
'Class': regex_start2 + 'class',
'Subclass': regex_start2 + 'subclas',
'Order': regex_start2 + 'order',
'Suborder': regex_start2 + 'suborde',
'Infraorder': regex_start1 + 'infraorde',
'Clade': regex_start2 + 'clade',
'Variety': regex_start2 + 'variet',
'Species': regex_start2 + 'species',
'Informal group': regex_start2 + 'informal group',
'Phylum': regex_start1 + 'phylum',
'Subphylum': regex_start1 + 'subphylu',
}
rank = None
# For this function only, just consider the first sentence
lead_sen = lead_txt.split('.')[0]
if len(lead_sen) > 26: # Don't do this if first sentence is unreasonably short
lead_txt = lead_sen
# Pre-filter with tight regex of lead_dict1 to extract any obvious match before attempting anything clever
for key, val in lead_dict1.items():
if re.search(val, lead_txt): # eg "is a genus ..."
rank = key
if verbose_stage:
print('lead_dict1 lead rank is ', rank)
return rank
# Must be Species if lead has eg "Abacetus alesi is a beetle ..."
try:
regex_sp1 = f'{title_nobra}\s(is\sa|is\san|was\sa|was\san)\s{name_singular}'
if re.search(regex_sp1, lead_txt):
if verbose_stage:
print('regex_sp1 lead match on Species')
return 'Species'
except re.error: # Fails if title includes a question mark
pass
# Most probably Species if lead has eg " ... is a beetle ..."
regex_sp2 = f"(is\sa|is\san|was\sa|was\san)\s{name_singular}"
if re.search(regex_sp2, lead_txt):
if verbose_stage:
print('regex_sp2 lead match on Species')
return 'Species'
# Work through the options with looser regex of lead_dict2. Return if exactly one rank matches, otherwise None
matched = False
for key, val in lead_dict2.items():
if re.search(val, lead_txt):
if verbose_stage:
print('lead_dict2 lead matches on ', key)
if matched:
return None
rank = key
matched = True
return rank
# Rank from various speciesboxes
def rank_from_speciesbox(text_compressed):
if '{{speciesbox' in text_compressed:
return 'Species'
if '{{subspeciesbox' in text_compressed:
return 'Subspecies'
if '{{infraspeciesbox' in text_compressed:
var1 = re.search('\|varietas=\w', text_compressed)
var2 = re.search('\|variety=\w', text_compressed)
if var1 or var2:
return 'Variety'
subsp = re.search('\|subspecies=\w', text_compressed)
if subsp:
return 'Subspecies'
return None
# Rank from general taxobox
def rank_from_taxobox(title_nobra, text_compressed):
match_taxobox_dict = {
'genus': 'Genus',
'varietas': 'Variety',
'variety': 'Variety',
'tribus': 'Tribe',
'superfamilia': 'Superfamily',
'subtribus': 'Subtribe',
'subspecies': 'Subspecies',
'trinomial': 'Subspecies',
'classis': 'Class',
'subfamilia': 'Subfamily',
'species': 'Species',
'subordo': 'Suborder',
'subgenus': 'Subgenus',
'ordo': 'Order',
'subclassis': 'Subclass',
'familia': 'Family',
'informalgroup': 'Informal group',
'stemgroup': 'Group',
'localgroup': 'Group',
'clade': 'Clade',
'cladus': 'Clade',
'phylum': 'Phylum',
'subphylum': 'Subphylum',
}
if '{{taxobox' not in text_compressed:
return None
rank = None
# Check whether the exact title_nobra is shown in bold italic.
# If so, that defines the rank (apart from species/subspecies/variety)
for key, val in match_taxobox_dict.items():
to_match = f"|{key}='''''{title_nobra}'''''" # No match if first part of a binomial is abbreviated
if to_match in text_compressed:
# print ("RETURNING 1")
return val
# Subspecies and variety. These are in italic only (not bold italic) so two quotation marks
if r"|'varietas=''" in text_compressed or r"|'variety=''" in text_compressed:
rank = 'Variety'
return rank
if r"|'subspecies=''" in text_compressed:
rank = 'Subspecies'
return rank
# Check what else is in bold italic
for key, val in match_taxobox_dict.items():
to_match = f"|{key}='''''" # No match if first part of a binomial is abbreviated
if to_match in text_compressed:
rank = val
# Exceptions for multiple matches
if rank == 'Genus' and r"|'subgenus='''''" in text_compressed:
rank = 'Subgenus'
# print("RETURNING 2")
return rank
if rank == 'Family' and r"|'subfamilia='''''" in text_compressed:
rank = 'Subfamily'
# print("RETURNING 3")
return rank
if rank == 'Family' and r"|'superfamilia='''''" in text_compressed:
rank = 'Superfamily'
return rank
if rank == 'Tribe' and r"|'subtribus='''''" in text_compressed:
rank = 'Subtribe'
# print("RETURNING 4")
return rank
if rank == 'Class' and r"|'subclassis='''''" in text_compressed:
rank = 'Subclass'
# print("RETURNING 6")
return rank
if rank == 'Order' and r"|'subordo='''''" in text_compressed:
rank = 'Suborder'
# print("RETURNING 7")
return rank
# Species/genus oddities
# Accept species if genus is not in bold
if "|genus='''''" not in text_compressed:
species_strs = ["|species='''''", "|binomial=''"] # (only two quote marks for binomial)
if any(x in text_compressed for x in species_strs):
rank = "Species"
# print("RETURNING 8")
return rank
# If both genus and species both in bold, probably a monotypic genus
if "|genus='''''" in text_compressed:
species_strs = ["|species='''''", "|binomial=''"] # (only two quote marks for binomial)
if any(x in text_compressed for x in species_strs):
rank = "Genus"
# print("RETURNING 9")
return rank
# print("RETURNING 10")
return rank
# Rank and extinct status from autotaxobox
def info_from_autobox(wikipedia, text_compressed):
match_auto_dict = {
'genus': 'Genus',
'varietas': 'Variety',
'variety': 'Variety',
'tribus': 'Tribe',
'superfamilia': 'Superfamily',
'subtribus': 'Subtribe',
'subspecies': 'Subspecies',
'classis': 'Class',
'subfamilia': 'Subfamily',
'species': 'Species',
'subordo': 'Suborder',
'subgenus': 'Subgenus',
'ordo': 'Order',
'subclassis': 'Subclass',
'familia': 'Family',
'informalgroup': 'Informal group',
'stemgroup': 'Group',
'localgroup': 'Group',
'clade': 'Clade',
'cladus': 'Clade',
'phylum': 'Phylum',
'subphylum': 'Subphylum',
}
if '{{automatictaxobox' not in text_compressed:
return None, False
isextinct = False
try:
taxon = find_between(text_compressed, 'taxon=', r'|')
taxo_template = f'Template:Taxonomy/{taxon.capitalize()}'
taxo_page = pywikibot.Page(wikipedia, taxo_template)
taxo_txt_comp = taxo_page.text.lower().replace(' ', '')
taxo_rank = find_between(taxo_txt_comp, 'rank=', r'|')
rank = match_auto_dict[taxo_rank]
if '|extinct=yes' in taxo_txt_comp or '|extinct=true' in taxo_txt_comp:
isextinct = True
return rank, isextinct
except:
return None, False