/** * Checks the Language Confidence by trying to detect the correct language for a text using the LangId.Py Service. * If LangId.Py is not set in configuration as it may not be available will return a negative confidence score and the default language: en. * * @param STRING $the_text :: The text to be checked * @return ARRAY :: The LangId.Py detection result: [ service-available, lang-id, confidence-score, error-message ] * */ public function getLanguageConfidence($the_text) { //-- if ((string) $this->langid_service_cfg['url'] == '') { return (array) $this->formatLanguageConfidenceAnswer('en', -1); // OK (LangID Service is not available ...) } //end if //-- $this->is_service_available = true; //-- $the_text = (string) trim((string) $the_text); if ((string) $the_text == '') { return (array) $this->formatLanguageConfidenceAnswer('en', -2, 'NOTICE: Empty Text to check ...'); } //end if //-- $http_client = new SmartHttpClient(); if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { $http_client->debug = 1; } //end if //-- $http_timeout = (int) $http_timeout; if ($http_timeout < 30) { $http_timeout = 30; } //end if if ($http_timeout > 300) { $http_timeout = 300; } //end if $http_client->connect_timeout = $http_timeout; //-- $http_client->postvars = array('q' => (string) $the_text); //-- $http_data = (array) $http_client->browse_url((string) $this->langid_service_cfg['url'], 'POST', (string) $this->langid_service_cfg['ssl'], (string) $this->langid_service_cfg['auth-user'], (string) $this->langid_service_cfg['auth-pass']); //-- if ((string) $http_data['code'] != '200') { return (array) $this->formatLanguageConfidenceAnswer('en', -3, 'Invalid HTTP Code != 200'); } //end if //-- $result = (array) Smart::json_decode($http_data['content']); //-- if ($result['responseStatus'] != 200) { return (array) $this->formatLanguageConfidenceAnswer('en', -4, 'Invalid (Json) Response Status != 200'); } //end if if (!is_array($result['responseData'])) { return (array) $this->formatLanguageConfidenceAnswer('en', -5, 'Invalid (Json) Response Data Array'); } //end if //-- $result['responseData']['language'] = (string) trim(strtolower((string) $result['responseData']['language'])); $result['responseData']['confidence'] = (double) $result['responseData']['confidence']; //-- if (strlen($result['responseData']['language']) != 2) { return (array) $this->formatLanguageConfidenceAnswer('en', -6, 'Invalid Language ID Length: ' . $result['responseData']['language']); } //end if //-- return (array) $this->formatLanguageConfidenceAnswer($result['responseData']['language'], $result['responseData']['confidence']); // OK //-- }