2013-03-02 55 views
0

我想在GATE中使用TermRaider功能。有人可能會發布一些示例代碼來加載和使用這個資源在Java類。我嘗試過跟隨但失敗。在GATE中使用TermRaider插件

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

請有人給樣品鏈接至少。 – 2013-03-03 11:46:53

回答

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」邏輯資源的新實例。 vanilla應用程序實際上是用於GUI而不是嵌入式應用程序,因此它不會將這些新LR的引用存儲在任何有用的位置 - 您將不得不詢問CreoleRegister以找到它們。你可能更願意讓自己的應用程序副本和調整控制腳本存儲termbank實例爲(比方說)功能的Corpus,通過添加類似

corpus.features.tfidfTermbank = termbank0 
corpus.features.annotationTermbank = termbank1 
corpus.features.hyponymyTermbank = termbank2 

控制腳本結束。然後,您可以通過corpus.getFeatures().get("tfidfTermbank")

訪問它們在Java代碼中由於這些Termbank類本身就是TermRaider插件的一部分,你可能會想添加gate-termraider.jar到主應用程序的類路徑,而不是通過GateClassLoader加載它。

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