xpath("//entity"); $returnEntityArr = array(); $returnRelevanceArr = array(); foreach ($entities as $key => $value) { $typeArr = $doc->xpath("/results/entities/entity[$key+1]/type"); $textArr = $doc->xpath("/results/entities/entity[$key+1]/text"); $relevanceArr = $doc->xpath("/results/entities/entity[$key+1]/relevance"); if (count($typeArr) > 0 && count($textArr) > 0 && strlen($typeArr[0]) > 0) { $type = "$typeArr[0]"; $text = "$textArr[0]"; $relevance = $relevanceArr[0]; $disambArr = $doc->xpath("/results/entities/entity[$key+1]/disambiguated/name"); if (count($disambArr) > 0 && strlen($disambArr[0]) > 0) { $doDisamb = get_option('alchemyapi-disamb-enabled'); if (strlen($doDisamb) < 1) { $doDisamb = "true"; } if ($doDisamb == "true") { $text = "$disambArr[0]"; } } //$relevance = ((float)$relevance ); //$returnEntityArr[] = ($text ." (". $relevance . ")"); $returnEntityArr[] = $text; $returnRelevanceArr[] = (float)$relevance; } } return array($returnEntityArr,$returnRelevanceArr); } function AlchemyAPI_ParseConceptResponse($result) { $doc = simplexml_load_string($result); $concepts = $doc->xpath("/results/concepts/concept"); $returnConceptArr = array(); $returnRelevanceArr = array(); foreach ($concepts as $key => $value) { $textArr = $doc->xpath("//results/concepts/concept[$key+1]/text"); $text = $textArr[0]; $relevanceArr = $doc->xpath("//results/concepts/concept[$key+1]/relevance"); if (count($textArr) > 0 && count($relevanceArr) > 0 && $text!="") { $relevance = $relevanceArr[0]; //$returnConceptArr[]=($text . " (" . $relevance . ")"); $returnConceptArr[] = $text; $returnRelevanceArr[]=(float)$relevance; } } return array($returnConceptArr,$returnRelevanceArr); } function AlchemyAPI_CheckAPIKey() { $apiKey = get_option('alchemyapi-key'); if (strlen($apiKey) < 1) { return "false"; } // Create an AlchemyAPI object. $alchemyObj = new AlchemyAPI(); //$alchemyObj->_apiKey = $apiKey; $alchemyObj->setApiKey($apiKey); $postContent = "This is an API key validation check."; try { $result = $alchemyObj->TextGetRankedNamedEntities($postContent); } catch (Exception $e) { return $e; } return "true"; } function AlchemyAPI_TagPost() { $apiKey = get_option('alchemyapi-key'); if (strlen($apiKey) < 1) { die("You must first configure an AlchemyAPI Access Key from within your plugins settings screen."); } // Create an AlchemyAPI object. $alchemyObj = new AlchemyAPI(); //$alchemyObj->_apiKey = $apiKey; $alchemyObj->setApiKey($apiKey); $postContent = stripslashes($_POST['text']); $currentTags = $_POST['currentTags']; $entityResult = ""; $conceptResult = ""; $error = false; $errorMsg = ""; try { if(get_option('alchemyapi-extract-mode') == 'named_entities' || get_option('alchemyapi-extract-mode') == 'both'){ $entityParams = new AlchemyAPI_NamedEntityParams(); $entityParams->setCustomParameters("atCurrentTags",$currentTags); $entityResult = $alchemyObj->HTMLGetRankedNamedEntities($postContent, "http://www.alchemyapi.com/tools/alchemytagger/?type=post", "xml",$entityParams); } if(get_option('alchemyapi-extract-mode') == 'concepts' || get_option('alchemyapi-extract-mode') == 'both'){ $conceptParams = new AlchemyAPI_ConceptParams(); $conceptParams->setCustomParameters("atCurrentTags",$currentTags); $conceptResult = $alchemyObj->HtmlGetRankedConcepts($postContent, "http://www.alchemyapi.com/tools/alchemytagger/?type=post", "xml", $conceptParams); } } catch (Exception $e) { $error = true; $errorMsg = $e->getMessage(); } if ($error == true) { if ("Enter some text to analyze." == $errorMsg) { die("Type some text first."); } else if ("Error making API call: unsupported-text-language" == $errorMsg) { die("Type some text first."); } else if( "Error making API call: invalid-api-key" == $errorMsg) { die("Check your API key!"); } else { //die($errorMsg); die($errorMsg); } } else { $count = 0; if(get_option('alchemyapi-extract-mode') == 'named_entities' || get_option('alchemyapi-extract-mode') == 'both'){ list($entityResultArr,$entityRelevanceArr)=AlchemyAPI_ParseEntityResponse($entityResult); $count += count($entityResultArr); } if(get_option('alchemyapi-extract-mode') == 'concepts' || get_option('alchemyapi-extract-mode') == 'both'){ list($conceptResultArr,$conceptRelevanceArr)=AlchemyAPI_ParseConceptResponse($conceptResult); $count += count($conceptResultArr); } if ($count < 1) { echo(" "); die("No tag suggestions at this time."); } } if(get_option('alchemyapi-extract-mode') == 'named_entities') { $resultArr = $entityResultArr; $relevanceArr = $entityRelevanceArr; } else if(get_option('alchemyapi-extract-mode') == 'concepts') { $resultArr = $conceptResultArr; $relevanceArr = $conceptRelevanceArr; } else if(get_option('alchemyapi-extract-mode') == 'both') { $diff_array = array_diff($entityResultArr, $conceptResultArr); $relevanceArr =array(); $resultArr =array(); foreach ($diff_array as $mKey => $mValue) { $relevanceArr[] = $entityRelevanceArr[$mKey]; $resultArr[] = $mValue; } $relevanceArr = array_merge($relevanceArr, $conceptRelevanceArr); $resultArr = array_merge($resultArr, $conceptResultArr); } array_multisort($relevanceArr, SORT_DESC, $resultArr); $outTable = "{\"tags\":["; $firstT = true; foreach ($resultArr as $typeKey => $typeVal) { if ($firstT == false) { $outTable = "$outTable,"; } $firstT = false; $outTable = "$outTable{\"text\":\"$typeVal\",\"relevance\":\"".$relevanceArr[$typeKey]."\"}"; } $outTable = "$outTable]}"; //header('Content-type: application/json'); echo "$outTable\n"; } function AlchemyAPI_Setup() { if (function_exists('add_meta_box')) { $mode = 'normal'; $guiMode = get_option('alchemyapi-gui-mode'); if (strlen($guiMode) > 0 && $guiMode == "advanced") { $mode = 'advanced'; } $priority = 'low'; $replaceGui = get_option('alchemyapi-replace-tag-gui'); if (strlen($replaceGui) < 1 || $replaceGui == "true") { $priority = 'high'; } add_meta_box('alchemyapi', 'AlchemyTagger', 'AlchemyAPI_GUI', 'post', $mode, $priority); } else { add_action('dbx_post_sidebar', 'AlchemyAPI_GUI', 1); } add_submenu_page('plugins.php', 'AlchemyTagger Configuration', 'AlchemyTagger Configuration', 'manage_options', 'alchemytagger-config', 'AlchemyAPI_Config'); } function AlchemyAPI_Config() { if (isset($_POST['alchemyapi-key'])) { $thisKey = $_POST['alchemyapi-key']; $thisKey = trim($thisKey); update_option('alchemyapi-key', $thisKey); if (isset($_POST['alchemyapi-gui-mode'])) { update_option('alchemyapi-gui-mode', $_POST['alchemyapi-gui-mode']); } else { update_option('alchemyapi-gui-mode', 'normal'); } if (isset($_POST['alchemyapi-replace-tag-gui'])) { update_option('alchemyapi-replace-tag-gui', $_POST['alchemyapi-replace-tag-gui']); } else { update_option('alchemyapi-replace-tag-gui', 'false'); } if (isset($_POST['alchemyapi-disamb-enabled'])) { update_option('alchemyapi-disamb-enabled', $_POST['alchemyapi-disamb-enabled']); } else { update_option('alchemyapi-disamb-enabled', 'false'); } if (isset($_POST['alchemyapi-extract-mode'])) { update_option('alchemyapi-extract-mode', $_POST['alchemyapi-extract-mode']); } else { update_option('alchemyapi-extract-mode', 'both'); } if (isset($_POST['alchemyapi-strict-keywords'])) { update_option('alchemyapi-strict-keywords', $_POST['alchemyapi-strict-keywords']); } else { update_option('alchemyapi-strict-keywords', 'false'); } if (isset($_POST['alchemyapi-async-enabled'])) { update_option('alchemyapi-async-enabled', $_POST['alchemyapi-async-enabled']); } else { update_option('alchemyapi-async-enabled', 'false'); } if (isset($_POST['alchemyapi-async-timeout'])) { $timeout = $_POST['alchemyapi-async-timeout']; $asyncTimeout = (int)$_POST['alchemyapi-async-timeout']; if ($asyncTimeout < 10) { $timeout = "10"; } if ($asyncTimeout > 120) { $timeout = "120"; } update_option('alchemyapi-async-timeout', $timeout); } else { update_option('alchemyapi-async-timeout', '10'); } } ?>