<commit_before>/*
 * 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.uima.util;

import java.io.IOException;
import java.io.InputStream;

import opennlp.tools.dictionary.Dictionary;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.annotator.AnnotatorConfigurationException;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.resource.ResourceAccessException;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.apache.uima.util.Logger;

/**
 * This is a util class for cas consumer.
 */
public final class CasConsumerUtil {
  
  private CasConsumerUtil(){
    // this is a util class must not be instanciated
  }
   
  public static InputStream getOptionalResourceAsStream(UimaContext context, 
	      String name) throws ResourceInitializationException {
      try {
	      return context.getResourceAsStream(name);
	    } catch (ResourceAccessException e) {
	      throw new ResourceInitializationException(
	          ResourceInitializationException.STANDARD_MESSAGE_CATALOG,
	          new Object[] { "There is an internal error in the UIMA SDK: " + 
	          e.getMessage(),
	          e });
	    }   
  }
  
  /**
   * Retrieves a resource as stream from the given context.
   * 
   * @param context
   * @param name
   * @return the stream
   * 
   * @throws AnnotatorConfigurationException
   */
  public static InputStream getResourceAsStream(UimaContext context, 
      String name) throws ResourceInitializationException {
    
    InputStream inResource = getOptionalResourceAsStream(context, name); 
    
    if (inResource == null) {
      throw new ResourceInitializationException(
          ResourceAccessException.STANDARD_MESSAGE_CATALOG,
          new Object[] { "Unable to load resource!" });
    }

    return inResource;
  }
  
  /**
   * Retrieves a type from the given type system.
   * 
   * @param typeSystem
   * @param name
   * @return the type<commit_msg>OPENNLP-669 Fixed @throws for the correct exception thrown.<commit_after>/*
 * 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.uima.util;

import java.io.IOException;
import java.io.InputStream;

import opennlp.tools.dictionary.Dictionary;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.annotator.AnnotatorConfigurationException;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.resource.ResourceAccessException;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;
import org.apache.uima.util.Logger;

/**
 * This is a util class for cas consumer.
 */
public final class CasConsumerUtil {
  
  private CasConsumerUtil(){
    // this is a util class must not be instanciated
  }
   
  public static InputStream getOptionalResourceAsStream(UimaContext context, 
	      String name) throws ResourceInitializationException {
      try {
	      return context.getResourceAsStream(name);
	    } catch (ResourceAccessException e) {
	      throw new ResourceInitializationException(
	          ResourceInitializationException.STANDARD_MESSAGE_CATALOG,
	          new Object[] { "There is an internal error in the UIMA SDK: " + 
	          e.getMessage(),
	          e });
	    }   
  }
  
  /**
   * Retrieves a resource as stream from the given context.
   * 
   * @param context
   * @param name
   * @return the stream
   * 
   * @throws ResourceInitializationException
   */
  public static InputStream getResourceAsStream(UimaContext context, 
      String name) throws ResourceInitializationException {
    
    InputStream inResource = getOptionalResourceAsStream(context, name); 
    
    if (inResource == null) {
      throw new ResourceInitializationException(
          ResourceAccessException.STANDARD_MESSAGE_CATALOG,
          new Object[] { "Unable to load resource!" });
    }

    return inResource;
  }
  
  /**
   * Retrieves a type from the given type system.
   * 
   * @param typeSystem
   * @param name
   * @return the type
{'fraction_non_alphanumeric': 0.07479760647659275, 'fraction_numerical': 0.0019359380499824005, 'mean_word_length': 3.4537617554858935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3435898', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1490, 'n_words': 516}