Sentence Extraction Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
vSky
Posts: 80
Joined: 30 May 2022, 10:13

Sentence Extraction

Post by vSky » 07 Aug 2022, 23:44

"Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."

I have a sample post above. I want to put "`n" after every 62 words. The text above will be as follows. How can I do that?

"Care should be taken not to make the above too lenient because`n
if you ever inadvertently introduce an infinite loop of keyst`n
rokes (via a Send command that accidentally triggers other hot`n
keys) your computer could become unresponsive due to the rapid`n
flood of keyboard events"

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

Re: Sentence Extraction

Post by boiler » 08 Aug 2022, 00:38

Use SubStr(), a loop, and a little math.

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Sentence Extraction  Topic is solved

Post by AHKStudent » 08 Aug 2022, 00:57

vSky wrote:
07 Aug 2022, 23:44
"Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."

I have a sample post above. I want to put "`n" after every 62 words. The text above will be as follows. How can I do that?

"Care should be taken not to make the above too lenient because`n
if you ever inadvertently introduce an infinite loop of keyst`n
rokes (via a Send command that accidentally triggers other hot`n
keys) your computer could become unresponsive due to the rapid`n
flood of keyboard events"
you mean characters

a method based on boilers idea

Code: Select all

theText := "Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."

newVar := 
startAt := 1
while(strlen(newVar) < strlen(theText))
{
newVar .= LTrim(substr(theText,startAt,62)," ") "`n"
startAt += 62
}
MsgBox, % newVar
exitapp

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Sentence Extraction

Post by teadrinker » 08 Aug 2022, 02:48

vSky wrote: 62 words
Words or characters?

Code: Select all

text := "Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."
MsgBox, % RegExReplace(text, "(^.{62}|.{61})\K ?", "`n")

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Sentence Extraction

Post by Xtra » 08 Aug 2022, 02:54

If you don't want your words cut in the middle:

Code: Select all

#NoEnv
theText := "Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."
maxLen := 62

Loop
{
    if (StrLen(theText) >= maxLen) {
        out .= (Line := SubStr(theText, 1, Instr(SubStr(theText, 1, maxLen), A_Space,, 0))) . "`n"
        theText := StrReplace(theText, Line)
        continue
    }
    out .= theText
    break
}

MsgBox, 4096, output, % Clipboard := out
ExitApp
output =
Care should be taken not to make the above too lenient
because if you ever inadvertently introduce an infinite loop
of keystrokes (via a Send command that accidentally triggers
other hotkeys) your computer could become unresponsive due to
the rapid flood of keyboard events.
Last edited by Xtra on 08 Aug 2022, 19:47, edited 1 time in total.

Rikk03
Posts: 192
Joined: 12 Oct 2020, 02:44

Re: Sentence Extraction

Post by Rikk03 » 08 Aug 2022, 03:08

This is great but it cuts words. It needs to be flexible enough to only cut the line if there is space for the next character. @AHKStudent

Rikk03
Posts: 192
Joined: 12 Oct 2020, 02:44

Re: Sentence Extraction

Post by Rikk03 » 08 Aug 2022, 03:21

@teadrinker yours also cuts words albeit less frequently.

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Sentence Extraction

Post by teadrinker » 08 Aug 2022, 03:39

@Rikk03
There was no "do not cut words" condition, see the example from the first post.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Sentence Extraction

Post by wetware05 » 08 Aug 2022, 06:14

Hi.

I have this function, but I don't remember where I got it from. Don't cut the words. The length is set on line 6. It can be put in a global variable, if it is necessary to change it interactively.

Code: Select all

PreviousClip:= "Care should be taken not to make the above too lenient because if you ever inadvertently introduce an infinite loop of keystrokes (via a Send command that accidentally triggers other hotkeys) your computer could become unresponsive due to the rapid flood of keyboard events."

CutTip:= st_wordWrap(PreviousClip)
MsgBox, %CutTip%

st_wordWrap(string, column=62, indentChar="")
{
    indentLength := StrLen(indentChar)
     
    Loop, Parse, string, `n, `r
    {
        If (StrLen(A_LoopField) > column)
        {
            pos := 1
            Loop, Parse, A_LoopField, %A_Space%
                If (pos + (loopLength := StrLen(A_LoopField)) <= column)
                    out .= (A_Index = 1 ? "" : " ") A_LoopField
                    , pos += loopLength + 1
                Else
                    pos := loopLength + 1 + indentLength
                    , out .= "`n" indentChar A_LoopField
             
            out .= "`n"
        } Else
            out .= A_LoopField "`n"
    }
     
    Return SubStr(out, 1, -1)
}
It may do the same as the Xtra script.

User avatar
vSky
Posts: 80
Joined: 30 May 2022, 10:13

Re: Sentence Extraction

Post by vSky » 08 Aug 2022, 10:13

boiler wrote:
08 Aug 2022, 00:38
Use SubStr(), a loop, and a little math.
There is a situation like this; There is such a process in the code given as a solution. (LTrim(substr(theText,startAt...) I just found out that you can use "Subtr" with LTrim in this way. The AHK Documents don't give much information about how to use operations in different ways. I don't like asking questions, I just don't know how to do it.

User avatar
vSky
Posts: 80
Joined: 30 May 2022, 10:13

Re: Sentence Extraction

Post by vSky » 08 Aug 2022, 10:14

AHKStudent wrote:
08 Aug 2022, 00:57

you mean characters

a method based on boilers idea
Yes This solution really worked for me. Thank you.
Xtra wrote:
08 Aug 2022, 02:54
If you don't want your words cut in the middle:
That was another question I wanted to do. I will use this. Thanks

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Sentence Extraction

Post by flyingDman » 08 Aug 2022, 11:52

@Xtra Nice! (but SubStr(theText, 1) ? ;) )
14.3 & 1.3.7

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Sentence Extraction

Post by flyingDman » 08 Aug 2022, 12:25

Or (not cutting words) :

Code: Select all

maxLen := 62
for x,y in strsplit(theText," ")
	strlen(nT . y) < maxLen ? nT .= y " " : (nnT .= Rtrim(nT," ") "`n", nT := y " ")
msgbox % out := nnT . Rtrim(nT," ")
(also trims each line of any final space)
14.3 & 1.3.7

Rikk03
Posts: 192
Joined: 12 Oct 2020, 02:44

Re: Sentence Extraction

Post by Rikk03 » 08 Aug 2022, 15:47

This is great, thankyou

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Sentence Extraction

Post by Xtra » 08 Aug 2022, 19:45

flyingDman wrote:
08 Aug 2022, 11:52
@Xtra Nice! (but SubStr(theText, 1) ? ;) )
Haha that's what happens when zzzz. I will correct it.

Post Reply

Return to “Ask for Help (v1)”