public function TextGetRankedNamedEntities($text, $outputMode = self::XML_OUTPUT_MODE, $namedEntityParams = null) { $this->CheckText($text, $outputMode); $this->CheckParamType("AlchemyAPI_NamedEntityParams", $namedEntityParams); if (is_null($namedEntityParams)) { $namedEntityParams = new AlchemyAPI_NamedEntityParams(); } $namedEntityParams->setText($text); $namedEntityParams->setOutputMode($outputMode); return $this->POST("TextGetRankedNamedEntities", "text", $namedEntityParams); }
<?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";
$alchemyObj = new AlchemyAPI(); // Load the API key from disk. $alchemyObj->loadAPIKey("api_key.txt"); // Extract sentiment from a web URL. $result = $alchemyObj->URLGetTextSentiment("http://www.techcrunch.com/"); echo "{$result}<br/><br/>\n"; // Extract sentiment from a text string. $result = $alchemyObj->TextGetTextSentiment("It's wonderful when the sun is shining and ABBA is playing."); echo "{$result}<br/><br/>\n"; // Load a HTML document to analyze. $htmlFile = file_get_contents("data/example.html"); // Extract sentiment from a HTML document. $result = $alchemyObj->HTMLGetTextSentiment($htmlFile, "http://www.test.com/"); echo "{$result}<br/><br/>\n"; // Enable entity-level sentiment. $namedEntityParams = new AlchemyAPI_NamedEntityParams(); $namedEntityParams->setSentiment(1); // Extract entities with entity-level sentiment. $result = $alchemyObj->TextGetRankedNamedEntities("Wyle E. Coyote is slow.", "xml", $namedEntityParams); echo "{$result}<br/><br/>\n"; // Enable keyword-level sentiment. $keywordParams = new AlchemyAPI_KeywordParams(); $keywordParams->setSentiment(1); // Extract keywords with keyword-level sentiment. $result = $alchemyObj->TextGetRankedKeywords("Wyle E. Coyote is slow.", "xml", $keywordParams); echo "{$result}<br/><br/>\n"; // Enable Targeted Sentiment $targetedSentimentParams = new AlchemyAPI_TargetedSentimentParams(); $targetedSentimentParams->setShowSourceText(1); $result = $alchemyObj->TextGetTargetedSentiment("This car is terrible.", "car", "xml", $targetedSentimentParams); echo "{$result}<br/><br/>\n";
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) { } }