[Python][UDT] Automatically set "Time Estimate" for chapters of a book

Moderators: abstr, Niko

Post Reply
Posts: 239
Joined: Wed Sep 18, 2019 6:48 am

[Python][UDT] Automatically set "Time Estimate" for chapters of a book

Post by Niko » Sat Apr 25, 2020 3:54 pm

I usually read a bunch of books.Most of them are free e-books from gutenberg.org.
But sometimes I don't have enough time for reading, only 20-40 minutes a day are free.
Some novels are structured pretty good, and it usually takes 30+- 5 minutes to read a chapter. Other novels don't have such a good structure or separation by time periods and it takes 1 or 1.5 hour to finish a chapter of a novel.
ToDoList .gif
ToDoList .gif (964.15 KiB) Viewed 1817 times
So I decided to write a python script that would allow me to better understand how much time it would take to read some chapter of a book.
The script do the following:
1) Opens .txt file
2) Splits the file by chapter into separate chapters
3) Counts the number of words in a chapter and divides it by your reading speed (words per minute)
4) Sends to ToDoList the name of a chapter and 'time estimate'
1.Create a new file - name.py
2. Copy and paste there the text below.
3. Make necessary changes in the script (see comments in the file)
4. Run and enjoy reading=).

Code: Select all

class BookParsing():
    # self - path to the book
    def __init__(self,path,wpm=None, start=None, chapter_name = None):
        self.path = path
        self.wpm = wpm
        self.start = start
        self.chapter_name = chapter_name
    # return raw text
    def split_text (self, text=None, chapters_len = None):
        with open (self.path, 'r', encoding='utf8') as rawtext:
            openfile_germinal = rawtext.read()

        text_split = openfile_germinal.split(self.chapter_name)
    # replace \n
        listtoappend = []
        for one_chapter in text_split:
            listtoappend.append(one_chapter.replace("\n", ''))
        self.text = listtoappend
        self.chapters_len  = len(self.text)
    def wpm_list (self):
        wpm_chapters_list = []
        # Start from chapter 1 
        # How many chapters are in the book? 45-30 - start from 15
        for each in self.text[len(self.text)-self.start:]:
            round_me = round((len(each)/self.wpm), 2)
        return wpm_chapters_list

### Send to ToDoList 
def td_list_send(word_perminunte_list):
    for name,estimate in zip(range(1,len(word_perminunte_list)+1),word_perminunte_list):
        pathtotdl = "C:/PATH_TO_TDL/ToDoList.exe /$(seltid)"  #### Path to your ToDoList
        variables1 = " -nt "
        variables2 = " -te "
        cmd = pathtotdl+variables1+str("Chapter_" + str(name))+variables2+str(estimate/60)
        sendcommand = os.system(cmd)

from time import sleep
import os
# 1 - The path to the file you want to read
# 2 - WOrds per minute
# 3 - Chapters in the book
# 4 - The name of chapters (ususally,UPPERCASE)

finn = BookParsing('HackFinn.txt', 800, 43, "CHAPTER ")   # HackFinn.txt - the name of a book you want to parse

#### Split text into a list Don't rename
#### Send to ToDoList 
# don't forget to replace the path to ToDoList to yours (pathtotdl - line 33)


Post Reply