我想在GATE中使用TermRaider功能。有人可以发布一些示例代码来加载并在java类中使用此资源。我试过跟随但失败了。
Gate.getCreoleRegister().registerDirectories(new URL("file:///D:/misc_workspace/gate-7.1-build4485-SRC/plugins/TermRaider"));
ProcessingResource termRaider = (ProcessingResource) Factory.
createResource("gate.termraider.TermRaiderEnglish",Factory.newFeatureMap());
Exception:
gate.termraider.TermRaiderEnglish cannot be cast to gate.ProcessingResource
有人可以建议我该怎么做。
答案 0 :(得分:0)
TermRaider系统不是单个PR,它是一个完整的应用程序(事实上是一个Groovy ScriptableController
)。 TermraiderEnglish
资源只是一个钩子,可以使该应用程序出现在GATE Developer GUI的“现成应用程序”菜单中。
在嵌入式代码中,您可以使用PersistenceManager
File termRaiderPlugin = new File(Gate.getPluginsHome(), "TermRaider");
File gappFile = new File(new File(termRaiderPlugin, "applications"),
"termraider-eng.gapp");
CorpusController trApp = (CorpusController)PersistenceManager.loadObjectFromFile(
gappFile);
当您在语料库上运行应用程序时,它会创建三个“termbank”LR的新实例,其中包含有关新发现的术语的信息。 vanilla应用程序实际上是用于GUI而不是嵌入式使用,因此它不会在任何有用的地方存储对这些新LR的引用 - 您必须询问CreoleRegister
才能找到它们。您可能更喜欢制作自己的应用程序副本并调整控制脚本以将术语库实例存储为Corpus
上的(例如)功能,方法是添加类似
corpus.features.tfidfTermbank = termbank0
corpus.features.annotationTermbank = termbank1
corpus.features.hyponymyTermbank = termbank2
到控制脚本的末尾。然后,您可以通过corpus.getFeatures().get("tfidfTermbank")
等
由于这些Termbank类本身是TermRaider
插件的一部分,因此您可能希望将gate-termraider.jar
添加到主应用程序类路径中,而不是通过GateClassLoader
加载它。
答案 1 :(得分:0)
import gate.Corpus;
import gate.CorpusController;
import gate.Document;
import gate.Factory;
import gate.FeatureMap;
import gate.Gate;
import gate.termraider.bank.AbstractTermbank;
import gate.termraider.output.CsvGenerator;
import gate.util.GateException;
import gate.util.Out;
import gate.util.persistence.PersistenceManager;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
public class termraider {
public static void main(String[] args) throws IOException, GateException {
// initialise the GATE library
Out.prln("Initialising GATE...");
Gate.init();
// Initialize GATE
File gateHome = Gate.getGateHome();
Out.prln("...GATE initialised");
//Load TermRaider plugin
File termRaiderPlugin = new File(Gate.getPluginsHome(), "TermRaider");
File gappFile = new File(new File(termRaiderPlugin, "applications"),
"termraider-eng.gapp");
CorpusController trApp = (CorpusController)PersistenceManager.loadObjectFromFile(gappFile);
System.out.println("TermRaider loaded successfully!!!");
//Loading txt files from a folder path
Corpus corpus = (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
//String dirname = "Desktop/Gate_corpus/About Us/New Folder";
String dirname = "Desktop/GermanHPFCompetition/termRaider";
File f1 = new File(dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++) {
String path = dirname + "/" + s[i];
path = URLDecoder.decode(path, "utf-8");
path = new File(path).getPath();
URL u=new URL("file:\\\\\\"+path);
FeatureMap params = Factory.newFeatureMap();
params.put("sourceUrl", u);
params.put("preserveOriginalContent", new Boolean(true));
params.put("collectRepositioningInfo", new Boolean(true));
//Out.prln("Creating doc for " + u);
Document doc = (Document)
Factory.createResource("gate.corpora.DocumentImpl", params);
corpus.add(doc);
} // for each file in the folder
//running TermRaider plugin with the corpus
trApp.init();
trApp.setCorpus(corpus);
trApp.execute();
Corpus output_corpus = (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
output_corpus=trApp.getCorpus();
System.out.println("TermRaider executed successfully!!!");
//Creating csv files as output
AbstractTermbank tb1 = (AbstractTermbank) output_corpus.getFeatures().get("tfidfTermbank");
AbstractTermbank tb2 = (AbstractTermbank) output_corpus.getFeatures().get("hyponymyTermbank");
AbstractTermbank tb3 = (AbstractTermbank) output_corpus.getFeatures().get("annotationTermbank");
System.out.println(tb1);
System.out.println(tb2);
System.out.println(tb3);
CsvGenerator generator = new CsvGenerator();
File outputFile1 = new File("Desktop/GermanHPFCompetition/termRaider/tfidfTermbank.csv");
File outputFile2 = new File("Desktop/GermanHPFCompetition/termRaider/hyponymyTermbank.csv");
File outputFile3 = new File("Desktop/GermanHPFCompeti`enter code here`tion/termRaider/annotationTermbank.csv");
double threshold1 = 0;
double threshold2 = 0;
double threshold3 = 0;
generator.generateAndSaveCsv(tb1, threshold1, outputFile1);
generator.generateAndSaveCsv(tb2, threshold2, outputFile2);
generator.generateAndSaveCsv(tb3, threshold3, outputFile3);
System.out.println("CSV files created!!!");
}//end of main
}//end of class