在JAVA中使用ambiversenlu

一、什麼是ambiversenlu?

Demo:https://ambiversenlu.mpi-inf.mpg.de

簡而言之,它能夠幫助我們進行實體識別、足夠方便的進行實體連接操作。

二、如何使用ambiversenlu?

在上面的demo鏈接頁面使用ambiversenlu十分方便,只需要將句子複製到輸入框,點擊analysis就能夠輕鬆得到結果。但是如何在程序中使用ambiversenlu呢?

(要在程序中使用ambiversenlu其實並不那麼容易。我花了近兩個晚上才實現在代碼中使用。)

在JAVA中使用ambiversenlu有兩種方式:一種是通過OKHTTP對ambiversenlu服務器訪問,進行實體識別、實體連接。一種是使用ambiverse-nlu/nlu-api-client-java

1、註冊ambiversenlu developerID

無論使用哪一種方法,都必須要註冊一個ambiversenlu賬號。註冊好後會有一個ID和Secret,如圖:

方法一:使用OKHTTP訪問:

(具體見:https://developer.ambiverse.com/overview

首先,在https://developer.ambiverse.com/docs中獲得一個token。這個token有效期是24小時。

如圖:

然後會得到一個token:

接着,在你配置好OKHTTP

配置好後就可以使用了!下面是簡單示例代碼:

public static void main(String[] args) {
		OkHttpClient client = new OkHttpClient();
		okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
		RequestBody body = RequestBody.create(mediaType, "{\"coherentDocument\": \"true\", \"confidenceThreshold\": \"0\", \"docId\": \"doc1\", \"text\": \"When Who played Tommy in Columbus, Pete was at his best.\", \"language\": \"en\",  \"annotatedMentions\" : [ { \"charLength\":3, \"charOffset\":5 } ] }");
		Request request = new Request.Builder()
		  .url("https://api.ambiverse.com/v2/entitylinking/analyze")
		  .post(body)
		  .addHeader("content-type", "application/json")
		  .addHeader("accept", "application/json")
		  .addHeader("authorization", "填入你得到的token")
		  .build();
		try (Response response = client.newCall(request).execute()) {
		      System.out.println(response.body().string()); 
		    } catch (IOException e) {
				e.printStackTrace();
			}
	}

方法二:使用Ambiverse Natural Language Understanding API Client Library for Java

這個項目可以直接在GitHub上找到,下載下來後主要是一些依賴問題。如果不適用maven的話,整個依賴jar包在這裏,把這些jar包導入項目,就可以運行了!(這些jar包我可是踩了很多坑踩弄全的!)

 

 

 

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