Remove duplicated calls to language detection

Also avoids overriding existing "lang" specification in an item
This commit is contained in:
Sandro Santilli 2015-09-23 10:47:34 +02:00
parent 1c19bcd322
commit 2a2dd6a1b9
2 changed files with 45 additions and 50 deletions

View file

@ -1096,6 +1096,48 @@ function add_guid($item) {
dbesc($item["uri"]), dbesc($item["network"])); dbesc($item["uri"]), dbesc($item["network"]));
} }
// Adds a "lang" specification in a "postopts" element of given $arr,
// if possible and not already present.
// Expects "body" element to exist in $arr.
// TODO: add a parameter to request forcing override
function item_add_language_opt(&$arr) {
if (version_compare(PHP_VERSION, '5.3.0', '<')) return; // LanguageDetect.php not available ?
if ( $arr['postopts'] )
{
if ( strstr($arr['postopts'], 'lang=') )
{
// do not override
// TODO: add parameter to request overriding
return;
}
$postopts = $arr['postopts'];
}
else
{
$postopts = "";
}
require_once('library/langdet/Text/LanguageDetect.php');
$naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
$l = new Text_LanguageDetect;
//$lng = $l->detectConfidence($naked_body);
//$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
$lng = $l->detect($naked_body, 3);
if (sizeof($lng) > 0) {
if ($postopts) $postopts .= '^'; // arbitrary separator, to be reviewed
$postopts .= 'lang=';
$sep = "";
foreach ($lng as $language => $score) {
$postopts .= $sep . $language.";".$score;
$sep = ':';
}
$arr['postopts'] = $postopts;
}
}
function item_store($arr,$force_parent = false, $notify = false, $dontcache = false) { function item_store($arr,$force_parent = false, $notify = false, $dontcache = false) {
// If it is a posting where users should get notifications, then define it as wall posting // If it is a posting where users should get notifications, then define it as wall posting
@ -1185,29 +1227,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
//if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false)) //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
// $arr['body'] = strip_tags($arr['body']); // $arr['body'] = strip_tags($arr['body']);
item_add_language_opt($arr);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
require_once('library/langdet/Text/LanguageDetect.php');
$naked_body = preg_replace('/\[(.+?)\]/','',$arr['body']);
$l = new Text_LanguageDetect;
//$lng = $l->detectConfidence($naked_body);
//$arr['postopts'] = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
$lng = $l->detect($naked_body, 3);
if (sizeof($lng) > 0) {
$postopts = "";
foreach ($lng as $language => $score) {
if ($postopts == "")
$postopts = "lang=";
else
$postopts .= ":";
$postopts .= $language.";".$score;
}
$arr['postopts'] = $postopts;
}
}
$arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0); $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0);
$arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $arr['network'])); $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $arr['network']));

View file

@ -18,7 +18,6 @@
require_once('include/crypto.php'); require_once('include/crypto.php');
require_once('include/enotify.php'); require_once('include/enotify.php');
require_once('include/email.php'); require_once('include/email.php');
require_once('library/langdet/Text/LanguageDetect.php');
require_once('include/tags.php'); require_once('include/tags.php');
require_once('include/files.php'); require_once('include/files.php');
require_once('include/threads.php'); require_once('include/threads.php');
@ -268,32 +267,8 @@ function item_post(&$a) {
$guid = get_guid(32); $guid = get_guid(32);
$naked_body = preg_replace('/\[(.+?)\]/','',$body); item_add_language_opt($_REQUEST);
$postopts = $_REQUEST['postopts'] ? $_REQUEST['postopts'] : "";
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
$l = new Text_LanguageDetect;
//$lng = $l->detectConfidence($naked_body);
//$postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
$lng = $l->detect($naked_body, 3);
if (sizeof($lng) > 0) {
$postopts = "";
foreach ($lng as $language => $score) {
if ($postopts == "")
$postopts = "lang=";
else
$postopts .= ":";
$postopts .= $language.";".$score;
}
}
logger('mod_item: detect language' . print_r($lng,true) . $naked_body, LOGGER_DATA);
}
else
$postopts = '';
$private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0); $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);