-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Text_Analysis.py
128 lines (111 loc) · 3.25 KB
/
Simple_Text_Analysis.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# This program takes user input and analyzes the text by counting words, sentences, and some statistics about the characters used.
# It prompts the user to input words until they enter 'stop', then analyzes the text and displays statistics.
from prettytable import PrettyTable
# Initialize a PrettyTable for displaying statistics
table = PrettyTable()
table.field_names = ["Here some statistics about your text:", "#"]
# Define a list of punctuation marks
marks = [".", "!", "?", ","]
# Initialize lists and dictionaries for storing data
words = [
"Hello",
"!",
"How",
"are",
"you",
"doing",
"today",
"?",
"I",
"'m",
"fine",
",",
"thank",
"you",
".",
]
modified_words = []
frequency = {}
max_chars = []
min_chars = []
def main(words):
# Prompt user to input words
ask_words(words)
# Print the formatted text and statistics
text, pr, word_count = print_text(words)
stat(text, pr, word_count, frequency)
def ask_words(words):
# Prompt user to input words until they enter 'stop'
word = input("Insert the word: ")
while word != "stop":
modified_word = word.lower().strip()
words.append(modified_word)
print(
"The words just introduced is: {0}\ntype stop to analyze the text so far typed".format(
modified_words
)
)
word = input("Insert the word: ")
return words
def print_text(words):
# Print the formatted text and count words and sentences
pr = 1
word_count = 0
text = ""
new_line = True
for word in words:
if new_line is True:
word.capitalize()
if word == "stop" and word not in marks:
word = "."
if word not in marks:
text = text + word + " "
word_count += 1
else:
text = text.rstrip()
text += word
if word in marks and word != ",":
text += "\n"
pr += 1
new_line = True
if word == ",":
text += " "
new_line = False
return text, pr, word_count
def stat(text, pr, word_count, frequency):
# Calculate and print statistics about the text
num_markers = 0
frequency = {}
num_words = 0
print("\n\nHere the text correctly formatted: \n{0} ".format(text))
for char in text.replace(" ", ""):
if char in marks:
num_markers += 1
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
max_char_count = max(frequency.values())
min_char_count = min(frequency.values())
for k, v in frequency.items():
if v == max_char_count:
max_chars.append(k)
if v == min_char_count:
min_chars.append(k)
table.add_row(["Total number of words given: ", word_count])
table.add_row(["Number of marks: ", num_markers])
table.add_row(["Number of marks: ", pr])
table.add_row(
[
"The most frequent ({}) character/s: ".format(max_char_count),
", ".join(max_chars),
]
)
table.add_row(
[
"The least frequent ({}) character/s: ".format(min_char_count),
", ".join(min_chars),
]
)
print(table)
main(words)