65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (2 votes)

Jan 26, 2021

CPOL
viewsIcon

5862

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

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:

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