public function getConceptsFor($text) { $concepts = $this->alchemy->concepts('text', $text); $words = array(); if (isset($concepts['concepts'])) { $total = 0; foreach ($concepts['concepts'] as $keyword) { if ($total++ < $this->maxKeywords) { $words[] = (string) $keyword['text']; } } } return $words; }
echo PHP_EOL; } } else { echo 'Error in the keyword extraction call: ', $response['statusInfo']; } echo PHP_EOL; echo PHP_EOL; echo PHP_EOL; echo '############################################', PHP_EOL; echo '# Concept Tagging Example #', PHP_EOL; echo '############################################', PHP_EOL; echo PHP_EOL; echo PHP_EOL; echo 'Processing text: ', $demo_text, PHP_EOL; echo PHP_EOL; $response = $alchemyapi->concepts('text', $demo_text, null); if ($response['status'] == 'OK') { echo '## Response Object ##', PHP_EOL; echo print_r($response); echo PHP_EOL; echo '## Concepts ##', PHP_EOL; foreach ($response['concepts'] as $concept) { echo 'text: ', $concept['text'], PHP_EOL; echo 'relevance: ', $concept['relevance'], PHP_EOL; echo PHP_EOL; } } else { echo 'Error in the concept tagging call: ', $response['statusInfo']; } echo PHP_EOL; echo PHP_EOL;
<?php require_once 'alchemyapi.php'; $alchemyapi = new AlchemyAPI(); $feed_url = "http://www.the-star.co.ke/star-health"; $content = file_get_contents($feed_url, true); $feed = json_decode($content); if (property_exists($feed, "nodes") && isFresh($content)) { //first dump raw content to file to check if new in subsequent pulls file_put_contents("oldsha.txt", sha1($content)); $items = $feed->nodes; $articles = array(); $concepts = array(); foreach ($items as $d) { $item = $d->node; $response = $alchemyapi->concepts('text', $item->body, null); $tags = array(); $weighted_tags = array(); if ($response['status'] == 'OK') { foreach ($response['concepts'] as $concept) { if (!array_key_exists($concept['text'], $tags)) { //No exact match //check if this key is substring of existing key or vice versa $existing_key = sub_exists($concept['text'], $concepts); if ($existing_key != null) { $new_value = $concepts[$existing_key] + 1; $concepts[$existing_key] = $new_value; $tags[] = $existing_key; } else { $concepts[$concept['text']] = 1; $tags[] = $concept['text'];
<?php require_once 'alchemyapi.php'; $alchemyapi = new AlchemyAPI(); extract($_GET); $res_arr = array(); $text = rawurldecode($demo_text); //echo $text; //$text_arr = explode(" ",$text); $response = $alchemyapi->concepts('text', $text, array('maxRetrieve' => 2)); if ($response['status'] == 'OK') { $con = array(); // if(isset($response['concepts'])) // echo "set"; // echo json_encode($response); foreach ($response['concepts'] as $concept) { //array_push($concepts, $concept['text']); $con[$concept['text']] = $concept['relevance']; // echo $concept['relevance']; } } else { echo 'Error in the concept tagging call: ', $response['statusInfo']; } // $con = array('computational learning theory'=> '0.971245', // 'Computer'=> '0.854866', // 'artificial intelligence'=> '0.793011', // 'Machine learning' => '0.714784', // 'subfield'=> '0.638192'); arsort($con); $concept = array_keys($con)[0]; // echo $concept;
echo 'Entity tests complete!', PHP_EOL, PHP_EOL; //Keywords echo 'Checking keywords . . . ', PHP_EOL; $response = $alchemyapi->keywords('text', $test_text, null); assert($response['status'] == 'OK'); $response = $alchemyapi->keywords('html', $test_html, null); assert($response['status'] == 'OK'); $response = $alchemyapi->keywords('url', $test_url, null); assert($response['status'] == 'OK'); $response = $alchemyapi->keywords('random', $test_url, null); assert($response['status'] == 'ERROR'); //invalid flavor echo 'Keyword tests complete!', PHP_EOL, PHP_EOL; //Concepts echo 'Checking concepts . . . ', PHP_EOL; $response = $alchemyapi->concepts('text', $test_text, null); assert($response['status'] == 'OK'); $response = $alchemyapi->concepts('html', $test_html, null); assert($response['status'] == 'OK'); $response = $alchemyapi->concepts('url', $test_url, null); assert($response['status'] == 'OK'); $response = $alchemyapi->concepts('random', $test_url, null); assert($response['status'] == 'ERROR'); //invalid flavor echo 'Concept tests complete!', PHP_EOL, PHP_EOL; //Sentiment echo 'Checking sentiment . . . ', PHP_EOL; $response = $alchemyapi->sentiment('text', $test_text, null); assert($response['status'] == 'OK'); $response = $alchemyapi->sentiment('html', $test_html, null); assert($response['status'] == 'OK');
if($prev_link){ ?> <div class="ccm-next-previous-previouslink"> <?php echo '<a href="'.$prev_link.'" alt="prev_page">« '.t('Previous').'</a>';?> </div> <?php } ?> <div class="spacer"></div> </div> <div> <hr> <? // $response = $alchemyapi->sentiment("text", $content, null); // echo "Sentiment: ", $response["docSentiment"]["type"], PHP_EOL; // $response = $alchemyapi->keywords("html", $content, null); // echo "Sentiment: ", $response["docSentiment"]["type"], PHP_EOL; $response = $alchemyapi->concepts("html", $content, null); fdump($response); echo "Sentiment: ", $response["docSentiment"]["type"], PHP_EOL; ?> </div>
if ($session_api == 'AYLIEN') { require_once 'AYLIEN/TextAPI.php'; $textapi = new AYLIEN\TextAPI("c3d2e5d7", "8b01b98b519dd820eaf883f49dd4ed82", false); if ($session_name == 'Concepts') { $concepts = $textapi->Concepts(array("text" => $session_text, "language" => "en")); $results = array(); foreach ($concepts->concepts as $uri => $value) { $surfaceForms = array_map(function ($sf) { return $sf->string; }, $value->surfaceForms); //$results = array_merge($results, $surfaceForms); foreach ($surfaceForms as $conceptString) { array_push($results, (object) array('url' => $uri, 'concept' => $conceptString)); } //printf("Concept: " . implode(",", $surfaceForms) . "<br />"); } echo json_encode($results); } } else { if ($session_api == 'AlchemyApi') { require_once 'AlchemyApi/alchemyapi.php'; $alchemyapi = new AlchemyAPI("c023b3df5d0a66ccc644a3137f498f0fccc3d304"); if ($session_name == 'Concepts') { $options = null; $options['outputMode'] = 'json'; $response = $alchemyapi->concepts('text', $session_text, $options); echo json_encode($response); } } else { } }