Last active
August 29, 2015 14:11
-
-
Save AYLIEN/d4f70a368990c22199cf to your computer and use it in GitHub Desktop.
Basic functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//********************************************************** | |
// Here's a basic app that uses AYLIEN's Text Analysis API | |
// through the Node.js SDK. To install the SDK using `npm`: | |
// $ npm install aylien_textapi | |
//********************************************************** | |
var AYLIENTextAPI = require('aylien_textapi'); | |
var textapi = new AYLIENTextAPI({ | |
application_id: 'YourApplicationId', | |
application_key: 'YourApplicationKey' | |
}); | |
//********************************************************** | |
// Language Detection | |
// Detects the language of a text | |
//********************************************************** | |
textapi.language('What language is this sentence written in?', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Language Detection Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
console.log("Language :", resp.lang); | |
console.log("Confidence :", resp.confidence); | |
} | |
}); | |
//********************************************************** | |
// Sentiment Analysis | |
// Finds out if a text is positive, negative or neutral | |
//********************************************************** | |
textapi.sentiment('John is a very good football player!', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Sentiment Analysis Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
console.log("Subjectivity :", resp.subjectivity); | |
console.log("Subjectivity Confidence :", resp.subjectivity_confidence); | |
console.log("Polarity :", resp.polarity); | |
console.log("Polarity Confidence :", resp.polarity_confidence); | |
} | |
}); | |
//********************************************************** | |
// Article Summarization | |
// Summarizes an article into a few key sentences | |
//********************************************************** | |
textapi.summarize('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Article Summarization Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
for (var i = 0; i < resp.sentences.length; i++) { | |
console.log("Sentence ", i + 1, ": ", resp.sentences[i]); | |
} | |
} | |
}); | |
//********************************************************** | |
// Article Classification | |
// Classifies an article into about 500 categories | |
//********************************************************** | |
textapi.classify('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Article Classification Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
for (var i = 0; i < resp.categories.length; i++) { | |
console.log("Label :", resp.categories[i].label); | |
console.log("Code :", resp.categories[i].code); | |
console.log("Confidence :", resp.categories[i].confidence); | |
} | |
} | |
}); | |
//********************************************************** | |
// Entity Extraction | |
// Extracts the entities mentioned in a text | |
//********************************************************** | |
textapi.entities('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Entity Extraction Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
console.log("Organisations :", resp.entities.organization); | |
console.log("Locations :", resp.entities.location); | |
console.log("Keywords :", resp.entities.keyword); | |
console.log("Dates :", resp.entities.date); | |
console.log("People :", resp.entities.person); | |
} | |
}); | |
//********************************************************** | |
// Concept Extraction | |
// Extracts the concepts mentioned in a text | |
//********************************************************** | |
textapi.concepts('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Concept Extraction Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
var concepts = Object.keys(resp.concepts); | |
concepts.forEach(function(c) { | |
var info = resp.concepts[c]; | |
console.log("\nConcept DBPedia URI :" + c); | |
console.log("Surface Form :" + info.surfaceForms[0].string); | |
console.log("Relavance Score :" + info.surfaceForms[0].score); | |
console.log("Offset Index :" + info.surfaceForms[0].offset); | |
}); | |
} | |
}); | |
//********************************************************** | |
// Hashtag Suggestion | |
// Suggests a set of hashtags for a piece of text | |
//********************************************************** | |
textapi.hashtags('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Hashtag Suggestion Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
for (var i = 0; i < resp.hashtags.length; i++) { | |
console.log("Hashtag ", i, ": ", resp.hashtags[i]); | |
} | |
} | |
}); | |
//********************************************************** | |
// Article Extraction | |
// Scrapes an article and extracts main text, images, etc | |
//********************************************************** | |
textapi.extract('http://www.bbc.com/news/science-environment-30097648', function(err, resp) { | |
console.log("\n************************************"); | |
console.log("Article Extraction Results:"); | |
console.log("************************************\n"); | |
if (err !== null) { | |
console.log("Error: " + err); | |
} else { | |
console.log("Article Title :", resp.title); | |
console.log("Article text :", resp.article); | |
} | |
}); | |
//************************************************************ | |
// Unsupervised Classification | |
// Classify a piece of text based on a specified set of labels | |
//************************************************************ | |
var params = { | |
text: "Organic molecules detected on comet", | |
'class': ['Cosmology', 'Chemistry', 'Agriculture'] | |
}; | |
textapi.unsupervisedClassify(params, function(error, response) { | |
console.log("\n************************************"); | |
console.log("Unsupervised Classification Results:"); | |
console.log("************************************\n"); | |
if (error !== null) { | |
console.log(error, response); | |
} else { | |
console.log("\nThe text to classify is : \n\n", response.text, "\n"); | |
for (var i = 0; i < response.classes.length; i++) { | |
console.log("label - ", response.classes[i].label, ", score -", response.classes[i].score, "\n"); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment