Page 1 of 1

Automating sentenses using loop parse of a text line?

Posted: 24 Dec 2018, 04:24
by pinkruru110
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!

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

Posted: 24 Dec 2018, 04:39
by AHKStudent
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

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

Posted: 24 Dec 2018, 05:38
by jeeswg
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

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

Posted: 24 Dec 2018, 05:48
by CyL0N
Oh WoW I Had TOTALY MIsread your question, my bad... Ooops, Ignore this.


:xmas:

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

Posted: 24 Dec 2018, 06:23
by just me
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: