Personify.jsJavaScript 语言分析库
Personify.js 是一个 JavaScript 库,可以让你轻松访问 IBM Watson 特性并利用 twitter 的数据。IBM Watson 提供很多高级的语言分析工具,而 Twitter 是最受欢迎的基于文本的通讯平台。
使用方法:
var Personify = require('personify');
// For every service you use through Watson, IBM will provide you with a separate set of
// OAuth credentials. See below to find out where to get these credentials.
var config = {
// example credentials for Watson Machine Translation service
translateConfig : {
service_url: '...',
service_username: '...',
service_password: '...'
},
// example credentials for Watson User Modeling service
personalityConfig : {
service_url: '...',
service_username: '...',
service_password: '...'
},
// example credentials for Twitter API
twitterConfig : {
consumer_key: '...',
consumer_secret: '...',
access_token: '...',
access_token_secret: '...'
}
};
//
// Instantiate a new Personify object and pass in OAuth credentials
// inside of an object literal
//
var P = new Personify(config);
//
// Use Watson to discover personality traits, values and needs for a Twitter user
// '@' can be used before a username, but is not required (e.g. '@userName')
//
var params1 = {
screen_name: 'userName',
count: 100
};
P.userPersonify( params1 , function (data, error) {
console.log(data, error);
});
//
// Watson provides a personality assessment of the combined input of tweets in a
// user's home timeline. Includes tweets from friends and accounts the user is following,
// and their retweets
//
var params2 = {
count: 100,
exclude_tweets: true
};
P.homePersonify( params2, function (data, error) {
console.log(data, error);
});
//
// Search Twitter with a (required) keyword. Accepts all of Twitter's optional search
// parameters and a few additional ones we've created for your convenience.
//
var params3 = {
q: '#JavaScript',
geocode: 'San Francisco' //geocode takes most major US cities and all US states
}; //see this method in API for a full list of city and state shortcut terms
P.searchPersonify( params3 , function (data, error) {
console.log(data, error);
});
//
// Grab a number of Tweets in a specified language and get back both the original text and its
// translation in another destination language. Most of the search parameters available
// here are the same as those in our searchTweet method.
//
var params4 = {
q: 'JavaScript',
fromLanguage: 'ar', // Translate from Arabic
toLanguage: 'en', // to English
outputType: 'text' // Choose from text, json or XML
};
P.searchTranslate( params4 , function (data, error) {
console.log(data, error);
});
//
// Input a Twitter handle and get back their tweets translated
//
var params5 = {
screen_name: 'userName',
fromLanguage: 'en',
toLanguage: 'fr',
outputType: 'text'
};
P.userTranslate( params5, function(data, error){
console.log(data, error);
});
//
// Get tweets from your home timeline and have them translated into another language
//
var params6 = {
count: 150,
fromLanguage: 'en',
toLanguage: 'fr',
outputType: 'json'
};
P.homeTranslate( params6, function(data, error){
console.log(data, error);
});
//
// Find public tweets talkng about the Large Hadron Collider using Twitter's Streaming API and
// translate them into another language
//
var params7 = {
track: 'twitter',
fromLanguage: 'en',
toLanguage: 'fr',
outputType: 'text'
};
P.streamTranslate( params7, function(data, error){
console.log(data, error);
});评论
