├── .gitignore ├── Gemfile ├── LICENSE ├── README.md ├── alchemyapi.rb ├── dog.jpg ├── example.rb └── tests.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | coverage 6 | InstalledFiles 7 | lib/bundler/man 8 | pkg 9 | rdoc 10 | spec/reports 11 | test/tmp 12 | test/version_tmp 13 | tmp 14 | 15 | # YARD artifacts 16 | .yardoc 17 | _yardoc 18 | doc/ 19 | 20 | api_key.txt 21 | Gemfile.lock 22 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # Gemfile for AlchemyAPI Ruby SDK 2 | source "https://rubygems.org" 3 | gem "json" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alchemyapi_ruby # 2 | 3 | A sdk for AlchemyAPI using Ruby - **_Please note that this legacy AlchemyAPI SDK is no longer supported by IBM. Please use the Watson SDKs https://github.com/watson-developer-cloud?utf8=✓&query=sdk_** 4 | 5 | 6 | ## AlchemyAPI ## 7 | 8 | AlchemyAPI offers artificial intelligence as a service. We teach computers to learn how to read and see, and apply our technology to text analysis and image recognition through a cloud-based API. Our customers use AlchemyAPI to transform their unstructured content such as blog posts, news articles, social media posts and images into much more useful structured data. 9 | 10 | AlchemyAPI is a tech startup located in downtown Denver, Colorado. As the world’s most popular text analysis service, AlchemyAPI serves over 3.5 billion monthly API requests to over 35,000 developers. To enable our services, we use artificial intelligence, machine learning, neural networks, natural language processing and massive-scale web crawling. Our technology powers use cases in a variety of industry verticals, including social media monitoring, business intelligence, content recommendations, financial trading and targeted advertising. 11 | 12 | More information at: http://www.alchemyapi.com 13 | 14 | 15 | 16 | ## API Key ## 17 | 18 | To use AlchemyAPI, you'll need to obtain an API key and attach that key to all requests. If you do not already have a key, please visit: http://www.alchemyapi.com/api/register.html 19 | 20 | 21 | 22 | ## Getting Started with the Ruby SDK ## 23 | 24 | To get started and run the example, simply: 25 | 26 | git clone https://github.com/AlchemyAPI/alchemyapi_ruby.git 27 | cd alchemyapi_ruby 28 | bundle install 29 | ruby alchemyapi.rb YOUR_KEY_HERE 30 | ruby example.rb 31 | 32 | 33 | Just replace YOUR_KEY_HERE with your key, and you should be good to go. 34 | 35 | Note: If you don't have bundle installed, simply use: gem install bundle (you may need to sudo) 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /alchemyapi.rb: -------------------------------------------------------------------------------- 1 | 2 | # Copyright 2013 AlchemyAPI 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | 17 | require 'rubygems' 18 | require 'net/http' 19 | require 'uri' 20 | require 'json' 21 | 22 | class AlchemyAPI 23 | 24 | #Setup the endpoints 25 | @@ENDPOINTS = {} 26 | @@ENDPOINTS['sentiment'] = {} 27 | @@ENDPOINTS['sentiment']['url'] = '/url/URLGetTextSentiment' 28 | @@ENDPOINTS['sentiment']['text'] = '/text/TextGetTextSentiment' 29 | @@ENDPOINTS['sentiment']['html'] = '/html/HTMLGetTextSentiment' 30 | @@ENDPOINTS['sentiment_targeted'] = {} 31 | @@ENDPOINTS['sentiment_targeted']['url'] = '/url/URLGetTargetedSentiment' 32 | @@ENDPOINTS['sentiment_targeted']['text'] = '/text/TextGetTargetedSentiment' 33 | @@ENDPOINTS['sentiment_targeted']['html'] = '/html/HTMLGetTargetedSentiment' 34 | @@ENDPOINTS['author'] = {} 35 | @@ENDPOINTS['author']['url'] = '/url/URLGetAuthor' 36 | @@ENDPOINTS['author']['html'] = '/html/HTMLGetAuthor' 37 | @@ENDPOINTS['keywords'] = {} 38 | @@ENDPOINTS['keywords']['url'] = '/url/URLGetRankedKeywords' 39 | @@ENDPOINTS['keywords']['text'] = '/text/TextGetRankedKeywords' 40 | @@ENDPOINTS['keywords']['html'] = '/html/HTMLGetRankedKeywords' 41 | @@ENDPOINTS['concepts'] = {} 42 | @@ENDPOINTS['concepts']['url'] = '/url/URLGetRankedConcepts' 43 | @@ENDPOINTS['concepts']['text'] = '/text/TextGetRankedConcepts' 44 | @@ENDPOINTS['concepts']['html'] = '/html/HTMLGetRankedConcepts' 45 | @@ENDPOINTS['entities'] = {} 46 | @@ENDPOINTS['entities']['url'] = '/url/URLGetRankedNamedEntities' 47 | @@ENDPOINTS['entities']['text'] = '/text/TextGetRankedNamedEntities' 48 | @@ENDPOINTS['entities']['html'] = '/html/HTMLGetRankedNamedEntities' 49 | @@ENDPOINTS['category'] = {} 50 | @@ENDPOINTS['category']['url'] = '/url/URLGetCategory' 51 | @@ENDPOINTS['category']['text'] = '/text/TextGetCategory' 52 | @@ENDPOINTS['category']['html'] = '/html/HTMLGetCategory' 53 | @@ENDPOINTS['relations'] = {} 54 | @@ENDPOINTS['relations']['url'] = '/url/URLGetRelations' 55 | @@ENDPOINTS['relations']['text'] = '/text/TextGetRelations' 56 | @@ENDPOINTS['relations']['html'] = '/html/HTMLGetRelations' 57 | @@ENDPOINTS['language'] = {} 58 | @@ENDPOINTS['language']['url'] = '/url/URLGetLanguage' 59 | @@ENDPOINTS['language']['text'] = '/text/TextGetLanguage' 60 | @@ENDPOINTS['language']['html'] = '/html/HTMLGetLanguage' 61 | @@ENDPOINTS['text'] = {} 62 | @@ENDPOINTS['text']['url'] = '/url/URLGetText' 63 | @@ENDPOINTS['text']['html'] = '/html/HTMLGetText' 64 | @@ENDPOINTS['text_raw'] = {} 65 | @@ENDPOINTS['text_raw']['url'] = '/url/URLGetRawText' 66 | @@ENDPOINTS['text_raw']['html'] = '/html/HTMLGetRawText' 67 | @@ENDPOINTS['title'] = {} 68 | @@ENDPOINTS['title']['url'] = '/url/URLGetTitle' 69 | @@ENDPOINTS['title']['html'] = '/html/HTMLGetTitle' 70 | @@ENDPOINTS['feeds'] = {} 71 | @@ENDPOINTS['feeds']['url'] = '/url/URLGetFeedLinks' 72 | @@ENDPOINTS['feeds']['html'] = '/html/HTMLGetFeedLinks' 73 | @@ENDPOINTS['microformats'] = {} 74 | @@ENDPOINTS['microformats']['url'] = '/url/URLGetMicroformatData' 75 | @@ENDPOINTS['microformats']['html'] = '/html/HTMLGetMicroformatData' 76 | @@ENDPOINTS['taxonomy'] = {} 77 | @@ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy' 78 | @@ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy' 79 | @@ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy' 80 | @@ENDPOINTS['combined'] = {} 81 | @@ENDPOINTS['combined']['url'] = '/url/URLGetCombinedData' 82 | @@ENDPOINTS['combined']['text'] = '/text/TextGetCombinedData' 83 | @@ENDPOINTS['image_extract'] = {} 84 | @@ENDPOINTS['image_extract']['url'] = '/url/URLGetImage' 85 | @@ENDPOINTS['image_tag'] = {} 86 | @@ENDPOINTS['image_tag']['url'] = '/url/URLGetRankedImageKeywords' 87 | @@ENDPOINTS['image_tag']['image'] = '/image/ImageGetRankedImageKeywords' 88 | 89 | @@BASE_URL = 'http://access.alchemyapi.com/calls' 90 | 91 | 92 | def initialize() 93 | 94 | begin 95 | key = File.read('api_key.txt') 96 | key.strip! 97 | 98 | if key.empty? 99 | #The key file should't be blank 100 | puts 'The api_key.txt file appears to be blank, please copy/paste your API key in the file: api_key.txt' 101 | puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html' 102 | Process.exit(1) 103 | end 104 | 105 | if key.length != 40 106 | #Keys should be exactly 40 characters long 107 | puts 'It appears that the key in api_key.txt is invalid. Please make sure the file only includes the API key, and it is the correct one.' 108 | Process.exit(1) 109 | end 110 | 111 | @apiKey = key 112 | rescue => err 113 | #The file doesn't exist, so show the message and create the file. 114 | puts 'API Key not found! Please copy/paste your API key into the file: api_key.txt' 115 | puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html' 116 | 117 | #create a blank file to hold the key 118 | File.open("api_key.txt", "w") {} 119 | Process.exit(1) 120 | end 121 | end 122 | 123 | 124 | 125 | 126 | 127 | # Calculates the sentiment for text, a URL or HTML. 128 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ 129 | # For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ 130 | # 131 | # INPUT: 132 | # flavor -> which version of the call, i.e. text, url or html. 133 | # data -> the data to analyze, either the text, the url or html code. 134 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 135 | # 136 | # Available Options: 137 | # showSourceText -> 0: disabled (default), 1: enabled 138 | # 139 | # OUTPUT: 140 | # The response, already converted from JSON to a Ruby object. 141 | # 142 | def sentiment(flavor, data, options = {}) 143 | unless @@ENDPOINTS['sentiment'].key?(flavor) 144 | return { 'status'=>'ERROR', 'statusInfo'=>'sentiment analysis for ' + flavor + ' not available' } 145 | end 146 | 147 | #Add the URL encoded data to the options and analyze 148 | options[flavor] = data 149 | return analyze(@@ENDPOINTS['sentiment'][flavor], options) 150 | end 151 | 152 | 153 | # Calculates the targeted sentiment for text, a URL or HTML. 154 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ 155 | # For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ 156 | # 157 | # INPUT: 158 | # flavor -> which version of the call, i.e. text, url or html. 159 | # data -> the data to analyze, either the text, the url or html code. 160 | # target -> the word or phrase to run sentiment analysis on. 161 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 162 | # 163 | # Available Options: 164 | # showSourceText -> 0: disabled, 1: enabled 165 | # 166 | # OUTPUT: 167 | # The response, already converted from JSON to a Ruby object. 168 | # 169 | def sentiment_targeted(flavor, data, target, options = {}) 170 | if target == '' || target == nil 171 | return { 'status'=>'ERROR', 'statusMessage'=>'targeted sentiment requires a non-null target' } 172 | end 173 | 174 | unless @@ENDPOINTS['sentiment_targeted'].key?(flavor) 175 | return { 'status'=>'ERROR', 'statusInfo'=>'targeted sentiment analysis for ' + flavor + ' not available' } 176 | end 177 | 178 | #Add the URL encoded data and the target to the options and analyze 179 | options[flavor] = data 180 | options['target'] = target 181 | return analyze(@@ENDPOINTS['sentiment_targeted'][flavor], options) 182 | end 183 | 184 | 185 | # Extracts the entities for text, a URL or HTML. 186 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/ 187 | # For the docs, please refer to: http://www.alchemyapi.com/api/entity-extraction/ 188 | # 189 | # INPUT: 190 | # flavor -> which version of the call, i.e. text, url or html. 191 | # data -> the data to analyze, either the text, the url or html code. 192 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 193 | # 194 | # Available Options: 195 | # disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 196 | # linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default) 197 | # coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default) 198 | # quotations -> extract quotations by entities. 0: disabled (default), 1: enabled. 199 | # sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled. 200 | # showSourceText -> 0: disabled (default), 1: enabled 201 | # maxRetrieve -> the maximum number of entities to retrieve (default: 50) 202 | # 203 | # OUTPUT: 204 | # The response, already converted from JSON to a Ruby object. 205 | # 206 | def entities(flavor, data, options = {}) 207 | unless @@ENDPOINTS['entities'].key?(flavor) 208 | return { 'status'=>'ERROR', 'statusInfo'=>'entity extraction for ' + flavor + ' not available' } 209 | end 210 | 211 | #Add the URL encoded data to the options and analyze 212 | options[flavor] = data 213 | return analyze(@@ENDPOINTS['entities'][flavor], options) 214 | end 215 | 216 | 217 | # Extracts the author from a URL or HTML. 218 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/author-extraction/ 219 | # For the docs, please refer to: http://www.alchemyapi.com/api/author-extraction/ 220 | # 221 | # INPUT: 222 | # flavor -> which version of the call, i.e. text, url or html. 223 | # data -> the data to analyze, either the text, the url or html code. 224 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 225 | # 226 | # Available Options: 227 | # none 228 | # 229 | # OUTPUT: 230 | # The response, already converted from JSON to a Ruby object. 231 | # 232 | def author(flavor, data, options = {}) 233 | unless @@ENDPOINTS['author'].key?(flavor) 234 | return { 'status'=>'ERROR', 'statusInfo'=>'author extraction for ' + flavor + ' not available' } 235 | end 236 | 237 | #Add the URL encoded data to the options and analyze 238 | options[flavor] = data 239 | return analyze(@@ENDPOINTS['author'][flavor], options) 240 | end 241 | 242 | 243 | # Extracts the keywords from text, a URL or HTML. 244 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/keyword-extraction/ 245 | # For the docs, please refer to: http://www.alchemyapi.com/api/keyword-extraction/ 246 | # 247 | # INPUT: 248 | # flavor -> which version of the call, i.e. text, url or html. 249 | # data -> the data to analyze, either the text, the url or html code. 250 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 251 | # 252 | # Available Options: 253 | # keywordExtractMode -> normal (default), strict 254 | # sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled. 255 | # showSourceText -> 0: disabled (default), 1: enabled. 256 | # maxRetrieve -> the max number of keywords returned (default: 50) 257 | # 258 | # OUTPUT: 259 | # The response, already converted from JSON to a Ruby object. 260 | # 261 | def keywords(flavor, data, options = {}) 262 | unless @@ENDPOINTS['keywords'].key?(flavor) 263 | return { 'status'=>'ERROR', 'statusInfo'=>'keyword extraction for ' + flavor + ' not available' } 264 | end 265 | 266 | #Add the URL encoded data to the options and analyze 267 | options[flavor] = data 268 | return analyze(@@ENDPOINTS['keywords'][flavor], options) 269 | end 270 | 271 | 272 | # Tags the concepts for text, a URL or HTML. 273 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/concept-tagging/ 274 | # For the docs, please refer to: http://www.alchemyapi.com/api/concept-tagging/ 275 | # 276 | # Available Options: 277 | # maxRetrieve -> the maximum number of concepts to retrieve (default: 8) 278 | # linkedData -> include linked data, 0: disabled, 1: enabled (default) 279 | # showSourceText -> 0:disabled (default), 1: enabled 280 | # 281 | # OUTPUT: 282 | # The response, already converted from JSON to a Ruby object. 283 | # 284 | def concepts(flavor, data, options = {}) 285 | unless @@ENDPOINTS['concepts'].key?(flavor) 286 | return { 'status'=>'ERROR', 'statusInfo'=>'concept tagging for ' + flavor + ' not available' } 287 | end 288 | 289 | #Add the URL encoded data to the options and analyze 290 | options[flavor] = data 291 | return analyze(@@ENDPOINTS['concepts'][flavor], options) 292 | end 293 | 294 | 295 | # Categorizes the text for text, a URL or HTML. 296 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/ 297 | # For the docs, please refer to: http://www.alchemyapi.com/api/text-categorization/ 298 | # 299 | # INPUT: 300 | # flavor -> which version of the call, i.e. text, url or html. 301 | # data -> the data to analyze, either the text, the url or html code. 302 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 303 | # 304 | # Available Options: 305 | # showSourceText -> 0: disabled (default), 1: enabled 306 | # 307 | # OUTPUT: 308 | # The response, already converted from JSON to a Ruby object. 309 | # 310 | def category(flavor, data, options = {}) 311 | unless @@ENDPOINTS['category'].key?(flavor) 312 | return { 'status'=>'ERROR', 'statusInfo'=>'text categorization for ' + flavor + ' not available' } 313 | end 314 | 315 | #Add the URL encoded data to the options and analyze 316 | options[flavor] = data 317 | return analyze(@@ENDPOINTS['category'][flavor], options) 318 | end 319 | 320 | 321 | # Extracts the relations for text, a URL or HTML. 322 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/relation-extraction/ 323 | # For the docs, please refer to: http://www.alchemyapi.com/api/relation-extraction/ 324 | # 325 | # INPUT: 326 | # flavor -> which version of the call, i.e. text, url or html. 327 | # data -> the data to analyze, either the text, the url or html code. 328 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 329 | # 330 | # Available Options: 331 | # sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 332 | # keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 333 | # entities -> extract entities from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 334 | # requireEntities -> only extract relations that have entities. 0: disabled (default), 1: enabled. 335 | # sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default) 336 | # disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 337 | # linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default). 338 | # coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default) 339 | # showSourceText -> 0: disabled (default), 1: enabled. 340 | # maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100) 341 | # 342 | # OUTPUT: 343 | # The response, already converted from JSON to a Ruby object. 344 | # 345 | def relations(flavor, data, options = {}) 346 | unless @@ENDPOINTS['relations'].key?(flavor) 347 | return { 'status'=>'ERROR', 'statusInfo'=>'relation extraction for ' + flavor + ' not available' } 348 | end 349 | 350 | #Add the URL encoded data to the options and analyze 351 | options[flavor] = data 352 | return analyze(@@ENDPOINTS['relations'][flavor], options) 353 | end 354 | 355 | 356 | # Detects the language for text, a URL or HTML. 357 | # For an overview, please refer to: http://www.alchemyapi.com/api/language-detection/ 358 | # For the docs, please refer to: http://www.alchemyapi.com/products/features/language-detection/ 359 | # 360 | # INPUT: 361 | # flavor -> which version of the call, i.e. text, url or html. 362 | # data -> the data to analyze, either the text, the url or html code. 363 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 364 | # 365 | # Available Options: 366 | # none 367 | # 368 | # OUTPUT: 369 | # The response, already converted from JSON to a Ruby object. 370 | # 371 | def language(flavor, data, options = {}) 372 | unless @@ENDPOINTS['language'].key?(flavor) 373 | return { 'status'=>'ERROR', 'statusInfo'=>'language detection for ' + flavor + ' not available' } 374 | end 375 | 376 | #Add the URL encoded data to the options and analyze 377 | options[flavor] = data 378 | return analyze(@@ENDPOINTS['language'][flavor], options) 379 | end 380 | 381 | 382 | # Extracts the cleaned text (removes ads, navigation, etc.) for text, a URL or HTML. 383 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 384 | # For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 385 | # 386 | # INPUT: 387 | # flavor -> which version of the call, i.e. text, url or html. 388 | # data -> the data to analyze, either the text, the url or html code. 389 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 390 | # 391 | # Available Options: 392 | # useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default) 393 | # extractLinks -> include links, 0: disabled (default), 1: enabled. 394 | # 395 | # OUTPUT: 396 | # The response, already converted from JSON to a Ruby object. 397 | # 398 | def text(flavor, data, options = {}) 399 | unless @@ENDPOINTS['text'].key?(flavor) 400 | return { 'status'=>'ERROR', 'statusInfo'=>'clean text extraction for ' + flavor + ' not available' } 401 | end 402 | 403 | #Add the URL encoded data to the options and analyze 404 | options[flavor] = data 405 | return analyze(@@ENDPOINTS['text'][flavor], options) 406 | end 407 | 408 | 409 | # Extracts the raw text (includes ads, navigation, etc.) for a URL or HTML. 410 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 411 | # For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 412 | # 413 | # INPUT: 414 | # flavor -> which version of the call, i.e. text, url or html. 415 | # data -> the data to analyze, either the text, the url or html code. 416 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 417 | # 418 | # Available Options: 419 | # none 420 | # 421 | # OUTPUT: 422 | # The response, already converted from JSON to a Ruby object. 423 | # 424 | def text_raw(flavor, data, options = {}) 425 | unless @@ENDPOINTS['text_raw'].key?(flavor) 426 | return { 'status'=>'ERROR', 'statusInfo'=>'raw text extraction for ' + flavor + ' not available' } 427 | end 428 | 429 | #Add the URL encoded data to the options and analyze 430 | options[flavor] = data 431 | return analyze(@@ENDPOINTS['text_raw'][flavor], options) 432 | end 433 | 434 | 435 | # Extracts the title for a URL or HTML. 436 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 437 | # For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 438 | # 439 | # INPUT: 440 | # flavor -> which version of the call, i.e. text, url or html. 441 | # data -> the data to analyze, either the text, the url or html code. 442 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 443 | # 444 | # Available Options: 445 | # useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default) 446 | 447 | # OUTPUT: 448 | # The response, already converted from JSON to a Ruby object. 449 | # 450 | def title(flavor, data, options = {}) 451 | unless @@ENDPOINTS['title'].key?(flavor) 452 | return { 'status'=>'ERROR', 'statusInfo'=>'title extraction for ' + flavor + ' not available' } 453 | end 454 | 455 | #Add the URL encoded data to the options and analyze 456 | options[flavor] = data 457 | return analyze(@@ENDPOINTS['title'][flavor], options) 458 | end 459 | 460 | 461 | # Parses the microformats for a URL or HTML. 462 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/microformats-parsing/ 463 | # For the docs, please refer to: http://www.alchemyapi.com/api/microformats-parsing/ 464 | # 465 | # INPUT: 466 | # flavor -> which version of the call, i.e. url or html. 467 | # data -> the data to analyze, either the the url or html code. 468 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 469 | # 470 | # Available Options: 471 | # none 472 | # 473 | # OUTPUT: 474 | # The response, already converted from JSON to a Ruby object. 475 | # 476 | def microformats(flavor, data, options = {}) 477 | unless @@ENDPOINTS['microformats'].key?(flavor) 478 | return { 'status'=>'ERROR', 'statusInfo'=>'microformats parsing for ' + flavor + ' not available' } 479 | end 480 | 481 | #Add the URL encoded data to the options and analyze 482 | options[flavor] = data 483 | return analyze(@@ENDPOINTS['microformats'][flavor], options) 484 | end 485 | 486 | 487 | # Detects the RSS/ATOM feeds for a URL or HTML. 488 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/feed-detection/ 489 | # For the docs, please refer to: http://www.alchemyapi.com/api/feed-detection/ 490 | # 491 | # INPUT: 492 | # flavor -> which version of the call, i.e. url or html. 493 | # data -> the data to analyze, either the the url or html code. 494 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 495 | # 496 | # Available Options: 497 | # none 498 | # 499 | # OUTPUT: 500 | # The response, already converted from JSON to a Ruby object. 501 | # 502 | def feeds(flavor, data, options = {}) 503 | unless @@ENDPOINTS['feeds'].key?(flavor) 504 | return { 'status'=>'ERROR', 'statusInfo'=>'feed detection for ' + flavor + ' not available' } 505 | end 506 | 507 | #Add the URL encoded data to the options and analyze 508 | options[flavor] = data 509 | return analyze(@@ENDPOINTS['feeds'][flavor], options) 510 | end 511 | 512 | 513 | # Categorizes the text for a URL, text or HTML. 514 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/ 515 | # For the docs, please refer to: http://www.alchemyapi.com/api/taxonomy/ 516 | # 517 | # INPUT: 518 | # flavor -> which version of the call, i.e. url, text or html. 519 | # data -> the data to analyze, either the the url, text or html code. 520 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 521 | # 522 | # Available Options: 523 | # showSourceText -> 0: disabled (default), 1: enabled. 524 | # 525 | # OUTPUT: 526 | # The response, already converted from JSON to a Ruby object. 527 | # 528 | def taxonomy(flavor, data, options = {}) 529 | unless @@ENDPOINTS['taxonomy'].key?(flavor) 530 | return { 'status'=>'ERROR', 'statusInfo'=>'Taxonomy info for ' + flavor + ' not available' } 531 | end 532 | 533 | #Add the URL encoded data to the options and analyze 534 | options[flavor] = data 535 | return analyze(@@ENDPOINTS['taxonomy'][flavor], options) 536 | end 537 | 538 | 539 | # Combined call (see options below for available extractions) for a URL or text. 540 | # 541 | # INPUT: 542 | # flavor -> which version of the call, i.e. url or text. 543 | # data -> the data to analyze, either the the url or text. 544 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 545 | # 546 | # Available Options: 547 | # extract -> VALUE,VALUE,VALUE,... (possible VALUEs: page-image,entity,keyword,title,author,taxonomy,concept,relation,doc-sentiment) 548 | # extractMode -> (only applies when 'page-image' VALUE passed to 'extract' option) 549 | # trust-metadata: less CPU-intensive, less accurate 550 | # always-infer: more CPU-intensive, more accurate 551 | # disambiguate -> whether to disambiguate detected entities, 0: disabled, 1: enabled (default) 552 | # linkedData -> whether to include Linked Data content links with disambiguated entities, 0: disabled, 1: enabled (default). disambiguate must be enabled to use this. 553 | # coreference -> whether to he/she/etc coreferences into detected entities, 0: disabled, 1: enabled (default) 554 | # quotations -> whether to enable quotations extraction, 0: disabled (default), 1: enabled 555 | # sentiment -> whether to enable entity-level sentiment analysis, 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 556 | # showSourceText -> 0: disabled (default), 1: enabled. 557 | # maxRetrieve -> maximum number of named entities to extract (default: 50) 558 | # 559 | # OUTPUT: 560 | # The response, already converted from JSON to a Ruby object. 561 | # 562 | def combined(flavor, data, options = {}) 563 | unless @@ENDPOINTS['combined'].key?(flavor) 564 | return { 'status'=>'ERROR', 'statusInfo'=>'Combined data for ' + flavor + ' not available' } 565 | end 566 | 567 | #Add the URL encoded data to the options and analyze 568 | options[flavor] = data 569 | return analyze(@@ENDPOINTS['combined'][flavor], options) 570 | end 571 | 572 | 573 | # Extract image from a URL. 574 | # 575 | # INPUT: 576 | # flavor -> which version of the call, i.e. url. 577 | # data -> the data to analyze, i.e. the url. 578 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 579 | # 580 | # Available Options: 581 | # extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate 582 | # 583 | # OUTPUT: 584 | # The response, already converted from JSON to a Ruby object. 585 | # 586 | def image_extract(flavor, data, options = {}) 587 | unless @@ENDPOINTS['image_extract'].key?(flavor) 588 | return { 'status'=>'ERROR', 'statusInfo'=>'Image for ' + flavor + ' not available' } 589 | end 590 | 591 | #Add the URL encoded data to the options and analyze 592 | options[flavor] = data 593 | return analyze(@@ENDPOINTS['image_extract'][flavor], options) 594 | end 595 | 596 | 597 | # Tag image from a URL or raw image data. 598 | # For an overview, please refer to: http://www.alchemyapi.com/products/features/image-tagging/ 599 | # For the docs, please refer to: http://www.alchemyapi.com/api/image-tagging/ 600 | # 601 | # INPUT: 602 | # flavor -> which version of the call, i.e. url or image. 603 | # data -> the data to analyze, the url 604 | # options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 605 | # 606 | # Available Options: 607 | # extractMode -> trust-metadata: less CPU-intensive and less accurate, always-infer: more CPU-intensive and more accurate 608 | # (image flavor only) 609 | # imagePostMode -> how you will post the image 610 | # raw: pass an unencoded image file using POST 611 | # 612 | # OUTPUT: 613 | # The response, already converted from JSON to a Ruby object. 614 | # 615 | def image_tag(flavor, data, options = {}, image = '') 616 | unless @@ENDPOINTS['image_tag'].key?(flavor) 617 | return { 'status'=>'ERROR', 'statusInfo'=>'Image tagging for ' + flavor + ' not available' } 618 | end 619 | 620 | #Add the URL encoded data to the options and analyze 621 | unless data.empty? 622 | options[flavor] = data 623 | end 624 | return analyze_image(@@ENDPOINTS['image_tag'][flavor], options, image) 625 | end 626 | 627 | 628 | private 629 | 630 | # HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface. 631 | # It makes the call, then converts the returned JSON string into a Ruby object. 632 | # 633 | # INPUT: 634 | # url -> the full URI encoded url 635 | # 636 | # OUTPUT: 637 | # The response, already converted from JSON to a Ruby object. 638 | # 639 | def analyze(url, options) 640 | 641 | #Insert the base URL 642 | url = @@BASE_URL + url 643 | 644 | #Add the API key and set the output mode to JSON 645 | options['apikey'] = @apiKey 646 | options['outputMode'] = 'json' 647 | 648 | uri = URI.parse(url) 649 | request = Net::HTTP::Post.new(uri.request_uri) 650 | request.set_form_data(options) 651 | 652 | # disable gzip encoding which blows up in Zlib due to Ruby 2.0 bug 653 | # otherwise you'll get Zlib::BufError: buffer error 654 | request['Accept-Encoding'] = 'identity' 655 | 656 | #Fire off the HTTP request 657 | res = Net::HTTP.start(uri.host, uri.port) do |http| 658 | http.request(request) 659 | end 660 | 661 | #parse and return the response 662 | return JSON.parse(res.body) 663 | end 664 | 665 | # HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface. 666 | # It makes the call, then converts the returned JSON string into a Ruby object. 667 | # 668 | # INPUT: 669 | # url -> the full URI encoded url 670 | # body -> the raw binary image data 671 | # 672 | # OUTPUT: 673 | # The response, already converted from JSON to a Ruby object. 674 | # 675 | def analyze_image(url, options, body) 676 | 677 | #Insert the base URL 678 | url = @@BASE_URL + url 679 | 680 | #Add the API key and set the output mode to JSON 681 | options['apikey'] = @apiKey 682 | options['outputMode'] = 'json' 683 | 684 | url += '?' 685 | options.each { |h,v| 686 | url += h + '=' + v + '&' 687 | } 688 | 689 | #Parse URL 690 | uri = URI.parse(url) 691 | 692 | request = Net::HTTP::Post.new(uri.request_uri) 693 | request.body = body.to_s 694 | 695 | # disable gzip encoding which blows up in Zlib due to Ruby 2.0 bug 696 | # otherwise you'll get Zlib::BufError: buffer error 697 | request['Accept-Encoding'] = 'identity' 698 | 699 | #Fire off the HTTP request 700 | res = Net::HTTP.start(uri.host, uri.port) do |http| 701 | http.request(request) 702 | end 703 | 704 | #parse and return the response 705 | return JSON.parse(res.body) 706 | end 707 | end 708 | 709 | 710 | 711 | # Writes the API key to api_key.txt file. It will create the file if it doesn't exist. 712 | # This function is intended to be called from the Python command line using: python -c 'import alchemyapi;alchemyapi.setkey("API_KEY");' 713 | # If you don't have an API key yet, register for one at: http://www.alchemyapi.com/api/register.html 714 | # 715 | # INPUT: 716 | # key -> Your API key from Should be 40 hex characters 717 | # 718 | # OUTPUT: 719 | # none 720 | # 721 | 722 | if __FILE__==$0 723 | # this will only run if the script was the main, not load'd or require'd 724 | if ARGV.length == 1 725 | if (ARGV[0].length == 40) 726 | puts 'Key: ' + ARGV[0] + ' was written to api_key.txt' 727 | puts 'You are now ready to start using AlchemyAPI. For an example, run: ruby example.rb' 728 | File.open('api_key.txt','w') {|f| f.write(ARGV[0]) } 729 | else 730 | puts 'The key appears to invalid. Please make sure to use the 40 character key assigned by AlchemyAPI' 731 | end 732 | end 733 | 734 | end 735 | 736 | 737 | -------------------------------------------------------------------------------- /dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlchemyAPI/alchemyapi_ruby/0de3a5a0417aec4a7480ee541887b7cafbaefe55/dog.jpg -------------------------------------------------------------------------------- /example.rb: -------------------------------------------------------------------------------- 1 | 2 | # Copyright 2013 AlchemyAPI 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | 17 | require './alchemyapi' 18 | 19 | demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.' 20 | demo_url= 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen' 21 | demo_html = 'Python Demo | alchemyapi

Did you know that alchemyapi works on HTML?

Well, you do now.

' 22 | 23 | alchemyapi = AlchemyAPI.new() 24 | 25 | puts '' 26 | puts '' 27 | puts '############################################' 28 | puts '# Entity Extraction Example #' 29 | puts '############################################' 30 | puts '' 31 | puts '' 32 | 33 | puts 'Processing text: ' + demo_text 34 | puts '' 35 | 36 | response = alchemyapi.entities('text', demo_text, { 'sentiment'=>1 }) 37 | 38 | if response['status'] == 'OK' 39 | puts '## Response Object ##' 40 | puts JSON.pretty_generate(response) 41 | 42 | 43 | puts '' 44 | puts '## Entities ##' 45 | for entity in response['entities'] 46 | puts 'text: ' + entity['text'] 47 | puts 'type: ' + entity['type'] 48 | puts 'relevance: ' + entity['relevance'] 49 | print 'sentiment: ' + entity['sentiment']['type'] 50 | 51 | #Make sure score exists (it's not returned for neutral sentiment 52 | if entity['sentiment'].key?('score') 53 | print ' (' + entity['sentiment']['score'] + ')' 54 | end 55 | 56 | puts '' 57 | end 58 | else 59 | puts 'Error in entity extraction call: ' + response['statusInfo'] 60 | end 61 | 62 | 63 | puts '' 64 | puts '' 65 | puts '' 66 | puts '############################################' 67 | puts '# Keyword Extration Example #' 68 | puts '############################################' 69 | puts '' 70 | puts '' 71 | 72 | puts 'Processing text: ' + demo_text 73 | puts '' 74 | 75 | response = alchemyapi.keywords('text', demo_text, { 'sentiment'=>1 }) 76 | 77 | if response['status'] == 'OK' 78 | puts '## Response Object ##' 79 | puts JSON.pretty_generate(response) 80 | 81 | 82 | puts '' 83 | puts '## Keywords ##' 84 | for keyword in response['keywords'] 85 | puts 'text: ' + keyword['text'] 86 | puts 'relevance: ' + keyword['relevance'] 87 | print 'sentiment: ' + keyword['sentiment']['type'] 88 | 89 | 90 | #Make sure score exists (it's not returned for neutral sentiment 91 | if keyword['sentiment'].key?('score') 92 | print ' (' + keyword['sentiment']['score'] + ')' 93 | end 94 | 95 | puts '' 96 | end 97 | else 98 | puts 'Error in keyword extraction call: ' + response['statusInfo'] 99 | end 100 | 101 | 102 | puts '' 103 | puts '' 104 | puts '' 105 | puts '############################################' 106 | puts '# Concept Tagging Example #' 107 | puts '############################################' 108 | puts '' 109 | puts '' 110 | 111 | puts 'Processing text: ' + demo_text 112 | puts '' 113 | 114 | response = alchemyapi.concepts('text', demo_text) 115 | 116 | if response['status'] == 'OK' 117 | puts '## Response Object ##' 118 | puts JSON.pretty_generate(response) 119 | 120 | 121 | puts '' 122 | puts '## Concepts ##' 123 | for concept in response['concepts'] 124 | puts 'text: ' + concept['text'] 125 | puts 'relevance: ' + concept['relevance'] 126 | puts '' 127 | end 128 | else 129 | puts 'Error in concept tagging call: ' + response['statusInfo'] 130 | end 131 | 132 | 133 | puts '' 134 | puts '' 135 | puts '' 136 | puts '############################################' 137 | puts '# Sentiment Analysis Example #' 138 | puts '############################################' 139 | puts '' 140 | puts '' 141 | 142 | puts 'Processing html: ' + demo_html 143 | puts '' 144 | 145 | response = alchemyapi.sentiment('html', demo_html) 146 | 147 | if response['status'] == 'OK' 148 | puts '## Response Object ##' 149 | puts JSON.pretty_generate(response) 150 | 151 | 152 | puts '' 153 | puts '## Document Sentiment ##' 154 | puts 'type: ' + response['docSentiment']['type'] 155 | 156 | #Make sure score exists (it's not returned for neutral sentiment 157 | if response['docSentiment'].key?('score') 158 | puts 'score: ' + response['docSentiment']['score'] 159 | end 160 | else 161 | puts 'Error in sentiment analysis call: ' + response['statusInfo'] 162 | end 163 | 164 | 165 | puts '' 166 | puts '' 167 | puts '' 168 | puts '############################################' 169 | puts '# Targeted Sentiment Analysis Example #' 170 | puts '############################################' 171 | puts '' 172 | puts '' 173 | 174 | puts 'Processing text: ' + demo_text 175 | puts '' 176 | 177 | response = alchemyapi.sentiment_targeted('text', demo_text, 'Denver') 178 | 179 | if response['status'] == 'OK' 180 | puts '## Response Object ##' 181 | puts JSON.pretty_generate(response) 182 | 183 | 184 | puts '' 185 | puts '## Targeted Sentiment ##' 186 | puts 'type: ' + response['docSentiment']['type'] 187 | 188 | #Make sure score exists (it's not returned for neutral sentiment 189 | if response['docSentiment'].key?('score') 190 | puts 'score: ' + response['docSentiment']['score'] 191 | end 192 | 193 | else 194 | puts 'Error in targeted sentiment analysis call: ' + response['statusInfo'] 195 | end 196 | 197 | 198 | puts '' 199 | puts '' 200 | puts '' 201 | puts '############################################' 202 | puts '# Text Extraction Example #' 203 | puts '############################################' 204 | puts '' 205 | puts '' 206 | 207 | puts 'Processing url: ' + demo_url 208 | puts '' 209 | 210 | response = alchemyapi.text('url', demo_url) 211 | 212 | if response['status'] == 'OK' 213 | puts '## Response Object ##' 214 | puts JSON.pretty_generate(response) 215 | 216 | 217 | puts '' 218 | puts '## Extracted Text ##' 219 | puts 'text: ' + response['text'] 220 | puts '' 221 | else 222 | puts 'Error in text extraction call: ' + response['statusInfo'] 223 | end 224 | 225 | 226 | puts '' 227 | puts '' 228 | puts '' 229 | puts '############################################' 230 | puts '# Author Extraction Example #' 231 | puts '############################################' 232 | puts '' 233 | puts '' 234 | 235 | puts 'Processing url: ' + demo_url 236 | puts '' 237 | 238 | response = alchemyapi.author('url', demo_url) 239 | 240 | if response['status'] == 'OK' 241 | puts '## Response Object ##' 242 | puts JSON.pretty_generate(response) 243 | 244 | 245 | puts '' 246 | puts '## Author ##' 247 | puts 'author: ' + response['author'] 248 | else 249 | puts 'Error in author extraction call: ' + response['statusInfo'] 250 | end 251 | 252 | 253 | puts '' 254 | puts '' 255 | puts '' 256 | puts '############################################' 257 | puts '# Language Detection Example #' 258 | puts '############################################' 259 | puts '' 260 | puts '' 261 | 262 | puts 'Processing text: ' + demo_text 263 | puts '' 264 | 265 | response = alchemyapi.language('text', demo_text) 266 | 267 | if response['status'] == 'OK' 268 | puts '## Response Object ##' 269 | puts JSON.pretty_generate(response) 270 | 271 | 272 | puts '' 273 | puts '## Language ##' 274 | puts 'text: ' + response['language'] 275 | puts 'iso-639-1: ' + response['iso-639-1'] 276 | puts 'native speakers: ' + response['native-speakers'] 277 | else 278 | puts 'Error in language detection call: ' + response['statusInfo'] 279 | end 280 | 281 | 282 | puts '' 283 | puts '' 284 | puts '' 285 | puts '############################################' 286 | puts '# Title Extraction Example #' 287 | puts '############################################' 288 | puts '' 289 | puts '' 290 | 291 | puts 'Processing url: ' + demo_url 292 | puts '' 293 | 294 | response = alchemyapi.title('url', demo_url) 295 | 296 | if response['status'] == 'OK' 297 | puts '## Response Object ##' 298 | puts JSON.pretty_generate(response) 299 | 300 | 301 | puts '' 302 | puts '## Title ##' 303 | puts 'title: ' + response['title'] 304 | else 305 | puts 'Error in title extraction call: ' + response['statusInfo'] 306 | end 307 | 308 | 309 | puts '' 310 | puts '' 311 | puts '' 312 | puts '############################################' 313 | puts '# Relation Extraction Example #' 314 | puts '############################################' 315 | puts '' 316 | puts '' 317 | 318 | puts 'Processing text: ' + demo_text 319 | puts '' 320 | 321 | response = alchemyapi.relations('text', demo_text) 322 | 323 | if response['status'] == 'OK' 324 | puts '## Response Object ##' 325 | puts JSON.pretty_generate(response) 326 | 327 | 328 | puts '' 329 | puts '## Relations ##' 330 | for relation in response['relations'] 331 | if relation.key?('subject') 332 | puts 'subject: ' + relation['subject']['text'] 333 | end 334 | 335 | if relation.key?('action') 336 | puts 'action: ' + relation['action']['text'] 337 | end 338 | 339 | if relation.key?('object') 340 | puts 'object: ' + relation['object']['text'] 341 | end 342 | puts '' 343 | end 344 | else 345 | puts 'Error in relation extraction call: ' + response['statusInfo'] 346 | end 347 | 348 | 349 | puts '' 350 | puts '' 351 | puts '' 352 | puts '############################################' 353 | puts '# Text Categorization Example #' 354 | puts '############################################' 355 | puts '' 356 | puts '' 357 | 358 | puts 'Processing text: ' + demo_text 359 | puts '' 360 | 361 | response = alchemyapi.category('text', demo_text) 362 | 363 | if response['status'] == 'OK' 364 | puts '## Response Object ##' 365 | puts JSON.pretty_generate(response) 366 | 367 | 368 | puts '' 369 | puts '## Category ##' 370 | puts 'text: ' + response['category'] 371 | puts 'score: ' + response['score'] 372 | else 373 | puts 'Error in text categorization call: ' + response['statusInfo'] 374 | end 375 | 376 | 377 | puts '' 378 | puts '' 379 | puts '' 380 | puts '############################################' 381 | puts '# Feed Detection Example #' 382 | puts '############################################' 383 | puts '' 384 | puts '' 385 | 386 | puts 'Processing url: ' + demo_url 387 | puts '' 388 | 389 | response = alchemyapi.feeds('url', demo_url) 390 | 391 | if response['status'] == 'OK' 392 | puts '## Response Object ##' 393 | puts JSON.pretty_generate(response) 394 | 395 | 396 | puts '' 397 | puts '## Feeds ##' 398 | for feed in response['feeds'] 399 | puts 'feed: ' + feed['feed'] 400 | end 401 | else 402 | puts 'Error in feed detection call: ' + response['statusInfo'] 403 | end 404 | 405 | 406 | puts '' 407 | puts '' 408 | puts '' 409 | puts '############################################' 410 | puts '# Microformats Parsing Example #' 411 | puts '############################################' 412 | puts '' 413 | puts '' 414 | 415 | puts 'Processing url: ' + demo_url 416 | puts '' 417 | 418 | response = alchemyapi.microformats('url', demo_url) 419 | 420 | if response['status'] == 'OK' 421 | puts '## Response Object ##' 422 | puts JSON.pretty_generate(response) 423 | 424 | 425 | puts '' 426 | puts '## Microformats ##' 427 | for microformat in response['microformats'] 428 | puts 'field: ' + microformat['field'] 429 | puts 'data: ' + microformat['data'] 430 | puts '' 431 | end 432 | else 433 | puts 'Error in microformats parsing call: ' + response['statusInfo'] 434 | end 435 | 436 | puts '' 437 | puts '' 438 | 439 | 440 | puts '' 441 | puts '' 442 | puts '' 443 | puts '############################################' 444 | puts '# Taxonomy Example #' 445 | puts '############################################' 446 | puts '' 447 | puts '' 448 | 449 | puts 'Processing url: ' + demo_url 450 | puts '' 451 | 452 | response = alchemyapi.taxonomy('url', demo_url) 453 | 454 | if response['status'] == 'OK' 455 | puts '## Response Object ##' 456 | puts JSON.pretty_generate(response) 457 | 458 | 459 | puts '' 460 | puts '## Taxonomy ##' 461 | for taxonomy in response['taxonomy'] 462 | puts 'label: ' + taxonomy['label'] 463 | puts 'score: ' + taxonomy['score'] 464 | if taxonomy.key?('confident') 465 | puts 'confident: ' + taxonomy['confident'] 466 | end 467 | puts '' 468 | end 469 | else 470 | puts 'Error in taxonomy call: ' + response['statusInfo'] 471 | end 472 | 473 | puts '' 474 | puts '' 475 | 476 | 477 | puts '' 478 | puts '' 479 | puts '' 480 | puts '############################################' 481 | puts '# Image Extraction Example #' 482 | puts '############################################' 483 | puts '' 484 | puts '' 485 | 486 | puts 'Processing url: ' + demo_url 487 | puts '' 488 | 489 | response = alchemyapi.image_extract('url', demo_url, { 'extractMode'=>'trust-metadata' }) 490 | 491 | if response['status'] == 'OK' 492 | puts '## Response Object ##' 493 | puts JSON.pretty_generate(response) 494 | 495 | 496 | puts '' 497 | puts '## Image Extraction ##' 498 | if response.key?('image') 499 | puts 'image: ' + response['image'] 500 | end 501 | puts '' 502 | else 503 | puts 'Error in image extraction call: ' + response['statusInfo'] 504 | end 505 | 506 | puts '' 507 | puts '' 508 | 509 | 510 | puts '' 511 | puts '' 512 | puts '' 513 | puts '############################################' 514 | puts '# Combined Example #' 515 | puts '############################################' 516 | puts '' 517 | puts '' 518 | 519 | puts 'Processing url: ' + demo_url 520 | puts '' 521 | 522 | response = alchemyapi.combined('url', demo_url, { 'extract'=>'page-image,keyword,entity' }) 523 | 524 | if response['status'] == 'OK' 525 | puts '## Response Object ##' 526 | puts JSON.pretty_generate(response) 527 | 528 | 529 | puts '' 530 | puts '## Combined Data ##' 531 | if response.key?('keywords') 532 | puts 'Keywords:' 533 | for keyword in response['keywords'] 534 | puts "\ttext: " + keyword['text'] 535 | puts "\trelevance: " + keyword['relevance'] 536 | puts '' 537 | end 538 | end 539 | if response.key?('image') 540 | puts 'image: ' + response['image'] 541 | puts '' 542 | end 543 | if response.key?('entities') 544 | puts 'Entities:' 545 | for entity in response['entities'] 546 | puts "\trelevance: " + entity['relevance'] 547 | puts "\ttext: " + entity['text'] 548 | puts "\tcount: " + entity['count'] 549 | puts "\ttype: " + entity['type'] 550 | if entity.key?('disambiguated') 551 | puts "\tdisambiguated: " 552 | if entity['disambiguated'].key?('dbpedia') 553 | puts "\t\tdbpedia: " + entity['disambiguated']['dbpedia'] 554 | end 555 | if entity['disambiguated'].key?('freebase') 556 | puts "\t\tfreebase: " + entity['disambiguated']['freebase'] 557 | end 558 | end 559 | puts '' 560 | end 561 | end 562 | puts '' 563 | else 564 | puts 'Error in combined call: ' + response['statusInfo'] 565 | end 566 | 567 | puts '' 568 | puts '' 569 | 570 | 571 | puts '' 572 | puts '' 573 | puts '' 574 | puts '############################################' 575 | puts '# Image Tagging Example #' 576 | puts '############################################' 577 | puts '' 578 | puts '' 579 | 580 | puts 'Processing url: ' + demo_url 581 | puts '' 582 | 583 | response = alchemyapi.image_tag('url', demo_url, { 'extractMode'=>'trust-metadata' }) 584 | 585 | if response['status'] == 'OK' 586 | puts '## Response Object ##' 587 | puts JSON.pretty_generate(response) 588 | 589 | 590 | puts '' 591 | puts '## Image Tagging ##' 592 | if response.key?('imageKeywords') 593 | puts 'Keywords:' 594 | for keyword in response['imageKeywords'] 595 | puts "\ttext: " + keyword['text'] 596 | puts "\tscore: " + keyword['score'] 597 | end 598 | end 599 | puts '' 600 | else 601 | puts 'Error in image tag call: ' + response['statusInfo'] 602 | end 603 | 604 | path_to_test_image = 'dog.jpg' 605 | test_image = File.binread(path_to_test_image) 606 | puts 'Processing image: ' + path_to_test_image 607 | puts '' 608 | 609 | response = alchemyapi.image_tag('image', '', { 'imagePostMode'=>'raw' }, test_image) 610 | 611 | if response['status'] == 'OK' 612 | puts '## Response Object ##' 613 | puts JSON.pretty_generate(response) 614 | 615 | 616 | puts '' 617 | puts '## Image Tagging ##' 618 | if response.key?('imageKeywords') 619 | puts 'Keywords:' 620 | for keyword in response['imageKeywords'] 621 | puts "\ttext: " + keyword['text'] 622 | puts "\tscore: " + keyword['score'] 623 | end 624 | end 625 | puts '' 626 | else 627 | puts 'Error in image tag call: ' + response['statusInfo'] 628 | end 629 | 630 | puts '' 631 | puts '' 632 | -------------------------------------------------------------------------------- /tests.rb: -------------------------------------------------------------------------------- 1 | 2 | # Copyright 2013 AlchemyAPI 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | 17 | require 'test/unit' 18 | require './alchemyapi' 19 | 20 | 21 | class Tests < Test::Unit::TestCase 22 | @@alchemyapi = AlchemyAPI.new() 23 | @@test_text = 'Bob broke my heart, and then made up this silly sentence to test the Ruby SDK' 24 | @@test_html = 'The best SDK Test | AlchemyAPI

Hello World!

My favorite language is Ruby

' 25 | @@test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0' 26 | @@test_image = File.binread('dog.jpg') 27 | 28 | def test_entities 29 | puts 'Checking entities . . . ' 30 | response = @@alchemyapi.entities('text', @@test_text) 31 | assert_equal response['status'], 'OK' 32 | response = @@alchemyapi.entities('html', @@test_html) 33 | assert_equal response['status'], 'OK' 34 | response = @@alchemyapi.entities('url', @@test_url) 35 | assert_equal response['status'], 'OK' 36 | response = @@alchemyapi.entities('random', @@test_text) 37 | assert_equal response['status'], 'ERROR' #invalid flavor 38 | puts 'Entity tests complete' 39 | puts '' 40 | end 41 | 42 | def test_keywords 43 | puts 'Checking keywords . . . ' 44 | response = @@alchemyapi.keywords('text', @@test_text) 45 | assert_equal response['status'], 'OK' 46 | response = @@alchemyapi.keywords('html', @@test_html) 47 | assert_equal response['status'], 'OK' 48 | response = @@alchemyapi.keywords('url', @@test_url) 49 | assert_equal response['status'], 'OK' 50 | response = @@alchemyapi.keywords('random', @@test_text) 51 | assert_equal response['status'], 'ERROR' #invalid flavor 52 | puts 'Keyword tests complete' 53 | puts '' 54 | end 55 | 56 | def test_concepts 57 | puts 'Checking concepts . . . ' 58 | response = @@alchemyapi.concepts('text', @@test_text) 59 | assert_equal response['status'], 'OK' 60 | response = @@alchemyapi.concepts('html', @@test_html) 61 | assert_equal response['status'], 'OK' 62 | response = @@alchemyapi.concepts('url', @@test_url) 63 | assert_equal response['status'], 'OK' 64 | response = @@alchemyapi.concepts('random', @@test_text) 65 | assert_equal response['status'], 'ERROR' #invalid flavor 66 | puts 'Concept tests complete' 67 | puts '' 68 | end 69 | 70 | def test_sentiment 71 | puts 'Checking sentiment . . . ' 72 | response = @@alchemyapi.sentiment('text', @@test_text) 73 | assert_equal response['status'], 'OK' 74 | response = @@alchemyapi.sentiment('html', @@test_html) 75 | assert_equal response['status'], 'OK' 76 | response = @@alchemyapi.sentiment('url', @@test_url) 77 | assert_equal response['status'], 'OK' 78 | response = @@alchemyapi.sentiment('random', @@test_text) 79 | assert_equal response['status'], 'ERROR' #invalid flavor 80 | puts 'Sentiment tests complete' 81 | puts '' 82 | end 83 | 84 | def test_sentiment_targeted 85 | puts 'Checking targeted sentiment . . . ' 86 | response = @@alchemyapi.sentiment_targeted('text', @@test_text, 'heart') 87 | assert_equal response['status'], 'OK' 88 | response = @@alchemyapi.sentiment_targeted('html', @@test_html, 'language') 89 | assert_equal response['status'], 'OK' 90 | response = @@alchemyapi.sentiment_targeted('url', @@test_url, 'Congress') 91 | assert_equal response['status'], 'OK' 92 | response = @@alchemyapi.sentiment_targeted('text', @@test_text, nil) 93 | assert_equal response['status'], 'ERROR' #invalid target 94 | response = @@alchemyapi.sentiment_targeted('random', @@test_url, 'Congress') 95 | assert_equal response['status'], 'ERROR' #invalid flavor 96 | puts 'Targeted sentiment tests complete' 97 | puts '' 98 | end 99 | 100 | def test_text 101 | puts 'Checking clean text . . . ' 102 | response = @@alchemyapi.text('text', @@test_text) 103 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 104 | response = @@alchemyapi.text('html', @@test_html) 105 | assert_equal response['status'], 'OK' 106 | response = @@alchemyapi.text('url', @@test_url) 107 | assert_equal response['status'], 'OK' 108 | puts 'Clean text tests complete' 109 | puts '' 110 | end 111 | 112 | def test_text_raw 113 | puts 'Checking raw text . . . ' 114 | response = @@alchemyapi.text_raw('text', @@test_text) 115 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 116 | response = @@alchemyapi.text_raw('html', @@test_html) 117 | assert_equal response['status'], 'OK' 118 | response = @@alchemyapi.text_raw('url', @@test_url) 119 | assert_equal response['status'], 'OK' 120 | puts 'Raw text tests complete' 121 | puts '' 122 | end 123 | 124 | def test_text_author 125 | puts 'Checking author. . . ' 126 | response = @@alchemyapi.author('text', @@test_text) 127 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 128 | response = @@alchemyapi.author('html', @@test_html) 129 | assert_equal response['status'], 'ERROR' #there is no author information in the test HTML 130 | response = @@alchemyapi.author('url', @@test_url) 131 | assert_equal response['status'], 'OK' 132 | puts 'Author tests complete' 133 | puts '' 134 | end 135 | 136 | def test_title 137 | puts 'Checking title . . . ' 138 | response = @@alchemyapi.title('text', @@test_text) 139 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 140 | response = @@alchemyapi.title('html', @@test_html) 141 | assert_equal response['status'], 'OK' 142 | response = @@alchemyapi.title('url', @@test_url) 143 | assert_equal response['status'], 'OK' 144 | puts 'Title tests complete' 145 | puts '' 146 | end 147 | 148 | def test_relations 149 | puts 'Checking relations . . . ' 150 | response = @@alchemyapi.relations('text', @@test_text) 151 | assert_equal response['status'], 'OK' 152 | response = @@alchemyapi.relations('html', @@test_html) 153 | assert_equal response['status'], 'OK' 154 | response = @@alchemyapi.relations('url', @@test_url) 155 | assert_equal response['status'], 'OK' 156 | response = @@alchemyapi.relations('random', @@test_text) 157 | assert_equal response['status'], 'ERROR' #invalid flavor 158 | puts 'Relations tests complete' 159 | puts '' 160 | end 161 | 162 | def test_category 163 | puts 'Checking category . . . ' 164 | response = @@alchemyapi.category('text', @@test_text) 165 | assert_equal response['status'], 'OK' 166 | response = @@alchemyapi.category('html', @@test_html, { 'url'=>'test' }) 167 | assert_equal response['status'], 'OK' 168 | response = @@alchemyapi.category('url', @@test_url) 169 | assert_equal response['status'], 'OK' 170 | response = @@alchemyapi.category('random', @@test_text) 171 | assert_equal response['status'], 'ERROR' #invalid flavor 172 | puts 'Category tests complete' 173 | puts '' 174 | end 175 | 176 | def test_feeds 177 | puts 'Checking feeds . . . ' 178 | response = @@alchemyapi.feeds('text', @@test_text) 179 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 180 | response = @@alchemyapi.feeds('html', @@test_html, { 'url'=>'test' }) 181 | assert_equal response['status'], 'OK' 182 | response = @@alchemyapi.feeds('url', @@test_url) 183 | assert_equal response['status'], 'OK' 184 | puts 'Feed tests complete' 185 | puts '' 186 | end 187 | 188 | def test_microformats 189 | puts 'Checking microformats . . . ' 190 | response = @@alchemyapi.microformats('text', @@test_text) 191 | assert_equal response['status'], 'ERROR' #only valid for HTML and URL content 192 | response = @@alchemyapi.microformats('html', @@test_html, { 'url'=>'test' }) 193 | assert_equal response['status'], 'OK' 194 | response = @@alchemyapi.microformats('url', @@test_url) 195 | assert_equal response['status'], 'OK' 196 | puts 'Microformat tests complete' 197 | puts '' 198 | end 199 | 200 | def test_taxonomy 201 | puts 'Checking taxonomy . . . ' 202 | response = @@alchemyapi.taxonomy('text', @@test_text) 203 | assert_equal response['status'], 'OK' 204 | response = @@alchemyapi.taxonomy('url', @@test_url) 205 | assert_equal response['status'], 'OK' 206 | response = @@alchemyapi.taxonomy('html', @@test_html, { 'url'=>'test' }) 207 | assert_equal response['status'], 'OK' 208 | response = @@alchemyapi.taxonomy('random', @@test_text) 209 | assert_equal response['status'], 'ERROR' #invalid flavor 210 | puts 'Taxonomy tests complete' 211 | puts '' 212 | end 213 | 214 | def test_combined 215 | puts 'Checking combined . . . ' 216 | response = @@alchemyapi.combined('html', @@test_html, { 'url'=>'test' }) 217 | assert_equal response['status'], 'ERROR' #only valid for text and URL content 218 | response = @@alchemyapi.combined('text', @@test_text) 219 | assert_equal response['status'], 'OK' 220 | response = @@alchemyapi.combined('url', @@test_url) 221 | assert_equal response['status'], 'OK' 222 | puts 'Combined tests complete' 223 | puts '' 224 | end 225 | 226 | def test_image_extract 227 | puts 'Checking image extract . . . ' 228 | response = @@alchemyapi.image_extract('text', @@test_text) 229 | assert_equal response['status'], 'ERROR' #only valid for URL content 230 | response = @@alchemyapi.image_extract('html', @@test_html, { 'url'=>'test' }) 231 | assert_equal response['status'], 'ERROR' #only valid for URL content 232 | response = @@alchemyapi.image_extract('url', @@test_url) 233 | assert_equal response['status'], 'OK' 234 | puts 'Image extract tests complete' 235 | puts '' 236 | end 237 | 238 | def test_image_tag 239 | puts 'Checking image tag . . . ' 240 | response = @@alchemyapi.image_tag('text', @@test_text) 241 | assert_equal response['status'], 'ERROR' #only valid for URL or image content 242 | response = @@alchemyapi.image_tag('html', @@test_html, { 'url'=>'test' }) 243 | assert_equal response['status'], 'ERROR' #only valid for URL or image content 244 | response = @@alchemyapi.image_tag('url', @@test_url) 245 | assert_equal response['status'], 'OK' 246 | response = @@alchemyapi.image_tag('image', '', { 'imagePostMode'=>'raw' }, @@test_image) 247 | assert_equal response['status'], 'OK' 248 | puts 'Image tag tests complete' 249 | puts '' 250 | end 251 | 252 | end 253 | 254 | 255 | 256 | 257 | --------------------------------------------------------------------------------