From dee592f9f5dc057d65bb8c587d75d7ca941deb0c Mon Sep 17 00:00:00 2001 From: Jan Niestadt Date: Tue, 25 Jun 2024 11:19:38 +0200 Subject: [PATCH] Clarify/simplify utility methods in HitPropertyContextBase. --- .../resultproperty/HitPropertyAfterHit.java | 2 +- .../resultproperty/HitPropertyBeforeHit.java | 2 +- .../HitPropertyContextBase.java | 34 ++++++++++++++----- .../HitPropertyContextPart.java | 4 +-- .../resultproperty/HitPropertyHitText.java | 2 +- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyAfterHit.java b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyAfterHit.java index 6c0189e06..211bcbe37 100644 --- a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyAfterHit.java +++ b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyAfterHit.java @@ -26,7 +26,7 @@ static HitPropertyAfterHit deserializeProp(BlackLabIndex index, AnnotatedField f DeserializeInfos i = deserializeProp(field, infos); int numberOfTokens = i.extraIntParam(0, contextSize.before()); String overrideField = i.extraParam(1); - Annotation annotation = determineAnnotation(index, field, i.annotation, overrideField); + Annotation annotation = annotationOverrideFieldOrVersion(index, i.annotation, overrideField); return new HitPropertyAfterHit(index, annotation, i.sensitivity, numberOfTokens); } diff --git a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyBeforeHit.java b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyBeforeHit.java index 193de9181..ef793e8f8 100644 --- a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyBeforeHit.java +++ b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyBeforeHit.java @@ -26,7 +26,7 @@ static HitPropertyBeforeHit deserializeProp(BlackLabIndex index, AnnotatedField DeserializeInfos i = deserializeProp(field, infos); int numberOfTokens = i.extraIntParam(0, contextSize.before()); String overrideField = i.extraParam(1); - Annotation annotation = determineAnnotation(index, field, i.annotation, overrideField); + Annotation annotation = annotationOverrideFieldOrVersion(index, i.annotation, overrideField); return new HitPropertyBeforeHit(index, annotation, i.sensitivity, numberOfTokens); } diff --git a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextBase.java b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextBase.java index 02c71f470..0e0d92e63 100644 --- a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextBase.java +++ b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextBase.java @@ -148,16 +148,34 @@ protected static int getOrDefaultContextSize(int i, int d) { return i <= 0 ? d : i; } - protected static Annotation determineAnnotation(BlackLabIndex index, AnnotatedField field, Annotation annotation, - String overrideField) { - if (!AnnotatedFieldNameUtil.isParallelField(overrideField)) { + /** + * Choose either the specified annotation, or if an override field/version is given, the equivalent in that field. + * @param index index to use + * @param annotation annotation to use (or annotation name, if overrideField is given) + * @param overrideFieldOrVersion field (or alternate parallel version of the annotation's field) to use + * instead of the annotation's field, or null to return annotation unchanged + * @return the annotation to use + */ + protected static Annotation annotationOverrideFieldOrVersion(BlackLabIndex index, Annotation annotation, + String overrideFieldOrVersion) { + String overrideField; + if (overrideFieldOrVersion != null && !AnnotatedFieldNameUtil.isParallelField(overrideFieldOrVersion)) { // Specified a parallel version, not a complete field name. - overrideField = AnnotatedFieldNameUtil.changeParallelFieldVersion(field.name(), overrideField); - } - return determineAnnotation(index, annotation, overrideField); + overrideField = AnnotatedFieldNameUtil.changeParallelFieldVersion(annotation.field().name(), + overrideFieldOrVersion); + } else + overrideField = overrideFieldOrVersion; + return annotationOverrideField(index, annotation, overrideField); } - protected static Annotation determineAnnotation(BlackLabIndex index, Annotation annotation, String overrideField) { + /** + * Choose either the specified annotation, or the equivalent in the overrideField if given. + * @param index index to use + * @param annotation annotation to use (or annotation name, if overrideField is given) + * @param overrideField field to use instead of the annotation's field, or null to return annotation unchanged + * @return the annotation to use + */ + protected static Annotation annotationOverrideField(BlackLabIndex index, Annotation annotation, String overrideField) { if (overrideField != null && !overrideField.equals(annotation.field().name())) { // Switch fields if necessary (e.g. for match info in a different annotated field, in a parallel corpus) annotation = index.annotatedField(overrideField).annotation(annotation.name()); @@ -191,7 +209,7 @@ void deserializeParam(String param) { public HitPropertyContextBase(HitPropertyContextBase prop, Hits hits, boolean invert, String overrideField) { super(prop, hits, invert); this.index = hits == null ? prop.index : hits.index(); - this.annotation = determineAnnotation(prop.index, prop.annotation, overrideField); + this.annotation = annotationOverrideField(prop.index, prop.annotation, overrideField); this.terms = index.annotationForwardIndex(this.annotation).terms(); // if (hits != null && !hits.field().equals(this.annotation.field())) { // throw new IllegalArgumentException( diff --git a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextPart.java b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextPart.java index a3bd87aed..c31fee7a4 100644 --- a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextPart.java +++ b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyContextPart.java @@ -35,13 +35,13 @@ public class HitPropertyContextPart extends HitPropertyContextBase { static HitPropertyContextPart deserializeProp(BlackLabIndex index, AnnotatedField field, List infos) { DeserializeInfos i = deserializeProp(field, infos); - Annotation annotation = determineAnnotation(index, field, i.annotation, i.extraParam(1)); + Annotation annotation = annotationOverrideFieldOrVersion(index, i.annotation, i.extraParam(1)); return new HitPropertyContextPart(index, annotation, i.sensitivity, i.extraParam(0)); } static HitProperty deserializePropContextWords(BlackLabIndex index, AnnotatedField field, List infos) { DeserializeInfos i = deserializeProp(field, infos); - Annotation annotation = determineAnnotation(index, field, i.annotation, i.extraParam(1)); + Annotation annotation = annotationOverrideFieldOrVersion(index, i.annotation, i.extraParam(1)); return contextWords(index, annotation, i.sensitivity, i.extraParam(0, "H1-")); } diff --git a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyHitText.java b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyHitText.java index 10c994c11..9ce75ef02 100644 --- a/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyHitText.java +++ b/engine/src/main/java/nl/inl/blacklab/resultproperty/HitPropertyHitText.java @@ -18,7 +18,7 @@ public class HitPropertyHitText extends HitPropertyContextBase { static HitPropertyHitText deserializeProp(BlackLabIndex index, AnnotatedField field, List infos) { DeserializeInfos i = deserializeProp(field, infos); - Annotation annotation = determineAnnotation(index, field, i.annotation, i.extraParam(0)); + Annotation annotation = annotationOverrideFieldOrVersion(index, i.annotation, i.extraParam(0)); return new HitPropertyHitText(index, annotation, i.sensitivity); }