Search in sources :

Example 31 with SOAPBody

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;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

Example 32 with SOAPBody

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;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

Example 33 with SOAPBody

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;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

Example 34 with SOAPBody

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;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) SOAPBody(javax.xml.soap.SOAPBody) HttpURLConnection(java.net.HttpURLConnection) SOAPPart(javax.xml.soap.SOAPPart) OutputStreamWriter(java.io.OutputStreamWriter) Vector(java.util.Vector)

Aggregations

SOAPBody (javax.xml.soap.SOAPBody)34 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPElement (javax.xml.soap.SOAPElement)17 SOAPException (javax.xml.soap.SOAPException)16 SOAPPart (javax.xml.soap.SOAPPart)14 MessageFactory (javax.xml.soap.MessageFactory)13 NodeList (org.w3c.dom.NodeList)12 QName (javax.xml.namespace.QName)9 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)9 Element (org.w3c.dom.Element)9 StreamSource (javax.xml.transform.stream.StreamSource)8 BufferedWriter (java.io.BufferedWriter)7 InputStream (java.io.InputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 Node (org.w3c.dom.Node)7 IOException (java.io.IOException)6 Test (org.testng.annotations.Test)6 Vector (java.util.Vector)5 SOAPHeader (javax.xml.soap.SOAPHeader)5