Groovy web console

subscribe to the feed Subscribe
to this
site
Aide ?la conjugaison du pr?utass (via #groovywebconsole)
tweet this snippet Tweet
this
script

Aide ?la conjugaison du pr?utass

Published 6 months ago by Fabian Bouton with tags Pr?utass? conjugaison francais
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.lang.Math;

public class Main {

    public static void main(String[] args) {
        test()    

        //def verb = args[1]?.toLowerCase()
        //def subject = args[0]?.toUpperCase()

        //println Grammar.conjugate(subject, verb)
    }

    public static void test() {
        def verbs = ["?re","avoir", "dire", "aller", 
        "allier", "pallier", "ch?ier", "plier", "plagier", "lier", "scier",
        "chuchotter", "zozoter",
        "p?ir", "sentir", "subir", "venir", "jouir", "polir", "lire", "tenir",
        "rire", "vivre",  
        "aimer", "m?er", "r?er", "violer", "alpaguer", "glacer", "passer",
        "mettre", "pendre", "fuire", "moudre", "taire", "connaitre",
        "pouvoir","voir", "choir", "devoir"]

        verbs.each { verb ->
            println "--- ${verb.capitalize()} ---"
            ["je","tu","il","nous","vous", "ils"].each { 
                println Grammar.conjugate(it, verb)
            }
            println "\n"
        }
    }
}

/**
* Aide ?la conjugaison au pr?utass?
*
* R?les appliqu?s pour l'obention des racines des verbes :
* On enl?e 'er' des verbes se terminant par 'er'. Ex : 'Mener' => 'Men'
* On enl?e 'oir' des verbes se terminant par 'oir'. Ex : 'Avoir' => 'Av'
* On enl?e 'e' des verbes se terminant par 're'. Ex : 'Prendre' => 'Prendr'
* On enl?e 'r' des verbes se terminant par 'ir'. Ex : 'Polir' => 'Poli'
*
* On ajoute les radicaux selon la personne :
* Je "eusai" Ex : Oser, os + eusai = J'oseusai
* Tu "eusas" Ex : Prendre, prendr + eusas = Tu prendreusas
* Il "eutat" Ex : Choir, ch + eutat = Il cheutat
* Nous "?es" Ex : Paumer, paum + ?es = Nous paum?es
* Vous "?ez" Ex : H?er, h? + ?ez = Vous h??er
* Ils "?ent" Ex : Tisser, Tiss + ?ent = Ils tiss?ent
*
* Seule exception
* Si la racine obtenue se termine par la lettre "i", on supprime les lettres "eu" des radicaux du singulier
* Je "sai" Ex : Vomir, vomi + sai = Je vomisai
* Tu "sas" Ex : Soutenir, souteni + sas = Tu soutenisas
* Il "tat" Ex : Jouir, joui + tat = Il jouitat
*
**/
public class Grammar {

    public enum SubjectEnum {

        JE, TU, IL, NOUS, VOUS, ILS

        static def getEnding = { subject ->
            switch (subject?.toUpperCase() as SubjectEnum) {
                case JE:
                   "eusai"
                break
                case TU:
                    "eusas"
                break
                case IL:
                    "eutat"
                break
                case NOUS:
                    "?es"
                break
                case VOUS:
                    "?ez"
                break
                case ILS:
                    "?ent"
                break
                default:
                    ""
                break
            }
        }    
    }

    static def conjugate = { subject, verb ->
        def ending = SubjectEnum.getEnding(subject)
        def stem = Grammar.extractStem(verb)
        def conjugated = stem.endsWith('ir')
            ? "${stem.replaceAll(/r$/,'')}${ending.replaceAll(/^eu/,'')}"
            : "${stem}${ending}"
        return "${subject.capitalize()} ${conjugated}"
    }

    static def extractStem = { verb ->
            switch (verb?.toLowerCase()) {
                case ~/.*oir$/:
                   verb.replaceAll(/oir$/,'')
                break
                case ~/.*ir$/:
                   // If verb ends by 'ir', the ending has to be speficic
                   verb
                break
                case ~/.*ier$/:
                    // If verb ends by 'ier', removing only the last 'er'
                    verb.replaceAll(/er$/,'')
                break
                case ~/.*er$/:
                    verb.replaceAll(/er$/,'')
                break
                case ~/.*re$/:
                    // If verb ends by 're', removing only the last 'e'
                    verb.replaceAll(/e$/,'')
                break
                default:
                    verb
                break
            }
    }
    
}