You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by GitBox <gi...@apache.org> on 2022/03/28 19:36:15 UTC

[GitHub] [opennlp] kinow commented on a change in pull request #364: OPENNLP-565 Support for the MASC format

kinow commented on a change in pull request #364:
URL: https://github.com/apache/opennlp/pull/364#discussion_r836779884



##########
File path: opennlp-tools/src/main/java/opennlp/tools/formats/masc/MascNamedEntityParser.java
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.masc;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * A class to process the MASC Named entity stand-off annotation file
+ */
+public class MascNamedEntityParser extends DefaultHandler {
+
+  private Map<Integer, String> entityIDtoEntityType = new HashMap<>();
+  private Map<Integer, List<Integer>> entityIDsToTokens = new HashMap<>();
+  private Map<Integer, String> tokenToEntity = new HashMap<>();
+
+  public Map<Integer, String> getEntityIDtoEntityType() {
+    return entityIDtoEntityType;
+  }
+
+  public Map<Integer, List<Integer>> getEntityIDsToTokens() {
+    return entityIDsToTokens;
+  }
+
+  @Override
+  public void startElement(String uri, String localName, String qName, Attributes attributes)
+      throws SAXException {
+
+    try {
+      if (qName.equals("a")) {
+        int entityID = Integer.parseInt(
+            attributes.getValue("ref").replaceFirst("ne-n", ""));
+        String label = attributes.getValue("label");
+        if (entityIDtoEntityType.containsKey(entityID)) {
+          throw new SAXException("Multiple labels for one named entity");
+        } else {
+          entityIDtoEntityType.put(entityID, label);
+        }
+      }
+
+      if (qName.equals("edge")) {
+        int entityID = Integer.parseInt(
+            attributes.getValue("from").replaceFirst("ne-n", ""));
+        int tokenID = Integer.parseInt(
+            attributes.getValue("to").replaceFirst("penn-n", ""));
+
+        if (!entityIDsToTokens.containsKey(entityID)) {
+          List<Integer> tokens = new ArrayList<>();
+          tokens.add(tokenID);
+          entityIDsToTokens.put(entityID, tokens);
+        } else {
+          entityIDsToTokens.get(entityID).add(tokenID);
+        }
+
+/*      Not sure what to do with this. There might be multiple entity links to one token.
+       E.g. Colorado will be one token with the entities "city" and "province".
+       For now, we'll only raise alarm when one TokenID should be assigned
+       to different top-level labels, e.g. person & location (since we are dropping the low-level
+       annotations at the moment). To make this work in OpenNLP (does not allow overlaps), we'll
+       keep only the first named entity type.
+ */
+        //todo: Do we want to give the user control over which types have priority?

Review comment:
       Good comment and the todo marker above should call attention of devs working with this format in case of a bug/issue related to this code :+1: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org