ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chen...@apache.org
Subject svn commit: r1678972 - in /ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae: ThreadedUmlsDictionaryLookupAnnotator.java UmlsDictionaryLookupAnnotator.java
Date Tue, 12 May 2015 15:22:52 GMT
Author: chenpei
Date: Tue May 12 15:22:52 2015
New Revision: 1678972

URL: http://svn.apache.org/r1678972
Log:
CTAKES-359 from trunk

Modified:
    ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/ThreadedUmlsDictionaryLookupAnnotator.java
    ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/UmlsDictionaryLookupAnnotator.java

Modified: ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/ThreadedUmlsDictionaryLookupAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/ThreadedUmlsDictionaryLookupAnnotator.java?rev=1678972&r1=1678971&r2=1678972&view=diff
==============================================================================
--- ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/ThreadedUmlsDictionaryLookupAnnotator.java
(original)
+++ ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/ThreadedUmlsDictionaryLookupAnnotator.java
Tue May 12 15:22:52 2015
@@ -47,7 +47,7 @@ public class ThreadedUmlsDictionaryLooku
 
 
    @Override
-  public void initialize( final UimaContext aContext ) throws ResourceInitializationException
{
+   public void initialize( final UimaContext aContext ) throws ResourceInitializationException
{
       super.initialize( aContext );
       final String umlsAddress = EnvironmentVariable.getEnv( UMLSADDR_PARAM, aContext );
       final String umlsVendor = EnvironmentVariable.getEnv( UMLSVENDOR_PARAM, aContext );
@@ -79,22 +79,24 @@ public class ThreadedUmlsDictionaryLooku
          final URL url = new URL( umlsaddr );
          final URLConnection connection = url.openConnection();
          connection.setDoOutput( true );
-         try(final OutputStreamWriter writer = new OutputStreamWriter( connection.getOutputStream()
);
-             final BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()
) )){
-         
-           writer.write( data );
-           writer.flush();
-           boolean result = false;
+         try ( final OutputStreamWriter writer = new OutputStreamWriter( connection.getOutputStream()
);
+               final BufferedReader reader = new BufferedReader( new InputStreamReader( connection
+                     .getInputStream() ) ) ) {
 
-           String line;
-           while ( (line = reader.readLine()) != null ) {
-             final String trimline = line.trim();
-             if ( trimline.isEmpty() ) {
-               break;
-             }
-             result = trimline.equalsIgnoreCase( "<Result>true</Result>" );
-           }
-           return result;
+            writer.write( data );
+            writer.flush();
+            boolean result = false;
+
+            String line;
+            while ( (line = reader.readLine()) != null ) {
+               final String trimline = line.trim();
+               if ( trimline.isEmpty() ) {
+                  break;
+               }
+               result = trimline.equalsIgnoreCase( "<Result>true</Result>" )
+                        || trimline.equalsIgnoreCase( "<?xml version='1.0' encoding='UTF-8'?><Result>true</Result>"
);
+            }
+            return result;
          }
       } catch ( IOException ioE ) {
          LOGGER.error( ioE.getMessage() );

Modified: ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/UmlsDictionaryLookupAnnotator.java
URL: http://svn.apache.org/viewvc/ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/UmlsDictionaryLookupAnnotator.java?rev=1678972&r1=1678971&r2=1678972&view=diff
==============================================================================
--- ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/UmlsDictionaryLookupAnnotator.java
(original)
+++ ctakes/branches/ctakes-3.2.2/ctakes-dictionary-lookup/src/main/java/org/apache/ctakes/dictionary/lookup/ae/UmlsDictionaryLookupAnnotator.java
Tue May 12 15:22:52 2015
@@ -18,14 +18,6 @@
  */
 package org.apache.ctakes.dictionary.lookup.ae;
 
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLEncoder;
-
 import org.apache.ctakes.core.resource.FileLocator;
 import org.apache.ctakes.core.resource.FileResourceImpl;
 import org.apache.ctakes.core.resource.JdbcConnectionResourceImpl;
@@ -33,128 +25,136 @@ import org.apache.ctakes.utils.env.Envir
 import org.apache.log4j.Logger;
 import org.apache.uima.UimaContext;
 import org.apache.uima.analysis_engine.AnalysisEngineDescription;
-import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.fit.factory.AnalysisEngineFactory;
 import org.apache.uima.fit.factory.ExternalResourceFactory;
+import org.apache.uima.resource.ResourceInitializationException;
+
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
 
 /**
  * UIMA annotator that identified entities based on lookup.
- * 
+ *
  * @author Mayo Clinic
  */
-public class UmlsDictionaryLookupAnnotator extends DictionaryLookupAnnotator
-{
-	/* Special implementation to pre bundle the UMLS SnowmedCT/RxNorm dictionaries
-	 * Performs a check for user's UMLS licence at init time via their RESTful API
-	 * User's will need to configure their UMLS username/password in their config
-	 */
-	public final static String UMLSADDR_PARAM = "ctakes.umlsaddr";
-	public final static String UMLSVENDOR_PARAM = "ctakes.umlsvendor";
-	public final static String UMLSUSER_PARAM = "ctakes.umlsuser";
-	public final static String UMLSPW_PARAM = "ctakes.umlspw";
-	
-	private Logger iv_logger = Logger.getLogger(getClass().getName());
-
-	private String UMLSAddr;
-	private String UMLSVendor;
-	private String UMLSUser;
-	private String UMLSPW;
-
-	@Override
-  public void initialize(UimaContext aContext)
-			throws ResourceInitializationException
-	{
-		super.initialize(aContext);
-
-		try {
-			UMLSAddr = EnvironmentVariable.getEnv(UMLSADDR_PARAM, aContext);
-			UMLSVendor = EnvironmentVariable.getEnv(UMLSVENDOR_PARAM, aContext);
-			UMLSUser = EnvironmentVariable.getEnv(UMLSUSER_PARAM, aContext);
-			UMLSPW = EnvironmentVariable.getEnv(UMLSPW_PARAM, aContext);
-			
-			iv_logger.info("Using " + UMLSADDR_PARAM + ": " + UMLSAddr + ": " + UMLSUser);
-			if(!isValidUMLSUser(UMLSAddr, UMLSVendor, UMLSUser, UMLSPW))
-			{
-				iv_logger.error("Error: Invalid UMLS License.  A UMLS License is required to use the
UMLS dictionary lookup. \n" +
-						"Error: You may request one at: https://uts.nlm.nih.gov/license.html \n" +
-						"Please verify your UMLS license settings in the DictionaryLookupAnnotatorUMLS.xml
configuration.");
-				throw new Exception("Failed to initilize.  Invalid UMLS License");
-			}
-			
-		} catch (Exception e) {
-			throw new ResourceInitializationException(e);
-		}	
-	}
-
-	public static boolean isValidUMLSUser(String umlsaddr, String vendor, String username, String
password) throws Exception {
-		String data = URLEncoder.encode("licenseCode", "UTF-8") + "="
-				+ URLEncoder.encode(vendor, "UTF-8");
-		data += "&" + URLEncoder.encode("user", "UTF-8") + "="
-				+ URLEncoder.encode(username, "UTF-8");
-		data += "&" + URLEncoder.encode("password", "UTF-8") + "="
-				+ URLEncoder.encode(password, "UTF-8");
-		URL url = new URL(umlsaddr);
-		URLConnection conn = url.openConnection();
-		conn.setDoOutput(true);
-		try(OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())){
-      wr.write(data);
-      wr.flush();
-		}
-		try(BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()))){
-		  boolean result = false;
-		  String line;
-		  while ((line = rd.readLine()) != null) {
-		    if(line.trim().length()>0)
-		    {
-		      result = line.trim().equalsIgnoreCase("<Result>true</Result>");
-		    }
-		  }
-		  return result;
-		}
-	}
-	
-	public static AnalysisEngineDescription createAnnotatorDescription() throws ResourceInitializationException{
-	  try {
-      return AnalysisEngineFactory.createEngineDescription(UmlsDictionaryLookupAnnotator.class,
-          UMLSADDR_PARAM,
-          "https://uts-ws.nlm.nih.gov/restful/isValidUMLSUser",
-          UMLSVENDOR_PARAM,
-          "NLM-6515182895",
-          "LookupDescriptor",
-          ExternalResourceFactory.createExternalResourceDescription(
-              FileResourceImpl.class,
-              FileLocator.locateFile("org/apache/ctakes/dictionary/lookup/LookupDesc_Db.xml")),
-          "DbConnection",
-          ExternalResourceFactory.createExternalResourceDescription(
-              JdbcConnectionResourceImpl.class,
-              "",
-              JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
-              "org.hsqldb.jdbcDriver",
-              JdbcConnectionResourceImpl.PARAM_URL,
-              // Should be the following but it's WAY too slow
-               "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/umls2011ab/umls"),
-              //"jdbc:hsqldb:file:target/unpacked/org/apache/ctakes/dictionary/lookup/umls2011ab/umls"),
-          "RxnormIndexReader",
-          ExternalResourceFactory.createExternalResourceDescription(
-              JdbcConnectionResourceImpl.class,
-              "",
-              JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
-              "org.hsqldb.jdbcDriver",
-              JdbcConnectionResourceImpl.PARAM_URL,
-              "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/rxnorm-hsqldb/umls"),
-          "OrangeBookIndexReader",
-          ExternalResourceFactory.createExternalResourceDescription(
-              JdbcConnectionResourceImpl.class,
-              "",
-              JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
-              "org.hsqldb.jdbcDriver",
-              JdbcConnectionResourceImpl.PARAM_URL,
-              "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/orange_book_hsqldb/umls")
-              );
-    } catch (FileNotFoundException e) {
-      e.printStackTrace();
-      throw new ResourceInitializationException(e);
-    }
-            
-	}
+public class UmlsDictionaryLookupAnnotator extends DictionaryLookupAnnotator {
+   /* Special implementation to pre bundle the UMLS SnowmedCT/RxNorm dictionaries
+    * Performs a check for user's UMLS licence at init time via their RESTful API
+    * User's will need to configure their UMLS username/password in their config
+    */
+   public final static String UMLSADDR_PARAM = "ctakes.umlsaddr";
+   public final static String UMLSVENDOR_PARAM = "ctakes.umlsvendor";
+   public final static String UMLSUSER_PARAM = "ctakes.umlsuser";
+   public final static String UMLSPW_PARAM = "ctakes.umlspw";
+
+   private Logger iv_logger = Logger.getLogger( getClass().getName() );
+
+   private String UMLSAddr;
+   private String UMLSVendor;
+   private String UMLSUser;
+   private String UMLSPW;
+
+   @Override
+   public void initialize( UimaContext aContext )
+         throws ResourceInitializationException {
+      super.initialize( aContext );
+
+      try {
+         UMLSAddr = EnvironmentVariable.getEnv( UMLSADDR_PARAM, aContext );
+         UMLSVendor = EnvironmentVariable.getEnv( UMLSVENDOR_PARAM, aContext );
+         UMLSUser = EnvironmentVariable.getEnv( UMLSUSER_PARAM, aContext );
+         UMLSPW = EnvironmentVariable.getEnv( UMLSPW_PARAM, aContext );
+
+         iv_logger.info( "Using " + UMLSADDR_PARAM + ": " + UMLSAddr + ": " + UMLSUser );
+         if ( !isValidUMLSUser( UMLSAddr, UMLSVendor, UMLSUser, UMLSPW ) ) {
+            iv_logger.error(
+                  "Error: Invalid UMLS License.  A UMLS License is required to use the UMLS
dictionary lookup. \n" +
+                  "Error: You may request one at: https://uts.nlm.nih.gov/license.html \n"
+
+                  "Please verify your UMLS license settings in the DictionaryLookupAnnotatorUMLS.xml
configuration." );
+            throw new Exception( "Failed to initilize.  Invalid UMLS License" );
+         }
+
+      } catch ( Exception e ) {
+         throw new ResourceInitializationException( e );
+      }
+   }
+
+   public static boolean isValidUMLSUser( String umlsaddr, String vendor, String username,
String password )
+         throws Exception {
+      String data = URLEncoder.encode( "licenseCode", "UTF-8" ) + "="
+                    + URLEncoder.encode( vendor, "UTF-8" );
+      data += "&" + URLEncoder.encode( "user", "UTF-8" ) + "="
+              + URLEncoder.encode( username, "UTF-8" );
+      data += "&" + URLEncoder.encode( "password", "UTF-8" ) + "="
+              + URLEncoder.encode( password, "UTF-8" );
+      URL url = new URL( umlsaddr );
+      URLConnection conn = url.openConnection();
+      conn.setDoOutput( true );
+      try ( OutputStreamWriter wr = new OutputStreamWriter( conn.getOutputStream() ) ) {
+         wr.write( data );
+         wr.flush();
+      }
+      try ( BufferedReader rd = new BufferedReader( new InputStreamReader( conn.getInputStream()
) ) ) {
+         boolean result = false;
+         String line;
+         while ( (line = rd.readLine()) != null ) {
+            if ( line.trim().length() > 0 ) {
+               result = line.trim().equalsIgnoreCase( "<Result>true</Result>"
)
+                        ||
+                        line.trim().equalsIgnoreCase( "<?xml version='1.0' encoding='UTF-8'?><Result>true</Result>"
);
+            }
+         }
+         return result;
+      }
+   }
+
+   public static AnalysisEngineDescription createAnnotatorDescription() throws ResourceInitializationException
{
+      try {
+         return AnalysisEngineFactory.createEngineDescription( UmlsDictionaryLookupAnnotator.class,
+               UMLSADDR_PARAM,
+               "https://uts-ws.nlm.nih.gov/restful/isValidUMLSUser",
+               UMLSVENDOR_PARAM,
+               "NLM-6515182895",
+               "LookupDescriptor",
+               ExternalResourceFactory.createExternalResourceDescription(
+                     FileResourceImpl.class,
+                     FileLocator.locateFile( "org/apache/ctakes/dictionary/lookup/LookupDesc_Db.xml"
) ),
+               "DbConnection",
+               ExternalResourceFactory.createExternalResourceDescription(
+                     JdbcConnectionResourceImpl.class,
+                     "",
+                     JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
+                     "org.hsqldb.jdbcDriver",
+                     JdbcConnectionResourceImpl.PARAM_URL,
+                     // Should be the following but it's WAY too slow
+                     "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/umls2011ab/umls"
),
+               //"jdbc:hsqldb:file:target/unpacked/org/apache/ctakes/dictionary/lookup/umls2011ab/umls"),
+               "RxnormIndexReader",
+               ExternalResourceFactory.createExternalResourceDescription(
+                     JdbcConnectionResourceImpl.class,
+                     "",
+                     JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
+                     "org.hsqldb.jdbcDriver",
+                     JdbcConnectionResourceImpl.PARAM_URL,
+                     "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/rxnorm-hsqldb/umls"
),
+               "OrangeBookIndexReader",
+               ExternalResourceFactory.createExternalResourceDescription(
+                     JdbcConnectionResourceImpl.class,
+                     "",
+                     JdbcConnectionResourceImpl.PARAM_DRIVER_CLASS,
+                     "org.hsqldb.jdbcDriver",
+                     JdbcConnectionResourceImpl.PARAM_URL,
+                     "jdbc:hsqldb:res:/org/apache/ctakes/dictionary/lookup/orange_book_hsqldb/umls"
)
+         );
+      } catch ( FileNotFoundException e ) {
+         e.printStackTrace();
+         throw new ResourceInitializationException( e );
+      }
+
+   }
 }



Mime
View raw message