如何获取 DictionaryAnnotator 的注释文本
How to get the annotated text for a DictionaryAnnotator
我有一个从 UIMA 的 DictionaryCreator 创建的字典,我想使用 DictionaryAnnotator 和上述字典来注释一段文本,但我不知道如何获取注释的文本。如果你这样做,请告诉我。任何帮助表示赞赏。下面提到了代码、字典文件和描述符,
P.S。我是 Apache UIMA 的新手。
XMLInputSource xml_in = new XMLInputSource("DictionaryAnnotatorDescriptor.xml");
ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(xml_in);
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
JCas jCas = ae.newJCas();
String inputText = "Mark and John went down the rabbit hole to meet a wise owl and have curry with the owl.";
jCas.setDocumentText(inputText);
printResults(jCas);
public static void printResults(JCas jcas) {
FSIndex<Annotation> index = jcas.getAnnotationIndex();
for (Iterator<Annotation> it = index.iterator(); it.hasNext(); ) {
Annotation annotation = it.next();
List<Feature> features;
features = annotation.getType().getFeatures();
List<String> fasl = new ArrayList<String>();
for (Feature feature : features) {
try {
String name = feature.getShortName();
System.out.println(feature.getName());
String value = annotation.getStringValue(feature);
fasl.add(name + "=\"" + value + "\"");
System.out.println(value);
}catch (Exception e){
continue;
}
}
}
}
my_dictionary.xml
<?xml version="1.0" encoding="UTF-8"?>
<dictionary xmlns="http://incubator.apache.org/uima" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="dictionary.xsd">
<typeCollection>
<dictionaryMetaData caseNormalization="true" multiWordEntries="true" multiWordSeparator=" "/>
<languageId>en</languageId>
<typeDescription>
<typeName>org.apache.uima.DictionaryEntry</typeName>
</typeDescription>
<entries>
<entry>
<key>Mark</key>
</entry>
<entry>
<key>John</key>
</entry>
<entry>
<key>Rabbit</key>
</entry>
<entry>
<key>Owl</key>
</entry>
<entry>
<key>Curry</key>
</entry>
<entry>
<key>ATH-MX50</key>
</entry>
<entry>
<key>CC234</key>
</entry>
</entries>
</typeCollection>
</dictionary>
DictionaryAnnotatorDescriptor.xml
<?xml version="1.0" encoding="UTF-8"?>
<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
<frameworkImplementation>org.apache.uima.java</frameworkImplementation>
<primitive>true</primitive>
<annotatorImplementationName>org.apache.uima.annotator.dict_annot.impl.DictionaryAnnotator</annotatorImplementationName>
<analysisEngineMetaData>
<name>GeneDictionaryAnnotator</name>
<description></description>
<version>0.1</version>
<vendor></vendor>
<configurationParameters>
<configurationParameter>
<name>DictionaryFiles</name>
<description>list of dictionary files to configure the annotator</description>
<type>String</type>
<multiValued>true</multiValued>
<mandatory>true</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchType</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>true</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchFeaturePath</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchFilterFeaturePath</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>FilterConditionOperator</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>FilterConditionValue</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
</configurationParameters>
<configurationParameterSettings>
<nameValuePair>
<name>DictionaryFiles</name>
<value>
<array>
<string>src/main/resources/my_dictionary.xml</string>
</array>
</value>
</nameValuePair>
<nameValuePair>
<name>InputMatchType</name>
<value>
<string>org.apache.uima.TokenAnnotation</string>
</value>
</nameValuePair>
</configurationParameterSettings>
<typeSystemDescription>
<types>
<typeDescription>
<name>org.apache.uima.DictionaryEntry</name>
<description></description>
<supertypeName>uima.tcas.Annotation</supertypeName>
</typeDescription>
<typeDescription>
<name>org.apache.uima.TokenAnnotation</name>
<description>Single token annotation</description>
<supertypeName>uima.tcas.Annotation</supertypeName>
<features>
<featureDescription>
<name>tokenType</name>
<description>token type</description>
<rangeTypeName>uima.cas.String</rangeTypeName>
</featureDescription>
</features>
</typeDescription>
<typeDescription>
<name>example.Name</name>
<description>A proper name.</description>
<supertypeName>uima.tcas.Annotation</supertypeName>
</typeDescription>
</types>
</typeSystemDescription>
<capabilities>
<capability>
<inputs/>
<outputs>
<type>example.Name</type>
</outputs>
<languagesSupported/>
</capability>
</capabilities>
<operationalProperties>
<modifiesCas>true</modifiesCas>
<multipleDeploymentAllowed>true</multipleDeploymentAllowed>
<outputsNewCASes>false</outputsNewCASes>
</operationalProperties>
</analysisEngineMetaData>
</analysisEngineDescription>
或者,您也可以使用 Apache Ruta,与 workbench(推荐开始使用)或 java 代码一起使用。
对于后者,我在 https://github.com/renaud/annotate_ruta_example 创建了一个示例项目。主要部分是:
src/main/resources/ruta/resources/names.txt
中的姓名列表(纯文本文件)
Mark
John
Rabbit
Owl
Curry
ATH-MX50
CC234
src/main/resources/ruta/scripts/Example.ruta
中的 Ruta 脚本
PACKAGE example.annotate; // optional package def
WORDLIST MyNames = 'names.txt'; // declare dictionary location
DECLARE Name; // declare an annotation
Document{-> MARKFAST(Name, MyNames)}; // annotate document
和一些 java 启动注释器的样板代码:
JCas jCas = JCasFactory.createJCas();
// the sample text to annotate
jCas.setDocumentText("Mark wants to buy CC234.");
// configure the engine with scripts and resources
AnalysisEngine rutaEngine = AnalysisEngineFactory.createEngine(
RutaEngine.class, //
RutaEngine.PARAM_RESOURCE_PATHS,
"src/main/resources/ruta/resources",//
RutaEngine.PARAM_SCRIPT_PATHS,
"src/main/resources/ruta/scripts",
RutaEngine.PARAM_MAIN_SCRIPT, "Example");
// run the script. instead of a jCas, you could also provide a UIMA collection reader to process many documents
SimplePipeline.runPipeline(jCas, rutaEngine);
// a simple select to print the matched Names
for (Name name : JCasUtil.select(jCas, Name.class)) {
System.out.println(name.getCoveredText());
}
还有一些UIMA类型(注解)定义,查看src/main/resources/desc/type/ExampleTypes.xml
、src/main/resources/META-INF/org.apache.uima.fit/types.txt
和src/main/java/example/annotate
。
如何测试
git clone https://github.com/renaud/annotate_ruta_example.git
cd annotate_ruta_example
mvn clean install
mvn exec:java -Dexec.mainClass="example.Annotate"
我有一个从 UIMA 的 DictionaryCreator 创建的字典,我想使用 DictionaryAnnotator 和上述字典来注释一段文本,但我不知道如何获取注释的文本。如果你这样做,请告诉我。任何帮助表示赞赏。下面提到了代码、字典文件和描述符, P.S。我是 Apache UIMA 的新手。
XMLInputSource xml_in = new XMLInputSource("DictionaryAnnotatorDescriptor.xml");
ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(xml_in);
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
JCas jCas = ae.newJCas();
String inputText = "Mark and John went down the rabbit hole to meet a wise owl and have curry with the owl.";
jCas.setDocumentText(inputText);
printResults(jCas);
public static void printResults(JCas jcas) {
FSIndex<Annotation> index = jcas.getAnnotationIndex();
for (Iterator<Annotation> it = index.iterator(); it.hasNext(); ) {
Annotation annotation = it.next();
List<Feature> features;
features = annotation.getType().getFeatures();
List<String> fasl = new ArrayList<String>();
for (Feature feature : features) {
try {
String name = feature.getShortName();
System.out.println(feature.getName());
String value = annotation.getStringValue(feature);
fasl.add(name + "=\"" + value + "\"");
System.out.println(value);
}catch (Exception e){
continue;
}
}
}
}
my_dictionary.xml
<?xml version="1.0" encoding="UTF-8"?>
<dictionary xmlns="http://incubator.apache.org/uima" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="dictionary.xsd">
<typeCollection>
<dictionaryMetaData caseNormalization="true" multiWordEntries="true" multiWordSeparator=" "/>
<languageId>en</languageId>
<typeDescription>
<typeName>org.apache.uima.DictionaryEntry</typeName>
</typeDescription>
<entries>
<entry>
<key>Mark</key>
</entry>
<entry>
<key>John</key>
</entry>
<entry>
<key>Rabbit</key>
</entry>
<entry>
<key>Owl</key>
</entry>
<entry>
<key>Curry</key>
</entry>
<entry>
<key>ATH-MX50</key>
</entry>
<entry>
<key>CC234</key>
</entry>
</entries>
</typeCollection>
</dictionary>
DictionaryAnnotatorDescriptor.xml
<?xml version="1.0" encoding="UTF-8"?>
<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
<frameworkImplementation>org.apache.uima.java</frameworkImplementation>
<primitive>true</primitive>
<annotatorImplementationName>org.apache.uima.annotator.dict_annot.impl.DictionaryAnnotator</annotatorImplementationName>
<analysisEngineMetaData>
<name>GeneDictionaryAnnotator</name>
<description></description>
<version>0.1</version>
<vendor></vendor>
<configurationParameters>
<configurationParameter>
<name>DictionaryFiles</name>
<description>list of dictionary files to configure the annotator</description>
<type>String</type>
<multiValued>true</multiValued>
<mandatory>true</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchType</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>true</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchFeaturePath</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>InputMatchFilterFeaturePath</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>FilterConditionOperator</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
<configurationParameter>
<name>FilterConditionValue</name>
<description></description>
<type>String</type>
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
</configurationParameters>
<configurationParameterSettings>
<nameValuePair>
<name>DictionaryFiles</name>
<value>
<array>
<string>src/main/resources/my_dictionary.xml</string>
</array>
</value>
</nameValuePair>
<nameValuePair>
<name>InputMatchType</name>
<value>
<string>org.apache.uima.TokenAnnotation</string>
</value>
</nameValuePair>
</configurationParameterSettings>
<typeSystemDescription>
<types>
<typeDescription>
<name>org.apache.uima.DictionaryEntry</name>
<description></description>
<supertypeName>uima.tcas.Annotation</supertypeName>
</typeDescription>
<typeDescription>
<name>org.apache.uima.TokenAnnotation</name>
<description>Single token annotation</description>
<supertypeName>uima.tcas.Annotation</supertypeName>
<features>
<featureDescription>
<name>tokenType</name>
<description>token type</description>
<rangeTypeName>uima.cas.String</rangeTypeName>
</featureDescription>
</features>
</typeDescription>
<typeDescription>
<name>example.Name</name>
<description>A proper name.</description>
<supertypeName>uima.tcas.Annotation</supertypeName>
</typeDescription>
</types>
</typeSystemDescription>
<capabilities>
<capability>
<inputs/>
<outputs>
<type>example.Name</type>
</outputs>
<languagesSupported/>
</capability>
</capabilities>
<operationalProperties>
<modifiesCas>true</modifiesCas>
<multipleDeploymentAllowed>true</multipleDeploymentAllowed>
<outputsNewCASes>false</outputsNewCASes>
</operationalProperties>
</analysisEngineMetaData>
</analysisEngineDescription>
或者,您也可以使用 Apache Ruta,与 workbench(推荐开始使用)或 java 代码一起使用。
对于后者,我在 https://github.com/renaud/annotate_ruta_example 创建了一个示例项目。主要部分是:
src/main/resources/ruta/resources/names.txt
中的姓名列表(纯文本文件)
Mark
John
Rabbit
Owl
Curry
ATH-MX50
CC234
src/main/resources/ruta/scripts/Example.ruta
PACKAGE example.annotate; // optional package def
WORDLIST MyNames = 'names.txt'; // declare dictionary location
DECLARE Name; // declare an annotation
Document{-> MARKFAST(Name, MyNames)}; // annotate document
和一些 java 启动注释器的样板代码:
JCas jCas = JCasFactory.createJCas();
// the sample text to annotate
jCas.setDocumentText("Mark wants to buy CC234.");
// configure the engine with scripts and resources
AnalysisEngine rutaEngine = AnalysisEngineFactory.createEngine(
RutaEngine.class, //
RutaEngine.PARAM_RESOURCE_PATHS,
"src/main/resources/ruta/resources",//
RutaEngine.PARAM_SCRIPT_PATHS,
"src/main/resources/ruta/scripts",
RutaEngine.PARAM_MAIN_SCRIPT, "Example");
// run the script. instead of a jCas, you could also provide a UIMA collection reader to process many documents
SimplePipeline.runPipeline(jCas, rutaEngine);
// a simple select to print the matched Names
for (Name name : JCasUtil.select(jCas, Name.class)) {
System.out.println(name.getCoveredText());
}
还有一些UIMA类型(注解)定义,查看src/main/resources/desc/type/ExampleTypes.xml
、src/main/resources/META-INF/org.apache.uima.fit/types.txt
和src/main/java/example/annotate
。
如何测试
git clone https://github.com/renaud/annotate_ruta_example.git
cd annotate_ruta_example
mvn clean install
mvn exec:java -Dexec.mainClass="example.Annotate"