001/* 002 * Copyright (c) 2012, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.timeml.eval; 025 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.HashSet; 029import java.util.List; 030import java.util.Set; 031 032import org.apache.uima.jcas.JCas; 033import org.cleartk.eval.AnnotationStatistics; 034import org.cleartk.timeml.type.Anchor; 035import org.cleartk.timeml.type.TemporalLink; 036import org.cleartk.timeml.util.CleartkInternalModelFactory; 037import org.apache.uima.fit.util.JCasUtil; 038 039import com.google.common.base.Function; 040import com.google.common.base.Objects; 041 042/** 043 * <br> 044 * Copyright (c) 2012, Regents of the University of Colorado <br> 045 * All rights reserved. 046 * 047 * @author Steven Bethard 048 */ 049public class TemporalLinkModelInfo extends ModelInfo<TemporalLink> { 050 051 public TemporalLinkModelInfo(CleartkInternalModelFactory modelFactory, String[] trainingArguments) { 052 super( 053 TemporalLink.class, 054 "relationType", 055 TemporalLinkSpan.FROM_TEMPORAL_LINK, 056 modelFactory, 057 trainingArguments); 058 } 059 060 public TemporalLinkModelInfo(CleartkInternalModelFactory modelFactory) { 061 this(modelFactory, new String[0]); 062 } 063 064 @Override 065 public void updateStatistics( 066 AnnotationStatistics<String> statistics, 067 JCas goldView, 068 JCas systemView) { 069 // restrict evaluation to only the TLINKs that were present in the gold data 070 Collection<TemporalLink> goldTlinks = JCasUtil.select(goldView, this.annotatedClass); 071 Set<Object> goldSpans = new HashSet<Object>(); 072 for (TemporalLink tlink : goldTlinks) { 073 goldSpans.add(this.getSpan.apply(tlink)); 074 } 075 List<TemporalLink> systemTlinks = new ArrayList<TemporalLink>(); 076 for (TemporalLink tlink : JCasUtil.select(systemView, this.annotatedClass)) { 077 if (goldSpans.contains(this.getSpan.apply(tlink))) { 078 systemTlinks.add(tlink); 079 } 080 } 081 statistics.add( 082 goldTlinks, 083 systemTlinks, 084 this.getSpan, 085 this.getOutcome); 086 } 087 088 private static class TemporalLinkSpan { 089 090 public static final Function<TemporalLink, TemporalLinkSpan> FROM_TEMPORAL_LINK = new Function<TemporalLink, TemporalLinkSpan>() { 091 @Override 092 public TemporalLinkSpan apply(TemporalLink tlink) { 093 return new TemporalLinkSpan(tlink); 094 } 095 }; 096 097 private int sourceBegin; 098 099 private int sourceEnd; 100 101 private int targetBegin; 102 103 private int targetEnd; 104 105 public TemporalLinkSpan(TemporalLink tlink) { 106 Anchor source = tlink.getSource(); 107 Anchor target = tlink.getTarget(); 108 this.sourceBegin = source.getBegin(); 109 this.sourceEnd = source.getEnd(); 110 this.targetBegin = target.getBegin(); 111 this.targetEnd = target.getEnd(); 112 } 113 114 @Override 115 public int hashCode() { 116 return Objects.hashCode(this.sourceBegin, this.sourceEnd, this.targetBegin, this.targetEnd); 117 } 118 119 @Override 120 public boolean equals(Object obj) { 121 if (this == obj) { 122 return true; 123 } 124 if (obj == null || this.getClass() != obj.getClass()) { 125 return false; 126 } 127 TemporalLinkSpan that = (TemporalLinkSpan) obj; 128 return this.sourceBegin == that.sourceBegin && this.sourceEnd == that.sourceEnd 129 && this.targetBegin == that.targetBegin && this.targetEnd == that.targetEnd; 130 } 131 132 } 133}