PHP simple past & past participle English auto verb generation

PHP simple past & past participle English auto verb generation

PHP simple past & past participle auto-generation for English verbs forms. We can use this script/code in a WordPress plugin.
Natural language processing (NLP) – PHP – WordPress – Plugin

Direct Downloads

Download English irregular verbs in the flowing formats

Introduction to PHP simple past & past participle auto generation

In this tip, we will discuss how to generate English verb forms using the PHP programming language. We can use the resulting code 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 participles. 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 the 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 a 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 a vowel + consonant, add “-ed” only. For example, “offer + ed = offered”.

past tense examples

  • past tense of ski: skied
  • past tense of glow: glowed
  • past tense of spit: spat

Suggested PHP simple past & past participle auto generation code

// 1st function of PHP simple past & past participle auto generation script
// it will gernate the verb form form its infinitive form
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;
}

//##################################
// 2nd function of PHP simple past & past participle auto generation script
// it will count the syllable in an English word
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;
}
//######################
// 3rd function of PHP simple past & past participle auto generation script
// it will count the syllable in an English word
// it is a simple version of the 2nd function
function syllable_count_full( 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;
    $aeoui = array( 'a', 'e', 'i', 'o', 'u' ); //y not included
    //y only at end is vowal
    
    $first = $word[ 0 ];
    if ( in_array( $first, $aeoui ) )$syllables++;
    for ( $i = 1; $i < $len; $i++ ) { if ( in_array( $word[ $i ], $aeoui ) && in_array( $word[ $i - 1 ], $aeoui ) )$syllables++; } $last = $word[ $i - 1 ]; $last2 = substr( $str_word, -2 ); $last3 = substr( $str_word, -3 ); #2) if doesn't end with "ted" or "tes" or "ses" or "ied" or "ies", discard "es" and "ed" at the end. # if it has only 1 vowel or 1 set of consecutive vowels, discard. (like "speed", "fled" etc.) if ( ( $syllables > 1 ) &&
        ( ( $last2 == 'es' ) || ( $last2 == 'ed' ) )
    ) {
        if ( !in_array( $last3, array( 'ted', 'tes', 'ied', 'ies' ) ) )
            $syllables--;
    }

    #3) discard trailing "e", except where ending is "le" 
    $le_except = array(
        'whole', 'mobile', 'pole', 'male', 'female',
        'hale', 'pale', 'tale', 'sale', 'aisle',
        'whale', 'while' );
    if ( in_array( $word, $le_except ) ) {
        $syllables--;
    } else if ( $last2 == 'le' ) {
        //
    } else if ( $last == 'e' ) {
        $syllables--;
    }


    $first2 = substr( $str_word, 0, 2 );

    #6) add one if starts with "mc"
    if ( $first2 == 'mc' )$syllables++;

    #7) add one if ends with "y" but is not surrouned by vowel
    if ( $last == 'y' && !in_array( $word[ $i - 2 ], $aeoui ) ) {
        $syllables++;
    }

    #9) if starts with "tri-" or "bi-" and is followed by a vowel, add one.
    if ( $last3 == "tri" && in_array( $word[ 3 ], $aeoui ) )$syllables++;
    if ( $last2 == "bi" && in_array( $word[ 3 ], $aeoui ) )$syllables++;

    #10) if ends with "-ian", should be counted as two syllables, except for "-tian" and "-cian"
    if ( $last3 == 'ain' ) {
        $last4 = substr( $str_word, -4 );
        if ( ( $last4 != "cian" ) && ( $last4 != "tian" ) )$syllables++;
    }

    #11) if starts with "co-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.    
    if ( $first2 == 'co' ) {
        $co_one = array(
            'cool', 'coach', 'coat',
            'coal', 'count', 'coin', 'coarse',
            'coup', 'coif', 'cook', 'coign', 'coiffe',
            'coof', 'court' );
        $co_two = array( 'coapt', 'coed', 'coinci' );
        $first4 = substr( $str_word, 0, 4 );
        $first5 = '';
        $first6 = '';
        if ( $len > 4 )$first5 = substr( $str_word, 0, 5 );
        if ( $len > 5 )$first6 = substr( $str_word, 0, 6 );
        if ( in_array( $first4, $co_two ) ||
            in_array( $first5, $co_two ) ||
            in_array( $first6, $co_two ) ) {
            //
            $syllables++;
        } else if ( in_array( $first4, $co_one ) ||
            in_array( $first5, $co_one ) ||
            in_array( $first6, $co_one ) ) {
            //pass
        } else {
            $syllables++;
        }
    }
    //
    $first3 = substr( $str_word, 0, 3 );

    #12) if starts with "pre-" and is followed by a vowel, check if exists in the double syllable dictionary, if not, check if in single dictionary and act accordingly.
    if ( $first3 == "pre" && in_array( $word[ 3 ], $aeoui ) &&
        ( strpos( $str_word, 'preach' ) === false ) ) {
        $syllables++;
    }

    #13) check for "-n't" and cross match with dictionary to add syllable.
    $negative = array( "doesn't", "isn't", "shouldn't",
        "couldn't", "wouldn't" );
    if ( ( $last3 == "n't" ) && in_array( $str_word, $negative ) )$syllables++;

    #14) Handling the exceptional words.
    $exception_add = array( 'serious', 'crucial' );
    $exception_del = array( 'fortunately', 'unfortunately' );
    if ( in_array( $str_word, $exception_del ) )$syllables--;
    if ( in_array( $str_word, $exception_add ) )$syllables++;

    if ( $syllables == 0 )$syllables++;
    return $syllables;
}
// end of the PHP simple past & past participle auto generation script

Useful links related to PHP simple past & past participle auto generation code

end of the article PHP simple past & past participle auto generation

totop