OpenCalais Macro for OpenOffice

  1. // OpenCalais Entity Extraction Macro for OpenOffice
  2. // To use: Select (highlight) a text region in an open document,
  3. // then invoke the macro. If all goes well, entity metadata
  4. // for your text selection will be appended to the document.
  5. // Of course, for this to work, you have to have an Internet connection...
  6.  
  7.  
  8. // Copyright 2009 Kas Thomas.
  9. // Twitter: kasthomas
  10. //
  11. // You may freely redistribute this script.
  12. // OpenCalais is a service of Thomson Reuters: http://www.opencalais.com
  13. // This script is not affiliated with Thomson Reuters.
  14. //
  15. // Disclaimer: Use this code at your own risk. No warranties of any
  16. // kind are made. Manufactured in a facility that processes peanuts.
  17.  
  18. importClass(Packages.com.sun.star.uno.UnoRuntime);
  19. importClass(Packages.com.sun.star.text.XTextDocument);
  20. importClass(Packages.com.sun.star.text.XText);
  21. importClass(Packages.com.sun.star.text.XTextRange );
  22. importClass(Packages.com.sun.star.text.XTextViewCursorSupplier);
  23.  
  24.  
  25. function appendTextToDocument( text ) {
  26.  
  27.         //get the document from the scripting context
  28.     var oDoc = XSCRIPTCONTEXT.getDocument( );
  29.  
  30.         //get the XTextDocument interface
  31.     var xTextDoc = UnoRuntime.queryInterface( XTextDocument,oDoc );
  32.  
  33.         //get the XText interface
  34.     var xText = xTextDoc.getText( );
  35.  
  36.         //get an (empty) XTextRange interface at the end of the text
  37.     var xTextRange = xText.getEnd( );
  38.  
  39.         //set the text in the XTextRange
  40.     xTextRange.setString( text );
  41. }
  42.  
  43.     // Get user-highlighted text selection from doc
  44.  
  45. function getSelectedText( ) {
  46.  
  47.     var controller = getController( );
  48.  
  49.     var xViewCursorSupplier =
  50.             UnoRuntime.queryInterface( XTextViewCursorSupplier, controller );
  51.  
  52.     var selectedText = xViewCursorSupplier.getViewCursor( ).getString( );
  53.     return selectedText;
  54. }
  55.  
  56. function getController( ) {   // standard OO.o cruft
  57.  
  58.     var oDoc = XSCRIPTCONTEXT.getDocument( );
  59.     var oController = oDoc.getCurrentController( );
  60.     return oController;
  61. }
  62.  
  63.    // Do a POST
  64. function doJavaPOST( url, content, headerData ) {
  65.  
  66.     var reply = "";
  67.  
  68.     try {
  69.         var URL = new java.net.URL( url );
  70.         var urlConn = URL.openConnection( );
  71.         urlConn.setDoInput ( true );
  72.         urlConn.setDoOutput ( true );
  73.         urlConn.setUseCaches( false );
  74.         if ( headerData != null && headerData instanceof Array ) {
  75.             for ( ;headerData.length > 0; )
  76.                 urlConn.setRequestProperty ( headerData.shift( ),
  77.                         headerData.shift( ) );
  78.         }
  79.         var printout =
  80.             new java.io.DataOutputStream ( urlConn.getOutputStream ( ) );
  81.         printout.writeBytes ( content );
  82.         printout.flush ( );
  83.         printout.close ( );
  84.         var input = new java.io.DataInputStream ( urlConn.getInputStream ( ) );
  85.         var str = "";
  86.         while( null != ( str=input.readLine( ) ) ) { reply += str; }
  87.     }
  88.     catch(exception) {
  89.         appendTextToDocument( "I/O failure: " + exception.getMessage() );
  90.  
  91.     }
  92.     finally {
  93.         if ( input != null ) input.close();
  94.     }
  95.  
  96.     return reply;
  97. }
  98.  
  99. function alert( msg ) {
  100.     var swing = Packages.javax.swing;
  101.     swing.JOptionPane.showMessageDialog( null,
  102.              msg, "", swing.JOptionPane.ERROR_MESSAGE );
  103. }
  104.  
  105. function getCalaisParamsXML( ) {
  106.  
  107.     // OpenCalais service requires 'paramsXML' argument
  108.     // sent in the following format.
  109.     // See http://www.opencalais.com/APIcalls#inputparameters
  110.  
  111.     var paramsXML = '<c:params xmlns:c="http://s.opencalais.com/1/pred/" ' +
  112.                 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' +
  113.         '<c:processingDirectives c:contentType="text/txt" ' +
  114.                 'c:enableMetadataType="GenericRelations" c:outputFormat="text/simple">' +
  115.         '</c:processingDirectives>' +
  116.     '</c:params>';
  117.  
  118.     return paramsXML;
  119. }
  120.  
  121. function getContent( ) {
  122.  
  123.     var text = getSelectedText( ); // get selection from document
  124.  
  125.     if ( text == "" ) {
  126.         alert( "Please highlight some text first." );
  127.         throw ( "No text to process." );
  128.     }
  129.     return text;
  130. }
  131.  
  132. function format ( markup ) {
  133.         // replace stripped newlines
  134.     return  markup.replace( />(\s+)/g, ">\n$1");
  135. }
  136.  
  137. // ========================  main ( )  =========================
  138.  
  139. ( function main ( ) {
  140.  
  141.         // OpenCalais service endpoint
  142.     var url = "http://api.opencalais.com/enlighten/rest/";
  143.  
  144.     var licensekey = *** INSERT YOUR LICENSE STRING HERE ***;  // Calais license key
  145.  
  146.         // craft the param string
  147.     var license = "licenseID=" + licensekey;
  148.     var theContent = "content=" + getContent( );
  149.     var params = "paramsXML=" + getCalaisParamsXML( );
  150.     var theData = license + "&" + theContent + "&" + params;
  151.  
  152.     var headerProperties = [  "Content-Type", "application/x-www-form-urlencoded"  ];
  153.  
  154.     // call the service
  155.     text = doJavaPOST( url, theData, headerProperties );
  156.  
  157.     if ( text == null || text == "" )
  158.         alert( "Yikes. No text returned." );
  159.     else
  160.         appendTextToDocument( format( text ) );
  161.  
  162. } ) ( );  // end main( )
Comments