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");
// Create a named entity API parameters object
$namedEntityParams = new AlchemyAPI_NamedEntityParams();
// Turn off quotations extraction
$namedEntityParams->setQuotations(0);
// Turn off entity disambiguation
$namedEntityParams->setDisambiguate(0);
// Turn on sentiment analysis
$namedEntityParams->setSentiment(1);
// Extract a ranked list of named entities from a web URL, using the previously created parameters object.
$result = $alchemyObj->URLGetRankedNamedEntities("http://www.techcrunch.com/", "xml", $namedEntityParams);
echo "{$result}<br/><br/>\n";
// Extract a ranked list of named entities from a text string, using the previously created parameters object.
$result = $alchemyObj->TextGetRankedNamedEntities("Hello my name is Bob.  I am speaking to you at this very moment.  Are you listening to me, Bob?", "xml", $namedEntityParams);
echo "{$result}<br/><br/>\n";
// Load a HTML document to analyze.
$htmlFile = file_get_contents("data/example.html");
// Extract a ranked list of named entities from a HTML document, using the previously created parameters object.
$result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile, "http://www.test.com/", "xml", $namedEntityParams);
echo "{$result}<br/><br/>\n";
Exemplo n.º 2
0
function CheckNamedEntityParams()
{
    $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");
    //Checking Quotation Param
    $namedEntityParams = new AlchemyAPI_NamedEntityParams();
    $namedEntityParams->setQuotations(0);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $quotations = $doc->xpath("//quotation");
    assert(count($quotations) == 0);
    $namedEntityParams->setQuotations(1);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $quotations = $doc->xpath("//quotation");
    assert(count($quotations) != 0);
    //Checking LinkedData Param
    $namedEntityParams = new AlchemyAPI_NamedEntityParams();
    $namedEntityParams->setLinkedData(0);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//dbpedia");
    assert(count($matches) == 0);
    $namedEntityParams->setLinkedData(1);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//dbpedia");
    assert(count($matches) != 0);
    //Checking Disambiguated Param
    $namedEntityParams = new AlchemyAPI_NamedEntityParams();
    $namedEntityParams->setDisambiguate(0);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $namedEntityParams->setDisambiguate(1);
    $result = $alchemyObj->URLGetRankedNamedEntities("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) != 0);
    $namedEntityParams->setDisambiguate(0);
    $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $namedEntityParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $namedEntityParams->setDisambiguate(1);
    $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $namedEntityParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) != 0);
    //Checking XPath Param
    $namedEntityParams = new AlchemyAPI_NamedEntityParams();
    $namedEntityParams->setSourceText("xpath");
    $namedEntityParams->setXPath("//a");
    $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $namedEntityParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//entity");
    assert(count($matches) != 0);
    //Checking Custom Param
    $namedEntityParams->setCustomParameters("disambiguate", "0");
    $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $namedEntityParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $namedEntityParams->setCustomParameters("disambiguate", "1");
    $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2, "http://www.test.com/", "xml", $namedEntityParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) != 0);
    try {
        $result = $alchemyObj->HTMLGetRankedNamedEntities($htmlFile2_nolinks, "http://www.test.com/", "xml", $namedEntityParams);
        //should return an error.  If there is no error, xpath found a link when there are none.
        assert(1 == 0);
    } catch (Exception $e) {
    }
}