|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + * |
| 14 | + * Original source: https://github.com/fab-bar/TextGammaTool.git |
| 15 | + */ |
| 16 | +package org.dkpro.statistics.agreement.aligning; |
| 17 | + |
| 18 | +import static java.util.Collections.sort; |
| 19 | +import static java.util.Collections.unmodifiableMap; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +import org.dkpro.statistics.agreement.aligning.data.Rater; |
| 29 | + |
| 30 | +public class AlignableAnnotationUnit |
| 31 | + implements IAlignableAnnotationUnit |
| 32 | +{ |
| 33 | + private static final long serialVersionUID = 8646000607502108382L; |
| 34 | + |
| 35 | + private final Rater rater; |
| 36 | + private final String type; |
| 37 | + private final Map<String, String> features = new HashMap<String, String>(); |
| 38 | + |
| 39 | + private final long begin; |
| 40 | + private final long end; |
| 41 | + |
| 42 | + public AlignableAnnotationUnit(Rater creator, int beg, int end) |
| 43 | + { |
| 44 | + this(creator, null, beg, end, null); |
| 45 | + } |
| 46 | + |
| 47 | + public AlignableAnnotationUnit(Rater creator, int beg, int end, Map<String, String> featureset) |
| 48 | + { |
| 49 | + this(creator, null, beg, end, featureset); |
| 50 | + } |
| 51 | + |
| 52 | + public AlignableAnnotationUnit(Rater aCreator, long aBegin, long aEnd, |
| 53 | + Map<String, String> aFeatures) |
| 54 | + { |
| 55 | + this(aCreator, NO_TYPE, aBegin, aEnd, null); |
| 56 | + } |
| 57 | + |
| 58 | + public AlignableAnnotationUnit(Rater aRater, String aType, long aBegin, long aEnd, |
| 59 | + Map<String, String> aFeatures) |
| 60 | + { |
| 61 | + if (aRater == null) { |
| 62 | + rater = new Rater("", -1); |
| 63 | + } |
| 64 | + else { |
| 65 | + rater = aRater; |
| 66 | + } |
| 67 | + |
| 68 | + if (aType == null) { |
| 69 | + type = NO_TYPE; |
| 70 | + } |
| 71 | + else { |
| 72 | + type = aType; |
| 73 | + } |
| 74 | + |
| 75 | + if (aBegin >= aEnd) { |
| 76 | + throw new IllegalArgumentException("Begin has to be smaller than end."); |
| 77 | + } |
| 78 | + |
| 79 | + begin = aBegin; |
| 80 | + end = aEnd; |
| 81 | + |
| 82 | + if (aFeatures != null) { |
| 83 | + features.putAll(aFeatures); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public Rater getRater() |
| 88 | + { |
| 89 | + return rater; |
| 90 | + } |
| 91 | + |
| 92 | + public String getType() |
| 93 | + { |
| 94 | + return type; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Object getCategory() |
| 99 | + { |
| 100 | + return getFeatures(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public long getBegin() |
| 105 | + { |
| 106 | + return begin; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public long getEnd() |
| 111 | + { |
| 112 | + return end; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int getRaterIdx() |
| 117 | + { |
| 118 | + return rater.getIndex(); |
| 119 | + } |
| 120 | + |
| 121 | + public Set<String> getFeatureNames() |
| 122 | + { |
| 123 | + return features.keySet(); |
| 124 | + } |
| 125 | + |
| 126 | + public String getFeatureValue(String attribute) |
| 127 | + { |
| 128 | + return features.get(attribute); |
| 129 | + } |
| 130 | + |
| 131 | + public Map<String, String> getFeatures() |
| 132 | + { |
| 133 | + return unmodifiableMap(features); |
| 134 | + } |
| 135 | + |
| 136 | + public boolean isCoextensive(AlignableAnnotationUnit aOther) |
| 137 | + { |
| 138 | + return (getBegin() == aOther.getBegin()) && (getEnd() == aOther.getEnd()); |
| 139 | + } |
| 140 | + |
| 141 | + public boolean overlaps(AlignableAnnotationUnit aOther) |
| 142 | + { |
| 143 | + return !(aOther.getEnd() <= getBegin() || aOther.getBegin() >= getEnd()); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean equals(Object o) |
| 148 | + { |
| 149 | + if (o == null) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + if (getClass() != o.getClass()) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + AlignableAnnotationUnit seg = (AlignableAnnotationUnit) o; |
| 157 | + // same type? |
| 158 | + if (!Objects.equals(getType(), seg.getType())) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + // same creator? |
| 163 | + if (!Objects.equals(getRater(), seg.getRater())) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + // same span? |
| 168 | + if (!this.isCoextensive(seg)) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + // same attributes? |
| 173 | + if (!this.getFeatureNames().equals(seg.getFeatureNames())) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + // same attribute values? |
| 177 | + for (String attribute : this.getFeatureNames()) { |
| 178 | + if (!Objects.equals(this.getFeatureValue(attribute), seg.getFeatureValue(attribute))) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public int compareTo(IAlignableAnnotationUnit aOther) |
| 188 | + { |
| 189 | + if (aOther == null) { |
| 190 | + return -1; |
| 191 | + } |
| 192 | + |
| 193 | + if (this.equals(aOther)) { |
| 194 | + return 0; |
| 195 | + } |
| 196 | + |
| 197 | + // first: start offset |
| 198 | + if (this.getBegin() < aOther.getBegin()) { |
| 199 | + return -1; |
| 200 | + } |
| 201 | + if (this.getBegin() > aOther.getBegin()) { |
| 202 | + return 1; |
| 203 | + } |
| 204 | + |
| 205 | + // second: end offset |
| 206 | + if (this.getEnd() < aOther.getEnd()) { |
| 207 | + return -1; |
| 208 | + } |
| 209 | + if (this.getEnd() > aOther.getEnd()) { |
| 210 | + return 1; |
| 211 | + } |
| 212 | + |
| 213 | + if (!(aOther instanceof AlignableAnnotationUnit)) { |
| 214 | + return -1; |
| 215 | + } |
| 216 | + |
| 217 | + AlignableAnnotationUnit other = (AlignableAnnotationUnit) aOther; |
| 218 | + |
| 219 | + // sort by Type |
| 220 | + if (!Objects.equals(getType(), other.getType())) { |
| 221 | + if (getType() != null && other.getType() != null) { |
| 222 | + return getType().compareTo(other.getType()); |
| 223 | + } |
| 224 | + else if (getType() == null) { |
| 225 | + return -1; |
| 226 | + } |
| 227 | + else { |
| 228 | + return 1; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + // sort by Creator |
| 233 | + if (!Objects.equals(getRater(), other.getRater())) { |
| 234 | + if (this.getRater() != null && other.getRater() != null) { |
| 235 | + return this.getRater().getName().compareTo(other.getRater().getName()); |
| 236 | + } |
| 237 | + else if (this.getRater() == null) { |
| 238 | + return -1; |
| 239 | + } |
| 240 | + else { |
| 241 | + return 1; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + // sort by number of attributes |
| 246 | + if (this.getFeatureNames().size() != other.getFeatureNames().size()) { |
| 247 | + return Integer.compare(this.getFeatureNames().size(), other.getFeatureNames().size()); |
| 248 | + } |
| 249 | + |
| 250 | + // sort by attributes names |
| 251 | + List<String> attributelistX = new ArrayList<String>(this.getFeatureNames()); |
| 252 | + sort(attributelistX); |
| 253 | + |
| 254 | + List<String> attributelistY = new ArrayList<String>(other.getFeatureNames()); |
| 255 | + sort(attributelistY); |
| 256 | + |
| 257 | + if (!attributelistX.equals(attributelistY)) { |
| 258 | + return String.join("", attributelistX).compareTo(String.join("", attributelistY)); |
| 259 | + } |
| 260 | + |
| 261 | + // sort by attribute values (in order of names) |
| 262 | + for (int i = 0; i < attributelistX.size(); i++) { |
| 263 | + String attr = attributelistX.get(i); |
| 264 | + if (!this.getFeatureValue(attr).equals(other.getFeatureValue(attr))) { |
| 265 | + return this.getFeatureValue(attr).compareTo(other.getFeatureValue(attr)); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + // annotations are equal |
| 270 | + // (but one is instantiation of subclass) |
| 271 | + return 0; |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | + public AlignableAnnotationUnit cloneWithDifferentLabel(String aType, String aLabel) |
| 276 | + { |
| 277 | + var feat = new HashMap<>(getFeatures()); |
| 278 | + feat.put(aType, aLabel); |
| 279 | + return new AlignableAnnotationUnit(rater, aType, begin, end, feat); |
| 280 | + } |
| 281 | + |
| 282 | + public AlignableAnnotationUnit cloneWithDifferentOffsets(long aBegin, long aEnd) |
| 283 | + { |
| 284 | + return new AlignableAnnotationUnit(rater, type, aBegin, aEnd, features); |
| 285 | + |
| 286 | + } |
| 287 | + |
| 288 | + public AlignableAnnotationUnit cloneWithDifferentRater(Rater aRater) |
| 289 | + { |
| 290 | + return new AlignableAnnotationUnit(aRater, type, begin, end, features); |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public int hashCode() |
| 295 | + { |
| 296 | + int hash = 0; |
| 297 | + hash += getRater().hashCode(); |
| 298 | + hash += getType().hashCode(); |
| 299 | + hash += features.hashCode(); |
| 300 | + hash += getBegin() + getEnd(); |
| 301 | + |
| 302 | + return hash; |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + public String toString() |
| 307 | + { |
| 308 | + return this.toString(new ArrayList<String>()); |
| 309 | + } |
| 310 | + |
| 311 | + public String toString(List<String> attributes) |
| 312 | + { |
| 313 | + var ret = new StringBuilder(); |
| 314 | + ret.append(String.valueOf(getBegin())); |
| 315 | + ret.append("-"); |
| 316 | + ret.append(String.valueOf(getEnd())); |
| 317 | + for (String attribute : attributes) { |
| 318 | + ret.append("\t"); |
| 319 | + if (this.getFeatureValue(attribute) != null) { |
| 320 | + ret.append(this.getFeatureValue(attribute)); |
| 321 | + } |
| 322 | + else { |
| 323 | + ret.append("--"); |
| 324 | + } |
| 325 | + } |
| 326 | + return ret.toString(); |
| 327 | + } |
| 328 | +} |
0 commit comments