Click here to Skip to main content
15,881,092 members
Articles / Hosted Services / WordPress
Tip/Trick

English Simple Past & Past Participle PHP Auto Generation and Syllable Count in English Word

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
26 Jan 2021CPOL1 min read 5.3K   3
English Simple Past & Past Participle PHP Auto Generation and Syllable Count in English Word
In this tip, we will discuss how to generate English verb forms using the PHP programming language. The resulting code can be used in the WordPress plugin.

Verbs Forms in English

Verbs in English are with forms: infinitive, simple past, and past participle. Add "ed" to the end of the regular verbs to generate simple past and past participle. The matter is not that simple.

Forms Cases

  • If the verb ends in "e", add only "d." For example, "arrive + d = arrived".
  • If the verb ends in consonant + "y," change the "y" to "i" and add "-ed". For example, "study + ed = studied".
  • If a verb ends in vowel + "y," add "-ed". For example, "play + ed = played".
  • If a one-syllable verb ends in vowel + consonant, double the consonant. For example, "stop + p + ed = stopped".
  • If the stress is on the final syllable of a verb that ends in vowel + consonant, double the consonant. For example, "prefer + r + ed = preferred".
  • If the stress is not on the final syllable of a verb that ends in vowel + consonant, add "-ed" only. For example, "óffer + ed = offered".

Suggested PHP Code

If you have any comments, please share in the comments below:

PHP
function english_verb_forms( string $infinitive ) {    
    $infinitive = trim( strtolower( $infinitive ) );
    if ( !$infinitive ) return ''; 
    #1 english irregular verbs
    $sql = "SELECT simple_past, past_participle, 
    regular, form_case FROM english_verbs WHERE 
    infinitive = '$infinitive'";
    global $wpdb;
    $verb = $wpdb->get_row( $sql, ARRAY_A );
    if ( $verb ) return $verb;    
    $verb = array();
    $verb['regular'] = 'regular' ;
    $last = substr( $infinitive, -1 );
    #2 infinitive ends with e
    if ( $last == 'e' ) {
        $verb['simple_past'] = $infinitive . 'd';
        $verb['past_participle'] = $verb['simple_past'];
        $verb['form_case'] = 'e' ;        
        return $verb;
    }
    #3 and #4 infinitive ends with y
    if ( $last == 'y' ) {
        $last2 = substr( $infinitive, -2, 1 );
        #3 infinitive ends with vowel + y
        if ( strpos( ' aeuio', $last2 ) === false ) {
            $i = strlen( $infinitive ) - 2;
            $verb['simple_past'] = substr( $infinitive, 0, $i ) . 'ied';
            $verb['past_participle'] = $verb['simple_past'];
            $verb['form_case'] = 'vowel + y' ;        
            return $verb;
        } else {
            #4 infinitive ends with consonant + y
            $verb['simple_past'] = $infinitive . 'ed';
            $verb['past_participle'] = $verb['simple_past'];
            $verb['form_case'] = 'consonant + y' ;        
            return $verb;            
        }
    }
    #5 infinitive ends with vowel
    if ( strpos( ' aeuiow', $last ) !== false ) {
        $verb['simple_past'] = $infinitive . 'ed';
        $verb['past_participle'] = $verb['simple_past'];
        $verb['form_case'] = '' ;        
        return $verb;            
    }
    #infinitive ends with vowel
    $syllables = syllable_count( $infinitive );
    if ( !$syllables ) {
        $verb['simple_past'] = $infinitive . 'ed';
        $verb['past_participle'] = $verb['simple_past'];
        $verb['form_case'] = '' ;        
        return $verb;                    
    }
    if ( $syllables == 1 ) {
        #5 infinitive has single syllable and ends with consonant
        $verb['simple_past'] = $infinitive . $last . 'ed';
        $verb['past_participle'] = $verb['simple_past'];
        $verb['form_case'] = 'double last' ;        
        return $verb;                    
    }
    #other cases
    return false;
}


//##################
function syllable_count( string $str_word ) {
    /* Count syllables in a single word, simple version */
    $str_word = trim( strtolower( $str_word ) );
    if ( !$str_word ) return 0;
    if ( strpos( $str_word, ' ' ) ) return 0;
    $len = strlen( $str_word );
    #1
    if ( $len <= 3 ) return 1;
    
    $word = str_split( $str_word );    
    $syllables = 0;
    $vowels = array( 'a', 'e', 'i', 'o', 'u', 'y' );
    $first = $word[ 0 ];
    if ( in_array( $first, $vowels ) )$syllables++;
    for ( $i = 1; $i < count( $word ); $i++ ) {
        if ( in_array( $word[ $i ], $vowels ) &&
            in_array( $word[ $i - 1 ], $vowels ) )$syllables++;
    }
    if ( substr( $str_word, -1 ) == 'e' )$syllables--;
    if ( $syllables == 0 )$syllables++;
    return $syllables;
}
//######################

 

History

  • 26th January 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Lebanon Lebanon
ASP.Net Hosting on Linux server
---------------------------
There is a developer behind every piece of code!
DNA is too complex what about it!
No junk DNA; There is a functional role for noncoding DNA

Comments and Discussions

 
QuestionWhich plugin can be used? Pin
Member 1554520123-Feb-22 0:16
Member 1554520123-Feb-22 0:16 
AnswerRe: Which plugin can be used? Pin
NewPast24-Feb-22 8:58
NewPast24-Feb-22 8:58 
GeneralMy vote of 1 Pin
Bitbeisser27-Jan-21 22:38
Bitbeisser27-Jan-21 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.