// 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"); // Extract a ranked list of relations from a web URL. $result = $alchemyObj->URLGetRelations("http://www.techcrunch.com/"); echo "{$result}<br/><br/>\n"; // Extract a ranked list of relations from a text string. $result = $alchemyObj->TextGetRelations("Hello my name is Bob. I am speaking to you at this very moment. Are you listening to me, Bob?"); echo "{$result}<br/><br/>\n"; // Load a HTML document to analyze. $htmlFile = file_get_contents("data/example.html"); // Extract a ranked list of relations from a HTML document. $result = $alchemyObj->HTMLGetRelations($htmlFile, "http://www.test.com/"); echo "{$result}<br/><br/>\n"; $relationParams = new AlchemyAPI_RelationParams(); // Turn off quotations extraction $relationParams->SetSentiment(1); $relationParams->SetEntities(1); $relationParams->SetDisambiguate(1); $relationParams->SetSentimentExcludeEntities(1); $result = $alchemyObj->TextGetRelations("Madonna enjoys tasty Pepsi. I love her style.", "xml", $relationParams); echo "{$result}<br/><br/>\n"; $relationParams->SetRequireEntities(1); $result = $alchemyObj->TextGetRelations("Madonna enjoys tasty Pepsi. I love her style.", "xml", $relationParams); echo "{$result}<br/><br/>\n";
public function TextGetRelations($text, $outputMode = self::XML_OUTPUT_MODE, $relationParams = null) { $this->CheckText($text, $outputMode); $this->CheckParamType("AlchemyAPI_RelationParams", $relationParams); if (is_null($relationParams)) { $relationParams = new AlchemyAPI_RelationParams(); } $relationParams->setText($text); $relationParams->setOutputMode($outputMode); return $this->POST("TextGetRelations", "text", $relationParams); }
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) { } }