// Check status from Semantria service if ($status == 202) { echo "Document ", $doc["id"], " queued successfully.", "\r\n"; } } // Count of the sample documents which need to be processed on Semantria $length = count($initialTexts); $results = array(); while (count($results) < $length) { echo "Please wait 10 sec for documents ...", "\r\n"; // As Semantria isn't real-time solution you need to wait some time before getting of the processed results // In real application here can be implemented two separate jobs, one for queuing of source data another one for retreiving // Wait ten seconds while Semantria process queued document sleep(20); // Requests processed results from Semantria service $status = $session->getProcessedDocuments(); // Check status from Semantria service if (is_array($status)) { $results = array_merge($results, $status); } echo count($status), " documents received successfully.", "\r\n"; } $analyzedArray = array(); $analyzedArray1 = array(); echo "<pre>"; print_r($results); echo "</pre>"; foreach ($results as $data) { // Printing of document sentiment score $analyzedArray[] = array('id' => $data["id"], 'score' => $data["sentiment_score"], 'themes' => $data["themes"]); //echo "Document ", $data["id"], " Sentiment score: ", $data["sentiment_score"], "\r\n";
function analyzeProjectData($initialTexts) { // Initializes new session with the serializer object and the keys. $session = new \Semantria\Session(CONSUMER_KEY, CONSUMER_SECRET, NULL, NULL, TRUE); // Initialize session callback handler $callback = new SessionCallbackHandler(); $session->setCallbackHandler($callback); foreach ($initialTexts as $text) { // Creates a sample document which need to be processed on Semantria // Unique document ID // Source text which need to be processed if ($text["comments"] != null && $text["comments"] != "") { $doc = array("id" => $text["id"] . "____" . $text["project_id"], "text" => $text["comments"]); // Queues document for processing on Semantria service $status = $session->queueDocument($doc); // Check status from Semantria service if ($status == 202) { echo "Document ", $doc["id"], " queued successfully.", "\r\n"; } } } // Count of the sample documents which need to be processed on Semantria $length = count($initialTexts); $results = array(); $sementriaTimes = 0; //while (count($results) < $length) { echo "Please wait 10 sec for documents ...", "\r\n"; // As Semantria isn't real-time solution you need to wait some time before getting of the processed results // In real application here can be implemented two separate jobs, one for queuing of source data another one for retreiving // Wait ten seconds while Semantria process queued document sleep(10); $sementriaTimes .= $sementriaTimes + 1; // Requests processed results from Semantria service $status = $session->getProcessedDocuments(); // Check status from Semantria service if (is_array($status)) { $results = array_merge($results, $status); } if ($sementriaTimes > 2) { $length = count($results); } echo count($status), " documents received successfully.", "\r\n"; //} $analyzedArray = array(); foreach ($results as $data) { // Printing of document sentiment score //echo "Document ", $data["id"], " Sentiment score: ", $data["sentiment_score"], "\r\n"; $analyzedArray[] = array('id' => $data["id"], 'score' => $data["sentiment_score"]); } return $analyzedArray; }
} } } if ($autoresponse_config == NULL) { $session->addConfigurations(array(array('name' => 'AutoResponseTest', 'language' => 'English', 'is_primary' => TRUE, 'auto_response' => TRUE))); } else { $autoresponse_config['is_primary'] = TRUE; $session->updateConfigurations(array($autoresponse_config)); } // Queues documents for analysis one by one $doc_counter = 0; foreach ($documents as $doc) { $session->queueDocument(array('id' => uniqid('', TRUE), 'text' => $doc)); $doc_counter += 1; usleep(100000); $results_len = count($results); print "Documents queued/received rate: {$doc_counter}/{$results_len}\n"; } // The final call to get remained data from server, Just for demo purposes. sleep(1); while (count($results) < count($documents)) { $result = $session->getProcessedDocuments(); foreach ($result as $item) { $results[] = $item; } usleep(500000); } $results_len = count($results); print "Documents queued/received rate: {$total_docs}/{$results_len}\n"; // Sets original primary configuration back after the test. $session->updateConfigurations(array($primary_config));
if ($res == 200 || $res == 202) { print "{$docsCount} documents queued successfully.\n"; } else { die("Unexpected error.\n"); } } print "\n"; $results = array(); // As Semantria isn't real-time solution you need to wait some time // before getting of the processed results. // In real application here can be implemented two separate jobs, // one for queuing of source data another one for retreiving. while (count(array_filter($tracker, 'queuedComparator'))) { usleep(500000); print "Retrieving your processed results...\n"; $response = $session->getProcessedDocuments(); foreach ($response as $item) { if (array_key_exists($item['id'], $tracker)) { $tracker[$item['id']] = $item['status']; $results[] = $item; } } } print "\n"; foreach ($results as $data) { // Printing of document sentiment score echo "Document ", $data["id"], " Sentiment score: ", $data["sentiment_score"], "\r\n"; // Printing of document themes echo "Document themes:", "\r\n"; if ($data["themes"]) { foreach ($data["themes"] as $theme) {