Java Code Examples for org.apache.uima.UIMAFramework#produceAnalysisEngine()

The following examples show how to use org.apache.uima.UIMAFramework#produceAnalysisEngine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentInfoTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testComponentInfo() throws Exception {
  //test the CAS.getCurrentComponentInfo() is null after a component has
  //been processed
  File descFile = JUnitExtension.getFile("TextAnalysisEngineImplTest/TestPrimitiveTae1.xml");
  AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
          new XMLInputSource(descFile));
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc);
  CAS cas = ae.newCAS();
  ae.process(cas);
  assertNull(((CASImpl)cas).getCurrentComponentInfo());
  
  //same test for aggregate
  //test the CAS.getCurrentComponentInfo() is null after a component has
  //been processed
  descFile = JUnitExtension.getFile("TextAnalysisEngineImplTest/AggregateTaeForMergeTest.xml");
  desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
          new XMLInputSource(descFile));
  ae = UIMAFramework.produceAnalysisEngine(desc);
  cas = ae.newCAS();
  ae.process(cas);
  assertNull(((CASImpl)cas).getCurrentComponentInfo());
}
 
Example 2
Source File: SpringContextResourceManagerTest.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
  // Acquire application context
  ApplicationContext ctx = getApplicationContext();

  // Create resource manager
  SpringContextResourceManager resMgr = new SpringContextResourceManager();
  resMgr.setApplicationContext(ctx);

  // Create component description
  AnalysisEngineDescription desc = createEngineDescription(MyAnalysisEngine.class);
  bindResourceOnceWithoutNested(desc, "injectedBean", "springBean");

  // Instantiate component
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc, resMgr, null);

  // Test that injection works
  ae.process(ae.newJCas());
}
 
Example 3
Source File: ConfigurationParameterInitializerTest.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Check that an Analysis Engine created from a descriptor declaring optional parameters but not
 * setting them actually uses the default values declared in the Java annotation
 */
@Test
public void testUnsetOptionalParameter() throws Exception {
  AnalysisEngineDescription aed = AnalysisEngineFactory.createEngineDescription(
          DefaultValueAE1.class, (Object[]) null);
  // Remove the settings from the descriptor, but leave the declarations.
  // The settings are already filled with default values by createPrimitiveDescription,
  // but here we want to simulate loading a descriptor without settings from a file.
  // The file of course would declare the parameters optional and thus the settings
  // for the optional parameters would be empty. We expect that a default value from the
  // annotation is used in this case.
  aed.getMetaData().setConfigurationParameterSettings(new ConfigurationParameterSettings_impl());
  AnalysisEngine template = UIMAFramework.produceAnalysisEngine(aed);
  DefaultValueAE1 ae = new DefaultValueAE1();
  ae.initialize(template.getUimaContext());
  assertEquals("green", ae.color);
}
 
Example 4
Source File: ResourceManager_implTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testOverrides() throws Exception {
  try {
    final String TEST_DATAPATH = JUnitExtension.getFile("AnnotatorContextTest").getPath();

    File descFile = JUnitExtension.getFile("ResourceManagerImplTest/ResourceTestAggregate.xml");
    AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(descFile));
    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(TEST_DATAPATH);
    UIMAFramework.produceAnalysisEngine(desc, resMgr, null);

    URL url = resMgr.getResourceURL("/Annotator1/TestFileResource");
    assertTrue(url.toString().endsWith("testDataFile2.dat"));
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 5
Source File: InstallationTester.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if a given analysis engine specifier file can be used to produce an instance of analysis
 * engine. Returns <code>true</code>, if an analysis engine can be instantiated,
 * <code>false</code> otherwise.
 * 
 * @param specifier the resource specifier
 * @param resource_manager a new resource_manager
 * @param status the place where to put the results
 *
 * @throws IOException
 *           If an I/O exception occurred while creating <code>XMLInputSource</code>.
 * @throws InvalidXMLException
 *           If the XML parser failed to parse the given input file.
 * @throws ResourceInitializationException
 *           If the specified AE cannot be instantiated.
 */
private void testAnalysisEngine(ResourceSpecifier specifier, 
                                ResourceManager resource_manager, 
                                TestStatus status) 
                                    throws IOException, InvalidXMLException, ResourceInitializationException {

      AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier, resource_manager, null);
      
      //create CAS from the analysis engine
      CAS cas = null;
      if (ae != null) {
        cas = ae.newCAS();
      }
      
      //check test result
      if (ae != null && cas != null) {
        status.setRetCode(TestStatus.TEST_SUCCESSFUL);
      } else {
        status.setRetCode(TestStatus.TEST_NOT_SUCCESSFUL);
        status.setMessage(I18nUtil.localizeMessage(PEAR_MESSAGE_RESOURCE_BUNDLE,
                "installation_verification_ae_not_created", new Object[] { this.pkgBrowser
                        .getInstallationDescriptor().getMainComponentId() }, null));
      }
}
 
Example 6
Source File: AnnotatorTester.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * change the parameter name for the given delegate analysis engine key.
 *
 * @param delegeteKey           analysis engine key
 * @param groupName           group name
 * @param paramName           parameter name
 * @param paramValue           parameter value
 * @throws InvalidXMLException passthru
 * @throws ResourceInitializationException passthru
 * @throws IOException passthru
 */
public void changeDelegateParameterSetting(String delegeteKey,
      String groupName, String paramName, Object paramValue)
      throws InvalidXMLException, ResourceInitializationException,
      IOException {
   // create CasConsumer description
   AnalysisEngineDescription aeSpecifier = UIMAFramework.getXMLParser()
         .parseAnalysisEngineDescription(new XMLInputSource(this.descFile));

   // get delegates
   Map delegates = aeSpecifier.getDelegateAnalysisEngineSpecifiers();

   // check if delegeteKey is available
   if (delegates.containsKey(delegeteKey)) {
      // create new import
      AnalysisEngineDescription delegate = (AnalysisEngineDescription) delegates
            .get(delegeteKey);

      if (groupName == null) {
         delegate.getMetaData().getConfigurationParameterSettings()
               .setParameterValue(paramName, paramValue);
      } else {
         delegate.getMetaData().getConfigurationParameterSettings()
               .setParameterValue(groupName, paramName, paramValue);
      }
   }

   // produce new AE
   this.ae = UIMAFramework
         .produceAnalysisEngine(aeSpecifier, this.mgr, null);

}
 
Example 7
Source File: KnowNER.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private AnalysisEngine initializeClassifier(String language) throws ResourceInitializationException, SQLException {
    logger.debug("Initializing KnowNER for '" + language + "'.");

    long start = System.currentTimeMillis();

    List<AnalysisEngineDescription> descriptions = new ArrayList<>();

    if (model.equals("NED")) {
        descriptions.add(AnalysisEngineFactory.createEngineDescription(BmeowTypeAnnotator.class,
                BmeowTypeAnnotator.GOLD, false));
        descriptions.add(AnalysisEngineFactory.createEngineDescription(RemoveNamedEntityAnnotator.class));
    }
    descriptions.add(AnalysisEngineFactory.createEngineDescription(DictionaryMatchAnnotator.class,
            DictionaryMatchAnnotator.PARAM_LANGUAGE, language));
    descriptions.add(AnalysisEngineFactory.createEngineDescription(DictionaryFeaturesAnnotator.class,
            DictionaryFeaturesAnnotator.PARAM_LANGUAGE, language));

    descriptions.add(createEngineDescription(LocalFeaturesTcAnnotator.class,
	TcAnnotator.PARAM_TC_MODEL_LOCATION, KnowNERSettings.getModelPath(language, model).toFile(),
	LocalFeaturesTcAnnotator.PARAM_LANGUAGE, language,
	TcAnnotator.PARAM_NAME_SEQUENCE_ANNOTATION, Sentence.class.getName(),
	TcAnnotator.PARAM_NAME_UNIT_ANNOTATION, Token.class.getName()));

    descriptions.add(createEngineDescription(KnowNERNamedEntityPostClassificationBMEOWAnnotator.class));

    AnalysisEngineDescription[] analysisEngineDescriptions = new AnalysisEngineDescription[descriptions.size()];
    for (int i = 0; i < descriptions.size(); i++) {
        analysisEngineDescriptions[i] = descriptions.get(i);
    }

    ResourceManager mgr = ((UimaContextAdmin) getContext()).getResourceManager();

    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(
            createEngineDescription(analysisEngineDescriptions), mgr, null);

    long dur = System.currentTimeMillis() - start;
    logger.info("Initialized KnowNER-" + language + " in " + dur/1000 + "s.");

    return ae;
}
 
Example 8
Source File: AnalysisEngineDescription_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void doFullValidation() throws ResourceInitializationException {
  // attempt to instantiate AE in "verification mode"
  Map<String, Object> m = new HashMap<>();
  m.put(AnalysisEngineImplBase.PARAM_VERIFICATION_MODE, Boolean.TRUE);
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(this, m);
  validateSofaMappings();
  ae.newCAS();
}
 
Example 9
Source File: ArrayIndexTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
  super.setUp();
  // Start up TAE
  XMLInputSource input = new XMLInputSource(JUnitExtension
          .getFile("CASTests/desc/ArrayIndexTest.xml"));
  AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(input);
  this.ae = UIMAFramework.produceAnalysisEngine(desc);

}
 
Example 10
Source File: AnalysisEngine_implTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Auxiliary method used by testProcess()
 * 
 * @param aTaeDesc
 *          description of TextAnalysisEngine to test
 */
protected void _testProcess(AnalysisEngineDescription aTaeDesc) throws UIMAException {
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aTaeDesc);
  CAS tcas = ae.newCAS();

  // process(CAS,ResultSpecification)
  ResultSpecification resultSpec = new ResultSpecification_impl(tcas.getTypeSystem());
  resultSpec.addResultType("NamedEntity", true);

  _testProcessInner(ae, tcas, resultSpec, resultSpec);
}
 
Example 11
Source File: AggregateWithReaderTest.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * Demo of running a collection reader as part of an aggregate engine. This allows to run a
 * pipeline an access the output CASes directly - no need to write the data to files.
 */
@Test
public void demoAggregateWithReader() throws UIMAException {
  ResourceSpecifierFactory factory = UIMAFramework.getResourceSpecifierFactory();

  CollectionReaderDescription reader = factory.createCollectionReaderDescription();
  reader.getMetaData().setName("reader");
  reader.setImplementationName(SimpleReader.class.getName());

  AnalysisEngineDescription analyzer = factory.createAnalysisEngineDescription();
  analyzer.getMetaData().setName("analyzer");
  analyzer.setPrimitive(true);
  analyzer.setImplementationName(SimpleAnalyzer.class.getName());

  FixedFlow flow = factory.createFixedFlow();
  flow.setFixedFlow(new String[] { "reader", "analyzer" });

  AnalysisEngineDescription aggregate = factory.createAnalysisEngineDescription();
  aggregate.getMetaData().setName("aggregate");
  aggregate.getAnalysisEngineMetaData().setFlowConstraints(flow);
  aggregate.getAnalysisEngineMetaData().getOperationalProperties().setOutputsNewCASes(true);
  aggregate.getAnalysisEngineMetaData().getOperationalProperties()
          .setMultipleDeploymentAllowed(false);
  aggregate.setPrimitive(false);
  aggregate.getDelegateAnalysisEngineSpecifiersWithImports().put("reader", reader);
  aggregate.getDelegateAnalysisEngineSpecifiersWithImports().put("analyzer", analyzer);

  AnalysisEngine pipeline = UIMAFramework.produceAnalysisEngine(aggregate);
  CasIterator iterator = pipeline.processAndOutputNewCASes(pipeline.newCAS());
  while (iterator.hasNext()) {
    CAS cas = iterator.next();
    System.out.printf("[%s] is [%s]%n", cas.getDocumentText(), cas.getDocumentLanguage());
  }
}
 
Example 12
Source File: CasPoolTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void setUp() {
  try {
    analysisEngine = UIMAFramework.produceAnalysisEngine(aed);
  } catch (ResourceInitializationException e) {
    throw new RuntimeException(e);
  }
  casManager = ((UimaContext_ImplBase)analysisEngine.getUimaContext()).getResourceManager().getCasManager();
}
 
Example 13
Source File: AnalysisEngine_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void manyDelegatesCommon() throws Exception {
  // Test that an aggregate can be copied preserving all comments and ordering of delegates
  XMLParser.ParsingOptions parsingOptions = new XMLParser.ParsingOptions(false);
  parsingOptions.preserveComments = true;
  XMLParser parser = UIMAFramework.getXMLParser();
  File inFile = JUnitExtension.getFile("TextAnalysisEngineImplTest/AggregateWithManyDelegates.xml");
  AnalysisEngineDescription desc = parser.parseAnalysisEngineDescription(new XMLInputSource(inFile), parsingOptions);

  // Write out descriptor
  File cloneFile = new File(inFile.getParentFile(), "CopyOfAggregateWithManyDelegates.xml");
  try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(cloneFile))) {
    XMLSerializer xmlSerializer = new XMLSerializer(false);
    xmlSerializer.setOutputStream(os);
    // set the amount to a value which will show up if used
    // indent should not be used because we're using a parser mode which preserves
    // comments and ignorable white space.
    // NOTE: Saxon appears to force the indent to be 3 - which is what the input file now uses.
    xmlSerializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    ContentHandler contentHandler = xmlSerializer.getContentHandler();
    contentHandler.startDocument();
    desc.toXML(contentHandler, true);
    contentHandler.endDocument();
  }
  
  String inXml = FileCompare.file2String(inFile);
  String cloneXml = FileCompare.file2String(cloneFile);
  XMLAssert.assertXMLEqual(inXml,  cloneXml);
  // When building from a source distribution the descriptor may not have
  // appropriate line-ends so compute the length as if always 1 byte.
  int diff = fileLength(cloneFile) - fileLength(inFile);
  // One platform inserts a blank line and a final newline, so don't insist on perfection
  // NOTE:  This fails with Saxon as it omits the xmlns attribute (why?) and omits the newlines between adjacent comments.
  // It also produces many differences in indentation if the input is not indented by 3
  assertTrue("File size changed by "+diff+" should be no more than 2", diff >= -2 && diff <= 2);

  // Initialize all delegates and check the initialization order (should be declaration order)
  TestAnnotator2.allContexts = "";
  UIMAFramework.produceAnalysisEngine(desc);
  assertEquals("D/C/B/A/F/E/", TestAnnotator2.allContexts);
  
  // Check that copying aggregate preserved the order of the delegates
  desc = parser.parseAnalysisEngineDescription(new XMLInputSource(cloneFile), parsingOptions);
  TestAnnotator2.allContexts = "";
  UIMAFramework.produceAnalysisEngine(desc);
  assertEquals("D/C/B/A/F/E/", TestAnnotator2.allContexts);
  cloneFile.delete();
}
 
Example 14
Source File: PearInstallerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testPearInstall() throws Exception {
  
  // check temporary working directory
  if (this.tempInstallDir == null)
    throw new FileNotFoundException("temp directory not found");
  // check sample PEAR files

  //get pear file to install
  File pearFile = JUnitExtension.getFile("pearTests/DateTime.pear");
  Assert.assertNotNull(pearFile);
  
  // Install PEAR package
  PackageBrowser instPear = PackageInstaller.installPackage(
          this.tempInstallDir, pearFile, true);

  //check pear PackageBrowser object
  Assert.assertNotNull(instPear);
  
  //check PEAR component ID
  String componentID = instPear.getInstallationDescriptor().getMainComponentId();
  Assert.assertEquals("uima.example.DateTimeAnnotator", componentID);
  
  //check PEAR datapath setting
  //pear file contains (uima.datapath = $main_root/my/test/data/path)
  File datapath = new File(this.tempInstallDir, "uima.example.DateTimeAnnotator/my/test/data/path");
  File pearDatapath = new File(instPear.getComponentDataPath());
  Assert.assertEquals(datapath, pearDatapath);
      
  // Create resouce manager and set PEAR package classpath
  ResourceManager rsrcMgr = UIMAFramework.newDefaultResourceManager();

  // Create analysis engine from the installed PEAR package
  XMLInputSource in = new XMLInputSource(instPear.getComponentPearDescPath());
  ResourceSpecifier specifier = UIMAFramework.getXMLParser()
        .parseResourceSpecifier(in);
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(
        specifier, rsrcMgr, null);
  Assert.assertNotNull(ae);
  
  
  // Create a CAS with a sample document text and process the CAS   
  CAS cas = ae.newCAS();
  cas.setDocumentText("Sample text to process with a date 05/29/07 and a time 9:45 AM");
  cas.setDocumentLanguage("en");
  ae.process(cas);
 
}
 
Example 15
Source File: SequencerFixedTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testSequencerFixedFooBar() throws Exception {
  AnalysisEngine ae = null;
  try {
    // create TempFile for test
    File outputReferenceFile = new File(this.testBaseDir, "SequencerTest.txt");
    outputReferenceFile.delete(); // delete file if exist
    outputReferenceFile.createNewFile(); // create new file
    outputReferenceFile.deleteOnExit(); // delete file after closing VM

    // Create an XML input source from the specifier file.
    XMLInputSource in = new XMLInputSource(JUnitExtension
            .getFile("SequencerTest/SequencerFixedAggregate.xml"));
    // Parse the specifier.
    ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
    // Create the Text Analysis Engine.
    ae = UIMAFramework.produceAnalysisEngine(specifier, null, null);
    // Create a new CAS.
    CAS cas = ae.newCAS();
    // Our sample text.
    String text = "Hello world!";
    // System.out.println("Processing text: \"" + text + "\"");
    // Set the document text on the CAS.
    cas.setDocumentText(text);
    cas.setDocumentLanguage("foo-BAR");
    // Process the sample document.
    ResultSpecification resultSpec = UIMAFramework.getResourceSpecifierFactory()
            .createResultSpecification();
    resultSpec.addCapabilities(ae.getAnalysisEngineMetaData().getCapabilities());
    ae.process(cas, resultSpec);
    // check fileoutput
    Assert.assertTrue(FileCompare.compare(outputReferenceFile, JUnitExtension
            .getFile("SequencerTest/SequencerFixedExpected.txt")));
    outputReferenceFile.delete();
    ((CASImpl)cas).traceFSflush();
  } catch (Exception ex) {
    JUnitExtension.handleException(ex);
  } finally {
    // Destroy the CAS, releasing resources.
    if (ae != null) {
      ae.destroy();
    }
  }
}
 
Example 16
Source File: PearMergerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Runs test for org.apache.uima.pear.merger.PMController class by merging 2 sample input PEARs
 * into the output aggregate PEAR. Then, the output PEAR is installed by using
 * org.apache.uima.pear.tools.InstallationController, and the installed component is verified by
 * instantiating the aggregate TAE and creating CAS object.
 * 
 * @throws Exception -
 */
public void testPearMerger() throws Exception {
  // check temporary working directory
  if (_tempWorkingDir == null)
    throw new FileNotFoundException("temp directory not found");
  // check sample PEAR files
  File[] inpPearFiles = new File[2];
  inpPearFiles[0] = JUnitExtension.getFile(TEST_FOLDER + File.separator + INP_PEAR_1_FILE);
  if (!inpPearFiles[0].isFile())
    throw new FileNotFoundException("sample PEAR 1 not found");
  inpPearFiles[1] = JUnitExtension.getFile(TEST_FOLDER + File.separator + INP_PEAR_2_FILE);
  if (!inpPearFiles[1].isFile())
    throw new FileNotFoundException("sample PEAR 2 not found");
  // specify output aggregate PEAR file
  File outPearFile = new File(_tempWorkingDir, OUT_PEAR_ID + ".pear");
  // create PMController instance and perform merging operation
  PMController.setLogFileEnabled(false);
  PMController pmController = new PMController(inpPearFiles, OUT_PEAR_ID, outPearFile);
  boolean done = pmController.mergePears();
  // check merging results
  Assert.assertTrue(done);
  Assert.assertTrue(outPearFile.isFile());
  // install the output PEAR file and check the results
  InstallationController insController = new InstallationController(OUT_PEAR_ID, outPearFile,
          _tempWorkingDir);
  InstallationDescriptor insDesc = insController.installComponent();
  Assert.assertTrue(insDesc != null);
  Assert.assertTrue(OUT_PEAR_ID.equals(insDesc.getMainComponentId()));
  // verify the installed component
  // customize ResourceManager by adding component CLASSPATH
  ResourceManager resMngr = UIMAFramework.newDefaultResourceManager();
  String compClassPath = InstallationController.buildComponentClassPath(insDesc
          .getMainComponentRoot(), insDesc, false);
  // instantiate the aggregate AE
  resMngr.setExtensionClassPath(compClassPath, true);
  String compDescFilePath = insDesc.getMainComponentDesc();
  XMLParser xmlPaser = UIMAFramework.getXMLParser();
  XMLInputSource xmlInput = new XMLInputSource(compDescFilePath);
  AnalysisEngineDescription aeSpec = xmlPaser.parseAnalysisEngineDescription(xmlInput);
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aeSpec, resMngr, null);
  Assert.assertTrue(ae != null);
  // create CAS object
  CAS cas = ae.newCAS();
  Assert.assertTrue(cas != null);
  
  //process CAS
  cas.setDocumentText("Sample text for testing");
  ae.process(cas);
  
  // clean-up the results
  pmController.cleanUp();
}
 
Example 17
Source File: SimpleRunCPM.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor for the class.
 *
 * @param args          command line arguments into the program - see class description
 * @throws UIMAException the UIMA exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public SimpleRunCPM(String args[]) throws UIMAException, IOException {
  mStartTime = System.currentTimeMillis();

  // check command line args
  if (args.length < 3) {
    printUsageMessage();
    System.exit(1);
  }

  // create components from their descriptors

  // Collection Reader
  System.out.println("Initializing Collection Reader");
  ResourceSpecifier colReaderSpecifier = UIMAFramework.getXMLParser()
          .parseCollectionReaderDescription(new XMLInputSource(args[0]));
  CollectionReader collectionReader = UIMAFramework.produceCollectionReader(colReaderSpecifier);

  // AnalysisEngine
  System.out.println("Initializing AnalysisEngine");
  ResourceSpecifier aeSpecifier = UIMAFramework.getXMLParser().parseResourceSpecifier(
          new XMLInputSource(args[1]));
  AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aeSpecifier);

  // CAS Consumer
  System.out.println("Initializing CAS Consumer");
  ResourceSpecifier consumerSpecifier = UIMAFramework.getXMLParser().parseCasConsumerDescription(
          new XMLInputSource(args[2]));
  CasConsumer casConsumer = UIMAFramework.produceCasConsumer(consumerSpecifier);

  // create a new Collection Processing Manager
  mCPM = UIMAFramework.newCollectionProcessingManager();

  // Register AE and CAS Consumer with the CPM
  mCPM.setAnalysisEngine(ae);
  mCPM.addCasConsumer(casConsumer);

  // Create and register a Status Callback Listener
  mCPM.addStatusCallbackListener(new StatusCallbackListenerImpl());

  // Finish setup
  mCPM.setPauseOnException(false);

  // Start Processing (in batches of 10, just for testing purposes)
  mCPM.process(collectionReader, 10);
}
 
Example 18
Source File: Preprocessor.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
private PreprocessorService asService(Lang lang, CorpusMetadata corpusMetadata) {
	PreprocessingPipelineBuilder builder = PreprocessingPipelineBuilder
			.create(lang, taggerPath)
			.setNbDocuments(corpusMetadata.getNbDocuments())
			.setCorpusSize(corpusMetadata.getTotalSize());
	
	if(tagger.isPresent()) 
		builder.setTagger(tagger.get());
	
	if(documentLoggingEnabled.isPresent())
		builder.setDocumentLoggingEnabled(documentLoggingEnabled.get());
	
	if(fixedExpressionEnabled.isPresent())
		builder.setFixedExpressionEnabled(fixedExpressionEnabled.get());
		
	if(listener.isPresent())
		builder.addPipelineListener(listener.get());
	
	for(AnalysisEngineDescription customAE:customAEs) 
		builder.addCustomAE(customAE);
	
	if(resourceOptions.isPresent()) 
		builder.setResourceConfig(resourceOptions.get());


	if(history.isPresent())
			builder.setHistory(history.get());
	
	final AnalysisEngine aae;
	try {
		logger.info("Initializing analysis engine");
		ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    	AnalysisEngineDescription aaeDesc;
		aaeDesc = createEngineDescription(builder.create());
		// Instantiate AAE
		aae = UIMAFramework.produceAnalysisEngine(aaeDesc, resMgr, null);
	} catch (ResourceInitializationException e) {
		throw new TermSuiteException(e);
	}
	
	
	return Guice.createInjector(
				new TermSuiteModule(),
				new PreprocessingModule(lang, aae))
			.getInstance(PreprocessorService.class);
}
 
Example 19
Source File: AnalysisEngine_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testProcess() throws Exception {
    try {
      // test simple primitive TextAnalysisEngine (using TestAnnotator class)
      // This test should work with or without a type system description
      AnalysisEngineDescription primitiveDesc = new AnalysisEngineDescription_impl();
      primitiveDesc.setPrimitive(true);
      primitiveDesc
              .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator");
      primitiveDesc.getMetaData().setName("Test Primitive TAE");

//      TypeSystemDescription tsd = new TypeSystemDescription_impl();
//      tsd.addType("NamedEntity", "", "uima.tcas.Annotation");
//      tsd.addType("DocumentStructure", "", "uima.cas.TOP");
//      primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(tsd);
      Capability cap = new Capability_impl();
      cap.addOutputType("NamedEntity", true);
      cap.addOutputType("DocumentStructure", true);
      Capability[] caps = new Capability[] {cap};
      primitiveDesc.getAnalysisEngineMetaData().setCapabilities(caps);
      _testProcess(primitiveDesc);

      primitiveDesc = new AnalysisEngineDescription_impl();
      primitiveDesc.setPrimitive(true);
      primitiveDesc
              .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator");
      primitiveDesc.getMetaData().setName("Test Primitive TAE");

      TypeSystemDescription tsd = new TypeSystemDescription_impl();
      tsd.addType("NamedEntity", "", "uima.tcas.Annotation");
      tsd.addType("DocumentStructure", "", "uima.cas.TOP");
      primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(tsd);
      cap = new Capability_impl();
      cap.addOutputType("NamedEntity", true);
      cap.addOutputType("DocumentStructure", true);
      caps = new Capability[] {cap};
      primitiveDesc.getAnalysisEngineMetaData().setCapabilities(caps);
      _testProcess(primitiveDesc);

      // test simple aggregate TextAnalysisEngine (again using TestAnnotator class)
      AnalysisEngineDescription aggDesc = new AnalysisEngineDescription_impl();
      aggDesc.setPrimitive(false);
      aggDesc.getMetaData().setName("Test Aggregate TAE");
      aggDesc.getDelegateAnalysisEngineSpecifiersWithImports().put("Test", primitiveDesc);
      FixedFlow_impl flow = new FixedFlow_impl();
      flow.setFixedFlow(new String[] { "Test" });
      aggDesc.getAnalysisEngineMetaData().setFlowConstraints(flow);
      aggDesc.getAnalysisEngineMetaData().setCapabilities(caps);
      _testProcess(aggDesc);

      // test aggregate TAE containing a CAS Consumer
      File outFile = JUnitExtension.getFile("CpmOutput.txt");
      if(outFile != null && outFile.exists()) {
        //outFile.delete() //can't be relied upon.  Instead set file to zero length.
        FileOutputStream fos = new FileOutputStream(outFile, false);
        fos.close();
        assertEquals(0,outFile.length());
      }

      AnalysisEngineDescription aggWithCcDesc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
              new XMLInputSource(JUnitExtension
                      .getFile("TextAnalysisEngineImplTest/AggregateTaeWithCasConsumer.xml")));
      
      _testProcess(aggWithCcDesc, new String[] {"en"});      
      // test that CAS Consumer ran
      if (null == outFile) {
        outFile = JUnitExtension.getFile("CpmOutput.txt");
      }
      assertTrue(outFile != null && outFile.exists());
      assertTrue(outFile.length() > 0);
      outFile.delete();
      
      //test aggregate that uses ParallelStep
      AnalysisEngineDescription desc = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
        new XMLInputSource(JUnitExtension.getFile("TextAnalysisEngineImplTest/AggregateForParallelStepTest.xml")));
      AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(desc);
      CAS cas = ae.newCAS();
      cas.setDocumentText("new test");
      ae.process(cas);
      assertEquals("new test", TestAnnotator.lastDocument);
      assertEquals("new test", TestAnnotator2.lastDocument);
      cas.reset();
      
    } catch (Exception e) {
      JUnitExtension.handleException(e);
    }
  }
 
Example 20
Source File: AnalysisEngineFactory.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * Get an {@link AnalysisEngine} from an XML descriptor file and a set of configuration
 * parameters.
 * 
 * @param descriptorPath
 *          The path to the XML descriptor file.
 * @param configurationData
 *          Any additional configuration parameters to be set. These should be supplied as (name,
 *          value) pairs, so there should always be an even number of parameters.
 * @return The {@link AnalysisEngine} created from the XML descriptor and the configuration
 *         parameters.
 * @throws IOException
 *           if an I/O error occurs
 * @throws InvalidXMLException
 *           if the input XML is not valid or does not specify a valid {@link ResourceSpecifier}
 * @throws ResourceInitializationException
 *           if a failure occurred during production of the resource.
 * @see <a href="package-summary.html#InstancesVsDescriptors">Why are descriptors better than
 *      component instances?</a>
 */
public static AnalysisEngine createEngineFromPath(String descriptorPath,
        Object... configurationData) throws InvalidXMLException, IOException,
        ResourceInitializationException {
  AnalysisEngineDescription desc = createEngineDescriptionFromPath(descriptorPath,
          configurationData);
  return UIMAFramework.produceAnalysisEngine(desc, ResourceManagerFactory.newResourceManager(),
          null);
}