Modifying a script to remove _ and replace with a space Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Whazzzzzup15
Posts: 94
Joined: 13 Apr 2019, 16:12

Modifying a script to remove _ and replace with a space

Post by Whazzzzzup15 » 14 May 2022, 23:15

The following script on GitHub is a TitleCase script. It comes with an INI file where you can find a character/phrase and replace it, however, I dont' know the command to replace with a space. I have used, " ", %A_Space

https://github.com/lintalist/TitleCase

Here is the INI file, I added the two bottom lines which work, it just needs to output a space instead.

Code: Select all

; -------------------------------------------------------------------------------------------
; TitleCase - https://github.com/lintalist/TitleCase
; ------------------------------------------------------------------------------------------

[en]
LowerCaseList=a,an,and,amid,as,at,atop,be,but,by,for,from,if,in,into,is,it,its,nor,not,of,off,on,onto,or,out,over,past,per,plus,than,the,till,to,up,upon,v,vs,via,with
UpperCaseList=AHK,IBM,UK,USA
MixedCaseList=AutoHotkey,iPod,iPad,iPhone
ExceptionsList=
AlwaysLowerCaseList=
OrdinalIndicator_Find=im)\b(\d+)(st|nd|rd|th)\b
OrdinalIndicator_Replace=$1$L{2}
Hypen1_Find=im)-\K(.)
Hypen1_Replace=$U{1}
Hypen2_Find=im)-(and|but|for|nor|of|or|so|yet)-
Hypen2_Replace=-$L{1}-
Hypen3_Find=_
Hypen3_Replace=" "


User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Modifying a script to remove _ and replace with a space

Post by mikeyww » 15 May 2022, 05:35

I have not tested it, but try:

Code: Select all

Hypen3_Replace := " "
Explained: Var := expression

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: Modifying a script to remove _ and replace with a space

Post by boiler » 15 May 2022, 06:24

mikeyww wrote: I have not tested it, but try:

Code: Select all

Hypen3_Replace := " "
Explained: Var := expression
The text shown in the code box is a .ini file, not an AHK script.

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

Re: Modifying a script to remove _ and replace with a space  Topic is solved

Post by list » 15 May 2022, 06:34

As it is ini format and a regexreplace ix performed I think it can't be done in one step (not that I can think of at the moment) - but it can be done in two steps, replace _ with " " then remove the " like so: But your mileage may vary if you have text that needs " in it. You can use other chars for " like @ if you are sure they aren't used in your text (to name an example)
Hypen3_Find=_
Hypen3_Replace=" "
Hypen4_Find="
Hypen4_Replace=
Easiest is probably to wrap the call to titlecase in a StrReplace like so

Code: Select all

text:=StrReplace(Titlecase(text),"_"," ")

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Modifying a script to remove _ and replace with a space

Post by mikeyww » 15 May 2022, 06:38

Nice idea. Sorry I missed the INI part! I often use ;;; in a situation like this, because that sequence is never used in normal text. Thus, perhaps:

Code: Select all

Hypen3_Find=_
Hypen3_Replace=@@@ @@@
Hypen4_Find=@@@
Hypen4_Replace=
(Not sure if the semicolons would cause confusion with comments.)

hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Modifying a script to remove _ and replace with a space

Post by hd0202 » 15 May 2022, 09:53

After a long row of tests I have added two lines in the code from github (marked "line two" and "line three", and some lines for test at the beginning of the code and 4 lines for test marked "Hypen3" and "Hypen4" as INI lines).
With this addition you can use 'a_space' or '" "' as shown in "Hypen3" and "Hypen4".

But, please, do not ask: What is the difference in the result between the two lines marked "line one" and "line three" ? I don't know, but it works !!!

Code: Select all

/*
Function   : TitleCase()
Purpose    : Title case (capital case, headline style), example
             "The Quick Brown Fox Jumps over the Lazy Dog"
             A mixed-case style with all words capitalised, except for certain subsets
             -- https://en.wikipedia.org/wiki/Letter_case#Title_case
Version    : 1.41
Source     : https://github.com/lintalist/TitleCase
License    : See license.txt for further details (GPL-2.0)

History:
v1.41 - removed static
v1.4 - replaced pairs object by simple list (ensures order of processing as listed in INI)
v1.3 - additional Find/Replace via INI setup - moved v1.2 to INI  
       read language section in one go
v1.2 - adding RegExReplace() to address 1st 2nd 3rd 4th etc
v1.1 - adding readme.md doc, minor mods to default LowerCaseList
v1.0 - initial version

Documentation see readme.md @ https://github.com/lintalist/TitleCase

*/

text := "das ist~ein_test"	; for test
msgbox, % TitleCase(text)	; for test
exitapp						; for test

TitleCase(Text,lang="en",ini="TitleCase.ini")
	{
	 settings:={}, pairs:=""
	 If !InStr(ini,"\")
		ini:=A_ScriptDir "\" ini
	 IfNotExist, %ini%
		TitleCase_Ini(ini)
	 IniRead, LangSection, %ini%, %lang%
	 Loop, parse, LangSection, `n, `r
		{
		 data:=StrSplit(A_LoopField,"=",2)
		 settings[data[1]]:=data[2]
		 if InStr(data[1],"_")
			{
			 pairdata:=StrSplit(A_LoopField,"_").1
			 If pairdata not in pairs
				pairs .= pairdata ","
			}
		}
	 StringLower, Text, Text, T
	 Text:=TitleCase_LowerCaseList(Text,settings.LowerCaseList)
	 Text:=TitleCase_UpperCaseList(Text,settings.UpperCaseList)
	 Text:=TitleCase_MixedCaseList(Text,settings.MixedCaseList)
	 Text:=TitleCase_ExceptionsList(Text,settings.ExceptionsList)
	 pairs:=trim(pairs,",")
	 loop, parse, pairs, CSV
		{
		 find:=settings[A_LoopField "_find"]
		 replace:=settings[A_LoopField "_replace"]			; line one
		 if (replace = "a_space") or (replace = """ """)	; line two
			replace := a_space								; line three
		 Text:=RegExReplace(Text,find,replace)
		}
	 Text:=TitleCase_AlwaysLowerCaseList(Text,settings.AlwaysLowerCaseList)

	 Text:=RegExReplace(Text,"^(.)","$U{1}") ; ensure first char is always upper case
	 Return Text
	}

TitleCase_LowerCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Text:=RegExReplace(Text, "im)([’'`])s","$1s") ; to prevent grocer'S
	 Return Text
	}

TitleCase_UpperCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

TitleCase_MixedCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

TitleCase_ExceptionsList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Text:=RegExReplace(Text, "im)[\.:;] \K([a-z])","$U{1}") ; first letter after .:; uppercase
	 Return Text
	}
	
TitleCase_AlwaysLowerCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

; create ini if not present, that way we don't overwrite any user changes in future updates
TitleCase_Ini(ini)
	{
FileAppend,
(
; -------------------------------------------------------------------------------------------
; TitleCase - https://github.com/lintalist/TitleCase
; ------------------------------------------------------------------------------------------

[en]
LowerCaseList=a,an,and,amid,as,at,atop,be,but,by,for,from,if,in,into,is,it,its,nor,not,of,off,on,onto,or,out,over,past,per,plus,than,the,till,to,up,upon,v,vs,via,with
UpperCaseList=AHK,IBM,UK,USA
MixedCaseList=AutoHotkey,iPod,iPad,iPhone
ExceptionsList=
AlwaysLowerCaseList=
OrdinalIndicator_Find=im)\b(\d+)(st|nd|rd|th)\b
OrdinalIndicator_Replace=$1$L{2}
Hypen1_Find=im)-\K(.)
Hypen1_Replace=$U{1}
Hypen2_Find=im)-(and|but|for|nor|of|or|so|yet)-
Hypen2_Replace=-$L{1}-
Hypen3_Find=_
Hypen3_Replace=a_space
Hypen4_Find=~
Hypen4_Replace=" "
)
, %ini%, UTF-16
	}
Hubert

Whazzzzzup15
Posts: 94
Joined: 13 Apr 2019, 16:12

Re: Modifying a script to remove _ and replace with a space

Post by Whazzzzzup15 » 15 May 2022, 10:14

Easiest is probably to wrap the call to titlecase in a StrReplace like so

Code: Select all

text:=StrReplace(Titlecase(text),"_"," ")
This is exactly how I was able to solve it, but I was hoping for a INI fix to solve multiple replacements. The issue with all these solutions with the INI file is you still need to re-run the script, because it doesn't title case anything after the _. For example. This_is_my_file.txt, will fix to "This is my file.txt" and you will have to re-run to get "This Is My File.txt"

Here is my solution, although the easiest, but doesn't require re-running but I think i would need to use several StrReplace. The goal was to create an INI file that would look for all characters to better formatting while replacing to a space. It makes it hard with the INI file without knowing the parameters for replacements

Characters to replace with a space = _ , %20, +, ., ,

Code: Select all

^t:: ; select text, press ^t
       ClipSaved := ClipboardAll
       Clipboard =
       Send ^c
       Sleep 200
       ClipWait, 0
       ;Clipboard:=TitleCase(clipboard) ;Only use if can solve the ini issue
       Clipboard:=TitleCase(StrReplace(TitleCase(clipboard),"_"," ")) ;solution without modifying the ini.
       Send ^v
       Sleep 200
       Clipboard := ClipSaved
Return
For future users, here is a full solution with multple replacements - Doesn't use the INI file.

Code: Select all

^o:: ; select text, press ^k
       ClipSaved := ClipboardAll
       Clipboard =
       Send ^c
       Sleep 200
       ClipWait, 0
       
       replace := {_:" ","%20":" ",+:" "}
       
       For what, with in replace
       StringReplace, Clipboard, Clipboard, %what%, %with%, All

       Clipboard:=TitleCase(Clipboard)

       Send ^v
       Sleep 200
       Clipboard := ClipSaved
Return

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

Re: Modifying a script to remove _ and replace with a space

Post by list » 15 May 2022, 10:56

I don't think you need to call TitleCase twice, this should suffice, it removes _ from the clipboard, then passed it on to title case Clipboard:=TitleCase(StrReplace(clipboard,"_"," "))
Quick test Msgbox % Clipboard:=TitleCase(StrReplace("This_is_my_file.txt","_"," ")) => This is My File.txt

Whazzzzzup15
Posts: 94
Joined: 13 Apr 2019, 16:12

Re: Modifying a script to remove _ and replace with a space

Post by Whazzzzzup15 » 15 May 2022, 11:02

list wrote:
15 May 2022, 10:56
I don't think you need to call TitleCase twice, this should suffice, it removes _ from the clipboard, then passed it on to title case Clipboard:=TitleCase(StrReplace(clipboard,"_"," "))
Quick test Msgbox % Clipboard:=TitleCase(StrReplace("This_is_my_file.txt","_"," ")) => This is My File.txt
Yeah, good catch, I fixed it on the last script.

hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Modifying a script to remove _ and replace with a space

Post by hd0202 » 15 May 2022, 11:50

In my previous post I concentrated on a_space and didn't pay attention to the main task of the script, sorry.
Here is my new version. The processing of the INI parameters with target "a_space" is separated and now done first.

Code: Select all

/*

Function   : TitleCase()
Purpose    : Title case (capital case, headline style), example
             "The Quick Brown Fox Jumps over the Lazy Dog"
             A mixed-case style with all words capitalised, except for certain subsets
             -- https://en.wikipedia.org/wiki/Letter_case#Title_case
Version    : 1.41
Source     : https://github.com/lintalist/TitleCase
License    : See license.txt for further details (GPL-2.0)

History:
v1.41 - removed static
v1.4 - replaced pairs object by simple list (ensures order of processing as listed in INI)
v1.3 - additional Find/Replace via INI setup - moved v1.2 to INI  
       read language section in one go
v1.2 - adding RegExReplace() to address 1st 2nd 3rd 4th etc
v1.1 - adding readme.md doc, minor mods to default LowerCaseList
v1.0 - initial version

Documentation see readme.md @ https://github.com/lintalist/TitleCase

*/
text := "das_ist~ein_test."
msgbox, % TitleCase(text)
TitleCase(Text,lang="en",ini="TitleCase.ini")
	{
	 settings:={}, pairs:=""
	 If !InStr(ini,"\")
		ini:=A_ScriptDir "\" ini
	 IfNotExist, %ini%
		TitleCase_Ini(ini)
	 IniRead, LangSection, %ini%, %lang%
	 Loop, parse, LangSection, `n, `r
		{
		 data:=StrSplit(A_LoopField,"=",2)
		 settings[data[1]]:=data[2]
		 if InStr(data[1],"_")
			{
			 pairdata:=StrSplit(A_LoopField,"_").1
			 If pairdata not in pairs
				pairs .= pairdata ","
			}
		}
	 pairs:=trim(pairs,",")
	 loop, parse, pairs, CSV
		{
		 find:=settings[A_LoopField "_find"]
		 replace:=settings[A_LoopField "_replace"]		; line one
		 if (replace = "a_space") or (replace = """ """)
		 {
			replace := a_space							; line two
			Text:=RegExReplace(Text,find,replace)
		 }
		}
	 StringLower, Text, Text, T
	 Text:=TitleCase_LowerCaseList(Text,settings.LowerCaseList)
	 Text:=TitleCase_UpperCaseList(Text,settings.UpperCaseList)
	 Text:=TitleCase_MixedCaseList(Text,settings.MixedCaseList)
	 Text:=TitleCase_ExceptionsList(Text,settings.ExceptionsList)
	 Text:=TitleCase_AlwaysLowerCaseList(Text,settings.AlwaysLowerCaseList)
	 pairs:=trim(pairs,",")
	 loop, parse, pairs, CSV
		{
		 find:=settings[A_LoopField "_find"]
		 replace:=settings[A_LoopField "_replace"]
		 if (replace = "a_space") or (replace = """ """)
			continue
		 Text:=RegExReplace(Text,find,replace)
		}

	 Text:=RegExReplace(Text,"^(.)","$U{1}") ; ensure first char is always upper case
	 Return Text
	}

TitleCase_LowerCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Text:=RegExReplace(Text, "im)([’'`])s","$1s") ; to prevent grocer'S
	 Return Text
	}

TitleCase_UpperCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

TitleCase_MixedCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

TitleCase_ExceptionsList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Text:=RegExReplace(Text, "im)[\.:;] \K([a-z])","$U{1}") ; first letter after .:; uppercase
	 Return Text
	}
	
TitleCase_AlwaysLowerCaseList(Text,list)
	{
	 loop, parse, list, CSV
		Text:=RegExReplace(Text, "im)\b" A_LoopField "\b",A_LoopField)
	 Return Text
	}

; create ini if not present, that way we don't overwrite any user changes in future updates
TitleCase_Ini(ini)
	{
filedelete, %ini%
FileAppend,
(
; -------------------------------------------------------------------------------------------
; TitleCase - https://github.com/lintalist/TitleCase
; ------------------------------------------------------------------------------------------

[en]
LowerCaseList=a,an,and,amid,as,at,atop,be,but,by,for,from,if,in,into,is,it,its,nor,not,of,off,on,onto,or,out,over,past,per,plus,than,the,till,to,up,upon,v,vs,via,with
UpperCaseList=AHK,IBM,UK,USA
MixedCaseList=AutoHotkey,iPod,iPad,iPhone
ExceptionsList=
AlwaysLowerCaseList=
OrdinalIndicator_Find=im)\b(\d+)(st|nd|rd|th)\b
OrdinalIndicator_Replace=$1$L{2}
Hypen1_Find=im)-\K(.)
Hypen1_Replace=$U{1}
Hypen2_Find=im)-(and|but|for|nor|of|or|so|yet)-
Hypen2_Replace=-$L{1}-
Hypen3_Find=_
Hypen3_Replace=a_space
Hypen4_Find=~
Hypen4_Replace=" "
)
, %ini%, UTF-16
	}
Hubert

Post Reply

Return to “Ask for Help (v1)”