Author: pkluegl Date: Tue Mar 26 10:29:16 2013 New Revision: 1461050 URL: http://svn.apache.org/r1461050 Log: UIMA-2745 - added TextMarker.apply(CAS, String) - added mention in documentation - added test - renamed file due to typo
Added: uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/engine/TextMarker.java uima/sandbox/textmarker/trunk/textmarker-core/src/main/resources/org/apache/uima/textmarker/engine/TypePriorities.xml - copied unchanged from r1442746, uima/sandbox/textmarker/trunk/textmarker-core/src/main/resources/org/apache/uima/textmarker/engine/TypePrioritites.xml uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/engine/TextMarkerTest.java uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/TypePriorities.xml Removed: uima/sandbox/textmarker/trunk/textmarker-core/src/main/resources/org/apache/uima/textmarker/engine/TypePrioritites.xml Modified: uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.overview.xml Added: uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/engine/TextMarker.java URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/engine/TextMarker.java?rev=1461050&view=auto ============================================================================== --- uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/engine/TextMarker.java (added) +++ uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/engine/TextMarker.java Tue Mar 26 10:29:16 2013 @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.uima.textmarker.engine; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.apache.uima.UIMAFramework; +import org.apache.uima.analysis_engine.AnalysisEngine; +import org.apache.uima.analysis_engine.AnalysisEngineDescription; +import org.apache.uima.analysis_engine.AnalysisEngineProcessException; +import org.apache.uima.cas.CAS; +import org.apache.uima.resource.ResourceConfigurationException; +import org.apache.uima.resource.ResourceInitializationException; +import org.apache.uima.resource.ResourceManager; +import org.apache.uima.resource.ResourceSpecifier; +import org.apache.uima.util.FileUtils; +import org.apache.uima.util.InvalidXMLException; +import org.apache.uima.util.XMLInputSource; + +public class TextMarker { + + public static void apply(CAS cas, String script) throws IOException, InvalidXMLException, + ResourceInitializationException, ResourceConfigurationException, + AnalysisEngineProcessException { + URL aedesc = TextMarkerEngine.class.getResource("BasicEngine.xml"); + XMLInputSource inae = new XMLInputSource(aedesc); + ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(inae); + ResourceManager resMgr = UIMAFramework.newDefaultResourceManager(); + AnalysisEngineDescription aed = (AnalysisEngineDescription) specifier; + +// TypeSystemDescription basicTypeSystem = aed.getAnalysisEngineMetaData().getTypeSystem(); +// Collection<TypeSystemDescription> tsds = new ArrayList<TypeSystemDescription>(); +// tsds.add(basicTypeSystem); +// TypeSystemDescription mergeTypeSystems = CasCreationUtils.mergeTypeSystems(tsds); +// aed.getAnalysisEngineMetaData().setTypeSystem(mergeTypeSystems); +// aed.resolveImports(resMgr); + + AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aed, resMgr, null); + File scriptFile = File.createTempFile("TextMarker", ".tm"); + scriptFile.deleteOnExit(); + if (!script.startsWith("PACKAGE")) { + script = "PACKAGE org.apache.uima.textmarker;\n" + script; + } + FileUtils.saveString2File(script, scriptFile); + ae.setConfigParameterValue(TextMarkerEngine.SCRIPT_PATHS, new String[] { scriptFile + .getParentFile().getAbsolutePath() }); + String name = scriptFile.getName().substring(0, scriptFile.getName().length() - 3); + ae.setConfigParameterValue(TextMarkerEngine.MAIN_SCRIPT, name); + ae.reconfigure(); + ae.process(cas); + scriptFile.delete(); + ae.destroy(); + } + +} Added: uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/engine/TextMarkerTest.java URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/engine/TextMarkerTest.java?rev=1461050&view=auto ============================================================================== --- uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/engine/TextMarkerTest.java (added) +++ uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/engine/TextMarkerTest.java Tue Mar 26 10:29:16 2013 @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.uima.textmarker.engine; + +import static org.junit.Assert.assertEquals; + +import java.net.URL; + +import org.apache.uima.UIMAFramework; +import org.apache.uima.cas.CAS; +import org.apache.uima.cas.FSIterator; +import org.apache.uima.cas.text.AnnotationFS; +import org.apache.uima.cas.text.AnnotationIndex; +import org.apache.uima.resource.metadata.FsIndexDescription; +import org.apache.uima.resource.metadata.TypePriorities; +import org.apache.uima.resource.metadata.TypeSystemDescription; +import org.apache.uima.textmarker.TextMarkerTestUtils; +import org.apache.uima.util.CasCreationUtils; +import org.apache.uima.util.XMLInputSource; +import org.junit.Test; + +public class TextMarkerTest { + + @Test + public void test() throws Exception { + URL typePrioritiesUrl = TextMarkerTestUtils.class.getResource("TypePriorities.xml"); + URL tsUrl = TextMarkerTestUtils.class.getResource("BasicTypeSystem.xml"); + Object descriptor = UIMAFramework.getXMLParser().parse(new XMLInputSource(tsUrl)); + TypeSystemDescription tsDesc = (TypeSystemDescription) descriptor; + tsDesc.addType("uima.textmarker.T1", "", "uima.tcas.Annotation"); + tsDesc.addType("uima.textmarker.T2", "", "uima.tcas.Annotation"); + tsDesc.addType("uima.textmarker.T3", "", "uima.tcas.Annotation"); + tsDesc.resolveImports(); + TypePriorities typePriorities = UIMAFramework.getXMLParser().parseTypePriorities(new XMLInputSource(typePrioritiesUrl)); + CAS cas = CasCreationUtils.createCas(tsDesc, typePriorities, new FsIndexDescription[0]); + + cas.setDocumentText("Some document."); + + TextMarker.apply(cas, "CW{-> MARK(T1)} SW;"); + AnnotationIndex<AnnotationFS> ai = null; + FSIterator<AnnotationFS> iterator = null; + ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("uima.textmarker.T1")); + iterator = ai.iterator(); + assertEquals(1, ai.size()); + assertEquals("Some", iterator.next().getCoveredText()); + + TextMarker.apply(cas, "T1 SW{-> MARK(T2)};"); + ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("uima.textmarker.T2")); + iterator = ai.iterator(); + assertEquals(1, ai.size()); + assertEquals("document", iterator.next().getCoveredText()); + + TextMarker.apply(cas, "T1{-> MARK(T3,1,2,3)} T2 PERIOD;"); + ai = cas.getAnnotationIndex(cas.getTypeSystem().getType("uima.textmarker.T3")); + iterator = ai.iterator(); + assertEquals(1, ai.size()); + assertEquals("Some document.", iterator.next().getCoveredText()); + + + cas.release(); + } +} Added: uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/TypePriorities.xml URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/TypePriorities.xml?rev=1461050&view=auto ============================================================================== --- uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/TypePriorities.xml (added) +++ uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/TypePriorities.xml Tue Mar 26 10:29:16 2013 @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<typePriorities xmlns="http://uima.apache.org/resourceSpecifier"> +<name>TypePrioritites</name> +<description></description> +<version>1.0</version> +<vendor></vendor> + + <priorityList> + <type>org.apache.uima.textmarker.type.TextMarkerFrame</type> + <type>uima.tcas.Annotation</type> + <type>org.apache.uima.textmarker.type.TextMarkerBasic</type> + </priorityList> + + +</typePriorities> + Modified: uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.overview.xml URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.overview.xml?rev=1461050&r1=1461049&r2=1461050&view=diff ============================================================================== --- uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.overview.xml (original) +++ uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.overview.xml Tue Mar 26 10:29:16 2013 @@ -591,7 +591,11 @@ ae.reconfigure(); CAS cas = ae.newCAS(); cas.setDocumentText("This is my document."); ae.process(cas);]]></programlisting> - + <para> + There is also a convenience implementation for applying simple scripts, which do not introduce new types. The following java code + applies a simple rule <quote>T1 SW{-> MARK(T2)};</quote> on the given CAS. Note that the types need to be already defined in the type system of the CAS. + </para> + <programlisting><![CDATA[TextMarker.apply(cas, "T1 SW{-> MARK(T2)};");]]></programlisting> </section> <section id="ugr.tools.tm.ae.basic.parameter"> <title>Configuration Parameters</title>