From 2f18407eae11e1d3983490992de21c730bbc98f2 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Sep 2015 12:29:06 +0200 Subject: [PATCH 1/4] Cleanup implementation of langfilter_prepare_body, and fix a typo --- langfilter/langfilter.php | 69 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/langfilter/langfilter.php b/langfilter/langfilter.php index f7ccd0a6..d9d35dbe 100644 --- a/langfilter/langfilter.php +++ b/langfilter/langfilter.php @@ -43,7 +43,7 @@ function langfilter_addon_settings(&$a,&$s) { '$title' => t("Language Filter"), '$intro' => t ('This addon tries to identify the language of a postings. If it does not match any language spoken by you (see below) the posting will be collapsed. Remember detecting the language is not perfect, especially with short postings.'), '$enabled' => array('langfilter_enable', t('Use the language filter'), $enable_checked, ''), - '$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations for languages you speak, comma seperated. For excample "de,it".') ), + '$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For excample "de,it".') ), '$submit' => t('Save Settings'), )); @@ -63,55 +63,56 @@ function langfilter_addon_settings_post(&$a,&$b) { $enable = ((x($_POST,'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0); $disable = 1-$enable; set_pconfig(local_user(),'langfilter','disable', $disable); + set_pconfig(local_user(),'langfilter','minconfidence', $disable); info( t('Language Filter Settings saved.') . EOL); } } /* Actually filter postings by their language * 1st check if the user wants to filter postings * 2nd get the user settings which languages shall be not filtered out - * 3rd determine the language of a posting + * 3rd extract the language of a posting * 4th if the determined language does not fit to the spoken languages * of the user, then collapse the posting, but provide a link to * expand it again. */ function langfilter_prepare_body(&$a,&$b) { - if(get_pconfig(local_user(),'langfilter','disable')) - return; - # Never filter own messages - # TODO: find a better way to extract this - $logged_user_profile = $a->config['system']['url'] . '/profile/' . $a->user['nickname']; - if ( $logged_user_profile == $b['item']['author-link'] ) return; + $logged_user = local_user(); + if ( ! $logged_user ) return; - if(local_user()) { - $langs = get_pconfig(local_user(),'langfilter','languages'); - } - if($langs) { - $arr = explode(',',$langs); - } else { - return; - } + # Never filter own messages + # TODO: find a better way to extract this + $logged_user_profile = $a->config['system']['url'] . '/profile/' . $a->user['nickname']; + if ( $logged_user_profile == $b['item']['author-link'] ) return; - $found = false; + # Don't filter if language filter is disabled + if( get_pconfig($logged_user,'langfilter','disable') ) return; + $spoken_config = get_pconfig(local_user(),'langfilter','languages'); + + # Don't filter if no spoken languages are configured + if ( ! $spoken_config ) return; + $spoken_languages = explode(',', $spoken_config); + + # Extract the language of the post $opts = $b['item']['postopts']; - if ( $opts ) { - if ( preg_match('/^lang=([^;]*)/', $opts, $matches ) ) - { - $lang = $matches[1]; - $lng = Text_LanguageDetect_ISO639::nameToCode2($lang); - } - } - if ($lng==null) - return; - if (! in_array($lng, $arr)) - $found = true; - if ($lng==null) - $found = false; + if ( ! $opts ) return; # no options associated to post + if ( ! preg_match('/\blang=([^;]*);([^:]*)/', $opts, $matches ) ) + return; # no lang options associated to post - if($found) { - $rnd = random_string(8); - $b['html'] = ''; - } + $lang = $matches[1]; + $confidence = $matches[2]; + + # TODO: accept a confidence threshold in settings + + $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang); + + if ( ! $iso2 ) return; + $spoken = in_array($iso2, $spoken_languages); + + if( ! $spoken ) { + $rnd = random_string(8); + $b['html'] = ''; + } } ?> From 05e02aae8b5474cb18a03de3dfb65a0af03bdc15 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Sep 2015 12:40:48 +0200 Subject: [PATCH 2/4] Prepare to filter by min confidence, and fix more typos --- langfilter/langfilter.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/langfilter/langfilter.php b/langfilter/langfilter.php index d9d35dbe..7adec235 100644 --- a/langfilter/langfilter.php +++ b/langfilter/langfilter.php @@ -43,7 +43,7 @@ function langfilter_addon_settings(&$a,&$s) { '$title' => t("Language Filter"), '$intro' => t ('This addon tries to identify the language of a postings. If it does not match any language spoken by you (see below) the posting will be collapsed. Remember detecting the language is not perfect, especially with short postings.'), '$enabled' => array('langfilter_enable', t('Use the language filter'), $enable_checked, ''), - '$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For excample "de,it".') ), + '$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For example "de,it".') ), '$submit' => t('Save Settings'), )); @@ -63,7 +63,8 @@ function langfilter_addon_settings_post(&$a,&$b) { $enable = ((x($_POST,'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0); $disable = 1-$enable; set_pconfig(local_user(),'langfilter','disable', $disable); - set_pconfig(local_user(),'langfilter','minconfidence', $disable); + $confidence = ((x($_POST,'langfilter_minconfidence')) ? intval($_POST['langfilter_minconfidence']) : 0); + set_pconfig(local_user(),'langfilter','minconfidence', $confidence); info( t('Language Filter Settings saved.') . EOL); } } @@ -89,6 +90,7 @@ function langfilter_prepare_body(&$a,&$b) { if( get_pconfig($logged_user,'langfilter','disable') ) return; $spoken_config = get_pconfig(local_user(),'langfilter','languages'); + $minconfidence = get_pconfig(local_user(),'langfilter','minconfidence'); # Don't filter if no spoken languages are configured if ( ! $spoken_config ) return; @@ -103,7 +105,8 @@ function langfilter_prepare_body(&$a,&$b) { $lang = $matches[1]; $confidence = $matches[2]; - # TODO: accept a confidence threshold in settings + # Do not filter if language detection confidence is too low + if ( $minconfidence && $confidence < $minconfidence ) return; $iso2 = Text_LanguageDetect_ISO639::nameToCode2($lang); From 6d2af1878afe5eb15b5349b9d9d25d9399d0c1e8 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Sep 2015 13:07:04 +0200 Subject: [PATCH 3/4] Add min confidence parameter to language filter --- langfilter/langfilter.php | 9 +++++++-- langfilter/templates/settings.tpl | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/langfilter/langfilter.php b/langfilter/langfilter.php index 7adec235..b29084c8 100644 --- a/langfilter/langfilter.php +++ b/langfilter/langfilter.php @@ -35,6 +35,7 @@ function langfilter_addon_settings(&$a,&$s) { $enable_checked = (intval(get_pconfig(local_user(),'langfilter','disable')) ? '' : ' checked="checked" '); $languages = get_pconfig(local_user(),'langfilter','languages'); + $minconfidence = get_pconfig(local_user(),'langfilter','minconfidence')*100; if(! $languages) $languages = 'en,de,fr,it,es'; @@ -44,6 +45,7 @@ function langfilter_addon_settings(&$a,&$s) { '$intro' => t ('This addon tries to identify the language of a postings. If it does not match any language spoken by you (see below) the posting will be collapsed. Remember detecting the language is not perfect, especially with short postings.'), '$enabled' => array('langfilter_enable', t('Use the language filter'), $enable_checked, ''), '$languages' => array('langfilter_languages', t('I speak'), $languages, t('List of abbreviations (iso2 codes) for languages you speak, comma separated. For example "de,it".') ), + '$minconfidence' => array('langfilter_minconfidence', t('Minimum confidence in language detection'), $minconfidence, t('Minimum confidence in language detection being correct, from 0 to 100. Posts will not be filtered when the confidence of language detection is below this percent value.') ), '$submit' => t('Save Settings'), )); @@ -63,8 +65,11 @@ function langfilter_addon_settings_post(&$a,&$b) { $enable = ((x($_POST,'langfilter_enable')) ? intval($_POST['langfilter_enable']) : 0); $disable = 1-$enable; set_pconfig(local_user(),'langfilter','disable', $disable); - $confidence = ((x($_POST,'langfilter_minconfidence')) ? intval($_POST['langfilter_minconfidence']) : 0); - set_pconfig(local_user(),'langfilter','minconfidence', $confidence); + $minconfidence = 0+$_POST['langfilter_minconfidence']; + if ( ! $minconfidence ) $minconfidence = 0; + else if ( $minconfidence < 0 ) $minconfidence = 0; + else if ( $minconfidence > 100 ) $minconfidence = 100; + set_pconfig(local_user(),'langfilter','minconfidence', $minconfidence/100.0); info( t('Language Filter Settings saved.') . EOL); } } diff --git a/langfilter/templates/settings.tpl b/langfilter/templates/settings.tpl index 0597d755..5d1fb25f 100644 --- a/langfilter/templates/settings.tpl +++ b/langfilter/templates/settings.tpl @@ -10,6 +10,7 @@

{{$intro}}

{{include file="field_checkbox.tpl" field=$enabled}} {{include file="field_input.tpl" field=$languages}} + {{include file="field_input.tpl" field=$minconfidence}}
From 72cb338fbbe5eb4e523a4275f94985ccfbeca9cd Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Sep 2015 15:18:47 +0200 Subject: [PATCH 4/4] Update translation file --- langfilter/lang/C/messages.po | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/langfilter/lang/C/messages.po b/langfilter/lang/C/messages.po index a53bb0ba..e4995ec9 100644 --- a/langfilter/lang/C/messages.po +++ b/langfilter/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2015-07-24 19:11+0200\n" +"POT-Creation-Date: 2015-09-22 15:18+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,11 +17,11 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: langfilter.php:43 +#: langfilter.php:44 msgid "Language Filter" msgstr "" -#: langfilter.php:44 +#: langfilter.php:45 msgid "" "This addon tries to identify the language of a postings. If it does not " "match any language spoken by you (see below) the posting will be collapsed. " @@ -29,29 +29,40 @@ msgid "" "postings." msgstr "" -#: langfilter.php:45 +#: langfilter.php:46 msgid "Use the language filter" msgstr "" -#: langfilter.php:46 +#: langfilter.php:47 msgid "I speak" msgstr "" -#: langfilter.php:46 +#: langfilter.php:47 msgid "" -"List of abbreviations for languages you speak, comma seperated. For excample " -"\"de,it\"." +"List of abbreviations (iso2 codes) for languages you speak, comma separated. " +"For example \"de,it\"." msgstr "" -#: langfilter.php:47 +#: langfilter.php:48 +msgid "Minimum confidence in language detection" +msgstr "" + +#: langfilter.php:48 +msgid "" +"Minimum confidence in language detection being correct, from 0 to 100. Posts " +"will not be filtered when the confidence of language detection is below this " +"percent value." +msgstr "" + +#: langfilter.php:49 msgid "Save Settings" msgstr "" -#: langfilter.php:66 +#: langfilter.php:73 msgid "Language Filter Settings saved." msgstr "" -#: langfilter.php:105 +#: langfilter.php:123 #, php-format msgid "unspoken language %s - Click to open/close" msgstr ""