├── .gitignore ├── .jshintrc ├── LICENSE ├── Makefile ├── README.md ├── calais.js ├── example.coffee ├── example.js ├── lib └── calais.js ├── package.json └── test └── calais.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "camelcase" : true, 3 | "indent" : 2, 4 | "maxlen" : 120, 5 | "trailing" : true, 6 | "bitwise" : true, 7 | "curly" : true, 8 | "eqeqeq" : true, 9 | "forin" : true, 10 | "noarg" : true, 11 | "noempty" : true, 12 | "nonew" : true, 13 | "undef" : true, 14 | "unused" : true, 15 | "devel" : true, 16 | "node" : true, 17 | "sub" : true, 18 | "quotmark" : "single" 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010 Mike Cantelon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | jshint: 2 | @./node_modules/.bin/jshint lib test 3 | 4 | .PHONY: jshint 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-calais # 2 | 3 | node-calais allows semantic analysis of text using the Calais web service. 4 | 5 | ## Install ## 6 | 7 | Install using npm: 8 | $ npm install calais 9 | 10 | ## Usage ## 11 | 12 | var calais = new Calais('') 13 | calais.set('content', 'The Federal Reserve is the enemy of Ron Paul.') 14 | calais.fetch(function(err, result) { 15 | // do something with result 16 | }) 17 | 18 | ## Example ## 19 | 20 | Run quick example: 21 | $ node example.js 22 | 23 | ## Options ## 24 | 25 | In addition to using the "set" method, a hash of option settings can be passed 26 | as the second argument during intialization. For example: 27 | 28 | var calais = new Calais('', {'cleanResult': false}) 29 | 30 | By default, node-calais will return fetched results as a Javascript object. 31 | Alternatively, the Calais standard "outputFormat" input parameter can be set to 32 | standard output formats (see Calais documentation). 33 | 34 | Any standard Calais input parameter can be specified as an option, as well as 35 | the 'cleanResult' parameter which will return a simplified Javascript object if 36 | "outputFormat" hasn't been set to something other than the default ('object'). 37 | 38 | ## CLI Tool ## 39 | 40 | The library also comes with a CLI tool for sending text files to Calais for 41 | analysis. 42 | 43 | Example: 44 | 45 | calais some_key_file -k my_api_key 46 | 47 | If you use the CLI tool regularly, you can specify a default API key by setting 48 | the `api_key` property in an .ini file at $HOME/.calais. 49 | 50 | ## Test ## 51 | 52 | Run tests: 53 | $ expresso test/calais.test.js 54 | 55 | Tested with node.js v0.11.13 56 | 57 | ## Contributors ## 58 | 59 | Mike Cantelon (github: mcantelon) 60 | 61 | D. Charbonnier (github: dcharbonnier) 62 | 63 | Dirk Gorissen (github: dgorissen) 64 | 65 | Harry Guillermo (github: hguillermo) 66 | 67 | (c) 2010-2014 Mike Cantelon, MIT license 68 | -------------------------------------------------------------------------------- /calais.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /*! 4 | * calais 5 | * Copyright(c) 2011 Mike Cantelon 6 | * MIT Licensed 7 | */ 8 | 9 | var fs = require('fs') 10 | , path = require('path') 11 | , Calais = require('./lib/calais').Calais 12 | , argv = require('optimist').argv 13 | , iniparser = require('iniparser') 14 | 15 | var home = process.env.HOME 16 | 17 | // if HOME not set, die 18 | if (home === undefined) { 19 | console.log('Error: HOME environmental variable not defined.') 20 | process.exit(1) 21 | } 22 | 23 | iniparser.parse(home + '/.calais', function (err, data) { 24 | 25 | var config = (err) ? false : data 26 | 27 | // deal with command line input 28 | if (argv['_'].length == 1) { 29 | 30 | var api_key = (argv['k'] && (argv['k'] != true)) 31 | ? argv['k'] 32 | : config.api_key 33 | 34 | if (!api_key) { 35 | 36 | var help = '' 37 | help += "Please specify an OpenCalais API key using the -k option.\n" 38 | help += "A default key may be specified by setting 'api_key' in an ini\n" 39 | help += "file at $HOME/.calais." 40 | 41 | console.log(help) 42 | process.exit(1) 43 | } 44 | 45 | var file = argv['_'][0] 46 | 47 | fs.exists(file, function (exists) { 48 | 49 | if (exists) { 50 | 51 | var content = fs.readFileSync(file) 52 | 53 | var calais = new Calais(api_key, { 54 | 'cleanResult': true 55 | }) 56 | calais.set('content', content) 57 | 58 | calais.fetch(function (error, result) { 59 | if (error) { 60 | console.log('Error attempting to fetch data.'); 61 | } else { 62 | console.log(result) 63 | } 64 | }) 65 | } 66 | else { 67 | 68 | console.log("Error: file doesn't exist.") 69 | process.exit(1) 70 | } 71 | }) 72 | } 73 | else { 74 | 75 | console.log('Usage: calais -k ') 76 | process.exit(1) 77 | } 78 | }) 79 | -------------------------------------------------------------------------------- /example.coffee: -------------------------------------------------------------------------------- 1 | sys = require('sys') 2 | Calais = require ('./lib/calais').Calais 3 | 4 | calais = new Calais 'your_api_key' 5 | calais.set 'content', 'The Federal Reserve is the enemy of Ron Paul.' 6 | calais.fetch (result) -> 7 | sys.puts sys.inspect result 8 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var util = require('util'), 2 | Calais = require('./lib/calais').Calais 3 | 4 | var calais = new Calais('your_api_key') 5 | calais.set('content', 'The Federal Reserve is the enemy of Ron Paul.') 6 | calais.fetch(function (result) { 7 | util.puts(util.inspect(result)) 8 | }) 9 | -------------------------------------------------------------------------------- /lib/calais.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * calais 3 | * Copyright(c) 2011 Mike Cantelon 4 | * Modified 2013 Harry Guillermo (github: hguillermo) 5 | * MIT Licensed 6 | */ 7 | 8 | var request = require('request'); 9 | 10 | var Calais = function (apiKey, options) { 11 | this.initialize(apiKey, options); 12 | }; 13 | 14 | Calais.prototype = { 15 | 16 | initialize: function (apiKey, options) { 17 | 18 | this.apiKey = apiKey; 19 | 20 | this.defaults = { 21 | 'apiHost' : 'api.opencalais.com', 22 | 'apiPath' : '/tag/rs/enrich', 23 | 'contentType' : 'text/raw', 24 | 'outputFormat' : 'object', 25 | 'reltagBaseURL' : '', 26 | 'calculateRelevanceScore': true, 27 | 'enableMetadataType' : 'GenericRelations,SocialTags', 28 | 'docRDFaccessible' : false, 29 | 'allowDistribution' : false, 30 | 'allowSearch' : false, 31 | 'cleanResult' : true, 32 | 'proxy' : '' 33 | }; 34 | 35 | this._setDefaultOptions(options); 36 | }, 37 | 38 | _setDefaultOptions: function (options) { 39 | 40 | options = options || {}; 41 | this.options = {}; 42 | 43 | var keys = Object.keys(this.defaults); 44 | for (var i = 0, l = keys.length; i < l; i++) { 45 | var index = keys[i]; 46 | this.options[index] = (this._undefinedOrNull(options[index])) ? 47 | this.defaults[index] 48 | : options[index]; 49 | } 50 | }, 51 | 52 | _undefinedOrNull: function (value) { 53 | return value === undefined || value === null; 54 | }, 55 | 56 | set: function (key, value) { 57 | this.options[key] = value; 58 | }, 59 | 60 | validateOptions: function () { 61 | return true; 62 | }, 63 | 64 | cleanResult: function (result) { 65 | var cleanResult = []; 66 | for (var i in result) { 67 | if (i !== 'doc') { 68 | cleanResult.push(result[i]); 69 | } 70 | } 71 | return cleanResult; 72 | }, 73 | 74 | fetch: function (callback) { 75 | 76 | var calais = this; 77 | 78 | if (this.validateOptions()) { 79 | 80 | var outputFormat = (calais.options.outputFormat === 'object') ? 'application/json' 81 | : this.options.outputFormat; 82 | 83 | var params = { 84 | 'Host' : this.options.apiHost, 85 | 'x-calais-licenseID' : this.apiKey, 86 | 'Content-Type' : this.options.contentType, 87 | 'Accept' : outputFormat, 88 | 'Content-Length' : this.options.content.length, 89 | 'calculateRelevanceScore': this.options.calculateRelevanceScore, 90 | 'enableMetadataType' : this.options.enableMetadataType, 91 | 'docRDFaccessible' : this.options.docRDFaccessible, 92 | 'allowDistribution' : this.options.allowDistribution, 93 | 'allowSearch' : this.options.allowSearch 94 | }; 95 | 96 | if (!this._undefinedOrNull(this.options['externalID'])) { 97 | params.externalID = this.options['externalID']; 98 | } 99 | 100 | if (!this._undefinedOrNull(this.options['submitter'])) { 101 | params.submitter = this.options['submitter']; 102 | } 103 | 104 | var options = { 105 | uri : 'http://' + this.options.apiHost + this.options.apiPath, 106 | method : 'POST', 107 | body : this.options.content, 108 | headers: params 109 | }; 110 | 111 | //ensure the proxy is set 112 | if (this.options.proxy) { 113 | options.proxy = this.options.proxy; 114 | } 115 | 116 | request(options, function (error, response, calaisData) { 117 | if (error) { 118 | callback(error); 119 | } 120 | if (response === undefined) { 121 | return callback(new Error('Open Calais http response is undefined')); 122 | } else if (response.statusCode === 200) { 123 | // take note of whether Javascript object output was requested 124 | var jsOutput = (calais.options.outputFormat === 'object'); 125 | // parse to a Javascript object if requested 126 | var result = (jsOutput) ? JSON.parse(calaisData) 127 | : calaisData; 128 | 129 | // ignore cleanResult preference if not requesting an object 130 | result = (jsOutput && calais.options.cleanResult) ? calais.cleanResult(result) 131 | : result; 132 | return callback(null, result, calais.errors); 133 | } else { 134 | return callback(new Error('Open Calais http response error ' + response.statusCode)); 135 | } 136 | }); 137 | } 138 | } 139 | }; 140 | 141 | exports.Calais = Calais; 142 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "calais", 3 | "version": "0.3.3", 4 | "description": "Semantically analyze text using the Calais web service. Original project http://github.com/mcantelon/node-calais", 5 | "tags": [ 6 | "semantic", 7 | "calais" 8 | ], 9 | "contributors": [], 10 | "dependencies": { 11 | "optimist": ">=0.1.3", 12 | "request": ">=2.25.1", 13 | "iniparser": ">=1.0.1" 14 | }, 15 | "main": "./lib/calais", 16 | "directories": { 17 | "lib": "./lib" 18 | }, 19 | "bin": { 20 | "calais": "./calais.js" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/mcantelon/node-calais" 25 | }, 26 | "engines": { 27 | "node": ">= 0.3.0" 28 | }, 29 | "bugs": { 30 | "web": "https://github.com/mcantelon/node-calais/issues" 31 | }, 32 | "licenses": [ 33 | { 34 | "type": "MIT", 35 | "url": "https://github.com/mcantelon/node-calais/blob/master/LICENSE" 36 | } 37 | ], 38 | "devDependencies": { 39 | "jshint": "^2.5.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/calais.test.js: -------------------------------------------------------------------------------- 1 | var Calais = require('../lib/calais').Calais; 2 | var assert = require('assert'); 3 | 4 | module.exports = { 5 | 'test api key setting': function () { 6 | var calais = new Calais('some_api_key'); 7 | assert.equal(calais.apiKey, 'some_api_key'); 8 | }, 9 | 10 | 'test option setting': function () { 11 | var calais = new Calais('some_api_key', {'cleanResult': false}); 12 | assert.equal(calais.options.cleanResult, false); 13 | } 14 | }; 15 | --------------------------------------------------------------------------------