[ 
https://issues.apache.org/jira/browse/OPENNLP-1172?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16297357#comment-16297357
 ] 

ASF GitHub Bot commented on OPENNLP-1172:
-----------------------------------------

danielruss closed pull request #299: OPENNLP-1172: Add AnnotatorNotes to 
BratDocument
URL: https://github.com/apache/opennlp/pull/299
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotatorNoteAnnotation.java
 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotatorNoteAnnotation.java
new file mode 100644
index 000000000..af14fb3c3
--- /dev/null
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotatorNoteAnnotation.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.formats.brat;
+
+
+public class AnnotatorNoteAnnotation extends BratAnnotation {
+  
+  private final String attachedId;
+  
+  protected AnnotatorNoteAnnotation(String id, String attachedId, String note) 
{
+    super(id, "#AnnotationNote");
+    this.attachedId = attachedId;
+    this.setNote(note);
+  }
+  
+  public String getAttachedId() {
+    return attachedId;
+  }
+  
+}
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotation.java 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotation.java
index 459f04ec1..a0077dae4 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotation.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotation.java
@@ -23,10 +23,12 @@
 
   private final String id;
   private final String type;
-
+  private String note;
+  
   protected BratAnnotation(String id, String type) {
     this.id = Objects.requireNonNull(id);
     this.type = Objects.requireNonNull(type);
+    this.note = "";
   }
 
   public String getId() {
@@ -37,8 +39,16 @@ public String getType() {
     return type;
   }
 
+  public void setNote(String note) {
+    this.note = note;
+  }
+  
+  public String getNote() {
+    return note;
+  }
+  
   @Override
   public String toString() {
-    return id + " " + type;
+    return (id + " " + type + " " + note).trim();
   }
 }
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotationStream.java
 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotationStream.java
index d147179a6..3d9077146 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotationStream.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratAnnotationStream.java
@@ -39,6 +39,7 @@
 
     static final int ID_OFFSET = 0;
     static final int TYPE_OFFSET = 1;
+    static final String NOTES_TYPE = "AnnotatorNotes";
 
     BratAnnotation parse(Span[] tokens, CharSequence line) throws IOException {
       return null;
@@ -178,6 +179,22 @@ BratAnnotation parse(Span[] values, CharSequence line) 
throws IOException {
     }
   }
 
+  static class AnnotatorNoteParser extends BratAnnotationParser {
+    private static final int ATTACH_TO_OFFSET = 2;
+    private static final int START_VALUE_OFFSET = 3;
+
+    @Override
+    BratAnnotation parse(Span[] tokens, CharSequence line) throws IOException {
+
+      
+      Span noteSpan = new Span( tokens[START_VALUE_OFFSET].getStart(), 
+          tokens[tokens.length - 1].getEnd() );      
+
+      return new 
AnnotatorNoteAnnotation(tokens[ID_OFFSET].getCoveredText(line).toString(), 
+          tokens[ATTACH_TO_OFFSET].getCoveredText(line).toString(), 
+          noteSpan.getCoveredText(line).toString());
+    }
+  }
   private final AnnotationConfiguration config;
   private final BufferedReader reader;
   private final String id;
@@ -219,9 +236,17 @@ public BratAnnotation read() throws IOException {
           case 'E':
             parser = new EventAnnotationParser();
             break;
-
+          case '#':
+            // the # can be a Note or a comment... if a note, handle it, 
otherwise skip the unsupported type..
+            if ( 
tokens[BratAnnotationParser.TYPE_OFFSET].getCoveredText(line).toString().equals(
+                BratAnnotationParser.NOTES_TYPE) ) {
+              parser = new AnnotatorNoteParser();
+            } else {
+              return read();
+            }
+            break;
           default:
-            // Skip it, do that for everything unsupported (e.g. "*" id)
+          // Skip it, do that for everything unsupported (e.g. "*" id)
             return read();
         }
 
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocument.java 
b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocument.java
index 51723bef4..7444ec6e2 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocument.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocument.java
@@ -26,6 +26,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import opennlp.tools.util.ObjectStream;
@@ -44,10 +45,25 @@ public BratDocument(AnnotationConfiguration config, String 
id, String text,
     this.text = text;
 
     Map<String, BratAnnotation> annMap = new HashMap<>();
+    List<AnnotatorNoteAnnotation> noteList = new ArrayList<>();
     for (BratAnnotation annotation : annotations) {
-      annMap.put(annotation.getId(), annotation);
+      if (annotation instanceof AnnotatorNoteAnnotation) {
+        noteList.add((AnnotatorNoteAnnotation)annotation);
+      } else {
+        annMap.put(annotation.getId(), annotation);
+      }
     }
 
+    // attach AnnotatorNote to the appropriate Annotation.
+    // the note should ALWAYS have an appropriate id in the map,
+    // but just to be safe, check for null.
+    for (AnnotatorNoteAnnotation note: noteList) {
+      BratAnnotation annotation = annMap.get(note.getAttachedId());
+      if (annotation != null) {
+        annotation.setNote(note.getNote());
+      }
+    }
+    
     annotationMap = Collections.unmodifiableMap(annMap);
   }
 
diff --git 
a/opennlp-tools/src/test/java/opennlp/tools/formats/brat/BratDocumentTest.java 
b/opennlp-tools/src/test/java/opennlp/tools/formats/brat/BratDocumentTest.java
index 65f77bd17..8cac25f0e 100644
--- 
a/opennlp-tools/src/test/java/opennlp/tools/formats/brat/BratDocumentTest.java
+++ 
b/opennlp-tools/src/test/java/opennlp/tools/formats/brat/BratDocumentTest.java
@@ -47,5 +47,18 @@ public void testDocumentWithEntitiesParsing() throws 
IOException {
     Assert.assertTrue(doc.getText().endsWith("multinational process . \n"));
 
     Assert.assertEquals(18, doc.getAnnotations().size());
+    
+    BratAnnotation annotation = doc.getAnnotation("T2");
+    checkNote(annotation, "Barack Obama", "President Obama was the 44th U.S. 
president");
+    annotation = doc.getAnnotation("T3");
+    checkNote(annotation,"South Korea","The capital of South Korea is Seoul");
+  }
+  
+  private void checkNote(BratAnnotation annotation, String 
expectedCoveredText, String expectedNote) {
+    Assert.assertTrue(annotation instanceof SpanAnnotation);
+    SpanAnnotation spanAnn = (SpanAnnotation) annotation;
+    Assert.assertEquals(expectedCoveredText, spanAnn.getCoveredText());
+    Assert.assertEquals(expectedNote, spanAnn.getNote());
+    
   }
 }
diff --git 
a/opennlp-tools/src/test/resources/opennlp/tools/formats/brat/voa-with-entities.ann
 
b/opennlp-tools/src/test/resources/opennlp/tools/formats/brat/voa-with-entities.ann
index 96476b67a..06e60c0a1 100644
--- 
a/opennlp-tools/src/test/resources/opennlp/tools/formats/brat/voa-with-entities.ann
+++ 
b/opennlp-tools/src/test/resources/opennlp/tools/formats/brat/voa-with-entities.ann
@@ -16,3 +16,5 @@ T15   Location 978 989        South Korea
 T16    Location 1000 1013      United States
 T17    Person 1121 1126        Obama
 T18    Location 1168 1177      Pyongyang
+#1     AnnotatorNotes  T2      President Obama was the 44th U.S. president
+#2     AnnotatorNotes  T3      The capital of South Korea is Seoul
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Add Annotator notes to BratAnnotation
> -------------------------------------
>
>                 Key: OPENNLP-1172
>                 URL: https://issues.apache.org/jira/browse/OPENNLP-1172
>             Project: OpenNLP
>          Issue Type: Improvement
>          Components: Formats
>    Affects Versions: 1.8.3
>            Reporter: Daniel Russ
>            Assignee: Daniel Russ
>            Priority: Minor
>             Fix For: 1.8.4
>
>
> The Brat Annotator allows Annotators to add Notes to entites/relations.  The 
> BratAnnotation class should reflect it.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to