Package org.apache.uima.analysis_engine

Examples of org.apache.uima.analysis_engine.AnalysisEngine


  public CAS analyzeInput(Reader input, String descriptorPath) throws InvalidXMLException,
          IOException, ResourceInitializationException, AnalysisEngineProcessException {
    URL url = UIMAAnalyzersUtils.class.getResource(descriptorPath);
    XMLInputSource in = new XMLInputSource(url);
    ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
    // AEProvider aeProvider = AEProviderFactory.getInstance().getAEProvider("", descriptorPath, new
    // HashMap<String, Object>());
    // AnalysisEngine ae = aeProvider.getAE();
    CAS cas = ae.newCAS();
    cas.setDocumentText(IOUtils.toString(input));
    ae.process(cas);
    ae.destroy();
    return cas;
  }
View Full Code Here


  /* process a field value executing UIMA the CAS containing it as document text */
  private JCas processText(String textFieldValue) throws ResourceInitializationException,
          AnalysisEngineProcessException {
    log.info(new StringBuffer("Analazying text").toString());
    /* get the UIMA analysis engine */
    AnalysisEngine ae = aeProvider.getAE();

    /* create a JCas which contain the text to analyze */
    JCas jcas = ae.newJCas();
    jcas.setDocumentText(textFieldValue);

    /* perform analysis on text field */
    ae.process(jcas);
    log.info(new StringBuilder("Text processing completed").toString());
    return jcas;
  }
View Full Code Here

            ArrayList<TestCasData> testCasData) {
      try {
        // create AE:
        XMLInputSource in = new XMLInputSource(engineDescriptorPath.toPortableString());
        ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
        AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
        // create (empty) CAS objects:
        String desc = null;
        desc = engineDescriptorPath.toPortableString();
        XMLInputSource in2 = new XMLInputSource(desc);
        Object descriptor = UIMAFramework.getXMLParser().parse(in2);
        CAS runCas = getEmptyCas(descriptor);
        CAS testCas = getEmptyCas(descriptor);

        for (TestCasData td : testCasData) {
          // init td etc
          runCas.reset();
          testCas.reset();
          String elementName = fScript.getLocation().lastSegment();
          monitor.setTaskName("Processing [w/o classpatch ext.] " + td.getPath().lastSegment());
          int lastIndexOf = elementName.lastIndexOf(RutaEngine.SCRIPT_FILE_EXTENSION);
          if (lastIndexOf != -1) {
            elementName = elementName.substring(0, lastIndexOf);
          }
          // deserialize CASes
          FileInputStream inputStreamTest = null;
          try {
            inputStreamTest = new FileInputStream(new File(td.getPath().toPortableString()));
            XmiCasDeserializer.deserialize(inputStreamTest, testCas, true);
          } finally {
            if (inputStreamTest != null) {
              inputStreamTest.close();
            }
          }
          FileInputStream inputStreamRun = null;
          try {
            inputStreamRun = new FileInputStream(new File(td.getPath().toPortableString()));
            XmiCasDeserializer.deserialize(inputStreamRun, runCas, true);
          } finally {
            if (inputStreamRun != null) {
              inputStreamRun.close();
            }
          }
          testCas = testCas.getView(viewCasName);
          runCas = runCas.getView(viewCasName);

          // gather uima eval-types
          prepareCas(runCas);

          // process run cas and evaluate it
          ae.process(runCas);
          evalLogicAndUpdateGUI(monitor, testPageView, debugPage, fScript, project, runCas,
                  testCas, td);
          if (monitor.isCanceled()) {
            ae.destroy();
            return Status.CANCEL_STATUS;
          }
        }
        ae.destroy();
      } catch (Exception e) {
        RutaAddonsPlugin.error(e);
        monitor.done();
        testPageView.showBusy(false);
        return new Status(Status.ERROR, RutaAddonsPlugin.PLUGIN_ID,
View Full Code Here

      IPath descriptorPath = RutaProjectUtils.getEngineDescriptorPath(
              scriptFile.getLocation(), scriptFile.getProject());
      IPath rootPath = RutaProjectUtils.getDescriptorRootPath(scriptFile.getProject());

      CAS cas = null;
      AnalysisEngine ae = null;
      try {
        XMLInputSource in = new XMLInputSource(descriptorPath.toPortableString());
        ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
        ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
        resMgr.setDataPath(rootPath.toPortableString());
        ae = UIMAFramework.produceAnalysisEngine(specifier, resMgr, null);
        initAE(ae);
        ae.reconfigure();
      } catch (Exception e) {
        DLTKCore.error(e.getMessage(), e);
        return Status.CANCEL_STATUS;
      }
      monitor.beginTask("Processing... ", paths.size());
      for (IPath path : paths) {
        if (monitor.isCanceled()) {
          break;
        }

        monitor.setTaskName("Processing " + path.lastSegment() + "... ");
        try {

          if (cas == null) {
            cas = ae.newCAS();
          } else {
            cas.reset();
          }
          if (path.getFileExtension().equals("xmi")) {
            XmiCasDeserializer.deserialize(new FileInputStream(path.toPortableString()), cas, true);
          } else {
            cas.setDocumentText(getText(path.toPortableString()));
          }
          RutaEngine.removeSourceDocumentInformation(cas);
          RutaEngine.addSourceDocumentInformation(cas, new File(path.toPortableString()));
          ae.process(cas);
        } catch (Exception e) {
          DLTKCore.error(e.getMessage(), e);
          monitor.worked(1);
          continue;
        }

        if (createXMI) {
          monitor.setTaskName("Writing " + path.lastSegment() + "... ");

          File newFile = null;
          if (path.getFileExtension().equals("xmi")) {
            newFile = new File(path.toPortableString());
          } else {
            newFile = new File(path.toPortableString() + ".xmi");
          }
          try {
            writeXmi(cas, newFile);
          } catch (Exception e) {
            DLTKCore.error(e.getMessage(), e);
            monitor.worked(1);
            continue;
          }
          IWorkspace workspace = ResourcesPlugin.getWorkspace();
          IWorkspaceRoot root = workspace.getRoot();
          IPath makeRelativeTo = path.makeRelativeTo(root.getLocation());
          IResource resource = root.findMember(makeRelativeTo);

          try {
            if (resource != null) {
              resource.getParent()
                      .refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
            }
          } catch (CoreException e) {
          }
        }
        monitor.worked(1);
      }
      if (cas != null) {
        cas.release();
      }
      if (ae != null) {
        ae.destroy();
      }
    }

    monitor.done();
    return Status.OK_STATUS;
View Full Code Here

    Collection<TypeSystemDescription> tsds = new ArrayList<TypeSystemDescription>();
    tsds.add(basicTypeSystem);
    TypeSystemDescription mergeTypeSystems = CasCreationUtils.mergeTypeSystems(tsds);
    aed.getAnalysisEngineMetaData().setTypeSystem(mergeTypeSystems);

    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS, new String[] { ruleFile
            .getParentFile().getPath() });
    String name = ruleFile.getName();
    if (name.endsWith(RutaEngine.SCRIPT_FILE_EXTENSION)) {
      name = name.substring(0, name.length() - 5);
    }
    ae.setConfigParameterValue(RutaEngine.PARAM_MAIN_SCRIPT, name);
    ae.setConfigParameterValue(RutaEngine.PARAM_DYNAMIC_ANCHORING, dynamicAnchoring);
    ae.setConfigParameterValue(RutaEngine.PARAM_SIMPLE_GREEDY_FOR_COMPOSED, simpleGreedyForComposed);
    if (resourceFile != null) {
      ae.setConfigParameterValue(RutaEngine.PARAM_RESOURCE_PATHS,
              new String[] { resourceFile.getPath() });
    }

    ae.reconfigure();
    if (cas == null) {
      cas = ae.newCAS();
      cas.setDocumentText(FileUtils.file2String(textFile, "UTF-8"));
    }
    ae.process(cas);

    ae.destroy();
    return cas;
  }
View Full Code Here

    mon.setTaskName("Initializing");
    Handler handler = initConsoleLink(config.getScriptFilePath().lastSegment());

    CAS cas = null;

    AnalysisEngine ae = null;
    try {
      File specFile = new File(engine);
      XMLInputSource in = new XMLInputSource(specFile);
      ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
      ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
      resMgr.setDataPath(rootPath.toPortableString());
      ae = UIMAFramework.produceAnalysisEngine(specifier, resMgr, null);
    } catch (Exception e) {
      String message = e.getMessage();
      DLTKCore.error(message, e);
      clearConsoleLink(handler);
      throw new CoreException(new Status(IStatus.ERROR, RutaIdeUIPlugin.PLUGIN_ID,
              ScriptLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e));
    }

    try {
      if ("debug".equals(launchMode)) {
        ae.setConfigParameterValue(RutaEngine.PARAM_DEBUG, true);
        ae.setConfigParameterValue(RutaEngine.PARAM_DEBUG_WITH_MATCHES, true);
        ae.setConfigParameterValue(RutaEngine.PARAM_PROFILE, true);
        ae.setConfigParameterValue(RutaEngine.PARAM_STATISTICS, true);
        ae.setConfigParameterValue(RutaEngine.PARAM_CREATED_BY, true);
        ae.reconfigure();
      }
    } catch (Exception e) {
      clearConsoleLink(handler);
      String message = e.getMessage();
      DLTKCore.error(message, e);
      throw new CoreException(new Status(IStatus.ERROR, RutaIdeUIPlugin.PLUGIN_ID,
              ScriptLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e));
    }
    mon.worked(1);
    for (File each : inputFiles) {

      mon.setTaskName("Processing " + each.getName());
      if (mon.isCanceled()) {
        break;
      }
      try {
        if (cas == null) {
          cas = ae.newCAS();
        } else {
          cas.reset();
        }
        if (each.getName().endsWith("xmi")) {
          XmiCasDeserializer.deserialize(new FileInputStream(each), cas, true);
        } else {
          cas.setDocumentText(getText(each));
        }

        if (mon.isCanceled()) {
          break;
        }

        RutaEngine.removeSourceDocumentInformation(cas);
        RutaEngine.addSourceDocumentInformation(cas, each);

        ae.process(cas);

        mon.worked(1);

        if (mon.isCanceled()) {
          break;
        }

        File outputFile = new File(outputDir, each.getName() + ".xmi");
        mon.setTaskName("Saving " + outputFile.getName());
        writeXmi(cas, outputFile);
        mon.worked(1);
      } catch (Exception e) {
        if (cas != null) {
          cas.release();
        }
        if (ae != null) {
          ae.destroy();
        }
        clearConsoleLink(handler);
        String message = e.getMessage();
        DLTKCore.error(message, e);
        throw new CoreException(new Status(IStatus.ERROR, RutaIdeUIPlugin.PLUGIN_ID,
                ScriptLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e));
      }
    }
    if (cas != null) {
      cas.release();
    }
    if (ae != null) {
      ae.destroy();
    }
    IFolder folder = outputFolder;
    folder.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    clearConsoleLink(handler);
    mon.done();
View Full Code Here

    }
    return result;
  }

  public static AnalysisEngine loadAnalysisEngine(AnalysisEngineDescription desc) {
    AnalysisEngine result = null;
    try {
      result = UIMAFramework.produceAnalysisEngine(desc);
    } catch (Exception e) {
      TextRulerPlugin.error(e);
      result = null;
View Full Code Here

    try {
      FileUtils.saveString2File(script, new File(tempRulesFileName));
    } catch (IOException e) {
      TextRulerPlugin.error(e);
    }
    AnalysisEngine analysisEngine = getAnalysisEngine();
    CAS testCAS = getTestCAS();
    doc.resetAndFillTestCAS(testCAS, target);
    try {
      analysisEngine.process(testCAS);
    } catch (AnalysisEngineProcessException e) {
      TextRulerPlugin.error(e);
    }
    return testCAS;
  }
View Full Code Here

    ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
    AnalysisEngineDescription aed = (AnalysisEngineDescription) specifier;
    TypeSystemDescription tsd = aed.getAnalysisEngineMetaData().getTypeSystem();
    tsd.addType(TEST_TYPE, "Type for Testing", "uima.tcas.Annotation");

    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
    String module = scriptFile.getName().substring(0, scriptFile.getName().length() - 5);
    ae.setConfigParameterValue(RutaEngine.PARAM_MAIN_SCRIPT, module);
    ae.setConfigParameterValue(RutaEngine.PARAM_SCRIPT_PATHS, new String[] { scriptFile.getParentFile()
            .getAbsolutePath() });
    String aeName = descFile.getName().substring(0, descFile.getName().length() - 4);
    ae.setConfigParameterValue(RutaEngine.PARAM_ADDITIONAL_ENGINES, new String[] { aeName });
    ae.setConfigParameterValue(RutaEngine.PARAM_DESCRIPTOR_PATHS, new String[] { descFile.getParentFile()
            .getAbsolutePath() });
    ae.reconfigure();

    CAS cas = ae.newCAS();
    cas.setDocumentText("This is the default view.");
    CAS newView = cas.createView(NEW_VIEW);
    newView.setDocumentText("This is a new view.");
    Type type = cas.getTypeSystem().getType(TEST_TYPE);
    AnnotationFS createAnnotation = newView.createAnnotation(type, 10, 13);
    newView.addFsToIndexes(createAnnotation);

    ae.process(cas);

    cas.reset();
    FileInputStream stream = new FileInputStream(xmiOutputFile);
    XmiCasDeserializer.deserialize(stream, cas, true);
View Full Code Here

import org.junit.Test;

public class UimafitTest {
  @Test
  public void test() throws Exception {
    AnalysisEngine ae = createEngine(RutaEngine.class,
    // Load script in "Java" notation, with "." as package separator and no extension.
    // File needs to be located in the path specified below with ending ".ruta".
            RutaEngine.PARAM_MAIN_SCRIPT, "org.apache.uima.ruta.engine.UimafitTest",
            // Path(s) where the scripts are located
            RutaEngine.PARAM_SCRIPT_PATHS, new String[] { "src/test/resources" });

    // Create a CAS from the AE so it has the required type priorities
    JCas jcas = ae.newJCas();

    // Fill the CAS with some tokens
    JCasBuilder builder = new JCasBuilder(jcas);
    builder.add("This", TruePositive.class);
    builder.add(" ");
    builder.add("is", TruePositive.class);
    builder.add(" ");
    builder.add("a", TruePositive.class);
    builder.add(" ");
    builder.add("test", TruePositive.class);
    builder.add(".", TruePositive.class);
    builder.close();

    // Apply the script
    ae.process(jcas);

    // Test the result
    AnnotationIndex<Annotation> ai = jcas.getAnnotationIndex(FalsePositive.type);
    FSIterator<Annotation> iterator = ai.iterator();
    assertEquals(1, ai.size());
View Full Code Here

TOP

Related Classes of org.apache.uima.analysis_engine.AnalysisEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.