如何在Java中使用GATE Annie获取命名实体提取?

时间:2016-05-17 10:09:20

标签: java nlp named-entity-recognition gate

我是 GATE ANNIE 的新手。我尝试了 GATE GUI界面并获得了完成任务的经验。我想知道如何在Java中实现命名实体提取

我做了R& D但无法找到任何关于命名实体提取的教程。

是否有任何代码可以在 Java GATE ANNIE 中查找命名实体提取

2 个答案:

答案 0 :(得分:6)

import gate.*;
import gate.creole.ANNIEConstants;
import gate.util.persistence.PersistenceManager;
import java.io.File;
import java.util.*;

public class AnnieNerExample {

    public static void main(String[] args) throws Exception {
        Gate.setGateHome(new File("C:\\Program Files\\GATE_Developer_8.1"));
        Gate.init();

        LanguageAnalyser controller = (LanguageAnalyser) PersistenceManager
                .loadObjectFromFile(new File(new File(Gate.getPluginsHome(),
                        ANNIEConstants.PLUGIN_DIR), ANNIEConstants.DEFAULT_FILE));

        Corpus corpus = Factory.newCorpus("corpus");
        Document document = Factory.newDocument(
                "Michael Jordan is a professor at the University of California, Berkeley.");
        corpus.add(document); controller.setCorpus(corpus); 
        controller.execute();

        document.getAnnotations().get(new HashSet<>(Arrays.asList("Person", "Organization", "Location")))
            .forEach(a -> System.err.format("%s - \"%s\" [%d to %d]\n", 
                    a.getType(), Utils.stringFor(document, a),
                    a.getStartNode().getOffset(), a.getEndNode().getOffset()));

        //Don't forget to release GATE resources 
        Factory.deleteResource(document); Factory.deleteResource(corpus); Factory.deleteResource(controller);
    }
}

输出:

Person - "Michael Jordan" [0 to 14]
Organization - "University of California" [37 to 61]
Location - "Berkeley" [63 to 71]

两种可能性:

  1. 手册
  2. Quick Start with GATE Embedded

      

    $GATE_HOME/bin/gate.jar$GATE_HOME/lib中的JAR文件添加到Java CLASSPATH($GATE_HOME是GATE根目录)

    1. 的Maven

      <dependency>
          <groupId>uk.ac.gate</groupId>
          <artifactId>gate-core</artifactId>
          <version>8.4</version>
      </dependency>
      

答案 1 :(得分:0)

用于开发和在Java代码中使用。最好使用嵌入式门。

pom.xml文件

<dependencies>
    <dependency>
        <groupId>uk.ac.gate</groupId>
        <artifactId>gate-core</artifactId>
        <version>8.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/uk.ac.gate.plugins/annie -->
    <dependency>
        <groupId>uk.ac.gate.plugins</groupId>
        <artifactId>annie</artifactId>
        <version>8.6</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

src中的Main.java文件

import gate.*;
import gate.creole.ANNIEConstants;
import gate.creole.Plugin;
import gate.creole.ResourceReference;
import gate.util.persistence.PersistenceManager;

import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;

public class Main {

    public static void main(String[] args) throws Exception {
        Gate.init();
        Plugin anniePlugin = new Plugin.Maven("uk.ac.gate.plugins", "annie", "8.6");
        Gate.getCreoleRegister().registerPlugin(anniePlugin);
        URL annieFile = new ResourceReference(anniePlugin, "resources/" + ANNIEConstants.DEFAULT_FILE).toURL();

        LanguageAnalyser controller = (LanguageAnalyser) PersistenceManager.loadObjectFromUrl(annieFile);

        Corpus corpus = Factory.newCorpus("corpus");
        Document document = Factory.newDocument("Michael Jordan is a professor at the University of California, Berkeley.");
        corpus.add(document);
        controller.setCorpus(corpus);
        controller.execute();


        for (Annotation obj : document.getAnnotations().get(new HashSet<String>(Arrays.asList("Person", "Organization", "Location")))) {
            System.out.print("type : " + obj.getType());
            System.out.print("\t data : " + Utils.stringFor(document, obj));
            System.out.print("\t start : " + obj.getStartNode().getOffset());
            System.out.println("\t end : " + obj.getEndNode().getOffset());
        }
        Factory.deleteResource(document);
        Factory.deleteResource(corpus);
        Factory.deleteResource(controller);
    }
}

示例代码https://github.com/hsali/gate-developer-annie-plugin-example