Revision: 16271
          http://gate.svn.sourceforge.net/gate/?rev=16271&view=rev
Author:   ian_roberts
Date:     2012-11-13 17:13:01 +0000 (Tue, 13 Nov 2012)
Log Message:
-----------
JAPE to transform the IOB chunk tags into spanning annotations.

Added Paths:
-----------
    gate/trunk/plugins/OpenNLP/resources/tag-to-chunk.jape

Added: gate/trunk/plugins/OpenNLP/resources/tag-to-chunk.jape
===================================================================
--- gate/trunk/plugins/OpenNLP/resources/tag-to-chunk.jape                      
        (rev 0)
+++ gate/trunk/plugins/OpenNLP/resources/tag-to-chunk.jape      2012-11-13 
17:13:01 UTC (rev 16271)
@@ -0,0 +1,66 @@
+Imports: {
+  import static gate.Utils.*;
+}
+
+Phase: TagToChunk
+Options: control = once
+Input: Token
+
+// check there is at least one chunk to process
+Rule: TagToChunk
+({Token.chunk != "O"}):tok
+-->
+{
+  try {
+    List<Annotation> tokens = inDocumentOrder(inputAS.get("Token"));
+    String currentChunkType = null;
+    Long chunkStart = null;
+    for(int i = 0; i < tokens.size(); i++) {
+      String chunkTag = (String)tokens.get(i).getFeatures().get("chunk");
+      if(chunkTag == null) {
+        throw new NonFatalJapeException("Found token (" + start(tokens.get(i))
+            + ":" + end(tokens.get(i)) + ") with no chunk tag");
+      }
+
+      if(currentChunkType == null) {
+        // case 1: not currently in a chunk
+        if("O".equals(chunkTag)) {
+          continue;
+        } else if(chunkTag.startsWith("B-")) {
+          chunkStart = start(tokens.get(i));
+          currentChunkType = chunkTag.substring(2);
+        } else {
+          throw new NonFatalJapeException("Found chunk tag " + chunkTag
+              + " outside a chunk");
+        }
+      } else {
+        // case 2: currently looking for the end of a chunk
+        if("O".equals(chunkTag)) {
+          outputAS.add(chunkStart, end(tokens.get(i-1)), currentChunkType,
+              featureMap());
+          currentChunkType = null;
+          chunkStart = null;
+        } else if(chunkTag.startsWith("B-")) {
+          // finish one chunk and start another
+          outputAS.add(chunkStart, end(tokens.get(i-1)), currentChunkType,
+              featureMap());
+          chunkStart = start(tokens.get(i));
+          currentChunkType = chunkTag.substring(2);
+        } else if(chunkTag.equals("I-" + currentChunkType)) {
+          // still inside the current chunk, do nothing
+        } else {
+          throw new NonFatalJapeException("Token (" + start(tokens.get(i))
+            + ":" + end(tokens.get(i)) + ") has invalid chunk tag " + 
chunkTag);
+        }
+      }
+    }
+
+    // check whether the last token was part of a chunk, and close it off
+    if(currentChunkType != null) {
+      outputAS.add(chunkStart, end(tokens.get(tokens.size()-1)), 
currentChunkType,
+          featureMap());
+    }
+  } catch(InvalidOffsetException e) {
+    throw new JapeException(e);
+  }
+}

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to