// status changes list (unless Maximum special case)
boolean isolate = false;
if (!findSubSection.hasNext())
{
DrugChangeStatusAnnotation dsa = (DrugChangeStatusAnnotation) statusChangeItr.next();
// Maximum case means the drug mention elements should
// be overridden by this value
if (((dsa.getChangeStatus().compareTo(
DrugChangeStatusElement.MAXIMUM_STATUS) != 0)
&& dsa.getEnd() < holdRightEnd)
&& (localSpan[0]<dsa.getBegin() && localSpan[1]> dsa.getEnd()))
{
holdStatusChanges.add(dsa);
} else if (dsa.getChangeStatus().compareTo(
DrugChangeStatusElement.MAXIMUM_STATUS) == 0
&& dsa.getEnd() < holdRightEnd)
{
maxExists = true;
maxOffsetEnd = dsa.getEnd();
}
} else
{
statusChangeItr.next();// Added this line to make sure the the next DrugChangeStatusAnnotation in the event that there is no subsection to look at
boolean noWeirdError = true;
boolean pullOut = false;
while (!pullOut && !isolate && findSubSection.hasNext()
&& noWeirdError)
{
try
{
// each status change is checked against all
// available sub-spans in that range
SubSectionAnnotation sub = (SubSectionAnnotation) findSubSection.next();
Iterator findStartLF = FSUtil.getAnnotationsIteratorInSpan(
jcas, NewlineToken.type, holdLeftStart, sub.getBegin() + 1);
Iterator findEndLF = FSUtil.getAnnotationsIteratorInSpan(jcas,
NewlineToken.type, sub.getEnd(), holdRightEnd);
if (findStartLF.hasNext() && findEndLF.hasNext())
{
while (findStartLF.hasNext())
{
// int countSymbols = 0;
NewlineToken nta = (NewlineToken) findStartLF.next();
// Iterator findSymbols =
// FSUtil.getAnnotationsInSpanIterator(jcas,
// SymbolToken.type,
// nta.getEnd(), sub.getBegin());
//
// while (findSymbols.hasNext())
// {
// findSymbols.next();
// countSymbols++;
// }
int countSymbols = FSUtil.countAnnotationsInSpan(jcas,
SymbolToken.type, nta.getEnd(), sub.getBegin());
if ((nta.getEnd() + countSymbols + 1) >= sub.getBegin())
{
isolate = true;
holdRightEnd = sub.getBegin();
end = sub.getBegin();
}
}
if (!isolate)
{
DrugChangeStatusAnnotation dsa = (DrugChangeStatusAnnotation) statusChangeItr.next();
holdStatusChanges.add(dsa);
pullOut = true;
sub.removeFromIndexes();
}
} else if (findEndLF.hasNext())
{
// subsection is on a prior separate line than the rest
// of the content
holdLeftStart = sub.getEnd();
// sub.removeFromIndexes();
} else if (sub.getBegin() > tokenAnt.getEnd())
{
end = sub.getBegin();
holdRightEnd = sub.getBegin();
sub.removeFromIndexes();
} else
{
holdLeftStart = sub.getEnd();
holdRightEnd = tokenAnt.getBegin();
}
} catch (NoSuchElementException nsee)
{
noWeirdError = false;
iv_logger.info(nsee.getLocalizedMessage());
}
}
}
}
// handles cases like "then discontinue" so the two change status mentions are merged and the last
// value is used for the change status i.e. 'discontinue'
List modifiedOrderDrugStatusChanges = new ArrayList();
Iterator sortStatusChanges = sortAnnotations(holdStatusChanges.toArray()).iterator();
Iterator sortNextStatusChanges = sortAnnotations(holdStatusChanges.toArray()).iterator();
// increment sortNextStatusChanges
if (sortNextStatusChanges.hasNext()) sortNextStatusChanges.next();
boolean skipNext = false;
int checkSkippedOffsetBegin = 0, checkSkippedOffsetEnd = 0;
while (sortStatusChanges.hasNext()) {
DrugChangeStatusAnnotation hos1 = (DrugChangeStatusAnnotation) sortStatusChanges.next();
if (sortNextStatusChanges.hasNext()) {
DrugChangeStatusAnnotation hos2 = (DrugChangeStatusAnnotation) sortNextStatusChanges.next();
if (hos1.getBegin() == hos2.getBegin()) {
if (hos1.getEnd() >= hos2.getEnd()) {
skipNext = true;
checkSkippedOffsetBegin = hos2.getBegin();
checkSkippedOffsetEnd = hos2.getEnd();
hos2.removeFromIndexes();
modifiedOrderDrugStatusChanges.add(hos1);
} else {
iv_logger.info("found reverse case . . need to handle");
}
} else if (!skipNext) {
modifiedOrderDrugStatusChanges.add(hos1);
} else
skipNext = false;
}
else if (checkSkippedOffsetBegin == 0 || (checkSkippedOffsetBegin != hos1.getBegin() && checkSkippedOffsetEnd != hos1.getEnd())){
modifiedOrderDrugStatusChanges.add(hos1);
}
}
Iterator orderedStatusChanges = sortAnnotations(holdStatusChanges.toArray()).iterator();
Iterator orderedDrugStatusChanges = sortAnnotations(holdStatusChanges.toArray()).iterator();
if (modifiedOrderDrugStatusChanges.size() > 0 ) {
int [] newSpan = {begin, end};
newSpan = statusChangePhraseGenerator ( jcas, begin, end, maxExists, uniqueNER,
orderedStatusChanges, modifiedOrderDrugStatusChanges, relatedStatus, drugTokenAnt,
globalDrugNER, countNER );
begin = newSpan[0];
end = newSpan[1];
if ((drugTokenAnt.getDrugChangeStatus() != null && drugTokenAnt.getDrugChangeStatus().equals(DrugChangeStatusToken.NOCHANGE)) ||
(drugTokenAnt.getDrugChangeStatus() != null && drugTokenAnt.getDrugChangeStatus().equals(DrugChangeStatusToken.OTHER))) {
keepNoChangeStatus = true;
if (drugTokenAnt.getDrugChangeStatus().equals(DrugChangeStatusToken.OTHER))
drugTokenAnt.setDrugChangeStatus(DrugChangeStatusToken.NOCHANGE);
}
// No change is default state since the change state has been handled
}
DrugMention dm = new DrugMention(jcas, begin, end);
boolean overrideStatus = false;
boolean statusFound = false;
if (!keepNoChangeStatus) {
// All entries may not be appropriate, so some
// filtering
// may need to be implemented here
JFSIndexRepository indexes = jcas.getJFSIndexRepository();
Iterator subSectionItr = indexes.getAnnotationIndex(
SubSectionAnnotation.type).iterator();
String statusKey = null;
while (subSectionItr.hasNext() && !statusFound)
{
SubSectionAnnotation ssid = (SubSectionAnnotation) subSectionItr.next();
if (ssid.getSubSectionBodyBegin() <= tokenAnt.getBegin()
&& ssid.getSubSectionBodyEnd() >= tokenAnt.getEnd())
{
// Look for special case where date comes before the
// drug mention
// A better means to locate the beginning of the chunk
// is lacking here mainly due
// to the fact that the sentence annotator cannot be
// trusted to find the beginning
// accurately.
boolean overrideDate = false;
Iterator statusSpecialDateItr = FSUtil.getAnnotationsIteratorInSpan(jcas, DateAnnotation.type, ssid.getEnd(), drugTokenAnt.getBegin());
while (statusSpecialDateItr.hasNext() && !overrideDate)
{
DateAnnotation specialDate = (DateAnnotation) statusSpecialDateItr.next();
Iterator findLF = FSUtil.getAnnotationsIteratorInSpan(jcas,
NewlineToken.type, ssid.getEnd(), specialDate.getBegin());
if (!findLF.hasNext())
{
// if (specialDate.getEnd() <=
// drugTokenAnt.getBegin() ){
drugTokenAnt.setStartDate(specialDate.getCoveredText());
overrideDate = true;
}
}
DrugChangeStatusAnnotation dsa = null;
if (orderedDrugStatusChanges.hasNext())
{
dsa = (DrugChangeStatusAnnotation) orderedDrugStatusChanges.next();
}
if (dsa != null
&& (dsa.getChangeStatus().compareTo(DrugChangeStatusElement.START_STATUS) == 0 ||
dsa.getChangeStatus().compareTo(DrugChangeStatusElement.STOP_STATUS) == 0))
{
// Should we override here? Let's get only the first
// one as an override
drugTokenAnt.setDrugChangeStatus(dsa.getChangeStatus());
} else
{
statusKey = dm.convertToChangeStatus(ssid.getCoveredText());
if (ssid.getStatus() == 1)
{
// drugTokenAnt.setCertainty(-1);
statusKey = DrugChangeStatusToken.STOP;
}
if (statusKey.compareTo(DrugChangeStatusToken.NOCHANGE) == 0)
{
Iterator oneDrugChangeStatus = FSUtil.getAnnotationsIteratorInSpan(jcas,
DrugChangeStatusAnnotation.type, ssid.getBegin(), ssid.getEnd() + 1);
if (oneDrugChangeStatus.hasNext())
{
dsa = (DrugChangeStatusAnnotation) oneDrugChangeStatus.next();
drugTokenAnt.setDrugChangeStatus(dsa.getChangeStatus());
statusKey = dsa.getChangeStatus();
}
}
drugTokenAnt.setStatus(ssid.getStatus());
dm.setDrugChangeStatusElement(statusKey, begin, end);
statusFound = true;
}
}
}
// Look for special case where status comes before the drug
// mention
// A better means to locate the beginning of the chunk is
// lacking here mainly due
// to the fact that the sentence annotator cannot be trusted to
// find the beginning
// accurately.
Iterator statusSpecialChangeItr = FSUtil.getAnnotationsIteratorInSpan(jcas, DrugChangeStatusAnnotation.type, begin - 20, drugTokenAnt.getBegin() + 1);
while (statusSpecialChangeItr.hasNext())
{
DrugChangeStatusAnnotation specialDsa = (DrugChangeStatusAnnotation) statusSpecialChangeItr.next();
if (specialDsa.getEnd() + 1 == drugTokenAnt.getBegin()
&& relatedStatus == null)
{
drugTokenAnt.setDrugChangeStatus(specialDsa.getChangeStatus());
drugTokenAnt.setChangeStatusBegin(specialDsa.getBegin());
drugTokenAnt.setChangeStatusEnd(specialDsa.getEnd());
overrideStatus = true;
}
}
}
// If a strength token is discovered before the next