PageRenderTime 80ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/example.php

https://github.com/lugrus2000/alchemyapi_php
PHP | 616 lines | 451 code | 114 blank | 51 comment | 60 complexity | 5bfd28b397a2c13ddd62a83a5877ef69 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. Copyright 2013 AlchemyAPI
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. require_once 'alchemyapi.php';
  15. $alchemyapi = new AlchemyAPI();
  16. $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.';
  17. $demo_url = 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen';
  18. $demo_html = '<html><head><title>PHP Demo | AlchemyAPI</title></head><body><h1>Did you know that AlchemyAPI works on HTML?</h1><p>Well, you do now.</p></body></html>';
  19. echo PHP_EOL;
  20. echo PHP_EOL;
  21. echo ' , ', PHP_EOL;
  22. echo ' .I7777~ ', PHP_EOL;
  23. echo ' .I7777777 ', PHP_EOL;
  24. echo ' +. 77777777 ', PHP_EOL;
  25. echo ' =???, I7777777= ', PHP_EOL;
  26. echo '=?????? 7777777? ,:::===? ', PHP_EOL;
  27. echo '=???????. 777777777777777777~ .77: ?? :7 =$, :$$$$$$+ =$? ', PHP_EOL;
  28. echo ' ????????: .777777777777777777 II77 ?? :7 $$7 :$? 7$7 =$? ', PHP_EOL;
  29. echo ' .???????= +7777777777777777 .7 =7: ?? :7777+ :7:I777? ?777I= 77~777? ,777I I7 77 +$?$: :$? $$ =$? ', PHP_EOL;
  30. echo ' ???????+ ~777???+===::: :7+ ~7 ?? .77 +7 :7?. II 7~ ,I7 77+ I77 ~7 ?7 =7: .$, =$ :$? ,$$? =$? ', PHP_EOL;
  31. echo ' ,???????~ 77 7: ?? ?I. 7 :7 :7 ~7 7 77 =7: 7 7 7~ 7$ $= :$$$$$$~ =$? ', PHP_EOL;
  32. echo ' .??????? ,???I77777777777~ :77777777~ ?? 7: :7 :7 777777777:77 =7 7 +7 ~7 $$$$$$$$I :$? =$? ', PHP_EOL;
  33. echo ' .??????? ,7777777777777777 7= 77 ?? I+ 7 :7 :7 ?? 7,77 =7 7 7~ 7, =$7 $$, :$? =$? ', PHP_EOL;
  34. echo ' .???????. I77777777777777777 +7 ,7??? 77 I7 :7 :7 7~ .?7 77 =7 7 ,77I $+ 7$ :$? =$? ', PHP_EOL;
  35. echo ' ,???????= :77777777777777777~ 7= ~7?? ~I77777 :7 :7 ,777777. 77 =7 7 77, +$ .$::$? =$? ', PHP_EOL;
  36. echo ',??????? :7777777 77 ', PHP_EOL;
  37. echo ' =????? ,7777777 77= ', PHP_EOL;
  38. echo ' +?+ 7777777? ', PHP_EOL;
  39. echo ' + ~7777777 ', PHP_EOL;
  40. echo ' I777777 ', PHP_EOL;
  41. echo ' :~ ', PHP_EOL;
  42. echo PHP_EOL;
  43. echo PHP_EOL;
  44. echo '############################################', PHP_EOL;
  45. echo '# Image Keyword Example #', PHP_EOL;
  46. echo '############################################', PHP_EOL;
  47. echo PHP_EOL;
  48. echo PHP_EOL;
  49. echo 'Processing Image URL: ', $demo_url, PHP_EOL;
  50. echo PHP_EOL;
  51. $response = $alchemyapi->image_keywords('url', $demo_url, array('extractMode'=>'trust-metadata'));
  52. if ($response['status'] == 'OK') {
  53. echo '## Response Object ##', PHP_EOL;
  54. echo print_r($response);
  55. echo PHP_EOL;
  56. echo '## Image Keywords ##', PHP_EOL;
  57. foreach ($response['imageKeywords'] as $imageKeywords) {
  58. echo 'image keyword: ', $imageKeywords['text'], PHP_EOL;
  59. echo 'score: ', $imageKeywords['score'], PHP_EOL;
  60. echo PHP_EOL;
  61. }
  62. } else {
  63. echo 'Error in the image keyword extraction call: ', $response['statusInfo'];
  64. }
  65. echo PHP_EOL;
  66. echo PHP_EOL;
  67. echo PHP_EOL;
  68. echo PHP_EOL;
  69. /*
  70. $imageName = "grumpy-cat-meme-hmmm.jpg";
  71. $imageFile = fopen($imageName, "r") or die("Unable to open file!");
  72. $imageData = fread($imageFile,filesize($imageName));
  73. fclose($imageFile);
  74. echo PHP_EOL;
  75. echo PHP_EOL;
  76. echo '############################################', PHP_EOL;
  77. echo '# Image Keyword Example with image #', PHP_EOL;
  78. echo '############################################', PHP_EOL;
  79. echo PHP_EOL;
  80. echo PHP_EOL;
  81. echo 'Processing Image File: ', $imageName, PHP_EOL;
  82. echo PHP_EOL;
  83. $response = $alchemyapi->image_keywords('image', $imageData, array('imagePostMode'=>'raw'));
  84. if ($response['status'] == 'OK') {
  85. echo '## Response Object ##', PHP_EOL;
  86. echo print_r($response);
  87. echo PHP_EOL;
  88. echo '## Image Keywords ##', PHP_EOL;
  89. foreach ($response['imageKeywords'] as $imageKeywords) {
  90. echo 'image keyword: ', $imageKeywords['text'], PHP_EOL;
  91. echo 'score: ', $imageKeywords['score'], PHP_EOL;
  92. echo PHP_EOL;
  93. }
  94. } else {
  95. echo 'Error in the image keyword extraction call: ', $response['statusInfo'];
  96. }
  97. echo PHP_EOL;
  98. echo PHP_EOL;*/
  99. echo PHP_EOL;
  100. echo PHP_EOL;
  101. echo PHP_EOL;
  102. echo '############################################', PHP_EOL;
  103. echo '# Entity Extraction Example #', PHP_EOL;
  104. echo '############################################', PHP_EOL;
  105. echo PHP_EOL;
  106. echo PHP_EOL;
  107. echo 'Processing text: ', $demo_text, PHP_EOL;
  108. echo PHP_EOL;
  109. $response = $alchemyapi->entities('text',$demo_text, array('sentiment'=>1));
  110. if ($response['status'] == 'OK') {
  111. echo '## Response Object ##', PHP_EOL;
  112. echo print_r($response);
  113. echo PHP_EOL;
  114. echo '## Entities ##', PHP_EOL;
  115. foreach ($response['entities'] as $entity) {
  116. echo 'entity: ', $entity['text'], PHP_EOL;
  117. echo 'type: ', $entity['type'], PHP_EOL;
  118. echo 'relevance: ', $entity['relevance'], PHP_EOL;
  119. echo 'sentiment: ', $entity['sentiment']['type'];
  120. if (array_key_exists('score', $entity['sentiment'])) {
  121. echo ' (' . $entity['sentiment']['score'] . ')', PHP_EOL;
  122. } else {
  123. echo PHP_EOL;
  124. }
  125. echo PHP_EOL;
  126. }
  127. } else {
  128. echo 'Error in the entity extraction call: ', $response['statusInfo'];
  129. }
  130. echo PHP_EOL;
  131. echo PHP_EOL;
  132. echo PHP_EOL;
  133. echo '############################################', PHP_EOL;
  134. echo '# Keyword Extraction Example #', PHP_EOL;
  135. echo '############################################', PHP_EOL;
  136. echo PHP_EOL;
  137. echo PHP_EOL;
  138. echo 'Processing text: ', $demo_text, PHP_EOL;
  139. echo PHP_EOL;
  140. $response = $alchemyapi->keywords('text',$demo_text, array('sentiment'=>1));
  141. if ($response['status'] == 'OK') {
  142. echo '## Response Object ##', PHP_EOL;
  143. echo print_r($response);
  144. echo PHP_EOL;
  145. echo '## Keywords ##', PHP_EOL;
  146. foreach ($response['keywords'] as $keyword) {
  147. echo 'keyword: ', $keyword['text'], PHP_EOL;
  148. echo 'relevance: ', $keyword['relevance'], PHP_EOL;
  149. echo 'sentiment: ', $keyword['sentiment']['type'];
  150. if (array_key_exists('score', $keyword['sentiment'])) {
  151. echo ' (' . $keyword['sentiment']['score'] . ')', PHP_EOL;
  152. } else {
  153. echo PHP_EOL;
  154. }
  155. echo PHP_EOL;
  156. }
  157. } else {
  158. echo 'Error in the keyword extraction call: ', $response['statusInfo'];
  159. }
  160. echo PHP_EOL;
  161. echo PHP_EOL;
  162. echo PHP_EOL;
  163. echo '############################################', PHP_EOL;
  164. echo '# Concept Tagging Example #', PHP_EOL;
  165. echo '############################################', PHP_EOL;
  166. echo PHP_EOL;
  167. echo PHP_EOL;
  168. echo 'Processing text: ', $demo_text, PHP_EOL;
  169. echo PHP_EOL;
  170. $response = $alchemyapi->concepts('text',$demo_text, null);
  171. if ($response['status'] == 'OK') {
  172. echo '## Response Object ##', PHP_EOL;
  173. echo print_r($response);
  174. echo PHP_EOL;
  175. echo '## Concepts ##', PHP_EOL;
  176. foreach ($response['concepts'] as $concept) {
  177. echo 'concept: ', $concept['text'], PHP_EOL;
  178. echo 'relevance: ', $concept['relevance'], PHP_EOL;
  179. echo PHP_EOL;
  180. }
  181. } else {
  182. echo 'Error in the concept tagging call: ', $response['statusInfo'];
  183. }
  184. echo PHP_EOL;
  185. echo PHP_EOL;
  186. echo PHP_EOL;
  187. echo '############################################', PHP_EOL;
  188. echo '# Sentiment Analysis Example #', PHP_EOL;
  189. echo '############################################', PHP_EOL;
  190. echo PHP_EOL;
  191. echo PHP_EOL;
  192. echo 'Processing HTML: ', $demo_html, PHP_EOL;
  193. echo PHP_EOL;
  194. $response = $alchemyapi->sentiment('html',$demo_html, null);
  195. if ($response['status'] == 'OK') {
  196. echo '## Response Object ##', PHP_EOL;
  197. echo print_r($response);
  198. echo PHP_EOL;
  199. echo '## Document Sentiment ##', PHP_EOL;
  200. echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL;
  201. if (array_key_exists('score', $response['docSentiment'])) {
  202. echo 'score: ', $response['docSentiment']['score'], PHP_EOL;
  203. }
  204. } else {
  205. echo 'Error in the sentiment analysis call: ', $response['statusInfo'];
  206. }
  207. echo PHP_EOL;
  208. echo PHP_EOL;
  209. echo PHP_EOL;
  210. echo '############################################', PHP_EOL;
  211. echo '# Targeted Sentiment Analysis Example #', PHP_EOL;
  212. echo '############################################', PHP_EOL;
  213. echo PHP_EOL;
  214. echo PHP_EOL;
  215. echo 'Processing text: ', $demo_text, PHP_EOL;
  216. echo 'Target: Denver, Colorado', PHP_EOL;
  217. echo PHP_EOL;
  218. $response = $alchemyapi->sentiment_targeted('text',$demo_text,'Denver', null);
  219. if ($response['status'] == 'OK') {
  220. echo '## Response Object ##', PHP_EOL;
  221. echo print_r($response);
  222. echo PHP_EOL;
  223. echo '## Targeted Sentiment ##', PHP_EOL;
  224. echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL;
  225. if (array_key_exists('score', $response['docSentiment'])) {
  226. echo 'score: ', $response['docSentiment']['score'], PHP_EOL;
  227. }
  228. } else {
  229. echo 'Error in the targeted sentiment analysis call: ', $response['statusInfo'];
  230. }
  231. echo PHP_EOL;
  232. echo PHP_EOL;
  233. echo PHP_EOL;
  234. echo '############################################', PHP_EOL;
  235. echo '# Text Extraction Example #', PHP_EOL;
  236. echo '############################################', PHP_EOL;
  237. echo PHP_EOL;
  238. echo PHP_EOL;
  239. echo 'Processing url: ', $demo_url, PHP_EOL;
  240. echo PHP_EOL;
  241. $response = $alchemyapi->text('url', $demo_url, null);
  242. if ($response['status'] == 'OK') {
  243. echo '## Response Object ##', PHP_EOL;
  244. echo print_r($response);
  245. echo PHP_EOL;
  246. echo '## Extracted Text ##', PHP_EOL;
  247. echo 'text: ',PHP_EOL, $response['text'], PHP_EOL;
  248. } else {
  249. echo 'Error in the text extraction call: ', $response['statusInfo'];
  250. }
  251. echo PHP_EOL;
  252. echo PHP_EOL;
  253. echo PHP_EOL;
  254. echo '############################################', PHP_EOL;
  255. echo '# Author Extraction Example #', PHP_EOL;
  256. echo '############################################', PHP_EOL;
  257. echo PHP_EOL;
  258. echo PHP_EOL;
  259. echo 'Processing url: ', $demo_url, PHP_EOL;
  260. echo PHP_EOL;
  261. $response = $alchemyapi->author('url',$demo_url, null);
  262. if ($response['status'] == 'OK') {
  263. echo '## Response Object ##', PHP_EOL;
  264. echo print_r($response);
  265. echo PHP_EOL;
  266. echo '## Author ##', PHP_EOL;
  267. echo 'author: ', $response['author'], PHP_EOL;
  268. } else {
  269. echo 'Error in the author extraction call: ', $response['statusInfo'];
  270. }
  271. echo PHP_EOL;
  272. echo PHP_EOL;
  273. echo PHP_EOL;
  274. echo '############################################', PHP_EOL;
  275. echo '# Language Detection Example #', PHP_EOL;
  276. echo '############################################', PHP_EOL;
  277. echo PHP_EOL;
  278. echo PHP_EOL;
  279. echo 'Processing text: ', $demo_text, PHP_EOL;
  280. echo PHP_EOL;
  281. $response = $alchemyapi->language('text',$demo_text, null);
  282. if ($response['status'] == 'OK') {
  283. echo '## Response Object ##', PHP_EOL;
  284. echo print_r($response);
  285. echo PHP_EOL;
  286. echo '## Language ##', PHP_EOL;
  287. echo 'language: ', $response['language'], PHP_EOL;
  288. echo 'iso-639-1: ', $response['iso-639-1'], PHP_EOL;
  289. echo 'native speakers: ', $response['native-speakers'], PHP_EOL;
  290. } else {
  291. echo 'Error in the language detection call: ', $response['statusInfo'];
  292. }
  293. echo PHP_EOL;
  294. echo PHP_EOL;
  295. echo PHP_EOL;
  296. echo '############################################', PHP_EOL;
  297. echo '# Title Extraction Example #', PHP_EOL;
  298. echo '############################################', PHP_EOL;
  299. echo PHP_EOL;
  300. echo PHP_EOL;
  301. echo 'Processing url: ', $demo_url, PHP_EOL;
  302. echo PHP_EOL;
  303. $response = $alchemyapi->title('url',$demo_url, null);
  304. if ($response['status'] == 'OK') {
  305. echo '## Response Object ##', PHP_EOL;
  306. echo print_r($response);
  307. echo PHP_EOL;
  308. echo '## Title ##', PHP_EOL;
  309. echo 'title: ', $response['title'], PHP_EOL;
  310. } else {
  311. echo 'Error in the title extraction call: ', $response['statusInfo'];
  312. }
  313. echo PHP_EOL;
  314. echo PHP_EOL;
  315. echo PHP_EOL;
  316. echo '############################################', PHP_EOL;
  317. echo '# Relation Extraction Example #', PHP_EOL;
  318. echo '############################################', PHP_EOL;
  319. echo PHP_EOL;
  320. echo PHP_EOL;
  321. echo 'Processing text: ', $demo_text, PHP_EOL;
  322. echo PHP_EOL;
  323. $response = $alchemyapi->relations('text',$demo_text, null);
  324. if ($response['status'] == 'OK') {
  325. echo '## Response Object ##', PHP_EOL;
  326. echo print_r($response);
  327. echo PHP_EOL;
  328. echo '## Relations ##', PHP_EOL;
  329. foreach ($response['relations'] as $relation) {
  330. if (array_key_exists('subject', $relation)) {
  331. echo 'Subject: ', $relation['subject']['text'], PHP_EOL;
  332. }
  333. if (array_key_exists('action', $relation)) {
  334. echo 'Action: ', $relation['action']['text'], PHP_EOL;
  335. }
  336. if (array_key_exists('object', $relation)) {
  337. echo 'Object: ', $relation['object']['text'], PHP_EOL;
  338. }
  339. echo PHP_EOL;
  340. }
  341. } else {
  342. echo 'Error in the relation extraction call: ', $response['statusInfo'];
  343. }
  344. echo PHP_EOL;
  345. echo PHP_EOL;
  346. echo PHP_EOL;
  347. echo '############################################', PHP_EOL;
  348. echo '# Text Categorization Example #', PHP_EOL;
  349. echo '############################################', PHP_EOL;
  350. echo PHP_EOL;
  351. echo PHP_EOL;
  352. echo 'Processing text: ', $demo_text, PHP_EOL;
  353. echo PHP_EOL;
  354. $response = $alchemyapi->category('text',$demo_text, null);
  355. if ($response['status'] == 'OK') {
  356. echo '## Response Object ##', PHP_EOL;
  357. echo print_r($response);
  358. echo PHP_EOL;
  359. echo '## Category ##', PHP_EOL;
  360. echo 'category: ', $response['category'], PHP_EOL;
  361. echo 'score: ', $response['score'], PHP_EOL;
  362. } else {
  363. echo 'Error in the text categorization call: ', $response['statusInfo'];
  364. }
  365. echo PHP_EOL;
  366. echo PHP_EOL;
  367. echo PHP_EOL;
  368. echo '############################################', PHP_EOL;
  369. echo '# Feed Detection Example #', PHP_EOL;
  370. echo '############################################', PHP_EOL;
  371. echo PHP_EOL;
  372. echo PHP_EOL;
  373. echo 'Processing url: ', $demo_url, PHP_EOL;
  374. echo PHP_EOL;
  375. $response = $alchemyapi->feeds('url',$demo_url, null);
  376. if ($response['status'] == 'OK') {
  377. echo '## Response Object ##', PHP_EOL;
  378. echo print_r($response);
  379. echo PHP_EOL;
  380. echo '## Feeds ##', PHP_EOL;
  381. foreach ($response['feeds'] as $feed) {
  382. echo 'feed: ', $feed['feed'], PHP_EOL;
  383. }
  384. } else {
  385. echo 'Error in the feed detection call: ', $response['statusInfo'];
  386. }
  387. echo PHP_EOL;
  388. echo PHP_EOL;
  389. echo PHP_EOL;
  390. echo '############################################', PHP_EOL;
  391. echo '# Microformats Parsing Example #', PHP_EOL;
  392. echo '############################################', PHP_EOL;
  393. echo PHP_EOL;
  394. echo PHP_EOL;
  395. echo 'Processing url: ', $demo_url, PHP_EOL;
  396. echo PHP_EOL;
  397. $response = $alchemyapi->microformats('url',$demo_url, null);
  398. if ($response['status'] == 'OK') {
  399. echo '## Response Object ##', PHP_EOL;
  400. echo print_r($response);
  401. echo PHP_EOL;
  402. echo '## Microformats ##', PHP_EOL;
  403. foreach ($response['microformats'] as $microformat) {
  404. echo 'field: ', $microformat['field'], PHP_EOL;
  405. echo 'data: ', $microformat['data'], PHP_EOL, PHP_EOL;
  406. }
  407. } else {
  408. echo 'Error in the microformat parsing call: ', $response['statusInfo'];
  409. }
  410. echo PHP_EOL;
  411. echo PHP_EOL;
  412. echo PHP_EOL;
  413. echo PHP_EOL;
  414. echo PHP_EOL;
  415. echo '############################################', PHP_EOL;
  416. echo '# Image Extraction Example #', PHP_EOL;
  417. echo '############################################', PHP_EOL;
  418. echo PHP_EOL;
  419. echo PHP_EOL;
  420. echo 'Processing url: ', $demo_url, PHP_EOL;
  421. echo PHP_EOL;
  422. $response = $alchemyapi->imageExtraction('url',$demo_url, null);
  423. if ($response['status'] == 'OK') {
  424. echo '## Response Object ##', PHP_EOL;
  425. echo print_r($response);
  426. echo PHP_EOL;
  427. echo '## Image ##', PHP_EOL;
  428. echo 'Image: ', $response['image'], PHP_EOL;
  429. } else {
  430. echo 'Error in the image extraction call: ', $response['statusInfo'];
  431. }
  432. echo PHP_EOL;
  433. echo PHP_EOL;
  434. echo PHP_EOL;
  435. echo PHP_EOL;
  436. echo PHP_EOL;
  437. echo '############################################', PHP_EOL;
  438. echo '# taxonomy Example #', PHP_EOL;
  439. echo '############################################', PHP_EOL;
  440. echo PHP_EOL;
  441. echo PHP_EOL;
  442. echo 'Processing text: ', $demo_text, PHP_EOL;
  443. echo PHP_EOL;
  444. $response = $alchemyapi->taxonomy('text',$demo_text, null);
  445. if ($response['status'] == 'OK') {
  446. echo '## Response Object ##', PHP_EOL;
  447. echo print_r($response);
  448. echo PHP_EOL;
  449. echo '## Categories ##', PHP_EOL;
  450. foreach ($response['taxonomy'] as $category) {
  451. echo $category['label'], ' : ', $category['score'], PHP_EOL;
  452. }
  453. } else {
  454. echo 'Error in the taxonomy call: ', $response['statusInfo'];
  455. }
  456. echo PHP_EOL;
  457. echo PHP_EOL;
  458. echo PHP_EOL;
  459. echo PHP_EOL;
  460. echo PHP_EOL;
  461. echo '############################################', PHP_EOL;
  462. echo '# combined Example #', PHP_EOL;
  463. echo '############################################', PHP_EOL;
  464. echo PHP_EOL;
  465. echo PHP_EOL;
  466. echo 'Processing text: ', $demo_text, PHP_EOL;
  467. echo PHP_EOL;
  468. $response = $alchemyapi->combined('text',$demo_text, null);
  469. if ($response['status'] == 'OK') {
  470. echo '## Response Object ##', PHP_EOL;
  471. echo print_r($response);
  472. echo PHP_EOL;
  473. echo '## Keywords ##', PHP_EOL;
  474. foreach ($response['keywords'] as $keyword) {
  475. echo $keyword['text'], ' : ', $keyword['relevance'], PHP_EOL;
  476. }
  477. echo PHP_EOL;
  478. echo '## Concepts ##', PHP_EOL;
  479. foreach ($response['concepts'] as $concept) {
  480. echo $concept['text'], ' : ', $concept['relevance'], PHP_EOL;
  481. }
  482. echo PHP_EOL;
  483. echo '## Entities ##', PHP_EOL;
  484. foreach ($response['entities'] as $entity) {
  485. echo $entity['type'], ' : ', $entity['text'], ' , ', $entity['relevance'], PHP_EOL;
  486. }
  487. echo PHP_EOL;
  488. } else {
  489. echo 'Error in the taxonomy call: ', $response['statusInfo'];
  490. }
  491. echo PHP_EOL;
  492. echo PHP_EOL;
  493. ?>