Vous êtes sur la page 1sur 6

HOW TO DEVELOP A NEW SERVICE

If you want develop a service using our resources and our services, you should firstly follow these three simple steps.

1. Sign up on the project website


http://54.213.50.118:9000/accounts/

2. Obtain an access token


e.g., 78cab860-d17d-4afa-b1acd6ff84e942d7

3. Sign in with your credentials

Compile a form with the following fields: name surname e-mail type of user ...

After signing in, youll be able to see a list of active services and resources. The requested URL will vary for services and resources: Resource: http://140.203.155.231:8890/sparql/{resource_id}, being resource_id a hash (e.g., c1eb1886-b3a1-4435-9284-fa1ecd9a8d55) Service: http://217.26.90.243:8080/EuroSentimentServices/services/server/access/{service_id}, being service_id a hash (e.g., 11eb1886-b3a1-4435-9284-fa1ecd9a8d55) Note that all the HTTP method are POST and all the input parameters are in json formats, e.g., {"input": bla, bla, bla..."}

Now, we enter at the heart of the question: consume an Eurosentiment service via its REST api for developing a new service. Suppose that we have choosen from the previous list the S/E analyzer service which performs sentiment and emotion analysis on a text. The service performs sentiment analysis with automatic language (within english, italian, german, spanish, catalan and portuguese languages) and domain detection (within hotel and electronics domains). The following picture shows the Java code to implement a simple REST client. In order to make a request to EuroSentiment LRP you must include a new HTTP header field, named xeurosentiment-token, with the value of your access-token (e.g., 78cab860-d17d-4afa-b1ac-d6ff84e942d7), as you can see in the code below.
import import import import com.sun.jersey.api.client.Client; com.sun.jersey.api.client.ClientResponse; com.sun.jersey.api.client.WebResource; com.sun.jersey.api.client.WebResource.Builder;

public class Services_Client {

public String postClient(String service, String req){ String results = ""; try { Client client = Client.create(); WebResource webResource = client.resource(service); Builder b = webResource.header("x-eurosentiment-token", "78cab860-d17d-4afa-b1ac-d6ff84e942d7 "); ClientResponse response = b.post(ClientResponse.class, req);
results = response.getEntity(String.class); } catch (Exception e) { e.printStackTrace(); } return results; } }

Note: in the code above we use Jersey APIs: https://jersey.java.net/download.html

At this point, in the main class you have to put the text that you want analyze, i.e., La stanza dell'hotel era spaziosa e pulita, sono molto contento. La colazione era abbondante e molto soddisfacente!, in json format.
JSONObject j = new JSONObject(); j.put("input", "La stanza dell'hotel era spaziosa e pulita, sono molto contento. La colazione era abbondante e molto soddisfacente!");

Then, you just have to perform the request to the REST endpoint of the S/E analyzer service that has id, e.g., 123456.
String results = Client.postClient(http://217.26.90.243:8080/EuroSentimentServices/services/server/access/123456, j.toString());

The results of the request are shown in JSON format and are ready to be used in every service that you'll want to implement!
{ "@context": "http://eurosentiment.eu/contexts/basecontext.jsonld", "lang": "it", "domain":"hotels", "results": { "strings": [ "La stanza dell'hotel era spaziosa e pulita, sono molto contento.", "La colazione era abbondante e molto soddisfacente!" ], "entities": [ { "id":"stanza", "words": {"spazioso": 90.00, "pulito": 90.00}, "emotions": {}, "opinions": {"polarity":"positive", "polarityValue": 90.00} }, { "id":"colazione", "words": {"soddisfacente":90.00}, "emotions": {"satisfaction": 90.00}, "opinions": {"polarity":"positive", "polarityValue": 90.00} } ], "emotions": {"satisfaction": 10.00}, "opinions": {"polarity":"positive", "polarityValue": 90.00}, "provider": "ExpertSystem } }

The following code is the method resulting by the previous steps; a simple java code to consume the S/E analyzer service. You can use this code for accessing at every Eurosentiment service simply changing the id of the URL.
public void service_test(){ Services_Client client = new Services_Client(); JSONObject j = new JSONObject(); j.put("input","bla bla bla"); String results = client.postClient(http://217.26.90.243:8080/EuroSentimentServices/services/server/access/123456", j.toString()); }

Vous aimerez peut-être aussi