1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.strategy.parser;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
import java.util.regex.Matcher;
|
15
|
import java.util.regex.Pattern;
|
16
|
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.joda.time.DateTimeFieldType;
|
20
|
import org.joda.time.Partial;
|
21
|
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.common.UTF8;
|
24
|
import eu.etaxonomy.cdm.model.agent.Person;
|
25
|
import eu.etaxonomy.cdm.model.agent.Team;
|
26
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.common.IParsable;
|
29
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
30
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
31
|
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
|
32
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
33
|
import eu.etaxonomy.cdm.model.name.IZoologicalName;
|
34
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
35
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
36
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
37
|
import eu.etaxonomy.cdm.model.name.Rank;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
39
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
40
|
import eu.etaxonomy.cdm.model.reference.IBook;
|
41
|
import eu.etaxonomy.cdm.model.reference.IBookSection;
|
42
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
43
|
import eu.etaxonomy.cdm.model.reference.ISeriesPart;
|
44
|
import eu.etaxonomy.cdm.model.reference.IVolumeReference;
|
45
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
46
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
47
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
48
|
import eu.etaxonomy.cdm.strategy.exceptions.StringNotParsableException;
|
49
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
50
|
|
51
|
|
52
|
/**
|
53
|
* Parser for {@link TaxonName}s of type NonViralName and below.
|
54
|
*
|
55
|
* @author a.mueller
|
56
|
*/
|
57
|
public class NonViralNameParserImpl
|
58
|
extends NonViralNameParserImplRegExBase
|
59
|
implements INonViralNameParser<INonViralName> {
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(NonViralNameParserImpl.class);
|
62
|
|
63
|
// good intro: http://java.sun.com/docs/books/tutorial/essential/regex/index.html
|
64
|
|
65
|
final static boolean MAKE_EMPTY = true;
|
66
|
final static boolean MAKE_NOT_EMPTY = false;
|
67
|
|
68
|
private final boolean authorIsAlwaysTeam = false;
|
69
|
private boolean removeSpaceAfterDot = false;
|
70
|
|
71
|
public static NonViralNameParserImpl NewInstance(){
|
72
|
return new NonViralNameParserImpl();
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public INonViralName parseSimpleName(String simpleName){
|
77
|
return parseSimpleName(simpleName, null, null);
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public INonViralName parseSimpleName(String simpleName, NomenclaturalCode code, Rank rank){
|
82
|
//"parseSimpleName() not yet implemented. Uses parseFullName() instead");
|
83
|
return parseFullName(simpleName, code, rank);
|
84
|
}
|
85
|
|
86
|
public void parseSimpleName(INonViralName nameToBeFilled, String simpleNameString, Rank rank, boolean makeEmpty){
|
87
|
//"parseSimpleName() not yet implemented. Uses parseFullName() instead");
|
88
|
parseFullName(nameToBeFilled, simpleNameString, rank, makeEmpty);
|
89
|
}
|
90
|
|
91
|
public INonViralName getNonViralNameInstance(String fullString, NomenclaturalCode code){
|
92
|
return getNonViralNameInstance(fullString, code, null);
|
93
|
}
|
94
|
|
95
|
public INonViralName getNonViralNameInstance(String fullString, NomenclaturalCode code, Rank rank){
|
96
|
INonViralName result = null;
|
97
|
if(code ==null) {
|
98
|
boolean isBotanicalName = anyBotanicFullNamePattern.matcher(fullString).find();
|
99
|
boolean isZoologicalName = anyZooFullNamePattern.matcher(fullString).find();;
|
100
|
boolean isBacteriologicalName = false;
|
101
|
boolean isCultivatedPlantName = false;
|
102
|
if ( (isBotanicalName || isCultivatedPlantName) && ! isZoologicalName && !isBacteriologicalName){
|
103
|
if (isBotanicalName){
|
104
|
result = TaxonNameFactory.NewBotanicalInstance(rank);
|
105
|
}else{
|
106
|
result = TaxonNameFactory.NewCultivarInstance(rank);
|
107
|
}
|
108
|
}else if ( isZoologicalName /*&& ! isBotanicalName*/ && !isBacteriologicalName && !isCultivatedPlantName){
|
109
|
result = TaxonNameFactory.NewZoologicalInstance(rank);
|
110
|
}else if ( isZoologicalName && ! isBotanicalName && !isBacteriologicalName && !isCultivatedPlantName){
|
111
|
result = TaxonNameFactory.NewBacterialInstance(rank);
|
112
|
}else {
|
113
|
result = TaxonNameFactory.NewNonViralInstance(rank);
|
114
|
}
|
115
|
} else {
|
116
|
switch (code) {
|
117
|
case ICNAFP:
|
118
|
result = TaxonNameFactory.NewBotanicalInstance(rank);
|
119
|
break;
|
120
|
case ICZN:
|
121
|
result = TaxonNameFactory.NewZoologicalInstance(rank);
|
122
|
break;
|
123
|
case Fungi:
|
124
|
result = TaxonNameFactory.NewFungusInstance(rank);
|
125
|
break;
|
126
|
case ICNCP:
|
127
|
logger.warn("ICNCP parsing not yet implemented");
|
128
|
result = TaxonNameFactory.NewCultivarInstance(rank);
|
129
|
break;
|
130
|
case ICNP:
|
131
|
logger.warn("ICNP not yet implemented");
|
132
|
result = TaxonNameFactory.NewBacterialInstance(rank);
|
133
|
break;
|
134
|
case ICVCN:
|
135
|
logger.error("Viral name is not a NonViralName !!");
|
136
|
break;
|
137
|
case NonViral:
|
138
|
logger.warn("NonViral parsing not yet implemented");
|
139
|
result = TaxonNameFactory.NewNonViralInstance(rank);
|
140
|
break;
|
141
|
default:
|
142
|
// FIXME Unreachable code
|
143
|
logger.error("Unknown Nomenclatural Code !!");
|
144
|
}
|
145
|
}
|
146
|
return result;
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
public TaxonName parseReferencedName(String fullReferenceString) {
|
151
|
return parseReferencedName(fullReferenceString, null, null);
|
152
|
}
|
153
|
|
154
|
@Override
|
155
|
public TaxonName parseReferencedName(String fullReferenceString, NomenclaturalCode nomCode, Rank rank) {
|
156
|
if (fullReferenceString == null){
|
157
|
return null;
|
158
|
}else{
|
159
|
INonViralName result = getNonViralNameInstance(fullReferenceString, nomCode, rank);
|
160
|
parseReferencedName(result, fullReferenceString, rank, MAKE_EMPTY);
|
161
|
return TaxonName.castAndDeproxy(result);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
private String standardize(INonViralName nameToBeFilled, String fullReferenceString, boolean makeEmpty){
|
166
|
//Check null and standardize
|
167
|
if (fullReferenceString == null){
|
168
|
//return null;
|
169
|
return null;
|
170
|
}
|
171
|
if (makeEmpty){
|
172
|
makeEmpty(nameToBeFilled);
|
173
|
}
|
174
|
fullReferenceString = fullReferenceString.replaceAll(oWs , " ");
|
175
|
fullReferenceString = fullReferenceString.trim();
|
176
|
if ("".equals(fullReferenceString)){
|
177
|
fullReferenceString = null;
|
178
|
}
|
179
|
return fullReferenceString;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Returns the regEx to be used for the full-name depending on the code
|
184
|
* @param nameToBeFilled
|
185
|
* @return
|
186
|
*/
|
187
|
private String getCodeSpecificFullNameRegEx(INonViralName nameToBeFilledOrig){
|
188
|
INonViralName nameToBeFilled = CdmBase.deproxy(nameToBeFilledOrig);
|
189
|
if (nameToBeFilled.isZoological()){
|
190
|
return anyZooFullName;
|
191
|
}else if (nameToBeFilled.isBotanical()) {
|
192
|
return anyBotanicFullName;
|
193
|
}else if (nameToBeFilled.isNonViral()) {
|
194
|
return anyBotanicFullName; //TODO ?
|
195
|
}else{
|
196
|
logger.warn("nameToBeFilled class not supported ("+nameToBeFilled.getClass()+")");
|
197
|
return null;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Returns the regEx to be used for the fsimple-name depending on the code
|
203
|
* @param nameToBeFilled
|
204
|
* @return
|
205
|
*/
|
206
|
private String getCodeSpecificSimpleNameRegEx(INonViralName nameToBeFilled){
|
207
|
nameToBeFilled = CdmBase.deproxy(nameToBeFilled);
|
208
|
|
209
|
if (nameToBeFilled.isZoological()){
|
210
|
return anyZooName;
|
211
|
}else if (nameToBeFilled.isBotanical()) {
|
212
|
return anyBotanicName;
|
213
|
}else if (nameToBeFilled.isNonViral()){
|
214
|
return anyZooName; //TODO ?
|
215
|
}else{
|
216
|
logger.warn("nameToBeFilled class not supported ("+nameToBeFilled.getClass()+")");
|
217
|
return null;
|
218
|
}
|
219
|
}
|
220
|
|
221
|
private Matcher getMatcher(String regEx, String matchString){
|
222
|
Pattern pattern = Pattern.compile(regEx);
|
223
|
Matcher matcher = pattern.matcher(matchString);
|
224
|
return matcher;
|
225
|
}
|
226
|
|
227
|
@Override
|
228
|
public void parseReferencedName(INonViralName nameToBeFilled, String fullReferenceStringOrig, Rank rank, boolean makeEmpty) {
|
229
|
//standardize
|
230
|
String fullReferenceString = standardize(nameToBeFilled, fullReferenceStringOrig, makeEmpty);
|
231
|
if (fullReferenceString == null){
|
232
|
return;
|
233
|
}
|
234
|
// happens already in standardize(...)
|
235
|
// makeProblemEmpty(nameToBeFilled);
|
236
|
|
237
|
//make nomenclatural status and replace it by empty string
|
238
|
fullReferenceString = parseNomStatus(fullReferenceString, nameToBeFilled, makeEmpty);
|
239
|
nameToBeFilled.setProblemEnds(fullReferenceString.length());
|
240
|
|
241
|
fullReferenceString = parseOriginalSpelling(fullReferenceString, nameToBeFilled, makeEmpty);
|
242
|
nameToBeFilled.setProblemEnds(fullReferenceString.length());
|
243
|
|
244
|
//get full name reg
|
245
|
String localFullNameRegEx = getCodeSpecificFullNameRegEx(nameToBeFilled);
|
246
|
//get full name reg
|
247
|
String localSimpleNameRegEx = getCodeSpecificSimpleNameRegEx(nameToBeFilled);
|
248
|
|
249
|
//separate name and reference part
|
250
|
String nameAndRefSeparatorRegEx = "(^" + localFullNameRegEx + ")("+ referenceSeperator + ")";
|
251
|
Matcher nameAndRefSeparatorMatcher = getMatcher (nameAndRefSeparatorRegEx, fullReferenceString);
|
252
|
|
253
|
Matcher onlyNameMatcher = getMatcher (localFullNameRegEx, fullReferenceString);
|
254
|
Matcher hybridMatcher = hybridFormulaPattern.matcher(fullReferenceString);
|
255
|
Matcher onlySimpleNameMatcher = getMatcher (localSimpleNameRegEx, fullReferenceString);
|
256
|
|
257
|
if (onlyNameMatcher.matches()){
|
258
|
makeEmpty = false;
|
259
|
parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty);
|
260
|
} else if (nameAndRefSeparatorMatcher.find()){
|
261
|
makeNameWithReference(nameToBeFilled, fullReferenceString, nameAndRefSeparatorMatcher, rank, makeEmpty);
|
262
|
}else if (hybridMatcher.matches() ){
|
263
|
//I do not remember why we need makeEmpty = false for onlyNameMatcher,
|
264
|
//but for hybridMatcher we need to remove old Hybrid Relationships if necessary, therefore
|
265
|
//I removed it from here
|
266
|
parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty);
|
267
|
}else if (onlySimpleNameMatcher.matches()){
|
268
|
makeEmpty = false;
|
269
|
parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty); //simpleName not yet implemented
|
270
|
}else{
|
271
|
makeNoFullRefMatch(nameToBeFilled, fullReferenceString, rank);
|
272
|
}
|
273
|
//problem handling. Start and end solved in subroutines
|
274
|
if (! nameToBeFilled.hasProblem()){
|
275
|
makeProblemEmpty(nameToBeFilled);
|
276
|
}
|
277
|
}
|
278
|
|
279
|
private void makeProblemEmpty(IParsable parsable){
|
280
|
boolean hasCheckRank = parsable.hasProblem(ParserProblem.CheckRank);
|
281
|
parsable.setParsingProblem(0);
|
282
|
if (hasCheckRank){
|
283
|
parsable.addParsingProblem(ParserProblem.CheckRank);
|
284
|
}
|
285
|
parsable.setProblemStarts(-1);
|
286
|
parsable.setProblemEnds(-1);
|
287
|
}
|
288
|
|
289
|
private void makeNoFullRefMatch(INonViralName nameToBeFilled, String fullReferenceString, Rank rank){
|
290
|
//try to parse first part as name, but keep in mind full string is not parsable
|
291
|
int start = 0;
|
292
|
|
293
|
String localFullName = getCodeSpecificFullNameRegEx(nameToBeFilled);
|
294
|
Matcher fullNameMatcher = getMatcher (pStart + localFullName, fullReferenceString);
|
295
|
if (fullNameMatcher.find()){
|
296
|
String fullNameString = fullNameMatcher.group(0);
|
297
|
nameToBeFilled.setProtectedNameCache(false);
|
298
|
parseFullName(nameToBeFilled, fullNameString, rank, false);
|
299
|
String sure = nameToBeFilled.getNameCache();
|
300
|
start = sure.length();
|
301
|
}
|
302
|
|
303
|
// String localSimpleName = getLocalSimpleName(nameToBeFilled);
|
304
|
// Matcher simpleNameMatcher = getMatcher (start + localSimpleName, fullReferenceString);
|
305
|
// if (simpleNameMatcher.find()){
|
306
|
// String simpleNameString = simpleNameMatcher.group(0);
|
307
|
// parseFullName(nameToBeFilled, simpleNameString, rank, false);
|
308
|
// start = simpleNameString.length();
|
309
|
// }
|
310
|
|
311
|
//don't parse if name can't be separated
|
312
|
nameToBeFilled.addParsingProblem(ParserProblem.NameReferenceSeparation);
|
313
|
nameToBeFilled.setTitleCache(fullReferenceString, true);
|
314
|
nameToBeFilled.setFullTitleCache(fullReferenceString, true);
|
315
|
// FIXME Quick fix, otherwise search would not deliver results for unparsable names
|
316
|
nameToBeFilled.setNameCache(fullReferenceString, true);
|
317
|
// END
|
318
|
nameToBeFilled.setProblemStarts(start);
|
319
|
nameToBeFilled.setProblemEnds(fullReferenceString.length());
|
320
|
logger.info("no applicable parsing rule could be found for \"" + fullReferenceString + "\"");
|
321
|
}
|
322
|
|
323
|
private void makeNameWithReference(INonViralName nameToBeFilled,
|
324
|
String fullReferenceString,
|
325
|
Matcher nameAndRefSeparatorMatcher,
|
326
|
Rank rank,
|
327
|
boolean makeEmpty){
|
328
|
|
329
|
String nameAndSeparator = nameAndRefSeparatorMatcher.group(0);
|
330
|
String name = nameAndRefSeparatorMatcher.group(1);
|
331
|
String referenceString = fullReferenceString.substring(nameAndRefSeparatorMatcher.end());
|
332
|
|
333
|
// is reference an in ref?
|
334
|
String separator = nameAndSeparator.substring(name.length());
|
335
|
boolean isInReference = separator.matches(inReferenceSeparator);
|
336
|
|
337
|
//parse subparts
|
338
|
|
339
|
int oldProblemEnds = nameToBeFilled.getProblemEnds();
|
340
|
parseFullName(nameToBeFilled, name, rank, makeEmpty);
|
341
|
nameToBeFilled.setProblemEnds(oldProblemEnds);
|
342
|
|
343
|
//original spelling
|
344
|
handleOriginalSpelling(nameToBeFilled);
|
345
|
|
346
|
//zoological new combinations should not have a nom. reference to be parsed
|
347
|
if (nameToBeFilled.isZoological()){
|
348
|
IZoologicalName zooName = (IZoologicalName)CdmBase.deproxy(nameToBeFilled);
|
349
|
//is name new combination?
|
350
|
if (zooName.getBasionymAuthorship() != null || zooName.getOriginalPublicationYear() != null){
|
351
|
ParserProblem parserProblem = ParserProblem.NewCombinationHasPublication;
|
352
|
zooName.addParsingProblem(parserProblem);
|
353
|
nameToBeFilled.setProblemStarts((nameToBeFilled.getProblemStarts()> -1) ? nameToBeFilled.getProblemStarts(): name.length());
|
354
|
nameToBeFilled.setProblemEnds(Math.max(fullReferenceString.length(), nameToBeFilled.getProblemEnds()));
|
355
|
}
|
356
|
}
|
357
|
|
358
|
parseReference(nameToBeFilled, referenceString, isInReference);
|
359
|
INomenclaturalReference ref = nameToBeFilled.getNomenclaturalReference();
|
360
|
|
361
|
//problem start
|
362
|
int start = nameToBeFilled.getProblemStarts();
|
363
|
int nameAndSeparatorLength = nameAndSeparator.length();
|
364
|
|
365
|
if (nameToBeFilled.isProtectedTitleCache() || nameToBeFilled.getParsingProblems().contains(ParserProblem.CheckRank)){
|
366
|
start = Math.max(0, start);
|
367
|
}else{
|
368
|
if (ref != null && ref.getParsingProblem()!=0){
|
369
|
start = Math.max(nameAndSeparatorLength, start);
|
370
|
//TODO search within ref
|
371
|
}
|
372
|
}
|
373
|
|
374
|
//end
|
375
|
int end = nameToBeFilled.getProblemEnds();
|
376
|
|
377
|
if (ref != null && ref.getParsingProblem()!=0){
|
378
|
end = Math.min(nameAndSeparatorLength + ref.getProblemEnds(), end);
|
379
|
}else{
|
380
|
if (nameToBeFilled.isProtectedTitleCache() ){
|
381
|
end = Math.min(end, nameAndSeparatorLength);
|
382
|
//TODO search within name
|
383
|
}
|
384
|
}
|
385
|
nameToBeFilled.setProblemStarts(start);
|
386
|
nameToBeFilled.setProblemEnds(end);
|
387
|
|
388
|
//delegate has problem to name
|
389
|
if (ref != null && ref.getParsingProblem()!=0){
|
390
|
nameToBeFilled.addParsingProblems(ref.getParsingProblem());
|
391
|
}
|
392
|
|
393
|
Reference nomRef;
|
394
|
if ( (nomRef = nameToBeFilled.getNomenclaturalReference()) != null ){
|
395
|
nomRef.setAuthorship(nameToBeFilled.getCombinationAuthorship());
|
396
|
}
|
397
|
}
|
398
|
|
399
|
/**
|
400
|
* Tries to create the best name (without authors) that makes an original spelling name for the nameToBeFilled.
|
401
|
*/
|
402
|
private void handleOriginalSpelling(INonViralName nameToBeFilled) {
|
403
|
if (nameToBeFilled.getNomenclaturalSource()== null || CdmUtils.isBlank(nameToBeFilled.getNomenclaturalSource().getOriginalNameString())){
|
404
|
return;
|
405
|
}
|
406
|
String originalNameString = nameToBeFilled.getNomenclaturalSource().getOriginalNameString();
|
407
|
|
408
|
final int n = 5;
|
409
|
String nameSplit[] = new String[n];
|
410
|
nameSplit[0] = nameToBeFilled.getGenusOrUninomial();
|
411
|
nameSplit[1] = nameToBeFilled.getInfraGenericEpithet();
|
412
|
nameSplit[2] = nameToBeFilled.getSpecificEpithet();
|
413
|
nameSplit[3] = nameToBeFilled.getRank()== null? null : nameToBeFilled.getRank().getAbbreviation();
|
414
|
nameSplit[4] = nameToBeFilled.getInfraSpecificEpithet();
|
415
|
String originalNameSplit[] = originalNameString.split(" ");
|
416
|
|
417
|
int bestOffset = -1;
|
418
|
int bestDiff = Integer.MAX_VALUE;
|
419
|
for (int off=0; off <= n - originalNameSplit.length; off++){
|
420
|
int diff = 0;
|
421
|
int off2 = 0;
|
422
|
for (int i=0+off;i<n && i-off-off2 < originalNameSplit.length; i++){
|
423
|
String namePart = CdmUtils.Nz(nameSplit[i]);
|
424
|
String originalPart = originalNameSplit[i-off-off2];
|
425
|
if (isBlank(namePart)){
|
426
|
originalPart = null;
|
427
|
off2++;
|
428
|
}
|
429
|
if (originalPart != null){
|
430
|
int newDiff = StringUtils.getLevenshteinDistance(namePart, originalPart);
|
431
|
diff += newDiff;
|
432
|
}
|
433
|
}
|
434
|
if (diff>0 && diff<=bestDiff){
|
435
|
bestOffset = off;
|
436
|
bestDiff = diff;
|
437
|
}
|
438
|
}
|
439
|
if (bestDiff == 0){
|
440
|
return;
|
441
|
}
|
442
|
|
443
|
String[] finalSplit = new String[n];
|
444
|
for (int i = 0; i < n; i++){
|
445
|
if (isBlank(nameSplit[i])){
|
446
|
if (bestOffset<i){
|
447
|
bestOffset++;
|
448
|
}
|
449
|
continue;
|
450
|
}
|
451
|
int oi = i-bestOffset;
|
452
|
finalSplit[i] = (oi)<0 || (oi>= originalNameSplit.length)? nameSplit[i]: originalNameSplit[oi];
|
453
|
}
|
454
|
TaxonName originalName = TaxonNameFactory.NewNameInstance(nameToBeFilled.getNameType(), nameToBeFilled.getRank(), nameToBeFilled.getHomotypicalGroup());
|
455
|
originalName.setGenusOrUninomial(finalSplit[0]);
|
456
|
originalName.setInfraSpecificEpithet(finalSplit[1]);
|
457
|
originalName.setSpecificEpithet(finalSplit[2]);
|
458
|
// originalName.setRank(finalSplit[3]);
|
459
|
originalName.setInfraSpecificEpithet(finalSplit[4]);
|
460
|
|
461
|
nameToBeFilled.setOriginalSpelling(originalName);
|
462
|
nameToBeFilled.setOriginalNameString(null);
|
463
|
return;
|
464
|
}
|
465
|
|
466
|
//TODO make it an Array of status
|
467
|
/**
|
468
|
* Extracts a {@link NomenclaturalStatus} from the reference String and adds it to the @link {@link TaxonName}.
|
469
|
* The nomenclatural status part is deleted from the reference String.
|
470
|
* @return String the new (shortend) reference String
|
471
|
*/
|
472
|
public String parseNomStatus(String fullString, INonViralName nameToBeFilled, boolean makeEmpty) {
|
473
|
Set<NomenclaturalStatusType> existingStatusTypeSet = new HashSet<>();
|
474
|
Set<NomenclaturalStatusType> newStatusTypeSet = new HashSet<>();
|
475
|
for (NomenclaturalStatus existingStatus : nameToBeFilled.getStatus()){
|
476
|
existingStatusTypeSet.add(existingStatus.getType());
|
477
|
}
|
478
|
|
479
|
String statusString;
|
480
|
Pattern hasStatusPattern = Pattern.compile("(" + pNomStatusPhrase + ")");
|
481
|
Matcher hasStatusMatcher = hasStatusPattern.matcher(fullString);
|
482
|
|
483
|
if (hasStatusMatcher.find()) {
|
484
|
String statusPhrase = hasStatusMatcher.group(0);
|
485
|
|
486
|
Pattern statusPattern = Pattern.compile(pNomStatus);
|
487
|
Matcher statusMatcher = statusPattern.matcher(statusPhrase);
|
488
|
statusMatcher.find();
|
489
|
statusString = statusMatcher.group(0);
|
490
|
try {
|
491
|
TaxonName nameToBeFilledCasted = TaxonName.castAndDeproxy(nameToBeFilled);
|
492
|
NomenclaturalStatusType nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusString, nameToBeFilledCasted);
|
493
|
if (! existingStatusTypeSet.contains(nomStatusType)){
|
494
|
NomenclaturalStatus nomStatus = NomenclaturalStatus.NewInstance(nomStatusType);
|
495
|
nameToBeFilled.addStatus(nomStatus);
|
496
|
}
|
497
|
newStatusTypeSet.add(nomStatusType);
|
498
|
fullString = fullString.replace(statusPhrase, "");
|
499
|
} catch (UnknownCdmTypeException e) {
|
500
|
//Do nothing
|
501
|
}
|
502
|
}
|
503
|
//remove not existing nom status
|
504
|
if (makeEmpty){
|
505
|
Set<NomenclaturalStatus> tmpStatus = new HashSet<>();
|
506
|
tmpStatus.addAll(nameToBeFilled.getStatus());
|
507
|
for (NomenclaturalStatus status : tmpStatus){
|
508
|
if (! newStatusTypeSet.contains(status.getType())){
|
509
|
nameToBeFilled.removeStatus(status);
|
510
|
}
|
511
|
}
|
512
|
}
|
513
|
|
514
|
return fullString;
|
515
|
}
|
516
|
|
517
|
/**
|
518
|
* Extracts the original spelling string and adds it to the original source as <code>originalNameString</code>.
|
519
|
* The original spelling part is deleted from the reference String.
|
520
|
* @return String the new (shortend) reference String
|
521
|
*/
|
522
|
public String parseOriginalSpelling(String fullString, INonViralName nameToBeFilled, boolean makeEmpty) {
|
523
|
|
524
|
Pattern hasOrginalSpellingPattern = Pattern.compile("(" + pOriginalSpelling + ")");
|
525
|
Matcher hasOrginalSpellingMatcher = hasOrginalSpellingPattern.matcher(fullString);
|
526
|
|
527
|
if (hasOrginalSpellingMatcher.find()) {
|
528
|
String originalSpellingPhrase = hasOrginalSpellingMatcher.group(0);
|
529
|
String originalSpellingStr = originalSpellingPhrase.substring(originalSpellingStart.length());
|
530
|
originalSpellingStr = originalSpellingStr.substring(0, originalSpellingStr.length() - originalSpellingEnd.length()+1);
|
531
|
nameToBeFilled.setOriginalNameString(originalSpellingStr);
|
532
|
fullString = fullString.replace(originalSpellingPhrase, "").trim();
|
533
|
}
|
534
|
|
535
|
if (makeEmpty){
|
536
|
nameToBeFilled.setOriginalSpelling(null);
|
537
|
}
|
538
|
|
539
|
return fullString;
|
540
|
}
|
541
|
|
542
|
|
543
|
private void parseReference(INonViralName nameToBeFilled, String strReference, boolean isInReference){
|
544
|
|
545
|
INomenclaturalReference ref;
|
546
|
String originalStrReference = strReference;
|
547
|
|
548
|
//End (just delete end (e.g. '.', may be ambigous for yearPhrase, but no real information gets lost
|
549
|
Matcher endMatcher = getMatcher(referenceEnd + end, strReference);
|
550
|
if (endMatcher.find()){
|
551
|
String endPart = endMatcher.group(0);
|
552
|
strReference = strReference.substring(0, strReference.length() - endPart.length());
|
553
|
}
|
554
|
|
555
|
// String pDetailYear = ".*" + detailSeparator + detail + fWs + yearSeperator + fWs + yearPhrase + fWs + end;
|
556
|
// Matcher detailYearMatcher = getMatcher(pDetailYear, strReference);
|
557
|
|
558
|
String strReferenceWithYear = strReference;
|
559
|
//year
|
560
|
String yearPart = null;
|
561
|
String pYearPhrase = yearSeperator + fWs + yearPhrase + fWs + end;
|
562
|
Matcher yearPhraseMatcher = getMatcher(pYearPhrase, strReference);
|
563
|
if (yearPhraseMatcher.find()){
|
564
|
yearPart = yearPhraseMatcher.group(0);
|
565
|
strReference = strReference.substring(0, strReference.length() - yearPart.length());
|
566
|
yearPart = yearPart.replaceFirst(pStart + yearSeperator, "").trim();
|
567
|
}else{
|
568
|
if (nameToBeFilled.isZoological()){
|
569
|
IZoologicalName zooName = (IZoologicalName)CdmBase.deproxy(nameToBeFilled);
|
570
|
yearPart = String.valueOf(zooName.getPublicationYear());
|
571
|
//continue
|
572
|
}else{
|
573
|
ref = makeDetailYearUnparsable(nameToBeFilled,strReference);
|
574
|
ref.setDatePublished(TimePeriodParser.parseStringVerbatim(yearPart));
|
575
|
return;
|
576
|
}
|
577
|
}
|
578
|
|
579
|
//detail
|
580
|
String pDetailPhrase = detailSeparator + fWs + detail + fWs + end;
|
581
|
Matcher detailPhraseMatcher = getMatcher(pDetailPhrase, strReference);
|
582
|
if (detailPhraseMatcher.find()){
|
583
|
String detailPart = detailPhraseMatcher.group(0);
|
584
|
strReference = strReference.substring(0, strReference.length() - detailPart.length());
|
585
|
detailPart = detailPart.replaceFirst(pStart + detailSeparator, "").trim();
|
586
|
nameToBeFilled.setNomenclaturalMicroReference(detailPart);
|
587
|
}else{
|
588
|
makeDetailYearUnparsable(nameToBeFilled, strReferenceWithYear);
|
589
|
return;
|
590
|
}
|
591
|
//parse title and author
|
592
|
ref = parseReferenceTitle(strReference, yearPart, isInReference);
|
593
|
if (ref.hasProblem()){
|
594
|
//we need to protect both caches otherwise the titleCache is incorrectly build from atomized parts
|
595
|
ref.setTitleCache( (isInReference ? "in ":"") + originalStrReference, true);
|
596
|
ref.setAbbrevTitleCache( (isInReference ? "in ":"") + originalStrReference, true);
|
597
|
}
|
598
|
nameToBeFilled.setNomenclaturalReference(ref);
|
599
|
int end = Math.min(strReference.length(), ref.getProblemEnds());
|
600
|
ref.setProblemEnds(end);
|
601
|
}
|
602
|
|
603
|
private Reference makeDetailYearUnparsable(INonViralName nameToBeFilled, String strReference) {
|
604
|
Reference ref;
|
605
|
|
606
|
ref = ReferenceFactory.newGeneric();
|
607
|
ref.setTitleCache(strReference, true);
|
608
|
ref.setAbbrevTitleCache(strReference, true);
|
609
|
ref.setProblemEnds(strReference.length());
|
610
|
ref.addParsingProblem(ParserProblem.CheckDetailOrYear);
|
611
|
nameToBeFilled.addParsingProblem(ParserProblem.CheckDetailOrYear);
|
612
|
nameToBeFilled.setNomenclaturalReference(ref);
|
613
|
return ref;
|
614
|
}
|
615
|
|
616
|
/**
|
617
|
* Parses the referenceTitlePart, including the author volume and edition.
|
618
|
*/
|
619
|
public INomenclaturalReference parseReferenceTitle(String strReference, String year, boolean isInReference){
|
620
|
IBook result = null;
|
621
|
|
622
|
Matcher refSineDetailMatcher = referenceSineDetailPattern.matcher(strReference);
|
623
|
if (! refSineDetailMatcher.matches()){
|
624
|
//TODO ?
|
625
|
}
|
626
|
|
627
|
Matcher articleMatcher = getMatcher(pArticleReference, strReference);
|
628
|
Matcher bookMatcher = getMatcher(pBookReference, strReference);
|
629
|
|
630
|
Matcher softArticleMatcher = getMatcher(pSoftArticleReference, strReference);
|
631
|
Matcher bookSectionMatcher = getMatcher(pBookSectionReference, strReference);
|
632
|
|
633
|
|
634
|
if(isInReference == false){
|
635
|
if (bookMatcher.matches() ){
|
636
|
result = parseBook(strReference);
|
637
|
}else{
|
638
|
logger.info("Non-InRef must be book but does not match book: "+ strReference);
|
639
|
result = ReferenceFactory.newBook();
|
640
|
makeUnparsableRefTitle(result, strReference);
|
641
|
}
|
642
|
}else{ //inRef
|
643
|
if (articleMatcher.matches()){
|
644
|
//article without separators like ","
|
645
|
result = parseArticle(strReference);
|
646
|
}else if (softArticleMatcher.matches()){
|
647
|
result = parseArticle(strReference);
|
648
|
}else if (bookSectionMatcher.matches()){
|
649
|
result = parseBookSection(strReference);
|
650
|
}else{
|
651
|
result = ReferenceFactory.newGeneric();
|
652
|
makeUnparsableRefTitle(result, "in " + strReference);
|
653
|
}
|
654
|
}
|
655
|
//make year
|
656
|
if (makeYear(result, year) == false){
|
657
|
//TODO
|
658
|
logger.warn("Year could not be parsed");
|
659
|
}
|
660
|
result.setProblemStarts(0);
|
661
|
result.setProblemEnds(strReference.length());
|
662
|
return result;
|
663
|
}
|
664
|
|
665
|
private void makeUnparsableRefTitle(INomenclaturalReference result, String reference){
|
666
|
//need to set both to protected otherwise titleCache is created from atomized parts
|
667
|
result.setTitleCache(reference, true);
|
668
|
result.setAbbrevTitleCache(reference, true);
|
669
|
result.addParsingProblem(ParserProblem.UnparsableReferenceTitle);
|
670
|
}
|
671
|
|
672
|
/**
|
673
|
* Parses a single date string. If the string is not parsable a StringNotParsableException is thrown
|
674
|
* @param singleDateString
|
675
|
* @return
|
676
|
* @throws StringNotParsableException
|
677
|
*/
|
678
|
private static Partial parseSingleDate(String singleDateString)
|
679
|
throws StringNotParsableException{
|
680
|
Partial dt = new Partial();
|
681
|
if (CdmUtils.isNumeric(singleDateString)){
|
682
|
try {
|
683
|
Integer year = Integer.valueOf(singleDateString.trim());
|
684
|
if (year > 1750 && year < 2050){
|
685
|
dt = dt.with(DateTimeFieldType.year(), year);
|
686
|
}else{
|
687
|
dt = null;
|
688
|
}
|
689
|
} catch (NumberFormatException e) {
|
690
|
logger.debug("Not a Integer format in getCalendar()");
|
691
|
throw new StringNotParsableException(singleDateString + "is not parsable as a single Date");
|
692
|
}
|
693
|
}
|
694
|
return dt;
|
695
|
}
|
696
|
|
697
|
|
698
|
/**
|
699
|
* Parses the publication date part.
|
700
|
* @param nomRef
|
701
|
* @param year
|
702
|
* @return If the string is not parsable <code>false</code>
|
703
|
* is returned. <code>True</code> otherwise
|
704
|
*/
|
705
|
private boolean makeYear(INomenclaturalReference nomRef, String year){
|
706
|
boolean result = true;
|
707
|
if (year == null){
|
708
|
return false;
|
709
|
}
|
710
|
if ("".equals(year.trim())){
|
711
|
return true;
|
712
|
}
|
713
|
VerbatimTimePeriod datePublished = TimePeriodParser.parseStringVerbatim(year);
|
714
|
|
715
|
if (nomRef.getType().equals(ReferenceType.BookSection)){
|
716
|
handleBookSectionYear((IBookSection)nomRef, datePublished);
|
717
|
}else if (nomRef instanceof Reference){
|
718
|
((Reference)nomRef).setDatePublished(datePublished);
|
719
|
}else{
|
720
|
throw new ClassCastException("nom Ref is not of type Reference but " + (nomRef.getClass()));
|
721
|
}
|
722
|
return result;
|
723
|
}
|
724
|
|
725
|
private String makeVolumeAndSeries(IVolumeReference nomRef, String strReference){
|
726
|
//volume
|
727
|
String volPart = null;
|
728
|
String pVolPhrase = volumeSeparator + volume + end;
|
729
|
Matcher volPhraseMatcher = getMatcher(pVolPhrase, strReference);
|
730
|
if (volPhraseMatcher.find()){
|
731
|
volPart = volPhraseMatcher.group(0);
|
732
|
strReference = strReference.substring(0, strReference.length() - volPart.length());
|
733
|
volPart = volPart.replaceFirst(pStart + volumeSeparator, "").trim();
|
734
|
nomRef.setVolume(volPart);
|
735
|
}
|
736
|
strReference = parseSeries(strReference, (ISeriesPart)nomRef);
|
737
|
return strReference;
|
738
|
}
|
739
|
|
740
|
private String parseSeries(String strReference, ISeriesPart nomRef) {
|
741
|
String seriesPart = null;
|
742
|
String seriesPhrase = pSeriesPart + end;
|
743
|
Matcher seriesPhraseMatcher = getMatcher(seriesPhrase, strReference);
|
744
|
if (seriesPhraseMatcher.find()){
|
745
|
seriesPart = seriesPhraseMatcher.group(0);
|
746
|
strReference = strReference.substring(0, strReference.length() - seriesPart.length());
|
747
|
seriesPart = seriesPart.startsWith(",")? seriesPart.substring(1): seriesPart;
|
748
|
seriesPart = seriesPart.endsWith(",")? seriesPart.substring(0, seriesPart.length()-1): seriesPart;
|
749
|
nomRef.setSeriesPart(seriesPart.trim());
|
750
|
}
|
751
|
return strReference;
|
752
|
}
|
753
|
|
754
|
private String makeEdition(IBook book, String strReference){
|
755
|
|
756
|
String editionPart = null;
|
757
|
Matcher editionPhraseMatcher = getMatcher(pEditionPart, strReference);
|
758
|
|
759
|
Matcher editionVolumeMatcher = getMatcher(pEditionVolPart, strReference);
|
760
|
boolean isEditionAndVol = editionVolumeMatcher.find();
|
761
|
|
762
|
if (editionPhraseMatcher.find()){
|
763
|
editionPart = editionPhraseMatcher.group(0);
|
764
|
int pos = strReference.indexOf(editionPart);
|
765
|
int posEnd = pos + editionPart.length();
|
766
|
if (isEditionAndVol){
|
767
|
posEnd++; //delete also comma
|
768
|
}
|
769
|
strReference = strReference.substring(0, pos) + strReference.substring(posEnd);
|
770
|
editionPart = editionPart.replaceFirst(pStart + editionSeparator, "").trim();
|
771
|
if (editionPart.startsWith(",")){
|
772
|
//, jubilee ed. case
|
773
|
editionPart = editionPart.substring(1).trim();
|
774
|
}else if (!editionPart.matches(pEdition)){
|
775
|
editionPart = "ed. " + editionPart;
|
776
|
}
|
777
|
book.setEdition(editionPart);
|
778
|
}
|
779
|
return strReference;
|
780
|
}
|
781
|
|
782
|
private IBook parseBook(String reference){
|
783
|
IBook result = ReferenceFactory.newBook();
|
784
|
reference = makeEdition(result, reference);
|
785
|
reference = makeVolumeAndSeries(result, reference);
|
786
|
result.setAbbrevTitle(reference);
|
787
|
return result;
|
788
|
}
|
789
|
|
790
|
private Reference parseArticle(String reference){
|
791
|
//if (articlePattern)
|
792
|
//(type, author, title, volume, editor, series;
|
793
|
Reference result = ReferenceFactory.newArticle();
|
794
|
reference = makeVolumeAndSeries(result, reference);
|
795
|
Reference inJournal = ReferenceFactory.newJournal();
|
796
|
inJournal.setAbbrevTitle(reference);
|
797
|
result.setInReference(inJournal);
|
798
|
return result;
|
799
|
}
|
800
|
|
801
|
private Reference parseBookSection(String reference){
|
802
|
Reference result = ReferenceFactory.newBookSection();
|
803
|
|
804
|
Pattern authorPattern = Pattern.compile("^" + authorTeam + referenceAuthorSeparator);
|
805
|
Matcher authorMatcher = authorPattern.matcher(reference);
|
806
|
boolean find = authorMatcher.find();
|
807
|
if (find){
|
808
|
String authorString = authorMatcher.group(0).trim();
|
809
|
String bookString = reference.substring(authorString.length()).trim();
|
810
|
authorString = authorString.substring(0, authorString.length() -1);
|
811
|
|
812
|
TeamOrPersonBase<?> authorTeam = author(authorString);
|
813
|
IBook inBook = parseBook(bookString);
|
814
|
inBook.setAuthorship(authorTeam);
|
815
|
result.setInBook(inBook);
|
816
|
}else{
|
817
|
logger.warn("Unexpected non matching book section author part");
|
818
|
//TODO do we want to record a 'problem' here?
|
819
|
result.setTitleCache(reference, true);
|
820
|
result.setAbbrevTitleCache(reference, true);
|
821
|
}
|
822
|
|
823
|
return result;
|
824
|
}
|
825
|
|
826
|
/**
|
827
|
* If the publication date of a book section and it's inBook do differ this is usually
|
828
|
* caused by the fact that a book has been published during a period, because originally
|
829
|
* it consisted of several parts that only later where put together to one book.
|
830
|
* If so, the book section's publication date may be a point in time (year or month of year)
|
831
|
* whereas the books publication date may be a period of several years.
|
832
|
* Therefore a valid nomenclatural reference string should use the book sections
|
833
|
* publication date rather then the book's publication date.<BR>
|
834
|
* This method in general adds the publication date to the book section.
|
835
|
* An exception exists if the publication date is a period. Then the parser
|
836
|
* assumes that the nomenclatural reference string does not follow the above rule but
|
837
|
* the books publication date is set.
|
838
|
* @param bookSection
|
839
|
* @param datePublished
|
840
|
*/
|
841
|
private void handleBookSectionYear(IBookSection bookSection, VerbatimTimePeriod datePublished){
|
842
|
if (datePublished == null || datePublished.getStart() == null || bookSection == null){
|
843
|
return;
|
844
|
}
|
845
|
if (datePublished.isPeriod() && bookSection.getInBook() != null){
|
846
|
bookSection.getInBook().setDatePublished(datePublished);
|
847
|
}else{
|
848
|
bookSection.setDatePublished(datePublished);
|
849
|
}
|
850
|
}
|
851
|
|
852
|
@Override
|
853
|
public INonViralName parseFullName(String fullNameString){
|
854
|
return parseFullName(fullNameString, null, null);
|
855
|
}
|
856
|
|
857
|
@Override
|
858
|
public INonViralName parseFullName(String fullNameString, NomenclaturalCode nomCode, Rank rank) {
|
859
|
|
860
|
if (fullNameString == null){
|
861
|
return null;
|
862
|
}else{
|
863
|
INonViralName result = getNonViralNameInstance(fullNameString, nomCode, rank);
|
864
|
parseFullName(result, fullNameString, rank, false);
|
865
|
return result;
|
866
|
}
|
867
|
}
|
868
|
|
869
|
@Override
|
870
|
public void parseFullName(INonViralName nameToBeFilledOrig, String fullNameStringOrig, Rank rank, boolean makeEmpty) {
|
871
|
INonViralName nameToBeFilled = nameToBeFilledOrig;
|
872
|
|
873
|
//TODO prol. etc.
|
874
|
boolean hasCheckRankProblem = false; //was rank guessed in a previous parsing process?
|
875
|
if (nameToBeFilled == null){
|
876
|
throw new IllegalArgumentException("NameToBeFilled must not be null in name parser");
|
877
|
}else{
|
878
|
hasCheckRankProblem = nameToBeFilled.hasProblem(ParserProblem.CheckRank);
|
879
|
nameToBeFilled.removeParsingProblem(ParserProblem.CheckRank);
|
880
|
}
|
881
|
String authorString = null;
|
882
|
if (fullNameStringOrig == null){
|
883
|
return;
|
884
|
}
|
885
|
if (makeEmpty){
|
886
|
makeEmpty(nameToBeFilled);
|
887
|
}
|
888
|
|
889
|
String fullNameString = fullNameStringOrig.replaceAll(oWs , " ").trim();
|
890
|
|
891
|
fullNameString = removeHybridBlanks(fullNameString);
|
892
|
fullNameString = removeSpNovBlanks(fullNameString);
|
893
|
String[] epi = pattern.split(fullNameString);
|
894
|
try {
|
895
|
//cultivars //TODO 2 implement cultivars
|
896
|
if ( parseCultivar(fullNameString, (TaxonName)nameToBeFilled) ){
|
897
|
//nothing to do, already parsed in method
|
898
|
}
|
899
|
|
900
|
else if (genusOrSupraGenusPattern.matcher(fullNameString).matches()){
|
901
|
//supraGeneric
|
902
|
if (rank != null && ! hasCheckRankProblem && (rank.isSupraGeneric()|| rank.isGenus())){
|
903
|
nameToBeFilled.setRank(rank);
|
904
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
905
|
}
|
906
|
//genus or guess rank
|
907
|
else {
|
908
|
rank = guessUninomialRank(nameToBeFilled, epi[0]);
|
909
|
nameToBeFilled.setRank(rank);
|
910
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
911
|
nameToBeFilled.addParsingProblem(ParserProblem.CheckRank);
|
912
|
nameToBeFilled.setProblemStarts(0);
|
913
|
nameToBeFilled.setProblemEnds(epi[0].length());
|
914
|
}
|
915
|
authorString = fullNameString.substring(epi[0].length());
|
916
|
}
|
917
|
//infra genus
|
918
|
else if (infraGenusPattern.matcher(fullNameString).matches()){
|
919
|
Rank infraGenericRank;
|
920
|
if ("[unranked]".equals(epi[1])||"[ranglos]".equals(epi[1])){
|
921
|
infraGenericRank = Rank.INFRAGENERICTAXON();
|
922
|
}else{
|
923
|
String infraGenericRankMarker = epi[1];
|
924
|
if (infraGenericRankMarker.startsWith(notho)){ //#3868
|
925
|
nameToBeFilled.setBinomHybrid(true);
|
926
|
infraGenericRankMarker = infraGenericRankMarker.substring(notho.length());
|
927
|
}else if(infraGenericRankMarker.startsWith("n")){
|
928
|
nameToBeFilled.setBinomHybrid(true);
|
929
|
infraGenericRankMarker = infraGenericRankMarker.substring(1);
|
930
|
}
|
931
|
infraGenericRank = Rank.getRankByIdInVoc(infraGenericRankMarker, nameToBeFilledOrig.getNameType());
|
932
|
}
|
933
|
nameToBeFilled.setRank(infraGenericRank);
|
934
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
935
|
nameToBeFilled.setInfraGenericEpithet(epi[2]);
|
936
|
authorString = fullNameString.substring(epi[0].length() + 1 + epi[1].length()+ 1 + epi[2].length());
|
937
|
}
|
938
|
//aggr. or group
|
939
|
else if (aggrOrGroupPattern.matcher(fullNameString).matches()){
|
940
|
nameToBeFilled.setRank(Rank.getRankByIdInVoc(epi[2]));
|
941
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
942
|
nameToBeFilled.setSpecificEpithet(epi[1]);
|
943
|
}
|
944
|
//species
|
945
|
else if (speciesPattern.matcher(fullNameString).matches()){
|
946
|
nameToBeFilled.setRank(Rank.SPECIES());
|
947
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
948
|
nameToBeFilled.setSpecificEpithet(normalizeSpNov(epi[1]));
|
949
|
authorString = fullNameString.substring(epi[0].length() + 1 + epi[1].length());
|
950
|
}
|
951
|
//species with infra generic epithet
|
952
|
else if (speciesWithInfraGenPattern.matcher(fullNameString).matches()){
|
953
|
nameToBeFilled.setRank(Rank.SPECIES());
|
954
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
955
|
nameToBeFilled.setInfraGenericEpithet(epi[2]);
|
956
|
nameToBeFilled.setSpecificEpithet(epi[4]);
|
957
|
authorString = fullNameString.substring(epi[0].length() + 2 + epi[2].length() + 2 + epi[4].length());
|
958
|
}
|
959
|
//autonym
|
960
|
else if (autonymPattern.matcher(fullNameString.replace(UTF8.HYBRID.toString(), "")).matches()){
|
961
|
String infraSpecRankMarker = epi[epi.length - 2];
|
962
|
boolean isTriHybrid = false;
|
963
|
if (infraSpecRankMarker.startsWith(notho)){ //#3868
|
964
|
nameToBeFilled.setTrinomHybrid(true);
|
965
|
infraSpecRankMarker = infraSpecRankMarker.substring(notho.length());
|
966
|
isTriHybrid = true;
|
967
|
}else if(infraSpecRankMarker.startsWith("n")){
|
968
|
nameToBeFilled.setTrinomHybrid(true);
|
969
|
infraSpecRankMarker = infraSpecRankMarker.substring(1);
|
970
|
isTriHybrid = true;
|
971
|
}
|
972
|
if (epi[1].startsWith(UTF8.HYBRID.toString())) {
|
973
|
nameToBeFilled.setBinomHybrid(true);
|
974
|
}
|
975
|
nameToBeFilled.setRank(Rank.getRankByIdInVoc(infraSpecRankMarker));
|
976
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
977
|
nameToBeFilled.setSpecificEpithet(epi[1]);
|
978
|
nameToBeFilled.setInfraSpecificEpithet(epi[epi.length - 1]);
|
979
|
int lenSpecies = 2 + epi[0].length()+epi[1].length();
|
980
|
int lenInfraSpecies = 2 + epi[epi.length - 2].length() + epi[epi.length - 1].length();
|
981
|
authorString = fullNameString.substring(lenSpecies, fullNameString.length() - lenInfraSpecies);
|
982
|
}
|
983
|
//genus autonym
|
984
|
else if (genusAutonymPattern.matcher(fullNameString.replace(UTF8.HYBRID.toString(), "")).matches()){
|
985
|
String infraGenRankMarker = epi[epi.length - 2];
|
986
|
|
987
|
nameToBeFilled.setRank(Rank.getRankByIdInVoc(infraGenRankMarker));
|
988
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
989
|
nameToBeFilled.setInfraGenericEpithet(epi[epi.length - 1]);
|
990
|
int lenGenus = 1 + epi[0].length();
|
991
|
int lenInfraGenus = 2 + epi[epi.length - 2].length() + epi[epi.length - 1].length();
|
992
|
authorString = fullNameString.substring(lenGenus, fullNameString.length() - lenInfraGenus);
|
993
|
}
|
994
|
//infraSpecies
|
995
|
else if (infraSpeciesPattern.matcher(fullNameString).matches()){
|
996
|
String infraSpecRankMarker = epi[2];
|
997
|
String infraSpecEpi = epi[3];
|
998
|
if ("tax.".equals(infraSpecRankMarker)){
|
999
|
infraSpecRankMarker += " " + epi[3];
|
1000
|
infraSpecEpi = epi[4];
|
1001
|
}
|
1002
|
Rank infraSpecificRank;
|
1003
|
if ("[unranked]".equals(infraSpecRankMarker)||"[ranglos]".equals(infraSpecRankMarker)){
|
1004
|
infraSpecificRank = Rank.INFRASPECIFICTAXON();
|
1005
|
}else{
|
1006
|
String localInfraSpecRankMarker;
|
1007
|
if (infraSpecRankMarker.startsWith(notho)){ //#3868
|
1008
|
nameToBeFilled.setTrinomHybrid(true);
|
1009
|
localInfraSpecRankMarker = infraSpecRankMarker.substring(notho.length());
|
1010
|
}else if(infraSpecRankMarker.startsWith("n")){
|
1011
|
nameToBeFilled.setTrinomHybrid(true);
|
1012
|
localInfraSpecRankMarker = infraSpecRankMarker.substring(1);
|
1013
|
}else{
|
1014
|
localInfraSpecRankMarker = infraSpecRankMarker;
|
1015
|
}
|
1016
|
infraSpecificRank = Rank.getRankByIdInVoc(localInfraSpecRankMarker);
|
1017
|
}
|
1018
|
nameToBeFilled.setRank(infraSpecificRank);
|
1019
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
1020
|
nameToBeFilled.setSpecificEpithet(epi[1]);
|
1021
|
nameToBeFilled.setInfraSpecificEpithet(infraSpecEpi);
|
1022
|
authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + infraSpecRankMarker.length() + 1 + infraSpecEpi.length());
|
1023
|
|
1024
|
}
|
1025
|
//infraSpecies without marker
|
1026
|
else if (zooInfraSpeciesPattern.matcher(fullNameString).matches()){
|
1027
|
String infraSpecEpi = epi[2];
|
1028
|
Rank infraSpecificRank = Rank.SUBSPECIES();
|
1029
|
nameToBeFilled.setRank(infraSpecificRank);
|
1030
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
1031
|
nameToBeFilled.setSpecificEpithet(epi[1]);
|
1032
|
nameToBeFilled.setInfraSpecificEpithet(infraSpecEpi);
|
1033
|
authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + infraSpecEpi.length());
|
1034
|
|
1035
|
}//old infraSpecies
|
1036
|
else if (oldInfraSpeciesPattern.matcher(fullNameString).matches()){
|
1037
|
boolean implemented = true;
|
1038
|
if (implemented){
|
1039
|
String infraSpecRankMarker = epi[2];
|
1040
|
String infraSpecEpi = epi[3];
|
1041
|
|
1042
|
Rank infraSpecificRank = Rank.getRankByNameOrIdInVoc(infraSpecRankMarker);
|
1043
|
nameToBeFilled.setRank(infraSpecificRank);
|
1044
|
nameToBeFilled.setGenusOrUninomial(epi[0]);
|
1045
|
nameToBeFilled.setSpecificEpithet(epi[1]);
|
1046
|
nameToBeFilled.setInfraSpecificEpithet(infraSpecEpi);
|
1047
|
authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + infraSpecRankMarker.length() + 1 + infraSpecEpi.length());
|
1048
|
|
1049
|
}else{
|
1050
|
nameToBeFilled.addParsingProblem(ParserProblem.OldInfraSpeciesNotSupported);
|
1051
|
nameToBeFilled.setTitleCache(fullNameString, true);
|
1052
|
// FIXME Quick fix, otherwise search would not deliver results for unparsable names
|
1053
|
nameToBeFilled.setNameCache(fullNameString,true);
|
1054
|
// END
|
1055
|
logger.info("Name string " + fullNameString + " could not be parsed because UnnnamedNamePhrase is not yet implemented!");
|
1056
|
}
|
1057
|
}
|
1058
|
//hybrid formula
|
1059
|
else if (hybridFormulaPattern.matcher(fullNameString).matches()){
|
1060
|
Set<HybridRelationship> existingRelations = new HashSet<>();
|
1061
|
Set<HybridRelationship> notToBeDeleted = new HashSet<>();
|
1062
|
|
1063
|
for ( HybridRelationship rel : nameToBeFilled.getHybridChildRelations()){
|
1064
|
existingRelations.add(rel);
|
1065
|
}
|
1066
|
|
1067
|
String givenNameString = "";
|
1068
|
String secondNameString = "";
|
1069
|
boolean isGivenName = true;
|
1070
|
for (String str : epi){
|
1071
|
if (str.matches(hybridSign)){
|
1072
|
isGivenName = false;
|
1073
|
}else if(isGivenName){
|
1074
|
givenNameString += " " + str;
|
1075
|
}else {
|
1076
|
secondNameString += " " + str;
|
1077
|
}
|
1078
|
}
|
1079
|
givenNameString = givenNameString.trim();
|
1080
|
secondNameString = secondNameString.trim();
|
1081
|
nameToBeFilled.setHybridFormula(true);
|
1082
|
NomenclaturalCode code = nameToBeFilled.getNameType();
|
1083
|
INonViralName firstName = this.parseFullName(givenNameString, code, rank);
|
1084
|
if (secondNameString.matches(abbrevHybridSecondPart)){
|
1085
|
secondNameString = extendSecondHybridPart(firstName, secondNameString);
|
1086
|
}
|
1087
|
INonViralName secondName = this.parseFullName(secondNameString, code, rank);
|
1088
|
HybridRelationship firstRel = nameToBeFilled.addHybridParent(firstName, HybridRelationshipType.FIRST_PARENT(), null);
|
1089
|
HybridRelationship second = nameToBeFilled.addHybridParent(secondName, HybridRelationshipType.SECOND_PARENT(), null);
|
1090
|
checkRelationExist(firstRel, existingRelations, notToBeDeleted);
|
1091
|
checkRelationExist(second, existingRelations, notToBeDeleted);
|
1092
|
|
1093
|
Rank newRank;
|
1094
|
Rank firstRank = firstName.getRank();
|
1095
|
Rank secondRank = secondName.getRank();
|
1096
|
|
1097
|
if (firstRank == null || (secondRank != null && firstRank.isHigher(secondRank))){
|
1098
|
newRank = secondRank;
|
1099
|
}else{
|
1100
|
newRank = firstRank;
|
1101
|
}
|
1102
|
nameToBeFilled.setRank(newRank);
|
1103
|
//remove not existing hybrid relation
|
1104
|
if (makeEmpty){
|
1105
|
Set<HybridRelationship> tmpChildRels = new HashSet<HybridRelationship>();
|
1106
|
tmpChildRels.addAll(nameToBeFilled.getHybridChildRelations());
|
1107
|
for (HybridRelationship rel : tmpChildRels){
|
1108
|
if (! notToBeDeleted.contains(rel)){
|
1109
|
nameToBeFilled.removeHybridRelationship(rel);
|
1110
|
}
|
1111
|
}
|
1112
|
}
|
1113
|
}
|
1114
|
//none
|
1115
|
else{
|
1116
|
nameToBeFilled.addParsingProblem(ParserProblem.UnparsableNamePart);
|
1117
|
nameToBeFilled.setTitleCache(fullNameString, true);
|
1118
|
// FIXME Quick fix, otherwise search would not deilver results for unparsable names
|
1119
|
nameToBeFilled.setNameCache(fullNameString, true);
|
1120
|
// END
|
1121
|
logger.info("no applicable parsing rule could be found for \"" + fullNameString + "\"");
|
1122
|
}
|
1123
|
//hybrid bits
|
1124
|
handleHybridBits(nameToBeFilled);
|
1125
|
if (!nameToBeFilled.isHybridFormula()){
|
1126
|
Set<HybridRelationship> hybridChildRelations = new HashSet<>();
|
1127
|
hybridChildRelations.addAll(nameToBeFilled.getHybridChildRelations());
|
1128
|
|
1129
|
for (HybridRelationship hybridRelationship: hybridChildRelations){
|
1130
|
nameToBeFilled.removeHybridRelationship(hybridRelationship);
|
1131
|
}
|
1132
|
}
|
1133
|
|
1134
|
//authors
|
1135
|
if (isNotBlank(authorString) ){
|
1136
|
handleAuthors(nameToBeFilled, fullNameString, authorString);
|
1137
|
}
|
1138
|
handleOriginalSpelling(nameToBeFilled);
|
1139
|
return;
|
1140
|
} catch (UnknownCdmTypeException e) {
|
1141
|
nameToBeFilled.addParsingProblem(ParserProblem.RankNotSupported);
|
1142
|
nameToBeFilled.setTitleCache(fullNameString, true);
|
1143
|
// FIXME Quick fix, otherwise search would not deliver results for unparsable names
|
1144
|
nameToBeFilled.setNameCache(fullNameString,true);
|
1145
|
// END
|
1146
|
logger.info("unknown rank (" + (rank == null? "null":rank) + ") or abbreviation in string " + fullNameString);
|
1147
|
//return result;
|
1148
|
return;
|
1149
|
}
|
1150
|
}
|
1151
|
|
1152
|
private String normalizeSpNov(String epi) {
|
1153
|
if (spNovPattern.matcher(epi).matches()){
|
1154
|
epi = epi.replace(".", ". ").replace("\\s+", " ").trim();
|
1155
|
}
|
1156
|
return epi;
|
1157
|
}
|
1158
|
|
1159
|
private String extendSecondHybridPart(INonViralName givenName, String secondNameString) {
|
1160
|
//first letter of genus given
|
1161
|
if (secondNameString.matches("^" + abbrevHybridGenus + ".*")){
|
1162
|
if (isNotBlank(givenName.getGenusOrUninomial())){
|
1163
|
if (secondNameString.substring(0,1).equals(givenName.getGenusOrUninomial().substring(0, 1))){
|
1164
|
secondNameString = secondNameString.replaceAll("^" + abbrevHybridGenus, givenName.getGenusOrUninomial() + " ");
|
1165
|
}
|
1166
|
}
|
1167
|
}else if (secondNameString.matches(abbrevHybridSecondPartOnlyInfraSpecies)){
|
1168
|
secondNameString = CdmUtils.concat(" " , givenName.getGenusOrUninomial(), givenName.getSpecificEpithet(), secondNameString);
|
1169
|
}else if (true){ //there will be further alternatives in future maybe
|
1170
|
secondNameString = CdmUtils.concat(" " , givenName.getGenusOrUninomial(), secondNameString);
|
1171
|
}
|
1172
|
return secondNameString;
|
1173
|
}
|
1174
|
|
1175
|
/**
|
1176
|
* Checks if a hybrid relation exists in the Set of existing relations
|
1177
|
* and <BR>
|
1178
|
* if it does not adds it to relations not to be deleted <BR>
|
1179
|
* if it does adds the existing relations to the relations not to be deleted
|
1180
|
*
|
1181
|
* @param firstRel
|
1182
|
* @param existingRelations
|
1183
|
* @param notToBeDeleted
|
1184
|
*/
|
1185
|
private void checkRelationExist(
|
1186
|
HybridRelationship newRelation,
|
1187
|
Set<HybridRelationship> existingRelations,
|
1188
|
Set<HybridRelationship> notToBeDeleted) {
|
1189
|
HybridRelationship relToKeep = newRelation;
|
1190
|
for (HybridRelationship existingRelation : existingRelations){
|
1191
|
if (existingRelation.equals(newRelation)){
|
1192
|
relToKeep = existingRelation;
|
1193
|
break;
|
1194
|
}
|
1195
|
}
|
1196
|
notToBeDeleted.add(relToKeep);
|
1197
|
}
|
1198
|
|
1199
|
private void handleHybridBits(INonViralName nameToBeFilled) {
|
1200
|
//uninomial
|
1201
|
String uninomial = CdmUtils.Nz(nameToBeFilled.getGenusOrUninomial());
|
1202
|
boolean isUninomialHybrid = uninomial.startsWith(hybridSign);
|
1203
|
if (isUninomialHybrid){
|
1204
|
nameToBeFilled.setMonomHybrid(true);
|
1205
|
nameToBeFilled.setGenusOrUninomial(uninomial.replace(hybridSign, ""));
|
1206
|
}
|
1207
|
//infrageneric
|
1208
|
String infrageneric = CdmUtils.Nz(nameToBeFilled.getInfraGenericEpithet());
|
1209
|
boolean isInfraGenericHybrid = infrageneric.startsWith(hybridSign);
|
1210
|
if (isInfraGenericHybrid){
|
1211
|
nameToBeFilled.setBinomHybrid(true);
|
1212
|
nameToBeFilled.setInfraGenericEpithet(infrageneric.replace(hybridSign, ""));
|
1213
|
}
|
1214
|
//species Epi
|
1215
|
String speciesEpi = CdmUtils.Nz(nameToBeFilled.getSpecificEpithet());
|
1216
|
boolean isSpeciesHybrid = speciesEpi.startsWith(hybridSign);
|
1217
|
if (isSpeciesHybrid){
|
1218
|
if (isBlank(infrageneric)){
|
1219
|
nameToBeFilled.setBinomHybrid(true);
|
1220
|
}else{
|
1221
|
nameToBeFilled.setTrinomHybrid(true);
|
1222
|
}
|
1223
|
nameToBeFilled.setSpecificEpithet(speciesEpi.replace(hybridSign, ""));
|
1224
|
}
|
1225
|
//infra species
|
1226
|
String infraSpeciesEpi = CdmUtils.Nz(nameToBeFilled.getInfraSpecificEpithet());
|
1227
|
boolean isInfraSpeciesHybrid = infraSpeciesEpi.startsWith(hybridSign);
|
1228
|
if (isInfraSpeciesHybrid){
|
1229
|
nameToBeFilled.setTrinomHybrid(true);
|
1230
|
nameToBeFilled.setInfraSpecificEpithet(infraSpeciesEpi.replace(hybridSign, ""));
|
1231
|
}
|
1232
|
|
1233
|
}
|
1234
|
|
1235
|
private String removeHybridBlanks(String fullNameString) {
|
1236
|
String result = fullNameString
|
1237
|
.replaceAll(oWs + "[xX]" + oWs + "(?=[A-Z])", " " + hybridSign + " ")
|
1238
|
.replaceAll(hybridFull, " " + hybridSign).trim();
|
1239
|
if (result.contains(hybridSign + " ") &&
|
1240
|
result.matches("^" + capitalEpiWord + oWs + hybridSign + oWs + nonCapitalEpiWord + ".*")){
|
1241
|
result = result.replaceFirst(hybridSign + oWs, hybridSign);
|
1242
|
}
|
1243
|
return result;
|
1244
|
}
|
1245
|
|
1246
|
|
1247
|
private String removeSpNovBlanks(String fullNameString) {
|
1248
|
Matcher spNovMatcher = spNovPattern.matcher(fullNameString);
|
1249
|
if (spNovMatcher.find()){
|
1250
|
String spNov = spNovMatcher.group(0);
|
1251
|
String spNovShort = spNov.replaceAll("\\s", "");
|
1252
|
if (spNov.length() != spNovShort.length()){
|
1253
|
fullNameString = fullNameString.replace(spNov, spNovShort);
|
1254
|
}
|
1255
|
}
|
1256
|
return fullNameString;
|
1257
|
}
|
1258
|
|
1259
|
|
1260
|
/**
|
1261
|
* Author parser for external use
|
1262
|
* @param nonViralName
|
1263
|
* @param authorString
|
1264
|
* @throws StringNotParsableException
|
1265
|
*/
|
1266
|
@Override
|
1267
|
public void parseAuthors(INonViralName nonViralNameOrig, String authorString) throws StringNotParsableException{
|
1268
|
INonViralName nonViralName = CdmBase.deproxy(nonViralNameOrig);
|
1269
|
TeamOrPersonBase<?>[] authors = new TeamOrPersonBase[4];
|
1270
|
Integer[] years = new Integer[4];
|
1271
|
NomenclaturalCode code = nonViralName.getNameType();
|
1272
|
fullAuthors(authorString, authors, years, code);
|
1273
|
nonViralName.setCombinationAuthorship(authors[0]);
|
1274
|
nonViralName.setExCombinationAuthorship(authors[1]);
|
1275
|
nonViralName.setBasionymAuthorship(authors[2]);
|
1276
|
nonViralName.setExBasionymAuthorship(authors[3]);
|
1277
|
if (nonViralName.isZoological()){
|
1278
|
IZoologicalName zooName = (IZoologicalName)nonViralName;
|
1279
|
zooName.setPublicationYear(years[0]);
|
1280
|
zooName.setOriginalPublicationYear(years[2]);
|
1281
|
}
|
1282
|
}
|
1283
|
|
1284
|
public void handleAuthors(INonViralName nameToBeFilled, String fullNameString, String authorString) {
|
1285
|
TeamOrPersonBase<?>[] authors = new TeamOrPersonBase[4];
|
1286
|
Integer[] years = new Integer[4];
|
1287
|
try {
|
1288
|
NomenclaturalCode code = nameToBeFilled.getNameType();
|
1289
|
fullAuthors(authorString, authors, years, code);
|
1290
|
} catch (StringNotParsableException e) {
|
1291
|
nameToBeFilled.addParsingProblem(ParserProblem.UnparsableAuthorPart);
|
1292
|
nameToBeFilled.setTitleCache(fullNameString, true);
|
1293
|
// FIXME Quick fix, otherwise search would not deliver results for unparsable names
|
1294
|
nameToBeFilled.setNameCache(fullNameString, true);
|
1295
|
// END
|
1296
|
logger.info("no applicable parsing rule could be found for \"" + fullNameString + "\"");
|
1297
|
}
|
1298
|
nameToBeFilled.setCombinationAuthorship(authors[0]);
|
1299
|
nameToBeFilled.setExCombinationAuthorship(authors[1]);
|
1300
|
nameToBeFilled.setBasionymAuthorship(authors[2]);
|
1301
|
nameToBeFilled.setExBasionymAuthorship(authors[3]);
|
1302
|
if (nameToBeFilled.isZoological()){
|
1303
|
IZoologicalName zooName = (IZoologicalName)nameToBeFilled;
|
1304
|
zooName.setPublicationYear(years[0]);
|
1305
|
zooName.setOriginalPublicationYear(years[2]);
|
1306
|
}
|
1307
|
}
|
1308
|
|
1309
|
/**
|
1310
|
* Guesses the rank of uninomial depending on the typical endings for ranks
|
1311
|
* @param nameToBeFilled
|
1312
|
* @param string
|
1313
|
*/
|
1314
|
private Rank guessUninomialRank(INonViralName nameToBeFilled, String uninomial) {
|
1315
|
Rank result = Rank.GENUS();
|
1316
|
if (nameToBeFilled.isBotanical()){
|
1317
|
if (false){
|
1318
|
//
|
1319
|
}else if (uninomial.endsWith("phyta") || uninomial.endsWith("mycota") ){ //plants, fungi
|
1320
|
result = Rank.SECTION_BOTANY();
|
1321
|
}else if (uninomial.endsWith("bionta")){
|
1322
|
result = Rank.SUBKINGDOM(); //TODO
|
1323
|
}else if (uninomial.endsWith("phytina")|| uninomial.endsWith("mycotina") ){ //plants, fungi
|
1324
|
result = Rank.SUBSECTION_BOTANY();
|
1325
|
}else if (uninomial.endsWith("opsida") || uninomial.endsWith("phyceae") || uninomial.endsWith("mycetes")){ //plants, algae, fungi
|
1326
|
result = Rank.CLASS();
|
1327
|
}else if (uninomial.endsWith("idae") || uninomial.endsWith("phycidae") || uninomial.endsWith("mycetidae")){ //plants, algae, fungi
|
1328
|
result = Rank.SUBCLASS();
|
1329
|
}else if (uninomial.endsWith("ales")){
|
1330
|
result = Rank.ORDER();
|
1331
|
}else if (uninomial.endsWith("ineae")){
|
1332
|
result = Rank.SUBORDER();
|
1333
|
}else if (uninomial.endsWith("aceae")){
|
1334
|
result = Rank.FAMILY();
|
1335
|
}else if (uninomial.endsWith("oideae")){
|
1336
|
result = Rank.SUBFAMILY();
|
1337
|
}else if (uninomial.endsWith("eae")){
|
1338
|
result = Rank.TRIBE();
|
1339
|
}else if (uninomial.endsWith("inae")){
|
1340
|
result = Rank.SUBTRIBE();
|
1341
|
}else if (uninomial.endsWith("ota")){
|
1342
|
result = Rank.KINGDOM(); //TODO
|
1343
|
}
|
1344
|
}else if (nameToBeFilled.isZoological()){
|
1345
|
if (false){
|
1346
|
//
|
1347
|
}else if (uninomial.endsWith("oideae")){
|
1348
|
result = Rank.SUPERFAMILY();
|
1349
|
}else if (uninomial.endsWith("idae")){
|
1350
|
result = Rank.FAMILY();
|
1351
|
}else if (uninomial.endsWith("inae")){
|
1352
|
result = Rank.SUBFAMILY();
|
1353
|
}else if (uninomial.endsWith("inae")){
|
1354
|
result = Rank.SUBFAMILY();
|
1355
|
}else if (uninomial.endsWith("ini")){
|
1356
|
result = Rank.TRIBE();
|
1357
|
}else if (uninomial.endsWith("ina")){
|
1358
|
result = Rank.SUBTRIBE();
|
1359
|
}
|
1360
|
}else{
|
1361
|
//
|
1362
|
}
|
1363
|
return result;
|
1364
|
}
|
1365
|
|
1366
|
/**
|
1367
|
* Parses the fullAuthorString
|
1368
|
* @param fullAuthorString
|
1369
|
* @return array of Teams containing the Team[0],
|
1370
|
* ExTeam[1], BasionymTeam[2], ExBasionymTeam[3]
|
1371
|
*/
|
1372
|
protected void fullAuthors (String fullAuthorStringOrig, TeamOrPersonBase<?>[] authors,
|
1373
|
Integer[] years, NomenclaturalCode code)
|
1374
|
throws StringNotParsableException{
|
1375
|
if (fullAuthorStringOrig == null || code == null){
|
1376
|
return;
|
1377
|
}
|
1378
|
String fullAuthorString = fullAuthorStringOrig.trim();
|
1379
|
|
1380
|
//Botanic
|
1381
|
if ( code.isBotanical() ){
|
1382
|
if (! fullBotanicAuthorStringPattern.matcher(fullAuthorString).matches() ){
|
1383
|
throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
|
1384
|
}
|
1385
|
}
|
1386
|
//Zoo
|
1387
|
else if ( code.isZoological() ){
|
1388
|
if (! fullZooAuthorStringPattern.matcher(fullAuthorString).matches() ){
|
1389
|
throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
|
1390
|
}
|
1391
|
}else {
|
1392
|
//TODO
|
1393
|
logger.warn ("Full author String parsable only for defined BotanicalNames or ZoologicalNames but this is " + code.getLabel());
|
1394
|
throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
|
1395
|
}
|
1396
|
fullAuthorsChecked(fullAuthorString, authors, years);
|
1397
|
}
|
1398
|
|
1399
|
/*
|
1400
|
* like fullTeams but without trim and match check
|
1401
|
*/
|
1402
|
protected void fullAuthorsChecked (String fullAuthorString, TeamOrPersonBase<?>[] authors, Integer[] years){
|
1403
|
int authorShipStart = 0;
|
1404
|
Matcher basionymMatcher = basionymPattern.matcher(fullAuthorString);
|
1405
|
|
1406
|
if (basionymMatcher.find(0)){
|
1407
|
|
1408
|
String basString = basionymMatcher.group();
|
1409
|
basString = basString.replaceFirst(basStart, "");
|
1410
|
basString = basString.replaceAll(basEnd, "").trim();
|
1411
|
authorShipStart = basionymMatcher.end(1);
|
1412
|
|
1413
|
TeamOrPersonBase<?>[] basAuthors = new TeamOrPersonBase[2];
|
1414
|
Integer[] basYears = new Integer[2];
|
1415
|
authorsAndEx(basString, basAuthors, basYears);
|
1416
|
authors[2]= basAuthors[0];
|
1417
|
years[2] = basYears[0];
|
1418
|
authors[3]= basAuthors[1];
|
1419
|
years[3] = basYears[1];
|
1420
|
}
|
1421
|
if (fullAuthorString.length() >= authorShipStart){
|
1422
|
TeamOrPersonBase<?>[] combinationAuthors = new TeamOrPersonBase[2];
|
1423
|
Integer[] combinationYears = new Integer[2];
|
1424
|
authorsAndEx(fullAuthorString.substring(authorShipStart), combinationAuthors, combinationYears);
|
1425
|
authors[0]= combinationAuthors[0] ;
|
1426
|
years[0] = combinationYears[0];
|
1427
|
authors[1]= combinationAuthors[1];
|
1428
|
years[1] = combinationYears[1];
|
1429
|
}
|
1430
|
}
|
1431
|
|
1432
|
|
1433
|
/**
|
1434
|
* Parses the author and ex-author String
|
1435
|
* @param authorShipStringOrig String representing the author and the ex-author team
|
1436
|
* @return array of Teams containing the Team[0] and the ExTeam[1]
|
1437
|
*/
|
1438
|
protected void authorsAndEx (String authorShipStringOrig, TeamOrPersonBase<?>[] authors, Integer[] years){
|
1439
|
//TODO noch allgemeiner am Anfang durch Replace etc.
|
1440
|
String authorShipString = authorShipStringOrig.trim();
|
1441
|
authorShipString = authorShipString.replaceFirst(oWs + "ex" + oWs, " ex. " );
|
1442
|
|
1443
|
//int authorEnd = authorTeamString.length();
|
1444
|
int authorBegin = 0;
|
1445
|
|
1446
|
Matcher exAuthorMatcher = exAuthorPattern.matcher(authorShipString);
|
1447
|
if (exAuthorMatcher.find(0)){
|
1448
|
authorBegin = exAuthorMatcher.end(0);
|
1449
|
int exAuthorEnd = exAuthorMatcher.start(0);
|
1450
|
String exString = authorShipString.substring(0, exAuthorEnd).trim();
|
1451
|
authors [1] = author(exString);
|
1452
|
}
|
1453
|
zooOrBotanicAuthor(authorShipString.substring(authorBegin), authors, years );
|
1454
|
}
|
1455
|
|
1456
|
/**
|
1457
|
* Parses the authorString and if it matches an botanical or zoological authorTeam it fills
|
1458
|
* the computes the AuthorTeam and fills it into the first field of the team array. Same applies
|
1459
|
* to the year in case of an zoological name.
|
1460
|
* @param authorString
|
1461
|
* @param team
|
1462
|
* @param year
|
1463
|
*/
|
1464
|
protected void zooOrBotanicAuthor(String authorString, TeamOrPersonBase<?>[] team, Integer[] year){
|
1465
|
if (authorString == null){
|
1466
|
return;
|
1467
|
}else if ((authorString = authorString.trim()).length() == 0){
|
1468
|
return;
|
1469
|
}
|
1470
|
Matcher zooAuthorAddidtionMatcher = zooAuthorAddidtionPattern.matcher(authorString);
|
1471
|
if (zooAuthorAddidtionMatcher.find()){
|
1472
|
int index = zooAuthorAddidtionMatcher.start(0);
|
1473
|
String strYear = authorString.substring(index);
|
1474
|
strYear = strYear.replaceAll(zooAuthorYearSeperator, "").trim();
|
1475
|
year[0] = Integer.valueOf(strYear);
|
1476
|
authorString = authorString.substring(0, index).trim();
|
1477
|
}
|
1478
|
team[0] = author(authorString);
|
1479
|
}
|
1480
|
|
1481
|
|
1482
|
/**
|
1483
|
* Parses an author (person or team) string and returns the Person or Team.
|
1484
|
* @param authorString String representing the author
|
1485
|
* @return a person or team
|
1486
|
*/
|
1487
|
public TeamOrPersonBase<?> author (String authorString){
|
1488
|
if (authorString == null){
|
1489
|
return null;
|
1490
|
}else if ((authorString = authorString.trim()).length() == 0){
|
1491
|
return null;
|
1492
|
}else if (! finalTeamSplitterPattern.matcher(authorString).find() && ! authorIsAlwaysTeam){
|
1493
|
//1 Person
|
1494
|
Person result = Person.NewInstance();
|
1495
|
authorString = normalizeNomenclaturalPersonString(authorString);
|
1496
|
result.setNomenclaturalTitle(authorString);
|
1497
|
return result;
|
1498
|
}else{
|
1499
|
return parsedTeam(authorString);
|
1500
|
}
|
1501
|
}
|
1502
|
|
1503
|
/**
|
1504
|
* Parses an authorString (representing a team into the single authors and add
|
1505
|
* them to the return Team.
|
1506
|
* @param authorString
|
1507
|
* @return Team
|
1508
|
*/
|
1509
|
protected Team parsedTeam(String authorString){
|
1510
|
Team result = Team.NewInstance();
|
1511
|
String[] authors = authorString.split(notFinalTeamSplitter);
|
1512
|
for (int i = 0; i < authors.length; i++){
|
1513
|
String author = authors[i];
|
1514
|
if ("al.".equals(author.trim()) && i == authors.length - 1){ //final al. is handled as hasMoreMembers
|
1515
|
result.setHasMoreMembers(true);
|
1516
|
}else{
|
1517
|
Person person = Person.NewInstance();
|
1518
|
author = normalizeNomenclaturalPersonString(author);
|
1519
|
person.setNomenclaturalTitle(author);
|
1520
|
result.addTeamMember(person);
|
1521
|
}
|
1522
|
}
|
1523
|
return result;
|
1524
|
}
|
1525
|
|
1526
|
private String normalizeNomenclaturalPersonString(String author) {
|
1527
|
if (removeSpaceAfterDot){
|
1528
|
author = author.replaceAll("\\.\\s", ".");
|
1529
|
}
|
1530
|
return author;
|
1531
|
}
|
1532
|
|
1533
|
// // Parsing of the given full name that has been identified as a cultivar already somwhere else.
|
1534
|
// // The ... cv. ... syntax is not covered here as it is not according the rules for naming cultivars.
|
1535
|
public boolean parseCultivar(String fullNameStr, TaxonName nameToBeFilled){
|
1536
|
|
1537
|
Matcher anyCultivarMatcher = anyCultivarPattern.matcher(fullNameStr);
|
1538
|
|
1539
|
if (anyCultivarMatcher.matches()){
|
1540
|
String scientificName = anyCultivarMatcher.group(1);
|
1541
|
String cultivarName = anyCultivarMatcher.group("cultivar");
|
1542
|
String groupName = anyCultivarMatcher.group("cultivarGroup");
|
1543
|
String brGroupName = anyCultivarMatcher.group("cultivarBrGroup");
|
1544
|
String grexName = anyCultivarMatcher.group("cultivarGrex");
|
1545
|
|
1546
|
//handle not exact matches
|
1547
|
if (isNotBlank(groupName) && isNotBlank(cultivarName)){
|
1548
|
return false;
|
1549
|
}else if (isNotBlank(brGroupName) && isNotBlank(grexName) && isBlank(cultivarName)){
|
1550
|
return false;
|
1551
|
}
|
1552
|
parseSimpleName(nameToBeFilled, scientificName, null, false);
|
1553
|
|
1554
|
if (isNotBlank(grexName)){
|
1555
|
nameToBeFilled.setRank(Rank.GREX());
|
1556
|
nameToBeFilled.setCultivarGroup(grexName);
|
1557
|
}
|
1558
|
if (isNotBlank(groupName) || isNotBlank(brGroupName)){
|
1559
|
nameToBeFilled.setRank(Rank.CULTIVARGROUP());
|
1560
|
if (isBlank(groupName)){
|
1561
|
groupName = brGroupName;
|
1562
|
}
|
1563
|
nameToBeFilled.setCultivarGroup(CdmUtils.concat(" ", grexName, groupName));
|
1564
|
}
|
1565
|
if (isNotBlank(cultivarName)){
|
1566
|
nameToBeFilled.setRank(Rank.CULTIVAR());
|
1567
|
cultivarName = cultivarName.substring(1, cultivarName.length()-1);
|
1568
|
nameToBeFilled.setCultivarName(cultivarName);
|
1569
|
}
|
1570
|
return true;
|
1571
|
}else{
|
1572
|
return false;
|
1573
|
}
|
1574
|
}
|
1575
|
|
1576
|
private void makeEmpty(INonViralName name){
|
1577
|
TaxonName nameToBeFilled = TaxonName.castAndDeproxy(name);
|
1578
|
nameToBeFilled.setRank(null);
|
1579
|
nameToBeFilled.setTitleCache(null, false);
|
1580
|
nameToBeFilled.setFullTitleCache(null, false);
|
1581
|
nameToBeFilled.setNameCache(null, false);
|
1582
|
|
1583
|
nameToBeFilled.setAppendedPhrase(null);
|
1584
|
nameToBeFilled.setBasionymAuthorship(null);
|
1585
|
nameToBeFilled.setCombinationAuthorship(null);
|
1586
|
nameToBeFilled.setExBasionymAuthorship(null);
|
1587
|
nameToBeFilled.setExCombinationAuthorship(null);
|
1588
|
nameToBeFilled.setAuthorshipCache(null, false);
|
1589
|
|
1590
|
|
1591
|
//delete problems except check rank
|
1592
|
makeProblemEmpty(nameToBeFilled);
|
1593
|
|
1594
|
// TODO ?
|
1595
|
//nameToBeFilled.setHomotypicalGroup(newHomotypicalGroup);
|
1596
|
|
1597
|
|
1598
|
nameToBeFilled.setGenusOrUninomial(null);
|
1599
|
nameToBeFilled.setInfraGenericEpithet(null);
|
1600
|
nameToBeFilled.setSpecificEpithet(null);
|
1601
|
nameToBeFilled.setInfraSpecificEpithet(null);
|
1602
|
|
1603
|
nameToBeFilled.setNomenclaturalMicroReference(null);
|
1604
|
nameToBeFilled.setNomenclaturalReference(null);
|
1605
|
|
1606
|
nameToBeFilled.setHybridFormula(false);
|
1607
|
nameToBeFilled.setMonomHybrid(false);
|
1608
|
nameToBeFilled.setBinomHybrid(false);
|
1609
|
nameToBeFilled.setTrinomHybrid(false);
|
1610
|
|
1611
|
nameToBeFilled.setAnamorphic(false);
|
1612
|
|
1613
|
nameToBeFilled.setBreed(null);
|
1614
|
nameToBeFilled.setOriginalPublicationYear(null);
|
1615
|
|
1616
|
//nom status handled in nom status parser, otherwise we loose additional information like reference etc.
|
1617
|
//hybrid relationships handled in hybrid formula and at end of fullNameParser
|
1618
|
}
|
1619
|
|
1620
|
|
1621
|
/**
|
1622
|
* If <code>true</code> author names are parsed such that spaces after the abbreviated
|
1623
|
* givenname are removed (IPNI style). see #7094
|
1624
|
*/
|
1625
|
public boolean isRemoveSpaceAfterDot() {
|
1626
|
return removeSpaceAfterDot;
|
1627
|
}
|
1628
|
/**
|
1629
|
* @see #isRemoveSpaceAfterDot()
|
1630
|
*/
|
1631
|
public void setRemoveSpaceAfterDot(boolean removeSpaceAfterDot) {
|
1632
|
this.removeSpaceAfterDot = removeSpaceAfterDot;
|
1633
|
}
|
1634
|
|
1635
|
private boolean isNotBlank(String str){
|
1636
|
return StringUtils.isNotBlank(str);
|
1637
|
}
|
1638
|
|
1639
|
private boolean isBlank(String str){
|
1640
|
return StringUtils.isBlank(str);
|
1641
|
}
|
1642
|
}
|