Ask Your Question
0

Macro to select a sentence in Python

asked 2018-11-19 16:46:14 +0200

smallcog gravatar image

Hello,

I'm hoping to write a macro that will perform the selection of text on a sentence by sentence basis.

i.e. a user hits a key-sequence and the current sentence is selected (like what happens when you triple left-click on a word. The user then hits another key-sequence and the next or previous sentence is selected. In this manner the user can navigate on a sentence by sentence basis in a document. (end use is an improved accessibility project)

My intial plan was to record a macro in basic and this would give the the pointer to the API call I need to make in Python. However when i triple click on a word, and thus select the sentence, nothing is recorded in the macro.

Any ideas ?

Best regards,

Colm

edit retag flag offensive close merge delete

Comments

I would imagine this could be a bit subjective or with imperfect results, but you could explode into an array after every period followed by a space and/or after a new line character? Possibly also use regular expression selectors? Sorry, I'm no python expert. Some world languages might not have the period-space-as-end-of-sentence convention, and stuff like "Mr. Smith" will probably prove difficult, but maybe perfection isn't important for your solution.

PhLo gravatar imagePhLo ( 2018-11-19 17:38:46 +0200 )edit

Thanks for the comment.

I was hoping to use what is already working in LibreOffice. i.e. the triple click already selects the current sentence perfectly, no need to worry about the regex side of things. It's that I have difficulty findign what I need in the LibreOffice API side of things

smallcog gravatar imagesmallcog ( 2018-11-19 17:57:12 +0200 )edit

There's definitely less documentation for macro scripting in languages other than the built-in flavor of Basic, which is why I chose it over Python and Javascript. Consequently, this answer might be of some use. Oops, didn't mean to answer. Changed to comment.

PhLo gravatar imagePhLo ( 2018-11-19 18:13:58 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-03-01 15:58:59 +0200

jmz gravatar image

updated 2019-03-01 16:01:22 +0200

Hi,

Here are two methods for navigating through sentences, which you can assign to the control you want:

def select_next_sentence(event=None):
    doc = XSCRIPTCONTEXT.getDocument()
    controller = doc.CurrentController
    T = doc.Text
    cursor = T.createTextCursor()
    cursor.gotoRange(controller.ViewCursor.End, False)
    if cursor.isEndOfSentence():
        cursor.gotoNextSentence(False)
    else:
        cursor.gotoStartOfSentence(False)
    cursor.gotoEndOfSentence(True)
    controller.select(cursor)

def select_previous_sentence(event=None):
    doc = XSCRIPTCONTEXT.getDocument()
    controller = doc.CurrentController
    T = doc.Text
    cursor = T.createTextCursor()
    cursor.gotoRange(controller.ViewCursor.End, False)
    if cursor.isEndOfSentence():
        cursor.gotoPreviousSentence(False)
    else:
        cursor.gotoStartOfSentence(False)
    cursor.gotoEndOfSentence(True)
    controller.select(cursor)

Regards.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-11-19 16:46:14 +0200

Seen: 141 times

Last updated: Mar 01