Access OpenCalais from Node.js

To access OpenCalais for entity recognition, you’ll need an API key, and some text to check:

let inputData = `
Professor Sir Richard Evans FBA is Provost of Gresham College and 
the President of Wolfson College, Cambridge. He was Regius Professor 
of History at the University of Cambridge from 2008 until his retirement 
in September 2014. He is a world-renowned historian and academic, with 
many of his books now acknowledged as seminal works in the field of 
modern history.`;

let token = 'insert token here';

This requires the Node HTTPs library as a dependency (there are a couple unmaintained wrappers around OpenCalais for Node, but I found none of them work).

let https = require('https');

Once you do this, you just need to drop in all the parameters you need, and run a typical Node HTTP request:

const options = {
  host: "api.thomsonreuters.com",
  port: "443",
  path: "/permid/calais",
  method: "POST",
  headers: {
    "Content-Type": "text/raw",
    "OutputFormat": "application/json",
    "X-AG-Access-Token": token,
    "Content-Length": new Buffer(inputData).length
  }
};

const req = https.request(options, 
  (res) => {
    let resultData = "";
    res.on("data", (d) => {
      resultData += d;
    });

    res.on("end", () => {
      console.log(
        JSON.stringify(
          JSON.parse(resultData), 
          null, 
          2));
    });
  }
);

req.write(inputData);
req.end();

Once you do this, you’ll get a ton of detail (on first glance, this seems much more detailed than AlchemyAPI):

  },
  "http://d.opencalais.com/genericHasher-1/8ab44b93-7601-3ce8-8b45-1bc2fa919150": {
    "_typeGroup": "relations",
    "_type": "Trial",
    "forenduserdisplay": "false",
    "_typeReference": "http://s.opencalais.com/1/type/em/r/Trial",
    "person": "http://d.opencalais.com/pershash-1/9af3227d-8f35-3529-8222-cc89ff24185a",
    "instances": [
      {
        "detection": "[About Hitler: The Holocaust, History, and the ]David Irving Trial[ (2002), In Defe
nce of History (1997), Rituals of]",
        "prefix": "About Hitler: The Holocaust, History, and the ",
        "exact": "David Irving Trial",
        "suffix": " (2002), In Defence of History (1997), Rituals of",
        "offset": 1742,
        "length": 18
      }
    ]
  },

Leave a Reply

Your email address will not be published. Required fields are marked *