Author |
Topic: Routine in BBC BASIC for Title Case (Read 440 times) |
|
David46
New Member
member is offline


Gender: 
Posts: 4
|
 |
Routine in BBC BASIC for Title Case
« Thread started on: Nov 24th, 2009, 11:08am » |
|
Has anyone already examined the algorithm needed for changing a string of text into Title Case, with intelligent exception handling, and come up with a good function call?
Title Case is where every word in a sentence (or title of something) starts with a Capital letter, except certain exceptions, like "O'Reilly", Roman numerals already in caps like "VIII" and various other special letter combinations.
Is it possible that the Windows API already has such a built-in function?
David
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Routine in BBC BASIC for Title Case
« Reply #1 on: Nov 24th, 2009, 11:59am » |
|
Quote:Title Case is where every word in a sentence (or title of something) starts with a Capital letter, except certain exceptions, like "O'Reilly", Roman numerals already in caps like "VIII" and various other special letter combinations.
Is it possible that the Windows API already has such a built-in function? |
|
I can't find an API function. Visual Basic has a 'proper case' function, but as far as I can see it literally only affects the first letter of each word and doesn't handle the exceptions you mention.
I've listed below a BBC BASIC function which does the same thing (it calls FNupper in FNUSING.BBC). As it affects only the first character of each word, the exceptions will be preserved so long as they are correct in the original string:
Code: INSTALL @lib$+"FNUSING"
PRINT FNtitle("mcFarlane legends series VIII - terry o'Reilly")
END
DEF FNtitle(A$)
LOCAL S%
REPEAT
MID$(A$,S%+1,1) = FNupper(MID$(A$,S%+1,1))
S% = INSTR(A$, " ", S%+1)
UNTIL S%=0
= A$ Richard.
|
|
Logged
|
|
|
|
David46
New Member
member is offline


Gender: 
Posts: 4
|
 |
Re: Routine in BBC BASIC for Title Case
« Reply #2 on: Dec 30th, 2009, 4:26pm » |
|
After a lot of trials, this rather specific-to-application-and-program-location code fragment seems to work for me in the specific case of Song Titles that may also contain Roman Numerals I to X and XX and certain words that must ALWAYS appear in Caps. This function is called as an argument of the library function FNlower():
Code:
output$=FNTitlecase(FNlower(source$))
DEF FNTitlecase(A$)
LOCAL B$,A%,C%,P%,S%,I%,L%,ac%:B$=A$:nextcap%=TRUE:elide%=FALSE:P%=0:S%=0:W%=1
roman$="iiivviiixx"
FOR A% = 1 TO LEN(A$)
C% = ASCMID$(A$,A%)
REM test for a space, and apostrophe or a round openbracket or a hyphen - capital must follow unless s t d m ll re ve)
IF(C%=32 OR C%=39 OR C%=40 OR C%=45) THEN
nextcap%=TRUE:IF C%=39 elide%=TRUE ELSE elide%=FALSE:REM 39 is apostrophe
ENDIF
IFelide%=TRUE THEN
REM an apostrophe preceded this letter which is d, m, s or t followed by a space, so do nothing to this letter
IF ((C%=100 OR C%=109 OR C%=115 OR C%=116) AND MID$(A$,A%+1,1)=" ") elide%=FALSE:nextcap%=FALSE:REM do nothing
IF ((MID$(A$,A%,3)="ll ") OR (MID$(A$,A%,3)="re ") OR (MID$(A$,A%,3)="ve ")) elide%=FALSE:A%=A%+1:nextcap%=FALSE
ENDIF
IF nextcap% IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32):nextcap%=FALSE
NEXT
WHILEW%>0
W%=INSTR(B$," ",P%)
IFW%>0 THEN
IFW%>S% S%=W%
word$=MID$(B$,P%,W%-P%):IF((LENword$>=1 AND LENword$<=4) AND INSTR(roman$,FNlower(word$))>0) MID$(A$,P%,W%-P%)=FNupper(word$)
ENDIF
P%=W%+1
ENDWHILE
word$=MID$(B$,S%+1):IF(LEN word$>=1 AND LEN word$<4 AND INSTR(roman$,word$)>0) MID$(A$,S%+1)=FNupper(word$)
FORI%=1 TO caps%
ac%=1
REPEAT
ac%=INSTR(B$,CHR$32+FNlower(cap$(I%)),L%):IF ac% MID$(A$,ac%+1)=FNupper(cap$(I%)):L%=ac%+LENcap$(I%)+1
UNTILac%=0 ORL%=LENA$
NEXT
= A$
DEFFNgetexceptions
LOCALI%:I%=0
C%=OPENIN("C:\ALLCAPS.txt")
IFC%>0 THEN
WHILENOTEOF#C%
i$=GET$#C%:IFASCi$<>35 AND i$>"" I%+=1
ENDWHILE
DIM cap$(I%):I%=1:PTR#C%=0
WHILE NOT EOF#C%
i$=GET$#C%:IFASCi$<>35 AND i$>"" cap$(I%)=i$:I%+=1
ENDWHILE
CLOSE#C%:C%=0
ELSE
ERROR 254, "Can't open ALLCAPS file!"
ENDIF
=I%-1 The exceptions text file ALLCAPS.txt contains the words that must ALWAYS appear in Caps; comment lines begin with #.
Thanks for the help with this, Richard. Try to excuse my clumsy programming style... David
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Routine in BBC BASIC for Title Case
« Reply #3 on: Dec 30th, 2009, 5:10pm » |
|
Quote:After a lot of trials, this rather specific-to-application-and-program-location code fragment seems to work for me |
|
The soon-to-be-released STRINGLIB library includes the function I listed here previously (which capitalises only the first character of the string and the character after each space). This should deal with what I presume to be the most common situation, where you want to take a string which might occur within the body of a paragraph (say) and turn it into a title. In that case other characters which need to be capitalised already will be.
Evidently your application is rather more specialised, in that you need to deal with source text which doesn't have this property (i.e. in which things like Roman numerals and Scottish surnames aren't already correctly capitalised). Hopefully that's not typical.
Incidentally, storing a file in the root directory (e.g. C:\ALLCAPS.TXT) is to be discouraged, other than as a temporary hack. Don't do it if your program is intended to be deployed on other people's PCs!
Richard.
|
|
Logged
|
|
|
|
David46
New Member
member is offline


Gender: 
Posts: 4
|
 |
Re: Routine in BBC BASIC for Title Case
« Reply #4 on: Dec 30th, 2009, 6:05pm » |
|
Thanks for that. Actually the root file location I showed isn't the real path, which is indeed not in the root, but I simplified it for clarity.
Unfortunately the capitalisation status of the source strings cannot be relied upon to be right, so we chose to FNlower them to start with, and then put them "as right as we could"! David
|
|
Logged
|
|
|
|
|