Skip to content

Commit

Permalink
Fixing another bug in case of units coming before quantity + unified …
Browse files Browse the repository at this point in the history
…the interval / range in a single code base #84
  • Loading branch information
lfoppiano committed Apr 1, 2019
1 parent 2f27840 commit bc4a5d2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ protected Element trainingExtraction(List<Measurement> measurements, String text
break;
pos++;
}
} else if (measurement.getType() == UnitUtilities.Measurement_Type.INTERVAL_MIN_MAX) {
} else if (measurement.getType() == UnitUtilities.Measurement_Type.INTERVAL_MIN_MAX ||
measurement.getType() == UnitUtilities.Measurement_Type.INTERVAL_BASE_RANGE) {
measure.addAttribute(new Attribute("type", "interval"));

Quantity quantityLeast = measurement.getQuantityLeast();
Quantity quantityMost = measurement.getQuantityMost();

if(measurement.getType() == UnitUtilities.Measurement_Type.INTERVAL_BASE_RANGE) {
quantityLeast = measurement.getQuantityBase();
quantityMost = measurement.getQuantityRange();
}

if (quantityLeast == null && quantityMost == null)
continue;

Expand Down Expand Up @@ -163,100 +170,42 @@ protected Element trainingExtraction(List<Measurement> measurements, String text
initPos = pos;
}
if (pos == startUL) {
measure.appendChild(text.substring(initPos, endUL));
// measure.appendChild(text.substring(initPos, endUL));
//
// pos = endUL;
// initPos = pos;

if (!addedMeasure) {
if (quantifiedObject != null) {
measure.addAttribute(new Attribute("ptr", "#" + quantifiedObjectID));
}
p.appendChild(text.substring(initPos, startUL));
p.appendChild(measure);
addedMeasure = true;
} else {
measure.appendChild(text.substring(initPos, startUL));
}
measure.appendChild(text.substring(startUL, endUL));
pos = endUL;
initPos = pos;
}
if ((pos == startUM) && (startUM != startUL)) {
measure.appendChild(text.substring(initPos, endUM));
pos = endUM;
initPos = pos;
}
if (pos == startO) {
p.appendChild(text.substring(initPos, startO));
p.appendChild(quantifiedObjectElement);
pos = endO;
initPos = pos;
}

if ((pos >= endQL) && (pos >= endQM)
&& (pos >= endUL) && (pos >= endUM)
&& (pos >= endO))
break;
pos++;
}
} else if (measurement.getType() == UnitUtilities.Measurement_Type.INTERVAL_BASE_RANGE) {
measure.addAttribute(new Attribute("type", "interval"));
Quantity quantityBase = measurement.getQuantityBase();
Quantity quantityRange = measurement.getQuantityRange();
// measure.appendChild(text.substring(initPos, endUM));
// pos = endUM;
// initPos = pos;

if (quantityRange == null)
continue;

int startQB = -1;
int endQB = -1;
int startUB = -1;
int endUB = -1;

if (quantityBase != null) {
startQB = quantityBase.getOffsetStart();
endQB = quantityBase.getOffsetEnd();
Unit unitL = quantityBase.getRawUnit();

if (unitL != null) {
startUB = unitL.getOffsetStart();
endUB = unitL.getOffsetEnd();
}
}

int startQR = quantityRange.getOffsetStart();
int endQR = quantityRange.getOffsetEnd();

Unit unitR = quantityRange.getRawUnit();

int startUR = -1;
int endUR = -1;
if (unitR != null) {
startUR = unitR.getOffsetStart();
endUR = unitR.getOffsetEnd();
}

boolean addedMeasure = false;
int initPos = pos;
while (pos < text.length()) {
if (pos == startQB) {
p.appendChild(text.substring(initPos, startQB));
measure.appendChild(text.substring(startQB, endQB));
p.appendChild(measure);
addedMeasure = true;
pos = endQB;
initPos = pos;
}
if (pos == startQR) {
if (!addedMeasure) {
p.appendChild(text.substring(initPos, startQR));
if (quantifiedObject != null) {
measure.addAttribute(new Attribute("ptr", "#" + quantifiedObjectID));
// markPosition(measure, quantifiedObject, startQR);
}
p.appendChild(text.substring(initPos, startUM));
p.appendChild(measure);
addedMeasure = true;
} else {
measure.appendChild(text.substring(initPos, startQR));
measure.appendChild(text.substring(initPos, startUM));
}
measure.appendChild(text.substring(startQR, endQR));

pos = endQR;
initPos = pos;
}
if (pos == startUB) {
measure.appendChild(text.substring(initPos, endUB));
pos = endUB;
initPos = pos;
}
if ((pos == startUR) && (startUR != startUB)) {
measure.appendChild(text.substring(initPos, endUR));
pos = endUR;
measure.appendChild(text.substring(startUM, endUM));
pos = endUM;
initPos = pos;
}
if (pos == startO) {
Expand All @@ -266,9 +215,9 @@ protected Element trainingExtraction(List<Measurement> measurements, String text
initPos = pos;
}

if ((pos >= endQB) && (pos >= endQR)
&& (pos >= endUB) && (pos >= endUR)
&& pos >= endO)
if ((pos >= endQL) && (pos >= endQM)
&& (pos >= endUL) && (pos >= endUM)
&& (pos >= endO))
break;
pos++;
}
Expand All @@ -292,7 +241,7 @@ protected Element trainingExtraction(List<Measurement> measurements, String text

while (pos < text.length()) {
if (pos == startQ) {
if(!measureAdded) {
if (!measureAdded) {
p.appendChild(text.substring(initPos, startQ));
} else {
measure.appendChild(text.substring(initPos, startQ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void testTrainingData_range() throws Exception {
Element out = target.trainingExtraction(measurementList, text);

verify(UUID.class);
assertThat(out.toXML(), is("<p xmlns=\"http://www.tei-c.org/ns/1.0\">We need <measure type=\"interval\">10 +- 2 grams</measure> of <quantifiedObject id=\"" + expectedID + "\">potatoes</quantifiedObject></p>"));
assertThat(out.toXML(), is("<p xmlns=\"http://www.tei-c.org/ns/1.0\">We need <measure type=\"interval\" ptr=\"#" + expectedID + "\">10 +- 2 grams</measure> of <quantifiedObject id=\"" + expectedID + "\">potatoes</quantifiedObject></p>"));
}

@Test
Expand Down Expand Up @@ -392,4 +392,33 @@ public void testTrainingExtraction2() throws Exception {
"after each stage.</p>"));

}

@Test
public void testTrainingExtraction3() throws Exception {
String text = "In recent years, there has been an increased interest in investigating the effect of aging on endurance running performances [1][2][3][4][5][6]. Over the last decades, the participation of master athletes (>40 years old) has increased, especially in the longer run distances such as half marathons [2,3], marathons [1][2][3] and ultra-marathons [7][8][9][10]. ";

List<Measurement> measurements = new ArrayList<>();
Measurement measurement1 = new Measurement(UnitUtilities.Measurement_Type.INTERVAL_MIN_MAX);
measurement1.setQuantityLeast(new Quantity("40", new Unit("decades", 159, 166), 210, 215));
measurement1.setQuantifiedObject(new QuantifiedObject("years", "year", 210, 215));
measurements.add(measurement1);

Measurement measurement2 = new Measurement(UnitUtilities.Measurement_Type.VALUE);
measurement2.setAtomicQuantity(new Quantity("half", null, 283, 287));
measurement2.setQuantifiedObject(new QuantifiedObject("marathons", "marathon", 288, 297));
measurements.add(measurement2);

UUID expectedID1 = UUID.randomUUID();
UUID expectedID2 = UUID.randomUUID();
mockStatic(UUID.class);
expect(UUID.randomUUID()).andReturn(expectedID1);
expect(UUID.randomUUID()).andReturn(expectedID2);
replay(UUID.class);

final Element element = target.trainingExtraction(measurements, text);

assertThat(element.toXML(), is("<p xmlns=\"http://www.tei-c.org/ns/1.0\">In recent years, there has been an increased interest in investigating the effect of aging on endurance running performances [1][2][3][4][5][6]. Over the last <measure type=\"interval\" ptr=\"#" + expectedID1 + "\">decades, the participation of master athletes (&gt;40 years</measure> old) has increased, especially in the longer run distances such as <measure type=\"value\" ptr=\"#" + expectedID2 + "\">half</measure> <quantifiedObject id=\"" + expectedID2 + "\">marathons</quantifiedObject> [2,3], marathons [1][2][3] and ultra-marathons [7][8][9][10]. </p>"));

}

}

0 comments on commit bc4a5d2

Please sign in to comment.