Tuesday, 20 August 2013

How to get the value at a special position of a set that create the hashmap

How to get the value at a special position of a set that create the hashmap

I am currently confuse and get stuck at the logic of the code. Go from the
beginning to the end, I run to a point that a condition happens and I want
to track back and get the text at that position, but I do not know how to
go inverse way to get it. I also give example of output to the comment,
which text should be extract and where it should be printed. Here is the
code:
public <ANNOTATION_TYPE, SPAN_TYPE> void add(
Collection<? extends ANNOTATION_TYPE> referenceAnnotations,
Collection<? extends ANNOTATION_TYPE> predictedAnnotations,
Function<ANNOTATION_TYPE, SPAN_TYPE> annotationToSpan) throws IOException {
// map gold spans to their outcomes
Map<SPAN_TYPE, String> referenceSpanOutcomes = new HashMap<SPAN_TYPE,
String>();
for (ANNOTATION_TYPE ann : referenceAnnotations) {
NamedEntityMention nm = (NamedEntityMention) ann;
NamedEntity ne = nm.getMentionedEntity();
String text= nm.getCoveredText(); // Here is the 1st text that should be
extract, but only at the place when the "else" condition is satisfied
String out = ne.getEntityType();
referenceSpanOutcomes.put(annotationToSpan.apply(ann), out);
}
// map system spans to their outcomes
Map<SPAN_TYPE, String> predictedSpanOutcomes = new HashMap<SPAN_TYPE,
String>();
for (ANNOTATION_TYPE ann : predictedAnnotations) {
String out = getFeature((Annotation)ann, "mentionType");
predictedSpanOutcomes.put(annotationToSpan.apply(ann), out);
NamedEntityMention nm2 = (NamedEntityMention) ann;
NamedEntity ne2 = nm2.getMentionedEntity();
String text2= nm2.getCoveredText(); // Here is the 2nd text that
should be extract, but only at the place when the "else" condition is
satisfied
}
// update the gold and system outcomes
this.referenceOutcomes.addAll(referenceSpanOutcomes.values());//output
[GPE x 38, PER x 313, FAC, VEH, LOC x 10, ORG x 48]
this.predictedOutcomes.addAll(predictedSpanOutcomes.values());//output
[GPE x 30, PER x 264, FAC x 6, WEA x 2, VEH, LOC x 9, ORG x 25]
// determine the outcomes that were correct
Set<SPAN_TYPE> intersection = new HashSet<SPAN_TYPE>();
intersection.addAll(referenceSpanOutcomes.keySet()); //output
[Span{begin=1785, end=1789}, Span{begin=2974, end=2976}, Span{begin=2559,
end=2569}, Span{begin=1381, end=1384}, Span{begin=2518, end=2531},etc.
intersection.retainAll(predictedSpanOutcomes.keySet());//output get all
same of two set: referenceSpanOutcomes.keySet and
predictedSpanOutcomes.keySet
for (SPAN_TYPE span : intersection) {
String goldOutcome = referenceSpanOutcomes.get(span);
//output goldOutcome: PER, FAC, LOC, ORG, GPE,
String systemOutcome = predictedSpanOutcomes.get(span);
//output systemOutcome: PER, FAC, LOC, ORG, GPE,
if (Objects.equal(goldOutcome, systemOutcome)) {
this.correctOutcomes.add(goldOutcome);
} //end if
else //here is where I want to get the 1st and 2nd text
{
//Print the 1st and 2nd text here to file !
}
}
}
What I think of is may be we also "put" that text/text2 to a List or
something so that it could have the same structure with the
referenceSpanOutcomes/predictedSpanOutcomes then later we just track back
at the same postion where the "else" condition happen. But how should it
be done ?

No comments:

Post a Comment