use of javax.xml.soap.SOAPBody in project stanbol by apache.
the class NERserviceClientHTTP method extractEntities.
public List<NamedEntity> extractEntities(String text, String lang) throws SOAPException, IOException {
if (text == null || text.isEmpty()) {
//no text -> no extractions
return Collections.emptyList();
}
//create the POST request
HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, -1);
//write content
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
writer.write(SOAP_PREFIX);
writer.write("<v0u0:processTextRequest><v0u0:text>");
StringEscapeUtils.escapeXml(writer, text);
writer.write("</v0u0:text><v0u0:language>" + lang + "</v0u0:language></v0u0:processTextRequest>");
writer.write(SOAP_SUFFIX);
writer.close();
//Call the service
long start = System.currentTimeMillis();
InputStream stream = con.getInputStream();
log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
// Create SoapMessage and parse the results
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
// Load the SOAP text into a stream source
StreamSource source = new StreamSource(stream);
// Set contents of message
soapPart.setContent(source);
SOAPBody soapBody = message.getSOAPBody();
//extract the results
List<NamedEntity> extractedNE = new Vector<NamedEntity>();
NodeList nlist = soapBody.getElementsByTagName("result");
for (int i = 0; i < nlist.getLength(); i++) {
Element result = (Element) nlist.item(i);
NamedEntity ne = new NamedEntity();
ne.setType(result.getElementsByTagNameNS("*", "type").item(0).getTextContent());
ne.setSource(result.getElementsByTagNameNS("*", "source").item(0).getTextContent());
ne.setFormKind(result.getElementsByTagNameNS("*", "formKind").item(0).getTextContent());
ne.setFrom(Long.parseLong(result.getElementsByTagNameNS("*", "from").item(0).getTextContent()));
ne.setTo(Long.parseLong(result.getElementsByTagNameNS("*", "to").item(0).getTextContent()));
extractedNE.add(ne);
}
return extractedNE;
}
use of javax.xml.soap.SOAPBody in project stanbol by apache.
the class ClassificationClientHTTP method extractConcepts.
public List<Concept> extractConcepts(String text, String lang) throws IOException, SOAPException {
if (text == null || text.isEmpty()) {
//no text -> no classification
return Collections.emptyList();
}
//create the POST request
HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
//"stream" the request content directly to the buffered writer
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
writer.write(SOAP_PREFIX);
writer.write("<clas:classify>");
//TODO: should the user be configurable?
writer.write("<clas:user>wiki</clas:user>");
writer.write("<clas:model>");
writer.write(lang);
writer.write("</clas:model>");
writer.write("<clas:text>");
//write the escaped text directly to the request
StringEscapeUtils.escapeXml(writer, text);
writer.write("</clas:text>");
writer.write("</clas:classify>");
writer.write(SOAP_SUFFIX);
writer.close();
//Call the service
long start = System.currentTimeMillis();
InputStream stream = con.getInputStream();
log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
StreamSource source = new StreamSource(stream);
// Set contents of message
soapPart.setContent(source);
SOAPBody soapBody = message.getSOAPBody();
List<Concept> extractedConcepts = new Vector<Concept>();
NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
for (int i = 0; i < nlist.getLength() && i < maxResultToReturn; i++) {
Element result = (Element) nlist.item(i);
//NOTE: (rwesten) implemented a mapping from the CELI classification
// to the Stanbol fise:TopicEnhancements (STANBOL-617) that
// * one fise:TopicAnnotation is generated per "model"
// * the whole label string is used as fise:entity-label
// * the uri of the most specific dbpedia ontology type (see
// selectClassificationClass) is used as fise:entity-reference
// This has the intuition that for users it is easier to grasp
// the meaning of the whole lable, while for machines the link
// to the most specific dbpedia ontology class is best suited.
String model = result.getElementsByTagNameNS("*", "label").item(0).getTextContent();
model = model.substring(1, model.length() - 1);
IRI modelConcept = selectClassificationClass(model);
String conf = result.getElementsByTagNameNS("*", "score").item(0).getTextContent();
Double confidence = new Double(conf);
extractedConcepts.add(new Concept(model, modelConcept, confidence));
}
return extractedConcepts;
}
use of javax.xml.soap.SOAPBody in project stanbol by apache.
the class LanguageIdentifierClientHTTP method guessLanguage.
//NOTE (rwesten): I rather do the error handling in the EnhancementEngine!
public List<GuessedLanguage> guessLanguage(String text) throws IOException, SOAPException {
if (text == null || text.isEmpty()) {
//no text -> no language
return Collections.emptyList();
}
//create the POST request
HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
//write content
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
writer.write(SOAP_PREFIX);
writer.write("<lan:guessLanguage><textToGuess>");
StringEscapeUtils.escapeXml(writer, text);
writer.write("</textToGuess></lan:guessLanguage>");
writer.write(SOAP_SUFFIX);
writer.close();
//Call the service
long start = System.currentTimeMillis();
InputStream stream = con.getInputStream();
log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
// Create SoapMessage and parse the results
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
// Load the SOAP text into a stream source
StreamSource source = new StreamSource(stream);
// Set contents of message
soapPart.setContent(source);
SOAPBody soapBody = message.getSOAPBody();
List<GuessedLanguage> guesses = new Vector<GuessedLanguage>();
NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
for (int i = 0; i < nlist.getLength(); i++) {
try {
Element result = (Element) nlist.item(i);
String lang = result.getAttribute("language");
double d = Double.parseDouble(result.getAttribute("guessConfidence"));
guesses.add(new GuessedLanguage(lang, d));
} catch (Exception e) {
e.printStackTrace();
}
}
return guesses;
}
use of javax.xml.soap.SOAPBody in project stanbol by apache.
the class LanguageIdentifierClientHTTP method guessQueryLanguage.
//NOTE (rwesten): I rather do the error handling in the EnhancementEngine!
public List<GuessedLanguage> guessQueryLanguage(String text) throws IOException, SOAPException {
if (text == null || text.isEmpty()) {
//no language
return Collections.emptyList();
}
//create the POST request
HttpURLConnection con = Utils.createPostRequest(serviceEP, requestHeaders, conTimeout);
//write content
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), UTF8));
writer.write(SOAP_PREFIX);
writer.write("<lan:guessQueryLanguage><textToGuess>");
StringEscapeUtils.escapeXml(writer, text);
writer.write("</textToGuess></lan:guessQueryLanguage>");
writer.write(SOAP_SUFFIX);
writer.close();
//Call the service
long start = System.currentTimeMillis();
InputStream stream = con.getInputStream();
log.debug("Request to {} took {}ms", serviceEP, System.currentTimeMillis() - start);
// Create SoapMessage and parse the results
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
// Load the SOAP text into a stream source
StreamSource source = new StreamSource(stream);
// Set contents of message
soapPart.setContent(source);
SOAPBody soapBody = message.getSOAPBody();
List<GuessedLanguage> guesses = new Vector<GuessedLanguage>();
NodeList nlist = soapBody.getElementsByTagNameNS("*", "return");
for (int i = 0; i < nlist.getLength(); i++) {
try {
Element result = (Element) nlist.item(i);
String lang = result.getAttribute("language");
double d = Double.parseDouble(result.getAttribute("guessConfidence"));
guesses.add(new GuessedLanguage(lang, d));
} catch (Exception e) {
e.printStackTrace();
}
}
return guesses;
}
Aggregations