Exemplo n.º 1
0
<?php

// Load the AlchemyAPI module code.
include "../module/AlchemyAPI.php";
// Or load the AlchemyAPI PHP+CURL module.
/*include "../module/AlchemyAPI_CURL.php";*/
// Create an AlchemyAPI object.
$alchemyObj = new AlchemyAPI();
// Load the API key from disk.
$alchemyObj->loadAPIKey("api_key.txt");
// Categorize a web URL.
$result = $alchemyObj->URLGetCategory("http://www.techcrunch.com/");
echo "{$result}<br/><br/>\n";
// Load a HTML document to analyze.
$htmlFile = file_get_contents("data/example.html");
// Categorize some text.
$result = $alchemyObj->TextGetCategory("Latest on the War in Iraq.");
echo "{$result}<br/><br/>\n";
// Categorize a HTML document.
$result = $alchemyObj->HTMLGetCategory($htmlFile, "http://www.test.com/");
echo "{$result}<br/><br/>\n";
Exemplo n.º 2
0
 function extract_keyword($content, $permalink)
 {
     // Set up the text to analyze keyword.
     // If the length of the feed is not up to quality standards, track down and load the actual article.
     if (strlen(strip_tags($content)) < 500) {
         $extracted_text = $this->feed_hunter($permalink)->text;
         // Make sure the newly loaded feed is up to quality standards.
         if (strlen(strip_tags($content)) > 500) {
             $specimen = $extracted_text;
         } else {
             $specimen = strip_tags($content);
         }
     } else {
         $specimen = strip_tags($content);
     }
     $characters = array('"');
     $replacements = array(' ');
     $text = str_replace($characters, $replacements, $specimen);
     // Load the AlchemyAPI and configure access key.
     require_once 'inc/AlchemyAPI.php';
     $alchemyObj = new AlchemyAPI();
     $alchemyObj->setAPIKey("1414657c7c56cc31a067f8daafabf1d6978c28fe");
     // Analyze the article, fetching the concept, keyword, and category of an article.
     $tags_json = json_decode($alchemyObj->TextGetRankedKeywords($text, 'json'));
     $concepts_json = json_decode($alchemyObj->TextGetRankedConcepts($text, 'json'));
     $category = json_decode($alchemyObj->TextGetCategory($text, 'json'));
     $this->feed_model->alchemy_meter(3);
     // Parse the result retrieved from AlchemyAPI into an associated array.
     $tags = array();
     foreach ($concepts_json->concepts as $concept) {
         $characters = array("'");
         $replacements = array("");
         $output = str_replace($characters, $replacements, $concept->text);
         $tag['keyword'] = $output;
         $tag['confidence'] = $concept->relevance;
         $tag['is_concept'] = TRUE;
         array_push($tags, $tag);
     }
     foreach ($tags_json->keywords as $keyword) {
         $characters = array("'");
         $replacements = array("");
         $output = str_replace($characters, $replacements, $keyword->text);
         $tag['keyword'] = $output;
         $tag['confidence'] = $keyword->relevance;
         $tag['is_concept'] = FALSE;
         array_push($tags, $tag);
     }
     // Return the result.
     $result['tags'] = $tags;
     $result['category'] = $category->category;
     return $result;
 }
Exemplo n.º 3
0
function CheckParameterTypes()
{
    $alchemyObj = new AlchemyAPI();
    $alchemyObj->loadAPIKey("api_key.txt");
    $htmlFile = file_get_contents("../example/data/example.html");
    $htmlFile2 = file_get_contents("../example/data/example2.html");
    $htmlFile2_nolinks = file_get_contents("../example/data/example2_nolinks.html");
    $namedEntityParams = new AlchemyAPI_NamedEntityParams();
    $keywordParams = new AlchemyAPI_KeywordParams();
    try {
        $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $keywordParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $keywordParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->TextGetRankedNamedEntities("Hello my name is Bob.  I am speaking to you at this very moment.  Are you listening to me, Bob?", "xml", $keywordParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetCategory("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->TextGetCategory("Latest on the War in Iraq.", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetCategory($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetConstraintQuery("http://microformats.org/wiki/hcard", "1st link", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetConstraintQuery($htmlFile, "http://www.test.com/", "1st link", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetFeedLinks("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetFeedLinks($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetRankedKeywords("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->TextGetRankedKeywords("Hello my name is Bob Jones.  I am speaking to you at this very moment.  Are you listening to me, Bob?", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetRankedKeywords($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetLanguage("http://www.techcrunch.fr/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->TextGetLanguage("Hello my name is Bob Jones.  I am speaking to you at this very moment.  Are you listening to me, Bob?", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetLanguage($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetMicroformats("http://microformats.org/wiki/hcard", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetMicroformats($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetTitle("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetText("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->URLGetRawText("http://www.techcrunch.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetTitle($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetText($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
    try {
        $result = $alchemyObj->HTMLGetRawText($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
        //should throw an exception for mismatched parameters
        assert(1 == 0);
    } catch (Exception $e) {
    }
}