Python Forum
Removing leading whitespaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing leading whitespaces
#1
Hi all

I am trying to build a simple (semi) live dictation script with Whisper. As Whisper do not support commands out of the box (eg 'new paragraph') I would need to catch these. I am using the

 keyboard 
module to enter the text outputted by Whisper into various apps (such as my word processor or web browser), via its write function. My script will then capture these commands from Whisper's output and translate them into the required action (in this example. pressing enter twice when 'new paragraph' is detected). In this case I am using re.sub to substitute 'new paragraph' with the escape sequence '\n\n'.

The part I am struggling with is that by using this, the paragraph output always have an indent which I do not want. This is my script:

import keyboard
import re

#dictionary containing list of spoken command and actions to be substituted
command_dict =COMMAND_DICT = {'new paragraph': '\n\n', 'question mark': '\b?'}

def write(text):

   for k,v in command_dict.items():   
       text = re.sub(k,v , text, flags = re.IGNORECASE)
       
   for line in text.splitlines():
       if not line.isspace():
           line = line.lstrip()
   
   keyboard.write(text)

#the output of Whisper will be fed into write()
write('I am a new paragraph old man question mark')
Desired output:
Output:
I am a old man?
What I get:
Output:
I am a old man?
I also tried using '\b\n\n' or '\n\n\b' for the value of the 'new paragraph' key, like the question mark key, but it didn't work (it worked as expected for the 'question mark' key).

Some help would be appreciated.
Reply
#2
You have spaces around the word "paragraph", and nothing happens to them. I'm going to replace them with dots here just for visibility. But you might start with the string A.paragraph.B. After substitution that becomes A.\n\n.B.

Printed, that would be:
Output:
A. .B
You could capture optional whitespace around the keywords as part of your regex and that might improve things. Or you could remove any whitespace following a newline if you never want any indents (intentional or not).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing leading\trailing spaces azizrasul 8 2,706 Oct-23-2022, 11:06 PM
Last Post: azizrasul
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,764 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  -i option changes sys.path (removes leading empty string '') markanth 6 1,991 Aug-26-2022, 09:27 PM
Last Post: markanth
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,043 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  How to keep leading zeros with pandas? eeps24 1 6,567 May-20-2020, 07:51 PM
Last Post: deanhystad
  Cancelling 'open directory' leading to crash Fairbz_ 1 2,189 May-08-2020, 03:14 PM
Last Post: DPaul
  leading zero number formatting RedSkeleton007 3 3,941 Jan-27-2019, 04:56 PM
Last Post: RedSkeleton007
  Probs with leading zeros falling away :-) Badosc 2 2,879 Dec-04-2018, 08:57 PM
Last Post: Badosc
  leading zeros kerzol81 7 7,092 Apr-23-2017, 03:53 PM
Last Post: kerzol81
  program is related to leading zeros amrita 3 3,839 Apr-13-2017, 06:57 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020