IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Compter les syllabes d'un mot français


Sujet :

Langage Delphi

  1. #1
    Membre régulier
    Inscrit en
    Mars 2003
    Messages
    106
    Détails du profil
    Informations forums :
    Inscription : Mars 2003
    Messages : 106
    Points : 97
    Points
    97
    Par défaut Compter les syllabes d'un mot français
    Bonjour,

    Je voudrais savoir si quelqu'un a une fonction qui permette de compter les syllabes d'un mot?

    Merci

    David

  2. #2
    Membre émérite
    Avatar de hpalpha
    Inscrit en
    Mars 2002
    Messages
    769
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 769
    Points : 2 545
    Points
    2 545
    Par défaut
    Ca marche plutot bien meme avec des mots francais

    Il te faut adapter le code pour compter les syllabes

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
     
     procedure Syllabify(Syllables: TStringList; s: string);
      const
        Consonants   = ['b','B','c','C','d','D','f','F','g','G',
                'h','H','j','J','k','K','l','L','m','M','n','N',
                'ñ','Ñ','p','P','q','Q','r','R','s','S','t','T',
                'v','V','w','W','x','X','y','Y','z','Z'];
        StrongVowels = ['a','A','á','Á','e','E','é','É',
                        'í','Í','o','ó','O','Ó','ú','Ú'];
        WeakVowels   = ['i','I','u','U','ü','Ü'];
        Vowels       = StrongVowels + WeakVowels;
        Letters      = Vowels + Consonants;
      var
        i, j, n, m, hyphen: integer;
      begin
        j := 2;
        s := #0 + s + #0;
        n := Length(s) - 1;
        i := 2;
        Syllables.Clear;
        while i <= n do begin
          hyphen := 0; // Do not hyphenate
          if s[i] in Consonants then begin
            if s[i+1] in Vowels then begin
              if s[i-1] in Vowels then hyphen := 1;
            end else if (s[i] in ['s', 'S']) and (s[i-1] in ['n', 'N'])
                 and (s[i+1] in Consonants) then begin
              hyphen := 2;
            end else if (s[i+1] in Consonants) and
                        (s[i-1] in Vowels) then begin
              if s[i+1] in ['r','R'] then begin
                if s[i] in ['b','B','c','C','d','D','f','F','g',
                    'G','k','K','p','P','r','R','t','T','v','V']
                then hyphen := 1 else hyphen := 2;
              end else if s[i+1] in ['l','L'] then begin
                if s[i] in ['b','B','c','C','d','D','f','F','g',
                    'G','k','K','l','L','p','P','t','T','v','V']
                then hyphen := 1 else hyphen := 2;
              end else if s[i+1] in ['h', 'H'] then begin
                if s[i] in ['c', 'C', 's', 'S', 'p', 'P']
                then hyphen := 1 else hyphen := 2;
              end else
                hyphen := 2;
            end;
          end else if s[i] in StrongVowels then begin
            if (s[i-1] in StrongVowels) then hyphen := 1
          end else if s[i] = '-' then begin
            Syllables.Add(Copy(s, j, i - j));
            Syllables.Add('-');
            inc(i);
            j := i;
          end;
          if hyphen = 1 then begin  // Hyphenate here
            Syllables.Add(Copy(s, j, i - j));
            j := i;
          end else if hyphen = 2 then begin  // Hyphenate after
            inc(i);
            Syllables.Add(Copy(s, j, i - j));
            j := i;
          end;
          inc(i);
        end;
        m := Syllables.Count - 1;
        if (j = n) and (m >= 0) and (s[n] in Consonants) then
          Syllables[m] := Syllables[m] + s[n]   // Last letter
        else
          Syllables.Add(Copy(s, j, n - j + 1)); // Last syllable
      end;
     
     
    // To test the procedure yon can drop a Textbox and a Label on a form and
    //in the Change event of the Textbox write:
     
      procedure TForm1.Edit1Change(Sender: TObject);
      var
        Syllables: TStringList;
      begin
        Syllables := TStringList.Create;
        try
          Syllabify(Syllables, Edit1.Text);
          Label1.Caption := StringReplace(Trim(Syllables.Text),
            #13#10, '-', [rfReplaceAll]);
        finally
          Syllables.Free;
        end;
      end;
    Delphi 2009 - ZeosLib - DevExpress - TMS - PgDAC
    PostgreSQL 8.4 sous Debian
    Sites : http://postgresql.developpez.com http://dgriessinger.developpez.com

  3. #3
    Membre régulier
    Inscrit en
    Mars 2003
    Messages
    106
    Détails du profil
    Informations forums :
    Inscription : Mars 2003
    Messages : 106
    Points : 97
    Points
    97
    Par défaut
    Impec! Merci beaucoup hpalpha!

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2003
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2003
    Messages : 43
    Points : 45
    Points
    45
    Par défaut
    Génial ce code

    Merci aussi, même si j'ai rien demandé.

  5. #5
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2014
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2014
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Un version C#, pour ceux que ça intéresse

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    static class Phono
        {
            #region Constantes de lettres
            readonly static char[] consonants = new char[] {
                'B','C','D','F','G',
                'H','J','K','L','M','N',
                'Ñ','P','Q','R','S',
                'T','V','W','X','Y','Z'};
            readonly static char[] strongVowels = new char[] {
                'A','Á','E','É',
                'Í','O','Ó','Ú'};
            readonly static char[] weakVowels = new char[] { 'I', 'U', 'Ü' };
            readonly static char[] vowels = new char[] {
                'A','Á','E','É',
                'Í','O','Ó','Ú',
                'I','U','Ü'};
            readonly static char[] letters = new char[] {
                'A','Á','E','É',
                'Í','O','Ó','Ú',
                'I','U','Ü',
                'B','C','D','F','G',
                'H','J','K','L','M','N',
                'Ñ','P','Q','R','S',
                'T','V','W','X','Y','Z'};
            #endregion
     
            public static List<string> Syllabiser(string s)
            {
                int i, j, n, m;
                s = s.ToUpper();
                i = 1;
                j = 0;
                n = s.Length - 1;
                List<string> syllabes = new List<string>();
     
                while (i < n)
                {
                    int hyphen = 0;
                    if (consonants.Contains(s[i]))
                    {
                        if (vowels.Contains(s[i + 1]))
                        {
                            if (vowels.Contains(s[i - 1]))
                                hyphen = 1;
                        }
                        else if (s[i] == 'S' && s[i - 1] == 'N' && (consonants.Contains(s[i + 1])))
                            hyphen = 2;
                        else if (consonants.Contains(s[i + 1]) && vowels.Contains(s[i - 1]))
                            if (s[i + 1] == 'R')
                                if (s[i] == 'B' || s[i] == 'C' || s[i] == 'D' || s[i] == 'F' || s[i] == 'G' ||
                                    s[i] == 'K' || s[i] == 'P' || s[i] == 'R' || s[i] == 'T' || s[i] == 'V')
                                    hyphen = 1;
                                else
                                    hyphen = 2;
                            else if (s[i + 1] == 'L')
                                if (s[i] == 'B' || s[i] == 'C' || s[i] == 'D' || s[i] == 'F' || s[i] == 'G' ||
                                    s[i] == 'K' || s[i] == 'L' || s[i] == 'P' || s[i] == 'T' || s[i] == 'V')
                                    hyphen = 1;
                                else
                                    hyphen = 2;
                            else if (s[i + 1] == 'H')
                                if (s[i] == 'C' || s[i] == 'S' || s[i] == 'P')
                                    hyphen = 1;
                                else
                                    hyphen = 2;
                            else
                                hyphen = 2;
                    }
                    else if (strongVowels.Contains(s[i]))
                    {
                        if (strongVowels.Contains(s[i - 1])) 
                            hyphen = 1;
                    }
                    else if (s[i] == '-')
                    {
                        syllabes.Add(s.Substring(j, i - j));
                        syllabes.Add("-");
                        i++;
                        j = i;
                    }
     
                    if (hyphen == 1)
                    {  // Hyphenate here
                        syllabes.Add(s.Substring(j, i - j));
                        j = i;
                    }
                    else if (hyphen == 2)
                    {  // Hyphenate after
                        i++;
                        syllabes.Add(s.Substring(j, i - j));
                        j = i;
                    }
     
                    i++;
                }
     
                m = syllabes.Count - 1;
                if (j == n && m >= 0 && consonants.Contains(s[n]))
                    syllabes[m] = syllabes[m] + s[n];   // Last letter
                else
                    syllabes.Add(s.Substring(j, n - j + 1));
     
                return syllabes;
            }
        }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 20/10/2012, 16h44
  2. [RegEx] Compter les occurences d'un mot dans un PHP
    Par FoxLeRenard dans le forum Langage
    Réponses: 2
    Dernier message: 19/02/2009, 08h18
  3. Compter les occurences d'un mot dans une chaîne
    Par mimi2311 dans le forum Pascal
    Réponses: 4
    Dernier message: 20/04/2008, 22h37
  4. Réponses: 2
    Dernier message: 09/04/2008, 21h03
  5. [Collections]Compter les mots différents d'une ArrayList
    Par alanpix dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 21/11/2005, 22h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo