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;