Author: seanfinan
Date: Fri Oct 23 15:48:47 2020
New Revision: 1882785

URL: http://svn.apache.org/viewvc?rev=1882785&view=rev
Log:
Add get methods that take multiple cuis, tuis, codes
Add get methods that take SemanticTui, SemanticGroup

Modified:
    
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/annotation/OntologyConceptUtil.java

Modified: 
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/annotation/OntologyConceptUtil.java
URL: 
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/annotation/OntologyConceptUtil.java?rev=1882785&r1=1882784&r2=1882785&view=diff
==============================================================================
--- 
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/annotation/OntologyConceptUtil.java
 (original)
+++ 
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/annotation/OntologyConceptUtil.java
 Fri Oct 23 15:48:47 2020
@@ -69,10 +69,10 @@ final public class OntologyConceptUtil {
     */
    static public Stream<OntologyConcept> getOntologyConceptStream( final 
IdentifiedAnnotation annotation ) {
       return Arrays.stream( getConceptFeatureStructures( annotation ) )
-            .filter( OntologyConcept.class::isInstance )
-            .map( fs -> (OntologyConcept)fs )
-            .filter( isSchemeOk )
-            .filter( isCodeOk );
+                   .filter( OntologyConcept.class::isInstance )
+                   .map( fs -> (OntologyConcept)fs )
+                   .filter( isSchemeOk )
+                   .filter( isCodeOk );
    }
 
    /**
@@ -89,8 +89,8 @@ final public class OntologyConceptUtil {
     */
    static public Stream<UmlsConcept> getUmlsConceptStream( final 
IdentifiedAnnotation annotation ) {
       return Arrays.stream( getConceptFeatureStructures( annotation ) )
-            .filter( UmlsConcept.class::isInstance )
-            .map( fs -> (UmlsConcept)fs );
+                   .filter( UmlsConcept.class::isInstance )
+                   .map( fs -> (UmlsConcept)fs );
    }
 
    /**
@@ -118,8 +118,6 @@ final public class OntologyConceptUtil {
       }
    }
 
-   ;
-
 
    /**
     * @param annotation -
@@ -263,6 +261,21 @@ final public class OntologyConceptUtil {
       return getCodes( JCasUtil.select( jcas, IdentifiedAnnotation.class ), 
schemeName );
    }
 
+   /**
+    * @param jcas -
+    * @return set of all semantic Types in jcas
+    */
+   static public Collection<SemanticTui> getSemanticTuis( final JCas jcas ) {
+      return getSemanticTuis( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ) );
+   }
+
+   /**
+    * @param jcas -
+    * @return set of all semantic groups in jcas
+    */
+   static public Collection<SemanticGroup> getSemanticGroups( final JCas jcas 
) {
+      return getSemanticGroups( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ) );
+   }
 
    //
    //   Get cuis, tuis, or codes for all IdentifiedAnnotations in a lookup 
window
@@ -327,9 +340,9 @@ final public class OntologyConceptUtil {
     */
    static public Collection<String> getCuis( final 
Collection<IdentifiedAnnotation> annotations ) {
       return annotations.stream()
-            .map( OntologyConceptUtil::getCuis )
-            .flatMap( Collection::stream )
-            .collect( Collectors.toSet() );
+                        .map( OntologyConceptUtil::getCuis )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
    }
 
    /**
@@ -338,9 +351,9 @@ final public class OntologyConceptUtil {
     */
    static public Map<String, Long> getCuiCounts( final 
Collection<IdentifiedAnnotation> annotations ) {
       return annotations.stream()
-            .map( OntologyConceptUtil::getCuis )
-            .flatMap( Collection::stream )
-            .collect( Collectors.groupingBy( Function.identity(), 
Collectors.counting() ) );
+                        .map( OntologyConceptUtil::getCuis )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.groupingBy( Function.identity(), 
Collectors.counting() ) );
    }
 
    /**
@@ -349,9 +362,9 @@ final public class OntologyConceptUtil {
     */
    static public Collection<String> getTuis( final 
Collection<IdentifiedAnnotation> annotations ) {
       return annotations.stream()
-            .map( OntologyConceptUtil::getTuis )
-            .flatMap( Collection::stream )
-            .collect( Collectors.toSet() );
+                        .map( OntologyConceptUtil::getTuis )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
 
    }
 
@@ -361,10 +374,10 @@ final public class OntologyConceptUtil {
     */
    static public Map<String, Collection<String>> getSchemeCodes( final 
Collection<IdentifiedAnnotation> annotations ) {
       return annotations.stream()
-            .map( OntologyConceptUtil::getSchemeCodes )
-            .map( Map::entrySet )
-            .flatMap( Collection::stream )
-            .collect( Collectors.toMap( Map.Entry::getKey, 
Map.Entry::getValue, mergeSets ) );
+                        .map( OntologyConceptUtil::getSchemeCodes )
+                        .map( Map::entrySet )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toMap( Map.Entry::getKey, 
Map.Entry::getValue, mergeSets ) );
    }
 
    /**
@@ -373,9 +386,9 @@ final public class OntologyConceptUtil {
     */
    static public Collection<String> getCodes( final 
Collection<IdentifiedAnnotation> annotations ) {
       return annotations.stream()
-            .map( OntologyConceptUtil::getCodes )
-            .flatMap( Collection::stream )
-            .collect( Collectors.toSet() );
+                        .map( OntologyConceptUtil::getCodes )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
    }
 
    /**
@@ -386,12 +399,34 @@ final public class OntologyConceptUtil {
    static public Collection<String> getCodes( final 
Collection<IdentifiedAnnotation> annotations,
                                               final String schemeName ) {
       return annotations.stream()
-            .map( annotation -> getCodes( annotation, schemeName ) )
-            .flatMap( Collection::stream )
-            .collect( Collectors.toSet() );
+                        .map( annotation -> getCodes( annotation, schemeName ) 
)
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
    }
 
 
+   /**
+    * @param annotations -
+    * @return set of Semantic Types associated the named scheme
+    */
+   static public Collection<SemanticTui> getSemanticTuis( final 
Collection<IdentifiedAnnotation> annotations ) {
+      return annotations.stream()
+                        .map( SemanticTui::getTuis )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations -
+    * @return set of ontology codes associated the named scheme
+    */
+   static public Collection<SemanticGroup> getSemanticGroups( final 
Collection<IdentifiedAnnotation> annotations ) {
+      return annotations.stream()
+                        .map( SemanticGroup::getGroups )
+                        .flatMap( Collection::stream )
+                        .collect( Collectors.toSet() );
+   }
+
    //
    //   Get all IdentifiedAnnotations in jcas with given cui, tui, or code
    //
@@ -426,6 +461,76 @@ final public class OntologyConceptUtil {
       return getAnnotationsByCode( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), code );
    }
 
+   /**
+    * @param jcas -
+    * @param tui  semantic type of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsBySemanticTui( 
final JCas jcas,
+                                                                               
final SemanticTui tui ) {
+      return getAnnotationsBySemanticTui( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), tui );
+   }
+
+   /**
+    * @param jcas  -
+    * @param group semantic group of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> 
getAnnotationsBySemanticGroup( final JCas jcas,
+                                                                               
  final SemanticGroup group ) {
+      return getAnnotationsBySemanticGroup( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), group );
+   }
+
+   /**
+    * @param jcas -
+    * @param cuis cuis of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByCuis( final 
JCas jcas,
+                                                                        final 
String... cuis ) {
+      return getAnnotationsByCuis( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), cuis );
+   }
+
+   /**
+    * @param jcas -
+    * @param tuis tuis of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByTuis( final 
JCas jcas,
+                                                                        final 
String... tuis ) {
+      return getAnnotationsByTuis( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), tuis );
+   }
+
+   /**
+    * @param jcas  -
+    * @param codes codes of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByCodes( final 
JCas jcas,
+                                                                         final 
String... codes ) {
+      return getAnnotationsByCodes( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), codes );
+   }
+
+   /**
+    * @param jcas -
+    * @param tuis tuis of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> 
getAnnotationsBySemanticTuis( final JCas jcas,
+                                                                               
 final SemanticTui... tuis ) {
+      return getAnnotationsBySemanticTuis( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), tuis );
+   }
+
+   /**
+    * @param jcas   -
+    * @param groups tuis of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> 
getAnnotationsBySemanticGroups( final JCas jcas,
+                                                                               
   final SemanticGroup... groups ) {
+      return getAnnotationsBySemanticGroups( JCasUtil.select( jcas, 
IdentifiedAnnotation.class ), groups );
+   }
+
 
    //
    //   Get all IdentifiedAnnotations in lookup window with given cui, tui, or 
code
@@ -481,8 +586,8 @@ final public class OntologyConceptUtil {
          final Collection<IdentifiedAnnotation> annotations,
          final String cui ) {
       return annotations.stream()
-            .filter( annotation -> getCuis( annotation ).contains( cui ) )
-            .collect( Collectors.toSet() );
+                        .filter( annotation -> getCuis( annotation ).contains( 
cui ) )
+                        .collect( Collectors.toSet() );
    }
 
 
@@ -495,8 +600,8 @@ final public class OntologyConceptUtil {
          final Collection<IdentifiedAnnotation> annotations,
          final String tui ) {
       return annotations.stream()
-            .filter( annotation -> getTuis( annotation ).contains( tui ) )
-            .collect( Collectors.toSet() );
+                        .filter( annotation -> getTuis( annotation ).contains( 
tui ) )
+                        .collect( Collectors.toSet() );
    }
 
 
@@ -509,8 +614,114 @@ final public class OntologyConceptUtil {
          final Collection<IdentifiedAnnotation> annotations,
          final String code ) {
       return annotations.stream()
-            .filter( annotation -> getCodes( annotation ).contains( code ) )
-            .collect( Collectors.toSet() );
+                        .filter( annotation -> getCodes( annotation 
).contains( code ) )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param tui         semantic type of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsBySemanticTui(
+         final Collection<IdentifiedAnnotation> annotations,
+         final SemanticTui tui ) {
+      return annotations.stream()
+                        .filter( annotation -> SemanticTui.getTuis( annotation 
).contains( tui ) )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param group       semantic group of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> 
getAnnotationsBySemanticGroup(
+         final Collection<IdentifiedAnnotation> annotations,
+         final SemanticGroup group ) {
+      return annotations.stream()
+                        .filter( annotation -> SemanticGroup.getGroups( 
annotation ).contains( group ) )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param cuis        cuis of interest
+    * @return all IdentifiedAnnotations that have the given cui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByCuis(
+         final Collection<IdentifiedAnnotation> annotations,
+         final String... cuis ) {
+      final Collection<String> cuiSet = new HashSet<>( Arrays.asList( cuis ) );
+      final Predicate<IdentifiedAnnotation> cuiMatch
+            = a -> getCuis( a ).stream().anyMatch( cuiSet::contains );
+      return annotations.stream()
+                        .filter( cuiMatch )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param tuis        tuis of interest
+    * @return all IdentifiedAnnotations that have the given tui
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByTuis(
+         final Collection<IdentifiedAnnotation> annotations,
+         final String... tuis ) {
+      final Collection<String> tuiSet = new HashSet<>( Arrays.asList( tuis ) );
+      final Predicate<IdentifiedAnnotation> tuiMatch
+            = a -> getTuis( a ).stream().anyMatch( tuiSet::contains );
+      return annotations.stream()
+                        .filter( tuiMatch )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param codes       codes of interest
+    * @return all IdentifiedAnnotations that have the given code
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsByCodes(
+         final Collection<IdentifiedAnnotation> annotations,
+         final String... codes ) {
+      final Collection<String> codeSet = new HashSet<>( Arrays.asList( codes ) 
);
+      final Predicate<IdentifiedAnnotation> codeMatch
+            = a -> getCodes( a ).stream().anyMatch( codeSet::contains );
+      return annotations.stream()
+                        .filter( codeMatch )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param tuis        semantic types of interest
+    * @return all IdentifiedAnnotations that have the given code
+    */
+   static public Collection<IdentifiedAnnotation> getAnnotationsBySemanticTuis(
+         final Collection<IdentifiedAnnotation> annotations,
+         final SemanticTui... tuis ) {
+      final Collection<SemanticTui> tuiSet = new HashSet<>( Arrays.asList( 
tuis ) );
+      final Predicate<IdentifiedAnnotation> tuiMatch
+            = a -> SemanticTui.getTuis( a ).stream().anyMatch( 
tuiSet::contains );
+      return annotations.stream()
+                        .filter( tuiMatch )
+                        .collect( Collectors.toSet() );
+   }
+
+   /**
+    * @param annotations annotations for which codes should be found
+    * @param groups      semantic groups of interest
+    * @return all IdentifiedAnnotations that have the given code
+    */
+   static public Collection<IdentifiedAnnotation> 
getAnnotationsBySemanticGroups(
+         final Collection<IdentifiedAnnotation> annotations,
+         final SemanticGroup... groups ) {
+      final Collection<SemanticGroup> groupSet = new HashSet<>( Arrays.asList( 
groups ) );
+      final Predicate<IdentifiedAnnotation> groupMatch
+            = a -> SemanticGroup.getGroups( a ).stream().anyMatch( 
groupSet::contains );
+      return annotations.stream()
+                        .filter( groupMatch )
+                        .collect( Collectors.toSet() );
    }
 
 }


Reply via email to