You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ctakes.apache.org by se...@apache.org on 2026/02/16 15:41:20 UTC

(ctakes) branch main updated: Added normalization for doc creation date and time.

This is an automated email from the ASF dual-hosted git repository.

seanfinan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ctakes.git


The following commit(s) were added to refs/heads/main by this push:
     new 404bbb6  Added normalization for doc creation date and time.
404bbb6 is described below

commit 404bbb62c2c870d125a500ef1572e5420d025f43
Author: Sean Finan <se...@childrens.harvard.edu>
AuthorDate: Mon Feb 16 10:41:04 2026 -0500

    Added normalization for doc creation date and time.
---
 .../ctakes/core/util/doc/SourceMetadataUtil.java   | 87 ++++++++++++++++++++++
 .../apache/ctakes/typesystem/types/TypeSystem.xml  |  9 +++
 2 files changed, 96 insertions(+)

diff --git a/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/SourceMetadataUtil.java b/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/SourceMetadataUtil.java
index e63c308..de3eed0 100644
--- a/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/SourceMetadataUtil.java
+++ b/ctakes-core/src/main/java/org/apache/ctakes/core/util/doc/SourceMetadataUtil.java
@@ -1,5 +1,8 @@
 package org.apache.ctakes.core.util.doc;
 
+import org.apache.ctakes.core.util.CalendarUtil;
+import org.apache.ctakes.typesystem.type.refsem.Date;
+import org.apache.ctakes.typesystem.type.refsem.Time;
 import org.apache.ctakes.typesystem.type.structured.Metadata;
 import org.apache.ctakes.typesystem.type.structured.SourceData;
 import org.slf4j.Logger;
@@ -10,6 +13,7 @@ import org.apache.uima.resource.ResourceProcessException;
 
 import java.sql.Timestamp;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
 
 /**
@@ -178,4 +182,87 @@ final public class SourceMetadataUtil {
       return Timestamp.valueOf( sourceDate );
    }
 
+   /**
+    * Get the ctakes type Date representation of the document creation date as stored by "SourceCreationDate".
+    * If that does not exist then get one as stored by "SourceOriginalDate", and if that doesn't exist "SourceRevisionDate".
+    * If those two do not exist or cannot be normalized, use the current date.
+    * @param jCas -
+    * @return A normalized Date representing the creation date of the document. The current date if one did not exist.
+    */
+   static public Date getDocDate( final JCas jCas ) {
+      final SourceData sourceData = getOrCreateSourceData( jCas );
+      final Date docDate = sourceData.getSourceCreationDate();
+      if ( docDate != null ) {
+         return docDate;
+      }
+      final String startDate = sourceData.getSourceOriginalDate();
+      if ( startDate != null && !startDate.isBlank() ) {
+         final Calendar calendar = CalendarUtil.getCalendar( startDate );
+         if ( !CalendarUtil.NULL_CALENDAR.equals( calendar ) ) {
+            return setDocCreationDate( jCas, calendar );
+         }
+      }
+      final String revisionDate = sourceData.getSourceRevisionDate();
+      if ( revisionDate != null && !revisionDate.isBlank() ) {
+         final Calendar calendar = CalendarUtil.getCalendar( revisionDate );
+         if ( !CalendarUtil.NULL_CALENDAR.equals( calendar ) ) {
+            return setDocCreationDate( jCas, calendar );
+         }
+      }
+      return setDocCreationDate( jCas, Calendar.getInstance() );
+   }
+
+   /**
+    * Get the ctakes type Time representation of the document creation time as stored by "SourceCreationTime".
+    * If that does not exist then get one as stored by "SourceOriginalDate", and if that doesn't exist "SourceRevisionDate".
+    * If those two do not exist or cannot be normalized, use the current time.
+    * @param jCas -
+    * @return A normalized Date representing the creation date of the document. The current date if one did not exist.
+    */
+   static public Time getDocTime( final JCas jCas ) {
+      final SourceData sourceData = getOrCreateSourceData( jCas );
+      final Time docTime = sourceData.getSourceCreationTime();
+      if ( docTime != null ) {
+         return docTime;
+      }
+      final String startDate = sourceData.getSourceOriginalDate();
+      if ( startDate != null && !startDate.isBlank() ) {
+         final Calendar calendar = CalendarUtil.getCalendar( startDate );
+         if ( !CalendarUtil.NULL_CALENDAR.equals( calendar ) ) {
+            setDocCreationDate( jCas, calendar );
+            return sourceData.getSourceCreationTime();
+         }
+      }
+      final String revisionDate = sourceData.getSourceRevisionDate();
+      if ( revisionDate != null && !revisionDate.isBlank() ) {
+         final Calendar calendar = CalendarUtil.getCalendar( revisionDate );
+         if ( !CalendarUtil.NULL_CALENDAR.equals( calendar ) ) {
+            setDocCreationDate( jCas, calendar );
+            return sourceData.getSourceCreationTime();
+         }
+      }
+      setDocCreationDate( jCas, Calendar.getInstance() );
+      return sourceData.getSourceCreationTime();
+   }
+
+
+   /**
+    *
+    * @param jCas -
+    * @param calendar old, simple java object representing a date and time.
+    * @return ctakes type system date made from the given calendar. The current Date if calendar is null.
+    */
+   static public Date setDocCreationDate( final JCas jCas, final Calendar calendar ) {
+      if ( calendar == null || CalendarUtil.NULL_CALENDAR.equals( calendar ) ) {
+         return setDocCreationDate( jCas, Calendar.getInstance() );
+      }
+      final Date date = CalendarUtil.createTypeDate( jCas, calendar );
+      final Time time = CalendarUtil.createTypeTime( jCas, calendar );
+      final SourceData sourceData = getOrCreateSourceData( jCas );
+      sourceData.setSourceCreationDate( date );
+      sourceData.setSourceCreationTime( time );
+      return date;
+   }
+
+
 }
diff --git a/ctakes-type-system/src/main/resources/org/apache/ctakes/typesystem/types/TypeSystem.xml b/ctakes-type-system/src/main/resources/org/apache/ctakes/typesystem/types/TypeSystem.xml
index e461574..a4b4df9 100644
--- a/ctakes-type-system/src/main/resources/org/apache/ctakes/typesystem/types/TypeSystem.xml
+++ b/ctakes-type-system/src/main/resources/org/apache/ctakes/typesystem/types/TypeSystem.xml
@@ -1567,6 +1567,15 @@ This instantiates a manages/treats relation which should be marked as conditiona
           <description/>
           <rangeTypeName>uima.cas.String</rangeTypeName>
         </featureDescription>
+          <!-- Normalized Date and Time -->
+          <featureDescription>
+              <name>sourceCreationDate</name>
+              <rangeTypeName>org.apache.ctakes.typesystem.type.refsem.Date</rangeTypeName>
+          </featureDescription>
+          <featureDescription>
+              <name>sourceCreationTime</name>
+              <rangeTypeName>org.apache.ctakes.typesystem.type.refsem.Time</rangeTypeName>
+          </featureDescription>
         <featureDescription>
           <name>sourceInstitution</name>
           <description/>