forked from bradleecrockett/SentenceLength
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentenceLength.py
More file actions
66 lines (52 loc) · 2.22 KB
/
SentenceLength.py
File metadata and controls
66 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sys
file_paths = sys.argv[1:]
def findAverageSentence(filePath, delimiters, minLength):
try:
file = open(filePath)
except:
print("This file was either invalid or not found. Please restart the program and use a proper file name.")
exit()
textInFile = file.read()
words = textInFile.split(" ")
# print(words)
sentences = textInFile.split(".")
sentences.remove("")
# print(sentences)
if (len(sentences) > 0):
aveSentenceLength = len(words) / len(sentences)
else:
aveSentenceLength = words
return round(aveSentenceLength, 0)
def main():
userFile = input("Enter the file path to the .txt file you wish to analyze. ")
#checks if userFile is a .txt or not vv
if not os.path.isfile(userFile):
print("File not .txt please enter a new file")
userFile = input("Enter the file path to the .txt file you wish to analyze. ")
userDelimeters = input("Enter the characters (punctuation) that you want to be sentence "
"delimiters separated by spaces. ")
userDelimeters = userDelimeters.split(" ")
minLength = eval(input("Enter the minimum length of a word (must be a positive integer). "))
if (minLength < 1):
minLength = 1
while True:
go_again = input("Would you like to enter a file? y/n: ")
if go_again == "y":
if (file_paths):
userFile = file_paths[0]
else:
userFile = input("Enter the file path to the .txt file you wish to analyze.")
userDelimeters = input("Enter the characters (punctuation) that you want to be sentence "
"delimiters separated by spaces")
userDelimeters = userDelimeters.split(" ")
minLength = eval(input("Enter the minimum length of a word (must be a positive integer)"))
if (minLength < 1):
minLength = 1
print("The average sentence length is", findAverageSentence(userFile, userDelimeters, minLength))
elif go_again == "n":
break
if __name__ == "__main__":
main()
exit = input("Press enter to stop")
# try to keep whose ever this addition was