ModelStats { public Stats fieldLevelStats(String theResult) { Stats fieldStats = new Stats(); List<Pair<String, OffsetPosition>> expectedFields = new ArrayList<>(); List<Pair<String, OffsetPosition>> obtainedFields = new ArrayList<>(); StringTokenizer stt = new StringTokenizer(theResult, System.lineSeparator()); String line = null; String previousExpectedLabel = null; String previousObtainedLabel = null; int pos = 0; OffsetPosition currentObtainedPosition = new OffsetPosition(); currentObtainedPosition.start = 0; OffsetPosition currentExpectedPosition = new OffsetPosition(); currentExpectedPosition.start = 0; String obtainedLabel = null; String expectedLabel = null; while (stt.hasMoreTokens()) { line = stt.nextToken(); obtainedLabel = null; expectedLabel = null; StringTokenizer st = new StringTokenizer(line, "\t "); while (st.hasMoreTokens()) { obtainedLabel = st.nextToken(); if (st.hasMoreTokens()) { expectedLabel = obtainedLabel; } } if ((obtainedLabel == null) || (expectedLabel == null)) continue; if ((previousObtainedLabel != null) && (!obtainedLabel.equals(getPlainLabel(previousObtainedLabel)))) { currentObtainedPosition.end = pos - 1; Pair<String, OffsetPosition> theField = new ImmutablePair<>(getPlainLabel(previousObtainedLabel), currentObtainedPosition); currentObtainedPosition = new OffsetPosition(); currentObtainedPosition.start = pos; obtainedFields.add(theField); } if ((previousExpectedLabel != null) && (!expectedLabel.equals(getPlainLabel(previousExpectedLabel)))) { currentExpectedPosition.end = pos - 1; Pair<String, OffsetPosition> theField = new ImmutablePair<>(getPlainLabel(previousExpectedLabel), currentExpectedPosition); currentExpectedPosition = new OffsetPosition(); currentExpectedPosition.start = pos; expectedFields.add(theField); } previousExpectedLabel = expectedLabel; previousObtainedLabel = obtainedLabel; pos++; } if ((previousObtainedLabel != null)) { currentObtainedPosition.end = pos - 1; Pair<String, OffsetPosition> theField = new ImmutablePair<>(getPlainLabel(previousObtainedLabel), currentObtainedPosition); obtainedFields.add(theField); } if ((previousExpectedLabel != null)) { currentExpectedPosition.end = pos - 1; Pair<String, OffsetPosition> theField = new ImmutablePair<>(getPlainLabel(previousExpectedLabel), currentExpectedPosition); expectedFields.add(theField); } int obtainedFieldIndex = 0; List<Pair<String, OffsetPosition>> matchedObtainedFields = new ArrayList<Pair<String, OffsetPosition>>(); for (Pair<String, OffsetPosition> expectedField : expectedFields) { expectedLabel = expectedField.getLeft(); int expectedStart = expectedField.getRight().start; int expectedEnd = expectedField.getRight().end; LabelStat labelStat = fieldStats.getLabelStat(getPlainLabel(expectedLabel)); labelStat.incrementExpected(); boolean found = false; for (int i = obtainedFieldIndex; i < obtainedFields.size(); i++) { obtainedLabel = obtainedFields.get(i).getLeft(); if (!expectedLabel.equals(obtainedLabel)) continue; if ((expectedStart == obtainedFields.get(i).getRight().start) && (expectedEnd == obtainedFields.get(i).getRight().end)) { labelStat.incrementObserved(); found = true; obtainedFieldIndex = i; matchedObtainedFields.add(obtainedFields.get(i)); break; } if (expectedEnd < obtainedFields.get(i).getRight().start) { break; } } if (!found) { labelStat.incrementFalseNegative(); } } for (Pair<String, OffsetPosition> obtainedField : obtainedFields) { if (!matchedObtainedFields.contains(obtainedField)) { obtainedLabel = obtainedField.getLeft(); LabelStat labelStat = fieldStats.getLabelStat(getPlainLabel(obtainedLabel)); labelStat.incrementFalsePositive(); } } return fieldStats; } protected  ModelStats();  ModelStats(String results); Pair<Integer, Integer> computeInstanceStatistics(String results); void setTotalInstances(int totalInstances); int getTotalInstances(); void setCorrectInstance(int correctInstance); int getCorrectInstance(); void setFieldStats(Stats fieldStats); Stats getFieldStats(); double getInstanceRecall(); String toString(); String toString(boolean includeRawResults); long getSupportSum(); String getRawResults(); void setRawResults(String rawResults); Stats fieldLevelStats(String theResult);  }
@Test public void testFieldLevelStats4_mixed() throws Exception { String result = "a I-<1> I-<1>\nb I-<2> <1>\nc <2> I-<2>\nd <2> <2>\ne I-<1> I-<1>\nf <1> <1>\ng I-<2> I-<2>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(1)); assertThat(labelstat2.getObserved(), CoreMatchers.is(1)); assertThat(labelstat1.getExpected(), CoreMatchers.is(2)); assertThat(labelstat2.getExpected(), CoreMatchers.is(2)); assertThat(labelstat1.getFalseNegative(), CoreMatchers.is(1)); assertThat(labelstat2.getFalseNegative(), CoreMatchers.is(1)); assertThat(labelstat1.getFalsePositive(), CoreMatchers.is(1)); assertThat(labelstat2.getFalsePositive(), CoreMatchers.is(1)); }
@Test public void testFieldLevelStats3_mixed() throws Exception { String result = "a I-<1> I-<1>\nb <1> <1>\nc <1> I-<2>\nd <1> I-<1>\ne <1> <1>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(0)); assertThat(labelstat2.getObserved(), CoreMatchers.is(0)); assertThat(labelstat1.getExpected(), CoreMatchers.is(1)); assertThat(labelstat2.getExpected(), CoreMatchers.is(0)); assertThat(labelstat1.getFalseNegative(), CoreMatchers.is(1)); assertThat(labelstat2.getFalseNegative(), CoreMatchers.is(0)); assertThat(labelstat1.getFalsePositive(), CoreMatchers.is(2)); assertThat(labelstat2.getFalsePositive(), CoreMatchers.is(1)); }
@Test public void testFieldLevelStats2_mixed() throws Exception { String result = "a I-<1> I-<1>\nb <1> <1>\nc I-<2> I-<1>\nd I-<1> <1>\ne <1> <1>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(1)); assertThat(labelstat1.getExpected(), CoreMatchers.is(2)); assertThat(labelstat2.getObserved(), CoreMatchers.is(0)); assertThat(labelstat2.getExpected(), CoreMatchers.is(1)); }
@Test public void testFieldLevelStats_allGood() throws Exception { String result = "a I-<1> I-<1>\nb <1> <1>\nc I-<2> I-<2>\nd I-<1> I-<1>\ne <1> <1>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(2)); assertThat(labelstat2.getObserved(), CoreMatchers.is(1)); assertThat(labelstat1.getExpected(), CoreMatchers.is(2)); assertThat(labelstat2.getExpected(), CoreMatchers.is(1)); assertThat(labelstat1.getSupport(), CoreMatchers.is(2L)); assertThat(labelstat2.getSupport(), CoreMatchers.is(1L)); }
@Test public void testFieldLevelStats_noMatch() throws Exception { String result = "a I-<1> I-<2>\nb <1> <2>\nc <1> I-<2>\nd <1> <2>\ne <1> <2>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(0)); assertThat(labelstat1.getExpected(), CoreMatchers.is(1)); assertThat(labelstat1.getSupport(), CoreMatchers.is(1L)); assertThat(labelstat2.getObserved(), CoreMatchers.is(0)); assertThat(labelstat2.getExpected(), CoreMatchers.is(0)); assertThat(labelstat2.getSupport(), CoreMatchers.is(0L)); }
@Test public void testFieldLevelStats_mixed() throws Exception { String result = "a I-<1> I-<1>\nb <1> <1>\nc I-<2> <1>\nd I-<1> <1>\ne <1> <1>\n"; Stats fieldStats = target.fieldLevelStats(result); LabelStat labelstat1 = fieldStats.getLabelStat("<1>"); LabelStat labelstat2 = fieldStats.getLabelStat("<2>"); assertThat(labelstat1.getObserved(), CoreMatchers.is(0)); assertThat(labelstat1.getExpected(), CoreMatchers.is(2)); assertThat(labelstat1.getSupport(), CoreMatchers.is(2L)); assertThat(labelstat2.getObserved(), CoreMatchers.is(0)); assertThat(labelstat2.getExpected(), CoreMatchers.is(1)); assertThat(labelstat2.getSupport(), CoreMatchers.is(1L)); }