|
| 1 | +# Author : Pranjal Dubey |
| 2 | +# Created : 29 Dec 2015 |
| 3 | +# Last Modified : |
| 4 | +# Version : 1.0 |
| 5 | +# Modifications : |
| 6 | +# Description : Parse text data from file/clipboard memory, count the number of words in it and copy the estimated reading time embeded in html into clipboard, assuming that average reading time is 200 wpm and where the average word length is taken as 5.1 letters |
| 7 | +# Known Bugs : |
| 8 | + |
| 9 | + |
| 10 | +#importing vendor modules |
| 11 | +import sys, pyperclip |
| 12 | + |
| 13 | + |
| 14 | +def countCharacters(fileData): |
| 15 | + ''' |
| 16 | + function to count the number of letters in a given string |
| 17 | + ''' |
| 18 | + count = 0 |
| 19 | + |
| 20 | + fileData = fileData.lower() |
| 21 | + |
| 22 | + for i in range(len(fileData)): |
| 23 | + if fileData[i] in 'abcdefghijklmnopqrstuvwxyz': |
| 24 | + count += 1 |
| 25 | + estimatedReadingTime(count//5.1) |
| 26 | + |
| 27 | +def estimatedReadingTime(words): |
| 28 | + ''' |
| 29 | + function to calculate estimated reading time from input words; then embed that value into predefined html code and load that into clipboard |
| 30 | + ''' |
| 31 | + |
| 32 | + #finding time |
| 33 | + time = str(round((1/200) * words, 2)).split(".") |
| 34 | + |
| 35 | + #number of hours |
| 36 | + hours = int(time[0]) // 60 |
| 37 | + |
| 38 | + #number of mins |
| 39 | + minutes = int(time[0]) % 60 |
| 40 | + |
| 41 | + #number of seconds |
| 42 | + seconds = round(int(time[1]) * 0.6) |
| 43 | + |
| 44 | + #converting to human readable form |
| 45 | + copyBuffer = '' |
| 46 | + |
| 47 | + if hours != 0: |
| 48 | + copyBuffer += str(hours) + ' ' |
| 49 | + if hours == 1: |
| 50 | + copyBuffer += 'hour ' |
| 51 | + else: |
| 52 | + copyBuffer += 'hours ' |
| 53 | + |
| 54 | + |
| 55 | + if minutes != 0: |
| 56 | + copyBuffer += str(minutes) + ' ' |
| 57 | + if minutes == 1: |
| 58 | + copyBuffer += 'minute ' |
| 59 | + else: |
| 60 | + copyBuffer += 'minutes ' |
| 61 | + |
| 62 | + copyBuffer += str(seconds) + ' seconds' |
| 63 | + |
| 64 | + htmlPrefixCode = "<div style=\"background:#f1ebeb;padding:1%;width:auto;margin-bottom:2%;\">Estimated reading time : " |
| 65 | + htmlSuffixCode = "</div>" |
| 66 | + |
| 67 | + copyBuffer = htmlPrefixCode + copyBuffer + htmlSuffixCode |
| 68 | + |
| 69 | + pyperclip.copy(copyBuffer) |
| 70 | + |
| 71 | + |
| 72 | + print("\nWords : " + str(words) + "\nCode copied in Clipboard!\n") |
| 73 | + |
| 74 | + |
| 75 | +#if no command line argument is passed, the script loads data from clipboard |
| 76 | +if len(sys.argv) > 1: |
| 77 | + filename = sys.argv[1] |
| 78 | + |
| 79 | + fileHandle = open(filename, "r") |
| 80 | + |
| 81 | + #number of words in file |
| 82 | + fileData = fileHandle.read() |
| 83 | + |
| 84 | + if fileData == '': |
| 85 | + print("File is empty!") |
| 86 | + else: |
| 87 | + countCharacters(fileData) |
| 88 | + |
| 89 | +else: |
| 90 | + #loading data from clipboard |
| 91 | + fileData = pyperclip.paste() |
| 92 | + |
| 93 | + if fileData == '': |
| 94 | + print("Clipboard is empty!") |
| 95 | + else: |
| 96 | + countCharacters(fileData) |
0 commit comments