1818
1919var extend = require ( 'extend' ) ;
2020var requestFactory = require ( '../../lib/requestwrapper' ) ;
21- var pick = require ( 'object.pick' ) ;
22- var omit = require ( 'object.omit' ) ;
23- var isStream = require ( 'isstream' ) ;
21+ var endpoints = require ( '../../lib/alchemy_endpoints.json' ) ;
22+ var helper = require ( '../../lib/helper' ) ;
23+
24+ function createRequest ( method ) {
25+ return function ( _params , callback ) {
26+ var params = _params || { } ;
27+ var accepted_formats = Object . keys ( endpoints [ method ] ) ;
28+ var format = helper . getFormat ( params , accepted_formats ) ;
29+
30+ if ( format === null ) {
31+ callback ( new Error ( 'Missing required parameters: ' +
32+ accepted_formats . join ( ', ' ) +
33+ ' needs to be specified' ) ) ;
34+ return ;
35+ }
36+
37+ var parameters = {
38+ options : {
39+ url : endpoints [ method ] [ format ] ,
40+ method : 'POST' ,
41+ json : true ,
42+ path : params ,
43+ form : extend ( { outputMode : 'json' } , params ) // change default output to json
44+ } ,
45+ defaultOptions : this . _options
46+ } ;
47+ return requestFactory ( parameters , callback ) ;
48+ } ;
49+ }
2450
2551function AlchemyLanguage ( options ) {
2652 // Default URL
2753 var serviceDefaults = {
2854 url : 'https://access.alchemyapi.com/calls' ,
2955 alchmemy : true
3056 } ;
31-
32- this . paths = {
33- text : 'text/TextGetRankedNamedEntities' ,
34- url : 'url/URLGetRankedNamedEntities' ,
35- html : 'html/HTMLGetRankedNamedEntities'
36- } ;
37-
3857 // Replace default options with user provided
3958 this . _options = extend ( serviceDefaults , options ) ;
4059}
4160
4261/**
4362 * Extracts a grouped, ranked list of named entities (people, companies,
44- * organizations, etc.) from within a text, url or html .
63+ * organizations, etc.) from text, a URL or HTML .
4564 */
46- AlchemyLanguage . prototype . entities = function ( _params , callback ) {
47- var params = _params || { } ;
48- var path = null ;
49-
50- if ( typeof ( params . text ) === 'undefined' &&
51- typeof ( params . url ) === 'undefined' &&
52- typeof ( params . html ) === 'undefined' ) {
53- callback ( new Error ( 'Missing required parameters: either text, ' +
54- 'url or html needs to be specified' ) ) ;
55- return ;
56- }
57-
58- if ( typeof ( params . text ) !== 'undefined' )
59- path = '/text/TextGetRankedNamedEntities' ;
60- else if ( typeof ( params . url ) !== 'undefined' )
61- path = '/url/URLGetRankedNamedEntities' ;
62- else
63- path = '/url/HTMLGetRankedNamedEntities' ;
64-
65- var parameters = {
66- options : {
67- url : path ,
68- method : 'GET' ,
69- json : true ,
70- path : params ,
71- qs : extend ( { outputMode : 'json' } , params ) // change default output to json
72- } ,
73- requiredParams : [ 'text' ] ,
74- defaultOptions : this . _options
75- } ;
76- return requestFactory ( parameters , callback ) ;
65+ AlchemyLanguage . prototype . entities = createRequest ( 'entities' ) ;
66+
67+ /**
68+ * Extracts the keywords from text, a URL or HTML.
69+ */
70+ AlchemyLanguage . prototype . keywords = createRequest ( 'keywords' ) ;
71+
72+ /**
73+ * Tags the concepts from text, a URL or HTML.
74+ */
75+ AlchemyLanguage . prototype . concepts = createRequest ( 'concepts' ) ;
76+
77+ /**
78+ * Calculates the sentiment for text, a URL or HTML.
79+ */
80+ AlchemyLanguage . prototype . sentiment = function ( params , callback ) {
81+ var service = ( params && params . target ) ? 'sentiment' : 'sentiment_targeted' ;
82+ return createRequest ( service ) . call ( this , params , callback ) ;
7783} ;
84+ /**
85+ * Extracts the cleaned text (removes ads, navigation, etc.) for a URL or HTML.
86+ * if raw = true, extracts the cleaned text (removes ads, navigation, etc.).
87+ */
88+ AlchemyLanguage . prototype . text = function ( params , callback ) {
89+ var service = ( params && params . raw ) ? 'text_raw' : 'text' ;
90+ return createRequest ( service ) . call ( this , params , callback ) ;
91+ } ;
92+
93+ /**
94+ * Extracts the authors from a URL or HTML.
95+ */
96+ AlchemyLanguage . prototype . authors = createRequest ( 'authors' ) ;
97+
98+ /**
99+ * Detects the language for text, a URL or HTML.
100+ */
101+ AlchemyLanguage . prototype . language = createRequest ( 'language' ) ;
102+
103+ /**
104+ * Extracts the title for a URL or HTML.
105+ */
106+ AlchemyLanguage . prototype . title = createRequest ( 'title' ) ;
107+
108+ /**
109+ * Extracts the relations for text, a URL or HTML.
110+ */
111+ AlchemyLanguage . prototype . relations = createRequest ( 'relations' ) ;
112+
113+ /**
114+ * Categorizes the text for text, a URL or HTML.
115+ */
116+ AlchemyLanguage . prototype . category = createRequest ( 'category' ) ;
117+
118+ /**
119+ * Detects the RSS/ATOM feeds for a URL or HTML.
120+ */
121+ AlchemyLanguage . prototype . feeds = createRequest ( 'category' ) ;
122+
123+ /**
124+ * Parses the microformats for a URL or HTML.
125+ */
126+ AlchemyLanguage . prototype . microformats = createRequest ( 'microformats' ) ;
127+
128+ /**
129+ * Categorized through the taxonomy call for text, HTML, or a URL.
130+ */
131+ AlchemyLanguage . prototype . taxonomy = createRequest ( 'taxonomy' ) ;
132+
133+ /**
134+ * Categorized through the taxonomy call for text, HTML, or a URL.
135+ */
136+ AlchemyLanguage . prototype . combined = createRequest ( 'combined' ) ;
78137
79138module . exports = AlchemyLanguage ;
0 commit comments