TitleCase() - capital case, headline style - mixed case style

Post your working scripts, libraries and tools for AHK v1.1 and older
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

TitleCase() - capital case, headline style - mixed case style

10 Nov 2018, 09:54

TitleCase() source @ https://github.com/lintalist/TitleCase

This is my take on an improved Title Case function. As you know the Title case option of StringUpper/Lower commands is not ideal as it doesn't take into account the many exceptions AHK -> Ahk and AutoHotkey -> Autohotkey for example.

This function makes use of an INI file so you can setup a number of rules for various languages or you can simply use different INI files for specific projects. If an INI file is not present a default one is created (more to illustrate how to use it). If you have examples for non-English INI setups feel free to post. Other suggestions also welcome of course. (This will be part of a future Lintalist update, but thought it was useful to share and also gather some feedback before doing so - perhaps the INI should also include some RegExReplace codes for example).
Documentation:
Options:
- Text sentence to convert to TitleCase
- lang language to use (reads from ini), default "en" for English
- ini ini file to use, default "TitleCase.ini". If there is no INI present in A_ScriptDir it will create a default one ("en" only)

The text is transformed first to AutoHotkey Title case format.

After that several functions are called to process exceptions in the order as outlined below in the INI setup.

Ini setup - all keys are CSV lists:

[lang]
LowerCaseList= words you would prefer to have lowercase: a,and,is,the,etc [1]
UpperCaseList= words you would prefer to have uppercase: AHK,IBM,UK
MixedCaseList= words you would prefer to have MixedCase: AutoHotkey,iPhone
ExceptionsList= [2]
AlwaysLowerCaseList= final check to ensure that any of the actions above haven't transformed specific words

[1] Also does RegExReplace(Text, "im)([’'`])s","$1s") ; to prevent grocer'S
[2] Also does RegExReplace(Text, "im)[\.:;] \K([a-z])","$U{1}") ; first letter after .:; uppercase
The ini file can have multiple [sections] for speficic languages - you can also use multiple INI files
Examples

In : autohotkey voted best scripting language in the world: IBM survey
Out: AutoHotkey Voted Best Scripting Language in the World: IBM Survey

In : the QUICK BROWN fox jumps over the lazy dog
Out: The Quick Brown Fox Jumps over the Lazy Dog

In : Get help with using AutoHotkey and its commands and hotkeys
Out: Get Help with Using AutoHotkey and its Commands and Hotkeys

In : Post your working scripts, libraries and tools
Out: Post Your Working Scripts, Libraries and Tools

In : Helpful script writing tricks and HowTo's
Out: Helpful Script Writing Tricks and Howto's

In : Discuss features, issues, about Editors for AHK
Out: Discuss Features, Issues, About Editors for AHK
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: TitleCase() - capital case, headline style - mixed case style

10 Nov 2018, 13:23

@list, very nice indeed. Many uses suggest themselves. Here's a primitive effort at one:
TitleCase.gif
TitleCase.gif (40.53 KiB) Viewed 4336 times
Spoiler
Regards,
burque505
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

20 Aug 2019, 11:46

v1.2 - adding RegExReplace() to address 1st 2nd 3rd 4th etc, so

1ST, 22ND should become 1st, 22nd

https://github.com/lintalist/TitleCase
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: TitleCase() - capital case, headline style - mixed case style

20 Aug 2019, 11:54

@list, thanks. Nice addition.
Regards,
burque505
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

12 Jan 2020, 06:45

Beta: I moved the regex added for v1.2 to the Ini file OrdinalIndicator_Find and OrdinalIndicator_Replace and introduced "pairs" so you can handle any special use cases directly from the ini by defining regular expressions for a RegExReplace() - see https://github.com/lintalist/TitleCase/tree/beta#pairs

https://github.com/lintalist/TitleCase/tree/beta

You may need to add some keys to your ini manually - https://github.com/lintalist/TitleCase/blob/beta/TitleCase.ahk#L111
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

14 Jan 2020, 17:59

Now at v1.41 with some further (minor) changes - https://github.com/lintalist/TitleCase/ (beta branch merged into master)
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: TitleCase() - capital case, headline style - mixed case style

23 Jan 2020, 16:35

@List I'm wondering if you could explain what I'm doing wrong here. Admittedly and obviously I am stupid so I apologize in advance.

Here is what I'm trying to use to test: (I have included the file, the ini is created although it was created not in the titlecase folder and I imagine it's my syntax that is wrong if that's the right word).

Code: Select all

:*:testit::
Text = %Clipboard% ;  text=autohotkey voted best scripting language in the world ibm survey  ;  (what I'd be doing is copying text in my work program and using that to change things)
Titlecase(Text, "en", "titlecase.ini")
Msgbox %Text%

Return
Obviously all I'm getting is my text sentence back in the MsgBox.

Thanks for your time.

K
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

23 Jan 2020, 18:00

You almost had it :) You need to assign the result of the function to a variable:

Code: Select all

:*:testit::
Text = %Clipboard% ;  text=autohotkey voted best scripting language in the world ibm survey  ;  (what I'd be doing is copying text in my work program and using that to change things)
text:=Titlecase(Text, "en", "titlecase.ini") ; or:
; text:=Titlecase(Text) ; no need in particular to pass on EN en INI if you are using these defaults, or:
; text:=Titlecase(Clipboard) ; you can use the clipboard directly here
Msgbox %Text%
Return
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: TitleCase() - capital case, headline style - mixed case style

23 Jan 2020, 20:38

Thanks SO much list, so close but yet so far! Much appreciated, have a great rest of your day.

K
Industry
Posts: 19
Joined: 26 Apr 2017, 14:50

Re: TitleCase() - capital case, headline style - mixed case style

14 May 2020, 13:53

Love this! Is there a way for TitleCase to be enabled / disabled and run on the fly? I would love to be able to hit a hotkey and have all of my words come out capitalized properly!
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

14 May 2020, 14:49

If you mean like typing and then have your words automatically in TitleCase, then no, not out of the box - although I'm sure a method could be found but it might be rather annoying as nearly every word becomes a hotstring in such a case.

But you can always select text and transform the selected text using TitleCase and paste it back in place - that would look something like this.*

Code: Select all

^k:: ; select text, press ^k
Send ^c
Sleep 200
clipboard:=TitleCase(clipboard)
Send ^v
Return
Or you can use a simple https://www.autohotkey.com/docs/commands/InputBox.htm to "turn it on" and "off" so to speak.

* In lintalist https://www.autohotkey.com/boards/viewtopic.php?f=6&t=3378 - it would be a simple snippet:

Code: Select all

[[selected=t]]
Industry
Posts: 19
Joined: 26 Apr 2017, 14:50

Re: TitleCase() - capital case, headline style - mixed case style

16 May 2020, 12:04

I guess it would have to be rewrittern to make every first letter capitalized (when enabled) and then turn the excluded words into lower case again.
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: TitleCase() - capital case, headline style - mixed case style

16 May 2020, 12:27

@Industry, you might have a look here to see if it would work for you. The script requires that you select some text, hit a hotkey, and it's transformed into @list's TitleCase (or uppercase, lowercase, or AHK's stock "title case" with other hotkeys. It's not exactly what you request, but maybe it will work.
Regards,
burque505
list
Posts: 222
Joined: 26 Mar 2014, 14:03
Contact:

Re: TitleCase() - capital case, headline style - mixed case style

16 May 2020, 12:47

If you just want to Capitalize each word as you type 26 hotstrings will do it :)

Code: Select all

:*C?: a:: A 
:*C?: b:: B
:*C?: c:: C
; etc
Of course would produce Iphone vs iPhone and Vs vs vs, a simple inputbox or something like burque505 suggests would be nicer I guess.

What you could do is use the crude method above, and then use an Ini file similar to TitleCase and use a simple loop and the Hotstrings function - https://www.autohotkey.com/docs/commands/Hotstring.htm - to setup hotstrings to automatically replace "iphone" with "iPhone" - you could indeed toggle it on/off in such a case as well.
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: TitleCase() - capital case, headline style - mixed case style

16 May 2020, 13:55

This is what I'd been using until yesterday, with a simple GUI.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;
#Include TitleCase().ahk
; by list -> https://github.com/lintalist/TitleCase

Gui Add, Text, x19 y4 w440 h28 +0x200, Enter or paste text to be transformed in the upper box. Transformed text is in the lower box.
Gui Add, Edit, x22 y44 w440 h100 vToTC
Gui Add, Edit, y+10 w440 h100 vFromTC
Gui Add, Button, x20 y+10 w120 h23, &Transform
Gui Add, Button, x+5 w120 h23, Clea&r
Gui Add, Button, x+5 w120 h23, &Copy

Gui Show, , Title Case Transform
Return

Escape::
GuiEscape:
GuiClose:
    ExitApp
	
ButtonTransform:
ControlGetText, tt, Edit1
ot := TitleCase(tt)
ControlSetText, Edit2, %ot%
return

ButtonCopy:
ControlGetText, tc, Edit2
Clipboard := tc
return

ButtonClear:
ControlSetText, Edit1
ControlSetText, Edit2
return
Regards,
burque505

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 158 guests