Author Topic: [MOD] Comment Sentence Normalizer  (Read 3952 times)

0 Members and 1 Guest are viewing this topic.

Offline kunik1962

  • Newbie
  • *
  • Posts: 15
    • View Profile
[MOD] Comment Sentence Normalizer
« on: August 27, 2010, 08:21:35 AM »
This MOD will examine comments text (NOT the TITLE) and then NORMALIZE the sentences after the user hits submit.  It basically cleans up ugly comments as they are posted by users.  I have a few users that insist on always posting their comments in ALL CAPS and this does the trick of cleaning it up.

Features:
1- Removes duplicated question marks, periods, and exclamation marks
2- Capitalize first letter of a sentence.
3- Removes all other CAPS
4- Splits sentences not only with "." but also with "?" and "!"
5- Puts a white space at the end of each sentence
6- Retains newlines

Works with 4images v1.78

Now, I do not claim ownership of this as I do not consider myself a PHP programmer and credit for the actual function needs to go to someone named ALEX that provided the code in the comment section of php.net online manual and in particular the section about the string function ucwords   (see:  http://www.php.net/manual/en/function.ucwords.php)  Also I cannot guarantee how this would work with other languages besides English.  Obviously this can be improved upon for performance or to handle other exceptions or times when text should be in caps.

Anyway here it goes:

--------- [Changed Files] ----------
details.php

STEP 1:  open details.php

Find:
Code: [Select]
//-----------------------------------------------------
//--- Save Comment ------------------------------------
//-----------------------------------------------------

Insert below
Code: [Select]
// define the Sentence Normalizer Function

function sentenceNormalizer($sentence_split) {
    $sentence_split = preg_replace(array('/[!]+/','/[?]+/','/[.]+/'),
                                   array('!','?','.'),$sentence_split);       
   
    $textbad = preg_split("/(\!|\.|\?|\n)/", $sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
    $newtext = array();
    $count = sizeof($textbad);
   
    foreach($textbad as $key => $string) {
        if (!empty($string)) {
            $text = trim($string, ' ');
            $size = strlen($text);
           
            if ($size > 1){     
                $newtext[] = ucfirst(strtolower($text));
            }
                elseif ($size == 1) {
                    $newtext[] = ($text == "\n") ? $text : $text . ' ';
                }     
        }
    }
   
    return implode($newtext);
}

STEP 1.1
Find:
Code: [Select]
$comment_text = un_htmlspecialchars(trim($HTTP_POST_VARS['comment_text']));
Insert below:
Code: [Select]
// Sentence Normalizer
    $comment_text_new = sentenceNormalizer($comment_text);
    $comment_text = $comment_text_new;

That's It!

Offline Sunny C.

  • Addicted member
  • ******
  • Posts: 1.806
  • I ♥ 4I
    • View Profile
Re: [MOD] Comment Sentence Normalizer
« Reply #1 on: December 04, 2013, 05:37:31 AM »
TOP!