使用UIMA从XML文件中提取文本

时间:2014-04-03 11:40:47

标签: xml uima

我正在使用UIMA构建XML的文本提取器。由于我是UIMA框架的初学者,我想知道如何去做。

据我所知,UIMA可以注释文件的特定部分,但如何有效地提取信息?任何帮助表示赞赏。

谢谢, Jatin

2 个答案:

答案 0 :(得分:3)

UIMA Ruta的开发人员的有限视角中,我使用了UIMA Ruta的HtmlAnnotator来处理这些用例。这当然不是最有效的方法。分析引擎不会对元素使用单独的类型,因为它只知道最常见的html标记,但是如果需要,我会在UIMA Ruta中执行转换为预定义类型系统。在后端,htmlparser已应用。

答案 1 :(得分:1)

这是一个集合阅读器,可以帮助您入门:

import static com.google.common.base.Preconditions.checkArgument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.uima.UimaContext;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.fit.descriptor.TypeCapability;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



@TypeCapability(outputs = "xxx")
public class XmlCollectionReader extends JCasCollectionReader_ImplBase {
    private static Logger LOG = LoggerFactory.getLogger(XmlCollectionReader.class);

    private SAXBuilder builder;
    private XMLOutputter xo;
    private XPathExpression<Object> sentenceXPath;

    @Override
    public void initialize(UimaContext context) throws ResourceInitializationException {
        super.initialize(context);
        try {
            File corpusDir = new File(inputDir);
            checkArgument(corpusDir.exists());
            fileIterator = DirectoryIterator.get(directoryIterator, corpusDir, "xml", false);
            builder = new SAXBuilder();
            xo = new XMLOutputter();
            xo.setFormat(Format.getRawFormat());
            sentenceXPath = XPathFactory.instance().compile("//S");
        } catch (Exception e) {
            throw new ResourceInitializationException(
                    ResourceInitializationException.NO_RESOURCE_FOR_PARAMETERS,
                    new Object[] { inputDir });
        }
    }

    public void getNext(JCas jcas) throws IOException, CollectionException {

        File file = fileIterator.next();
        try {
            LOG.debug("reading {}", file.getName());
            Document doc = builder.build(new FileInputStream(file));
            Element rootNode = doc.getRootElement();

            String title = xo.outputString(rootNode.getChild("Title").getContent());

            for (Object sentence : sentenceXPath.evaluate(rootNode)) {
                Element sentenceE = (Element) sentence;
                    ...
                }
            }

            jcas.setDocumentText(...);

        } catch (JDOMException e) {
            throw new CollectionException(e);
        }
    }
}