in opennlp-tools/src/main/java/opennlp/tools/formats/BioNLP2004NameSampleStream.java [90:186]
public NameSample read() throws IOException {
List<String> sentence = new ArrayList<>();
List<String> tags = new ArrayList<>();
boolean isClearAdaptiveData = false;
// Empty line indicates end of sentence
String line;
while ((line = lineStream.read()) != null && !StringUtil.isEmpty(line.trim())) {
if (line.startsWith("###MEDLINE:")) {
isClearAdaptiveData = true;
lineStream.read();
continue;
}
if (line.contains("ABSTRACT TRUNCATED"))
continue;
String[] fields = line.split("\t");
if (fields.length == 2) {
sentence.add(fields[0]);
tags.add(fields[1]);
}
else {
throw new IOException("Expected two fields per line in training data, got " +
fields.length + " for line '" + line + "'!");
}
}
if (!sentence.isEmpty()) {
// convert name tags into spans
List<Span> names = new ArrayList<>();
int beginIndex = -1;
int endIndex = -1;
for (int i = 0; i < tags.size(); i++) {
String tag = tags.get(i);
if (tag.endsWith("DNA") && (types & GENERATE_DNA_ENTITIES) == 0)
tag = CODEC_TAG_O;
if (tag.endsWith("protein") && (types & GENERATE_PROTEIN_ENTITIES) == 0)
tag = CODEC_TAG_O;
if (tag.endsWith("cell_type") && (types & GENERATE_CELLTYPE_ENTITIES) == 0)
tag = CODEC_TAG_O;
if (tag.endsWith("cell_line") && (types & GENERATE_CELLTYPE_ENTITIES) == 0)
tag = CODEC_TAG_O;
if (tag.endsWith("RNA") && (types & GENERATE_RNA_ENTITIES) == 0)
tag = CODEC_TAG_O;
if (tag.startsWith(CODEC_TAG_B)) {
if (beginIndex != -1) {
names.add(new Span(beginIndex, endIndex, tags.get(beginIndex).substring(2)));
}
beginIndex = i;
endIndex = i + 1;
}
else if (tag.startsWith(CODEC_TAG_I)) {
endIndex++;
}
else if (tag.equals(CODEC_TAG_O)) {
if (beginIndex != -1) {
names.add(new Span(beginIndex, endIndex, tags.get(beginIndex).substring(2)));
beginIndex = -1;
endIndex = -1;
}
}
else {
throw new IOException("Invalid tag: " + tag);
}
}
// if one span remains, create it here
if (beginIndex != -1)
names.add(new Span(beginIndex, endIndex, tags.get(beginIndex).substring(2)));
return new NameSample(sentence.toArray(new String[0]),
names.toArray(new Span[0]), isClearAdaptiveData);
}
else if (line != null) {
// Just filter out empty events, if two lines in a row are empty
return read();
}
else {
// source stream is not returning anymore lines
return null;
}
}