Exemplo n.º 1
0
function CheckRelationParams()
{
    $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 Entities Param
    $relationParams = new AlchemyAPI_RelationParams();
    $relationParams->setEntities(0);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//entity");
    assert(count($matches) == 0);
    $relationParams->setEntities(1);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//entity");
    assert(count($matches) != 0);
    //Checking Sentiment Param
    $relationParams = new AlchemyAPI_RelationParams();
    $relationParams->setSentiment(0);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//sentiment");
    assert(count($matches) == 0);
    $relationParams->setSentiment(1);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//sentiment");
    assert(count($matches) != 0);
    //Checking LinkedData Param
    $relationParams = new AlchemyAPI_RelationParams();
    $relationParams->setEntities(1);
    $relationParams->setLinkedData(0);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//dbpedia");
    assert(count($matches) == 0);
    $relationParams->setEntities(1);
    $relationParams->setLinkedData(1);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//dbpedia");
    assert(count($matches) != 0);
    //Checking Disambiguated Param
    $relationParams = new AlchemyAPI_RelationParams();
    $relationParams->setEntities(1);
    $relationParams->setDisambiguate(0);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $relationParams->setEntities(1);
    $relationParams->setDisambiguate(1);
    $result = $alchemyObj->URLGetRelations("http://www.cnn.com/2010/HEALTH/06/03/gulf.fishermans.wife/index.html?hpt=C2", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) != 0);
    $relationParams->setDisambiguate(0);
    $result = $alchemyObj->HTMLGetRelations($htmlFile2, "http://www.test.com/", "xml", $relationParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $relationParams->setDisambiguate(1);
    $result = $alchemyObj->HTMLGetRelations($htmlFile2, "http://www.test.com/", "xml", $relationParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) != 0);
    //Checking XPath Param
    $relationParams = new AlchemyAPI_RelationParams();
    $relationParams->setSourceText("xpath");
    $relationParams->setXPath("//a");
    $result = $alchemyObj->HTMLGetRelations($htmlFile2, "http://www.test.com/", "xml", $relationParams);
    $doc = simplexml_load_string($result);
    CheckForOKStatus($result);
    $matches = $doc->xpath("//relation");
    assert(count($matches) != 0);
    //Checking Custom Param
    $relationParams->setCustomParameters("disambiguate", "0");
    $result = $alchemyObj->HTMLGetRelations($htmlFile2, "http://www.test.com/", "xml", $relationParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//disambiguated");
    assert(count($matches) == 0);
    $relationParams->setCustomParameters("entities", "1");
    $result = $alchemyObj->HTMLGetRelations($htmlFile2, "http://www.test.com/", "xml", $relationParams);
    CheckForOKStatus($result);
    $doc = simplexml_load_string($result);
    $matches = $doc->xpath("//entity");
    assert(count($matches) != 0);
    try {
        $result = $alchemyObj->HTMLGetRelations($htmlFile2_nolinks, "http://www.test.com/", "xml", $relationParams);
        //should return an error.  If there is no error, xpath found a link when there are none.
        assert(1 == 0);
    } catch (Exception $e) {
    }
}