HyphenateString()
Author: Jare
Added: 8. elokuuta 2012 kello 0.15
Edited: 8. elokuuta 2012 kello 11.28
Category: Merkkijonot
Description
Code
Select all1 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 | stri$ = "Tavuttaa merkkijonon suomen kielen tavutussääntöjen mukaisesti. Tämä ei ole täysin aukoton. Esimerkiksi nämä menee pieleen: demokratia, Citroën."
MakeError HyphenateString(stri)
Function HyphenateString(stri$, mode$="-")
//Hyphenates Finnish Text.
//mode parameter controls the hyphenation:
// - mode <> "": uses mode's value As a hyphen And returns the hyphenated Text. Length of mode can also be greater than 1.
// - mode = "": returns a list of character positions where the Function suggests that a hyphen would be inserted. Example: "3,7,10".
//Rules can be found from http://www.cs.hut.fi/Opinnot/T-106.1240/s2007/harjoitukset/kierros_2/harj_1/index.html (checked 2012-08-07),
//but they are written in Finnish.
original_stri$ = stri
stri = Lower(stri)
vowels$ = "aeiouyåäö"
result$ = ""
hyphenated = 1
For i = 2 To Len(stri)
chr1$ = Mid(stri, i-1,1)
chr2$ = Mid(stri, i,1)
If False=IsLetter(chr1) Or False=IsLetter(chr2) Then Goto continue
//Rules
hyphenate = False
If InStr(vowels, chr1) And False = InStr(vowels, chr2) Then
//A) Hyphenate If a consonant comes After a vowel.
wovel_follows = False
For n = i+1 To Len(stri)
//Find Last consonant in a queue of consonants.
chr_n$ = Mid(stri, n,1)
wovel_follows = InStr(vowels, chr_n)
If wovel_follows Or False = IsLetter(chr_n) Then
n-1
Exit
EndIf
Next n
If wovel_follows Then hyphenate = n - (n>=Len(stri)) //Only hyphenate If the queue of consonants is followed by a vowel.
EndIf
If InStr(vowels, chr1) And InStr(vowels, chr2) Then
//B) Hyphenate If a vowel comes After a vowel, unless an exception happens.
exception = False
If chr1 = chr2 Then exception = True
If chr2 = "i" Then exception = True
If InStr("au,eu,ey,ie,iu,ou,uo,yö,äy,öy", chr1+chr2) Then exception = True
If Not exception Then
//No exceptions, so do hyphenating
hyphenate = i
Else
//C) Exception For exceptions: If a vowel comes After the chr1+chr2 pair, hyphenate between chr2 And the vowel.
If i = Len(stri) Then Exit
chr3$ = Mid(stri, i+1,1)
If InStr(vowels, chr3) Then hyphenate = i+1
EndIf
EndIf
If hyphenate > hyphenated Then
If mode = "" Then
If result <> "" Then result + " "
result + hyphenate
Else
result = result + Mid(original_stri, hyphenated, hyphenate-hyphenated) + mode
EndIf
hyphenated = hyphenate
EndIf
continue:
Next i
If mode <> "" Then result = result + Mid(original_stri, hyphenated)
Return result
EndFunction
Function IsLetter(char$)
//Checks If a given character is a-z, å, ä Or ö.
//NOTE! Uppercase letters are considered As non-letters!
ascii = Asc(char)
Return (ascii >= 97 And ascii <= 122) Or ascii = 228 Or ascii = 229 Or ascii = 246
EndFunction
|
Comments
#39 Sent by: Jare, 19. syyskuuta 2012 kello 19.42
Koska koodaan täysin englanniksi joka paikassa. En vaihda koodaustyyliäni vaikka ohjema olisikin muuta kieltä kuin englantia varten. :)
#40 Sent by: Pettis, 8. joulukuuta 2012 kello 11.56
Englanti sopii muutenkin ohjelmoinnissa kommentteihin paremmin, koska yleensä kaikki ohjelmointikielien avainsanat ovat englannin kielestä. Englanti on koodareiden ammattikieli :)
#36 Sent by: MaGetzUb, 26. elokuuta 2012 kello 19.07
Mielenkiintoista. Miksi olet kommentoinut koodin englanniksi, vaikka koodi/funktio itse tavuttaa suomenkielellä lauseita?