예제 #1
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) {
    }
}