Как получить аннотированный текст для DictionaryAnnotator

У меня есть словарь, созданный DictionaryCreator от UIMA, я хотел бы аннотировать кусок текста с помощью 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>

person Vinoth Kumar Asaithambi    schedule 10.02.2015    source источник


Ответы (1)


В качестве альтернативы вы также можете использовать Apache Ruta либо с верстак (рекомендуется для начала) или с кодом 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"
person Renaud    schedule 11.02.2015
comment
Спасибо, Рено! Я ценю его! Не могли бы вы также сказать мне, как сделать его нечувствительным к регистру, например, чтобы комментировать, даже если в тексте есть скажем ath-mx50? - person Vinoth Kumar Asaithambi; 11.02.2015
comment
вы можете настроить его так, чтобы он игнорировал регистр, если слово длиннее x символов, см. uima.apache.org/d/ruta-current/ - person Renaud; 12.02.2015
comment
Создание JCa также должно быть короче с помощью JCasFactory.createJCas(). - person rec; 12.02.2015
comment
Спасибо ребята! Я очень ценю это. Не могли бы вы также рассказать мне, как заменить окружающий текст аннотации. например, я Марк, сказав: «Я ‹a href=somesite/com/id›Mark‹ /a›' представьте, что ссылка взята из файла named.csv. Ваше здоровье! - person Vinoth Kumar Asaithambi; 12.02.2015
comment
Вам необходимо использовать MarkTable uima.apache.org/d/ruta-current/ и получить идентификатор в цикле for. - person Renaud; 13.02.2015