AffiliationAddressParser extends AbstractParser { protected ArrayList<Affiliation> resultBuilder(String result, List<LayoutToken> tokenizations, boolean usePreLabel) { ArrayList<Affiliation> fullAffiliations = null; if (result == null) { return fullAffiliations; } result = result.replace("\n\n", "\n \n"); try { if ((result == null) || (result.length() == 0)) { return null; } StringTokenizer st2 = new StringTokenizer(result, "\n"); String lastTag = null; org.grobid.core.data.Affiliation aff = new Affiliation(); int lineCount = 0; boolean hasInstitution; boolean hasDepartment = false; boolean hasAddress = false; boolean hasLaboratory; boolean newMarker = false; boolean useMarker = false; String currentMarker = null; int p = 0; while (st2.hasMoreTokens()) { boolean addSpace = false; String line = st2.nextToken(); Integer lineCountInt = lineCount; if (line.trim().length() == 0) { if (aff.notNull()) { if (fullAffiliations == null) { fullAffiliations = new ArrayList<Affiliation>(); } fullAffiliations.add(aff); aff = new Affiliation(); currentMarker = null; } hasInstitution = false; hasDepartment = false; hasLaboratory = false; hasAddress = false; continue; } String delimiter = "\t"; if (line.indexOf(delimiter) == -1) delimiter = " "; StringTokenizer st3 = new StringTokenizer(line, delimiter); int ll = st3.countTokens(); int i = 0; String s1 = null; String s2 = null; String s3 = null; ArrayList<String> localFeatures = new ArrayList<String>(); while (st3.hasMoreTokens()) { String s = st3.nextToken().trim(); if (i == 0) { s2 = s; boolean strop = false; while ((!strop) && (p < tokenizations.size())) { String tokOriginal = tokenizations.get(p).getText(); if (tokOriginal.equals(" ")) { addSpace = true; } else if (tokOriginal.equals(s)) { strop = true; } p++; } } else if (i == ll - 2) { s3 = s; } else if (i == ll - 1) { s1 = s; } else { localFeatures.add(s); } i++; } if (s1.equals("<marker>")) { if (currentMarker == null) currentMarker = s2; else { if (addSpace) { currentMarker += " " + s2; } else currentMarker += s2; } aff.setMarker(currentMarker); newMarker = false; useMarker = true; } else if (s1.equals("I-<marker>")) { currentMarker = s2; newMarker = true; useMarker = true; } if (newMarker) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } aff = new Affiliation(); hasInstitution = false; hasLaboratory = false; hasDepartment = false; hasAddress = false; if (currentMarker != null) { aff.setMarker(currentMarker); } newMarker = false; } else if (s1.equals("<institution>") || s1.equals("I-<institution>")) { if ((!usePreLabel) || ((usePreLabel) && (s3.equals("<affiliation>") || s3.equals("I-<affiliation>"))) ) { hasInstitution = true; if (aff.getInstitutions() != null) { if (s1.equals("I-<institution>") && (localFeatures.contains("LINESTART"))) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } hasInstitution = true; hasDepartment = false; hasLaboratory = false; hasAddress = false; aff = new Affiliation(); aff.addInstitution(s2); if (currentMarker != null) aff.setMarker(currentMarker.trim()); } else if (s1.equals("I-<institution>") && hasInstitution && hasAddress && (!lastTag.equals("<institution>"))) { if (aff.notNull()) { if (fullAffiliations == null) { fullAffiliations = new ArrayList<Affiliation>(); } fullAffiliations.add(aff); } hasInstitution = true; hasDepartment = false; hasLaboratory = false; hasAddress = false; aff = new Affiliation(); aff.addInstitution(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else if (s1.equals("I-<institution>")) { aff.addInstitution(s2); } else if (addSpace) { aff.extendLastInstitution(" " + s2); } else { aff.extendLastInstitution(s2); } } else { aff.addInstitution(s2); } } else if ((usePreLabel) && (s3.equals("<address>") || s3.equals("I-<address>"))) { if (aff.getAddressString() != null) { if (addSpace) { aff.setAddressString(aff.getAddressString() + " " + s2); } else { aff.setAddressString(aff.getAddressString() + s2); } } else { aff.setAddressString(s2); } } } else if (s1.equals("<addrLine>") || s1.equals("I-<addrLine>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getAddrLine() != null) { if (s1.equals(lastTag) || lastTag.equals("I-<addrLine>")) { if (s1.equals("I-<addrLine>")) { aff.setAddrLine(aff.getAddrLine() + " ; " + s2); } else if (addSpace) { aff.setAddrLine(aff.getAddrLine() + " " + s2); } else { aff.setAddrLine(aff.getAddrLine() + s2); } } else { aff.setAddrLine(aff.getAddrLine() + ", " + s2); } } else { aff.setAddrLine(s2); } hasAddress = true; } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (s1.equals(lastTag)) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(aff.getAffiliationString() + " ; " + s2); } } else { aff.setAffiliationString(s2); } } } else if (s1.equals("<department>") || s1.equals("I-<department>")) { if ((!usePreLabel) || ((usePreLabel) && (s3.equals("<affiliation>") || s3.equals("I-<affiliation>"))) ) { if (aff.getDepartments() != null) { if ((s1.equals("I-<department>")) && (localFeatures.contains("LINESTART")) ) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } hasInstitution = false; hasDepartment = true; hasLaboratory = false; hasAddress = false; aff = new Affiliation(); aff.addDepartment(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else if ((s1.equals("I-<department>")) && hasDepartment && hasAddress && !lastTag.equals("<department>")) { if (aff.notNull()) { if (fullAffiliations == null) { fullAffiliations = new ArrayList<Affiliation>(); } fullAffiliations.add(aff); } hasInstitution = false; hasDepartment = true; hasAddress = false; hasLaboratory = false; aff = new Affiliation(); aff.addDepartment(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else if (s1.equals("I-<department>")) { aff.addDepartment(s2); } else if (addSpace) { aff.extendLastDepartment(" " + s2); } else { aff.extendLastDepartment(s2); } } else if (aff.getInstitutions() != null) { if ((s1.equals("I-<department>")) && hasAddress && (localFeatures.contains("LINESTART")) ) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } hasInstitution = false; hasDepartment = true; hasLaboratory = false; hasAddress = false; aff = new Affiliation(); aff.addDepartment(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else { aff.addDepartment(s2); } } else { aff.addDepartment(s2); } } else if ((usePreLabel) && (s3.equals("<address>") || s3.equals("I-<address>"))) { if (aff.getAddressString() != null) { if (addSpace) { aff.setAddressString(aff.getAddressString() + " " + s2); } else { aff.setAddressString(aff.getAddressString() + s2); } } else { aff.setAddressString(s2); } } } else if (s1.equals("<laboratory>") || s1.equals("I-<laboratory>")) { if ((!usePreLabel) || ((usePreLabel) && (s3.equals("<affiliation>") || s3.equals("I-<affiliation>"))) ) { hasLaboratory = true; if (aff.getLaboratories() != null) { if (s1.equals("I-<laboratory>") && (localFeatures.contains("LINESTART"))) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } hasInstitution = false; hasLaboratory = true; hasDepartment = false; hasAddress = false; aff = new Affiliation(); aff.addLaboratory(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else if (s1.equals("I-<laboratory>") && hasLaboratory && hasAddress && (!lastTag.equals("<laboratory>"))) { if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); } hasInstitution = false; hasLaboratory = true; hasDepartment = false; hasAddress = false; aff = new Affiliation(); aff.addLaboratory(s2); if (currentMarker != null) { aff.setMarker(currentMarker.trim()); } } else if (s1.equals("I-<laboratory>")) { aff.addLaboratory(s2); } else if (addSpace) { aff.extendLastLaboratory(" " + s2); } else { aff.extendLastLaboratory(s2); } } else { aff.addLaboratory(s2); } } else if ((usePreLabel) && (s3.equals("<address>") || s3.equals("I-<address>"))) { if (aff.getAddressString() != null) { if (addSpace) { aff.setAddressString(aff.getAddressString() + " " + s2); } else { aff.setAddressString(aff.getAddressString() + s2); } } else { aff.setAddressString(s2); } } } else if (s1.equals("<country>") || s1.equals("I-<country>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getCountry() != null) { if (s1.equals("I-<country>")) { aff.setCountry(aff.getCountry() + ", " + s2); } else if (addSpace) { aff.setCountry(aff.getCountry() + " " + s2); } else { aff.setCountry(aff.getCountry() + s2); } } else { aff.setCountry(s2); } hasAddress = true; } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(s2); } } } else if (s1.equals("<postCode>") || s1.equals("I-<postCode>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getPostCode() != null) { if (s1.equals("I-<postCode>")) { aff.setPostCode(aff.getPostCode() + ", " + s2); } else if (addSpace) { aff.setPostCode(aff.getPostCode() + " " + s2); } else { aff.setPostCode(aff.getPostCode() + s2); } } else { aff.setPostCode(s2); } } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(s2); } } } else if (s1.equals("<postBox>") || s1.equals("I-<postBox>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getPostBox() != null) { if (s1.equals("I-<postBox>")) { aff.setPostBox(aff.getPostBox() + ", " + s2); } else if (addSpace) { aff.setPostBox(aff.getPostBox() + " " + s2); } else { aff.setPostBox(aff.getPostBox() + s2); } } else { aff.setPostBox(s2); } } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(s2); } } } else if (s1.equals("<region>") || s1.equals("I-<region>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getRegion() != null) { if (s1.equals("I-<region>")) { aff.setRegion(aff.getRegion() + ", " + s2); } else if (addSpace) { aff.setRegion(aff.getRegion() + " " + s2); } else { aff.setRegion(aff.getRegion() + s2); } } else { aff.setRegion(s2); } } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(s2); } } } else if (s1.equals("<settlement>") || s1.equals("I-<settlement>")) { if ((!usePreLabel) || ((usePreLabel) && ((s3.equals("<address>") || s3.equals("I-<address>"))))) { if (aff.getSettlement() != null) { if (s1.equals("I-<settlement>")) { aff.setSettlement(aff.getSettlement() + ", " + s2); } else if (addSpace) { aff.setSettlement(aff.getSettlement() + " " + s2); } else { aff.setSettlement(aff.getSettlement() + s2); } } else { aff.setSettlement(s2); } hasAddress = true; } else if ((usePreLabel) && ((s3.equals("<affiliation>") || s3.equals("I-<affiliation>")))) { if (aff.getAffiliationString() != null) { if (addSpace) { aff.setAffiliationString(aff.getAffiliationString() + " " + s2); } else { aff.setAffiliationString(aff.getAffiliationString() + s2); } } else { aff.setAffiliationString(s2); } } } if (!s1.endsWith("<marker>")) { if (aff.getRawAffiliationString() == null) { aff.setRawAffiliationString(s2); } else if (addSpace) { aff.setRawAffiliationString(aff.getRawAffiliationString() + " " + s2); } else { aff.setRawAffiliationString(aff.getRawAffiliationString() + s2); } } lastTag = s1; lineCount++; newMarker = false; } if (aff.notNull()) { if (fullAffiliations == null) fullAffiliations = new ArrayList<Affiliation>(); fullAffiliations.add(aff); hasInstitution = false; hasDepartment = false; hasAddress = false; } if (fullAffiliations != null) { for (Affiliation affi : fullAffiliations) { affi.clean(); } } } catch (Exception e) { throw new GrobidException("An exception occurred while running Grobid.", e); } return fullAffiliations; } AffiliationAddressParser(); ArrayList<Affiliation> processing(String input); List<Affiliation> processReflow(String result, List<LayoutToken> tokenizations); StringBuilder trainingExtraction(String result,
                                           List<LayoutToken> tokenizations); public Lexicon lexicon; }
@Test public void shouldNotFailOnEmptyLabelResult() throws Exception { String labelResult = ""; List<LayoutToken> tokenizations = Collections.emptyList(); List<Affiliation> result = this.target.resultBuilder( labelResult, tokenizations, NO_USE_PRELABEL ); assertThat("affiliations should be null", result, is(nullValue())); }