基於Gate的中文信息抽取API調用方式--未成功

學習Gate快一週了,一直在看官方的英文文檔,卻一直收穫不到,想在自己的程序中通過API的方式實現調用Gate完成信息抽取。Gate中的ANNIE可以實現英文的命名實體識別,但是卻不支持中文,後來發現Gate提供了中文的插件即放於目錄plugins下的Lang_Chinese文件夾中的內容,並且提供了分詞的功能,然後嘗試了一下,具體思路如下。

1.設置Gate的home等系統變量。
2.調用Gate.init()進行初始化。
3.採用語句加載中文插件: Gate.getCreoleRegister().registerDirectories(new File(pluginsHome, "Lang_Chinese").toURI().toURL())。
4.採用語句
 (ConditionalSerialAnalyserController) PersistenceManager.loadObjectFromFile(new File(new File(Gate.getPluginsHome(),"Lang_Chinese"), "resources/chinese.gapp"))
創建控制器。
5.設置語料路徑,語料是經過分詞後以空格隔開的詞語。
6.程序運行。
其中我採用的是IKAnalyer進行分詞,詞語以空格隔開,作爲Gate的輸入內容。

部分的代碼如下:

		setGateHome();
		Gate.init();

		// Load ANNIE plugin
		File gateHome = Gate.getGateHome();
		File pluginsHome = new File(gateHome, "plugins");
		Gate.getCreoleRegister().registerDirectories(new File(pluginsHome, "Lang_Chinese").toURI().toURL());
		Out.prln("...GATE initialised");

		// initialise ANNIE (this may take several minutes)
		StandAloneChinese chnie = new StandAloneChinese();
		chnie.initCHNIE();

		// create a GATE corpus and add a document for each command-line
		// argument
		Corpus corpus = (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
		// for(int i = 0; i < args.length; i++) {
		// URL u = new URL(args[i]);
//		URL u = new URL("file:/d:\\corpus\\corpusedFile.txt");
		StringBuffer corpusDir = new StringBuffer("");
		corpusDir.append("file:/").append(System.getProperty("user.dir")).append("\\corpusedFile.txt");
		URL u = new URL(corpusDir.toString());
		FeatureMap params = Factory.newFeatureMap();
		params.put("sourceUrl", u);
		params.put("preserveOriginalContent", new Boolean(true));
		params.put("collectRepositioningInfo", new Boolean(true));
		params.put("encoding", "UTF-8");//以UTF-8編碼讀取信息 否則會出現亂碼
		Out.prln("Creating doc for " + u);
		Document docs = (Document) Factory.createResource("gate.corpora.DocumentImpl", params);
		corpus.add(docs);
		// } // for each of args

		// tell the pipeline about the corpus and run it
		chnie.setCorpus(corpus);
		chnie.execute();

其中 setGateHome()方法用於設置Gate所需的一些系統屬性。

	public static void setGateHome() {
		System.setProperty("gate.home", "D:\\gate");
		String configFileDir = System.getProperty("user.dir") + "\\gate.xml";
		System.setProperty("gate.user.config", configFileDir);
		String sessionFileDir = System.getProperty("user.dir") + "\\gate.session";
		System.setProperty("gate.user.session", sessionFileDir);
		System.out.println(System.getProperties().getProperty("gate.home"));
		System.out.println(System.getProperties().getProperty("gate.user.config"));
		System.out.println(System.getProperties().getProperty("gate.user.session"));
	}

下面是中文插件的初始化操作

	public void initCHNIE() throws GateException, IOException {
		Out.prln("Initialising CHNIE...");
		chnController = (ConditionalSerialAnalyserController) PersistenceManager.loadObjectFromFile(new File(new File(Gate.getPluginsHome(),"Lang_Chinese"), "resources/chinese.gapp"));
		
		Out.prln("...CHNIE loaded");
	} 

最終 程序可以運行,但是卻沒有標註結果。花了好幾天一直沒解決掉,真是百思不得其解。只能採取擴展ANNIE的方式進行中文處理了。

若有人發現了 上面所存在的錯誤,還望指出。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章