2016-11-19 90 views
1

我在尝试uima ruta的示例: hereUIMA Ruta - 基本示例

我想创建ruta脚本并将其应用到我的文本(从普通的java没有任何工作台)。

1.我如何从纯java(没有工作台)获取类型系统描述符? 2.我什么时候拿到工作台? (如果我运行ruta脚本,则没有描述。)

回答

2

主要问题是脚本是否声明新类型。

如果没有声明新类型,文档中的链接示例应该足够。

如果在脚本中声明了新类型,则需要在CAS的创建过程中创建并包含类型系统描述,然后才能将脚本应用于CAS。

含有可以创建以下方式在脚本中声明的类型的类型描述的脚本的类型系统描述:

  • 的芸香工作台为每个脚本自动创建内的简单的类型系统描述当脚本被保存时,Ruta项目。如果未创建任何说明,则该脚本很可能无法解析并且包含语法错误。
  • 在maven构建的项目中,ruta-maven-plugin可用于创建Ruta脚本的类型系统描述。
  • 在普通Java中,可以使用RutaDescriptorFactory以编程方式创建类型系统描述。这是一个code example

有几种方法可以在纯java代码中创建和执行基于ruta的分析引擎。下面是一个例子,而无需使用额外的文件:

String rutaScript = "DECLARE MyType; CW{-> MyType};"; 

RutaDescriptorFactory descriptorFactory = new RutaDescriptorFactory(); 
RutaBuildOptions options = new RutaBuildOptions(); 
options.setResolveImports(true); 
options.setImportByName(true); 
RutaDescriptorInformation descriptorInformation = descriptorFactory 
     .parseDescriptorInformation(rutaScript, options); 
// replace null values for build environment if necessary (e.g., location in classpath) 
Pair<AnalysisEngineDescription, TypeSystemDescription> descriptions = descriptorFactory 
     .createDescriptions(null, null, descriptorInformation, options, null, null, null); 

AnalysisEngineDescription rutaAnalysisEngineDescription = descriptions.getKey(); 
rutaAnalysisEngineDescription.getAnalysisEngineMetaData().getConfigurationParameterSettings().setParameterValue(RutaEngine.PARAM_RULES, rutaScript); 
TypeSystemDescription rutaTypeSystemDescription = descriptions.getValue(); 
// directly set type system description since no file will be created 
rutaAnalysisEngineDescription.getAnalysisEngineMetaData().setTypeSystem(rutaTypeSystemDescription); 

ResourceManager resourceManager = UIMAFramework.newDefaultResourceManager(); 
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(rutaAnalysisEngineDescription); 

List<TypeSystemDescription> typeSystemDescriptions = new ArrayList<>(); 
TypeSystemDescription scannedTypeSystemDescription = TypeSystemDescriptionFactory.createTypeSystemDescription(); 
typeSystemDescriptions.add(scannedTypeSystemDescription); 
typeSystemDescriptions.add(rutaTypeSystemDescription); 
TypeSystemDescription mergeTypeSystemDescription = CasCreationUtils.mergeTypeSystems(typeSystemDescriptions, resourceManager); 

JCas jCas = JCasFactory.createJCas(mergeTypeSystemDescription); 
CAS cas = jCas.getCas(); 
jCas.setDocumentText("This is my document."); 
ae.process(jCas); 

Collection<AnnotationFS> select = CasUtil.select(cas, cas.getTypeSystem().getType("Anonymous.MyType")); 
for (AnnotationFS each : select) { 
    System.out.println(each.getCoveredText()); 
} 

免责声明:我是UIMA鲁塔为您解答开发者

+0

谢谢。我不确定我是否理解了RutaDescriptorFactory示例...我如何将它插入上面的示例([link](https://uima.apache.org/d/ruta-current/tools.ruta.book.html #ugr.tools.ruta.ae.basic.apply)) – CyKon

+0

我将延伸答案 –

+0

非常感谢! :-) – CyKon