KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: custom command to select current sentence
Topic Summary:
Created On: 09/17/2021 07:54 PM
Status: Post and Reply
Linear : Threading : Single : Branch
 custom command to select current sentence   - wristofdoom - 09/17/2021 07:54 PM  
 custom command to select current sentence   - Alan Cantor - 09/17/2021 08:39 PM  
 custom command to select current sentence   - wristofdoom - 09/17/2021 10:18 PM  
 custom command to select current sentence   - Alan Cantor - 09/18/2021 11:36 PM  
 custom command to select current sentence   - Alan Cantor - 09/19/2021 06:13 PM  
Keyword
 09/17/2021 07:54 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 244
Joined: 09/03/2020

Does anybody have a custom command for selecting the current sentence that (mostly) works in any application?

 

I know that such a command will not be perfect, and will not pick up on common sense ideas about what constitutes a sentence.

 

However, a command that will move the caret left until it hits a "." character, and then move/select to the right until it hits another "." character seems achievable, albeit clunky. 

 

My pseudocode would look something like this:

 

 

sendkeys "^{left}" // move one word to the left

SendKeys "+{left}" // select one character to the left

SendKeys "^v" // get the selected character into the clipboard

//do some fancy logic I don't know how to do, to test if the selected character is a "period" or not. If no, then repeat the above. If yes, then start doing a new loop: 

SendKeys "{right}" // deselect the character

SendKeys "^{right}" // move one word to the right

SendKeys "+{right}" // select one character to the right

//do some logic to test to see if the character is a ".".

// If not, then increment a counter by 1, because we need to know how many words apart the two periods are from each other 

// Continue to loop the ^{right} command, and test each character to the right after each jump to see if the character is a period, and increment the counter by 1 every time it is NOT a period 

// when you finally reached a period, then select back X amount of words, using SendKeys "+^{left}"  were X = value of the counter

 

This is how I would do it as a human using the keyboard, but sounds very slow and buggy.

 

Anybody have a better way?

 



-------------------------

Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017.

 09/17/2021 08:39 PM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 4183
Joined: 12/08/2007

In Word and Outlook messages, pressing F8 three times selects the sentence. It doesn't matter where the cursor is in the sentence. The command automatically finds the start and end of the sentence:

SendKeys "{F8}{F8}{F8}"

(Re Outlook: this technique works in HTML and Rich Text messages, but not in plain text messages.)

To make this command more bulletproof, I suggest prefacing the above with Esc:

SendKeys "{Esc}{F8}{F8}{F8}"

That's because pressing F8 turns on "Select Mode." Pressing Esc cancels Select Mode. If you omit Esc, and if you run the same command twice in a row, you will inadvertently select the entire document, as you have effectively pressed F8 six times. Here are the main F8 key sequences:

F8 = Select mode on. (Select from the current cursor position to the next typed character)
F8 x2 = Select current word
F8 x3 = Select current sentence
F8 x4 = Select current paragraph
F8 x5 = Select current section (which may be the same as the document if it doesn't contain sections)
F8 x6 = Select document

Note: the F8 "trick" only works in Word and Outlook. I know of no other applications that support this, although pressing F8 once toggles ON select mode in a few other programs, such as Excel. (This provides an easy way to select rows and columns in Excel. For example, press F8, then press down arrow twice. You have selected two rows by keyboard, without the need to hold down the Shift key.)



 09/17/2021 10:18 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 244
Joined: 09/03/2020

Yeah I know that some apps have it. Writemonkey has it too.

What I am trying to achieve is an app-neutral way to get the sentence on the basis of delimiters.

-------------------------

Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017.

 09/18/2021 11:36 PM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 4183
Joined: 12/08/2007

This AutoHotkeyScript, triggered by pressing Alt + 1, probes left, character by character, to identify the start of a sentence. And then it probes right to find the end of the sentence. And then it selects the sentence.

The script will fail at times. For example, it goes into an infinite loop when the sentence is the first in a field or a document. The script only recognizes the period/full stop as a sentence delimiter.

For the last sentence in a field or document, an extra character might be selected. The script also runs rather slowly.

Despite the limitations of the script, hopefully it's a step in the right direction. Feel free to use and modify it. It can be improved.

!1::
CountLeft = 0
CountRight = 0

; Probe left to end of previous sentence
Loop
{
Clipboard := ""
SendInput +{Left}^c
ClipWait 0.1
SendInput {Left}
++CountLeft

If (Clipboard = .)
{
Break
}
}

SendInput {Right %CountLeft%}

; Probe right to end of current sentence
Loop
{
Clipboard := ""
SendInput +{Right}^c
ClipWait 0.1
SendInput {Right}
++CountRight

If (Clipboard = .)
{
Break
}
}

; Select the sentence
Send +{Left %CountRight%}
CountLeft := CountLeft - 2
Send +{Left %CountLeft%}
 09/19/2021 06:13 PM
User is offline View Users Profile Print this message


Alan Cantor
Top-Tier Member

Posts: 4183
Joined: 12/08/2007

 

Here is a slightly more robust version. The script now treats periods, question marks, and exclamation marks as sentence delimiters. It watches for the start of a document or field; the script doesn't loop forever for the first sentence. The selection is more accurate when there is more than one space between sentences.

 

The script doesn't deal with multiple delimiters in a row!! Like this??? Yes...

 

The script still might fail or be inaccurate under certain conditions, but so far, it seems to work well, albeit slowly. There is still room for improvement. Hopefully someone who knows AutoHotkey better than I do will take that on.

 

This is an AutoHotkey script triggered by Alt + 2. Not sure the code can be "translated" into Advanced Scripting or DVC... that's not a project I can take on!

 

!2::

Offset = 0

 

; Probe LEFT by character to the previous sentence delimiter. Also check for start of field/document

Loop

          {

          Clipboard := ""

          SendInput +{Left}^c

          ClipWait 0.1

          SendInput {Left}

 

          If (Clipboard = .) or (Clipboard = "!") or (Clipboard = "?")

                     {

                     SendInput {Right}

                     Break

                     }

 

          If (Clipboard = "")     ; Insertion point is at the start of field or document

                     {

                     Break

                     }

          }

 

; Probe RIGHT by character until a non-space is reached

Loop

          {

          Clipboard := ""

          SendInput +{Right}^c

          ClipWait 0.1

          SendInput {Right}

 

          If (Clipboard != " ")  ; Assume there are spaces between sentences, not tabs or anything else.

                     {

                     SendInput {Left}

                     Break

                     }

          }

 

; Probe RIGHT by character until a sentence delimiter is reached

Loop

          {

          Clipboard := ""

          SendInput +{Right}^c

          ClipWait 0.1

          SendInput {Right}

          ++Offset

 

          If (Clipboard = .) or (Clipboard = "!") or (Clipboard = "?")

                     {

                     Break

                     }

          }

 

SendInput +{Left %Offset%}

 

Return



Statistics
32177 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 0 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 311 guests browsing this forum, which makes a total of 311 users using this forum.

FuseTalk Standard Edition v4.0 - © 1999-2021 FuseTalk™ Inc. All rights reserved.