Automating sentenses using loop parse of a text line?

Get help with using AutoHotkey and its commands and hotkeys
pinkruru110
Posts: 1
Joined: 24 Dec 2018, 03:42

Automating sentenses using loop parse of a text line?

24 Dec 2018, 04:24

Example:
I want to type
Rice food China India Spain

and expand it to

Rice is a food that is eaten in China, India, and Spain. <--the structure of the sentence is very rigid so easy to automate based on pre-defined insertion site of the variables.
- China
- India
- Spain

I'm having quite a bit of trouble with this simple, automation task to generate custom but well-defined sentences based on the available texts
I want to be able to press a shortcut that tells ahk to read first or current line of the text (Rice food China India), then using loop parse or which ever function to generate rice=var1, food=var2, china=var3, india=var4 etc...

then generate custom sentense using fields?
%var1% is a %var2% that is eaten in %var3%, %var4%, and %var5%.
- %var3%
- %var4%
- %var5%
- etc....

Thank you!
AHKStudent
Posts: 909
Joined: 05 May 2018, 12:23

Re: Automating sentenses using loop parse of a text line?

24 Dec 2018, 04:39

maybe this can help you get started

Code: Select all

wordsare := "Rice food China India Spain"
foodstring := StrSplit(wordsare, A_Space)
msgbox, % foodstring[1] " is a  "foodstring[2] " that is eaten in " foodstring[3] " " foodstring[4] " and " foodstring[5] 
ExitApp
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Automating sentenses using loop parse of a text line?

24 Dec 2018, 05:38

Here's an attempt. Cheers.

Code: Select all

q:: ;create sentences based on lists
vList := "
(
rice|food|China India Spain
rice|food|China India
rice|food|China
)"

vTextOrig := "[FIELD1] is a [FIELD2] that is eaten in [FIELD3]."

Loop, Parse, vList, `n, `r
{
	oTemp := StrSplit(A_LoopField, "|")
	oTemp.1 := Format("{:T}", oTemp.1) ;title case
	oTemp.3 := StrReplace(oTemp.3, " ", ", ")
	oTemp.3 := RegExReplace(oTemp.3, ", (?!.* )", " and ") ;replace last ', ' with ' and '
	vText := vTextOrig
	Loop, 3
		vText := StrReplace(vText, "[FIELD" A_Index "]", oTemp[A_Index])
	MsgBox, % vText
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Automating sentenses using loop parse of a text line?

24 Dec 2018, 05:48

Oh WoW I Had TOTALY MIsread your question, my bad... Ooops, Ignore this.


:xmas:
Last edited by CyL0N on 24 Dec 2018, 08:20, edited 1 time in total.
live ? long & prosper : regards
just me
Posts: 7403
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Automating sentenses using loop parse of a text line?

24 Dec 2018, 06:23

My attempt:

Code: Select all

#NoEnv
WordsStr := "Rice food China India Spain"
WordsArr := StrSplit(WordsStr, A_Space, , 3)
If (WordsArr.Length() <> 3)
   ThingStr := WordsStr
Else {
   ThingStr := WordsArr[1] . " is a " . WordsArr[2] . " that is eaten in "
   ThingVals := StrSplit(WordsArr[3], A_Space)
   MaxVal := ThingVals.Length()
   For Index, ThingVal In ThingVals
      ThingStr .= ThingVal . ((Index = MaxVal) ? "." : (Index = (MaxVal - 1)) ? ", and " : ", ")
   For Index, ThingVal In ThingVals
      ThingStr .= "`n- " . ThingVal

}
MsgBox, 0, Result, %WordsStr%`n`n->`n`n%ThingStr%
ExitApp
Merry Christmas! :xmas:

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, boiler, Saarthak Kamra, TAC109, zKade and 31 guests