File size: 10,474 Bytes
82ef429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6622464
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
 
82ef429
6622464
 
 
 
 
82ef429
 
6622464
82ef429
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import gradio as gr

"""Module for counting various text elements."""

def count_letters(word: str) -> int:
    """Count the number of letters in a word.
    
    Args:
        word: The word or text to count letters in.
        
    Returns:
        The number of alphabetic characters in the input.
    """
    word = str(word)  # Ensure string input
    return sum(1 for char in word if char.isalpha())

def count_words(sentence: str) -> int:
    """Count the number of words in a sentence.
    
    Args:
        sentence: The sentence or text to count words in.
        
    Returns:
        The number of words in the input text.
    """
    sentence = str(sentence)  # Ensure string input
    words = sentence.split()
    return len(words)

def count_sentences(paragraph: str) -> int:
    """Count the number of sentences in a paragraph.
    
    Args:
        paragraph: The paragraph or text to count sentences in.
        
    Returns:
        The number of sentences in the input text.
    """
    paragraph = str(paragraph)  # Ensure string input
    # Split on multiple sentence terminators
    import re
    sentences = re.split(r'[.!?]+', paragraph)
    return len([s for s in sentences if s.strip()])

def count_paragraphs(text: str) -> int:
    """Count the number of paragraphs in a text.
    
    Args:
        text: The text to count paragraphs in.
        
    Returns:
        The number of paragraphs in the input text.
    """
    text = str(text)  # Ensure string input
    paragraphs = text.split('\n\n')
    return len([p for p in paragraphs if p.strip()])

def count_vowels(word: str) -> int:
    """Count the number of vowels in a word.
    
    Args:
        word: The word or text to count vowels in.
        
    Returns:
        The number of vowel characters in the input.
    """
    word = str(word)  # Ensure string input
    vowels = 'aeiouAEIOU'
    return sum(1 for char in word if char in vowels)

def count_consonants(word: str) -> int:
    """Count the number of consonants in a word.
    
    Args:
        word: The word or text to count consonants in.
        
    Returns:
        The number of consonant characters in the input.
    """
    word = str(word)  # Ensure string input
    vowels = 'aeiouAEIOU'
    return sum(1 for char in word if char.isalpha() and char not in vowels)

def count_special_characters(text: str) -> int:
    """Count the number of special characters in a text.
    
    Args:
        text: The text to count special characters in.
        
    Returns:
        The number of special characters in the input.
    """
    text = str(text)  # Ensure string input
    special_characters = '!@#$%^&*()_+-=[]{}|;:\'",.<>?/`~'
    return sum(1 for char in text if char in special_characters)

def count_digits(text: str) -> int:
    """Count the number of digits in a text.
    
    Args:
        text: The text to count digits in.
        
    Returns:
        The number of digit characters in the input.
    """
    text = str(text)  # Ensure string input
    return sum(1 for char in text if char.isdigit())

def count_whitespaces(text: str) -> int:
    """Count the number of whitespace characters in a text.
    
    Args:
        text: The text to count whitespace characters in.
        
    Returns:
        The number of whitespace characters in the input.
    """
    text = str(text)  # Ensure string input
    return sum(1 for char in text if char.isspace())

def count_uppercase_letters(text: str) -> int:
    """Count the number of uppercase letters in a text.
    
    Args:
        text: The text to count uppercase letters in.
        
    Returns:
        The number of uppercase letters in the input.
    """
    text = str(text)  # Ensure string input
    return sum(1 for char in text if char.isupper())

def count_lowercase_letters(text: str) -> int:
    """Count the number of lowercase letters in a text.
    
    Args:
        text: The text to count lowercase letters in.
        
    Returns:
        The number of lowercase letters in the input.
    """
    text = str(text)  # Ensure string input
    return sum(1 for char in text if char.islower())

def count_unique_words(sentence: str) -> int:
    """Count the number of unique words in a sentence.
    
    Args:
        sentence: The sentence or text to count unique words in.
        
    Returns:
        The number of unique words in the input.
    """
    sentence = str(sentence)  # Ensure string input
    words = sentence.split()
    unique_words = set(words)
    return len(unique_words)

def count_syllables(word: str) -> int:
    """Count the number of syllables in a word.
    
    Args:
        word: The word to count syllables in.
        
    Returns:
        The estimated number of syllables in the word.
    """
    word = str(word).lower().strip()  # Ensure string input and normalize
    if not word:
        return 0
    
    vowels = 'aeiou'
    count = 0
    in_vowel_group = False
    
    for char in word:
        if char in vowels:
            if not in_vowel_group:
                count += 1
                in_vowel_group = True
        else:
            in_vowel_group = False
    
    # Handle silent 'e' at the end
    if word.endswith('e') and count > 1:
        count -= 1
    
    # Ensure at least one syllable for non-empty words
    return max(1, count) if any(c.isalpha() for c in word) else 0


# Create a standard Gradio interface to test the functions

with gr.Blocks() as demo:
    gr.Markdown("# Module for counting various text elements")
    
    with gr.Tab("Letter Counter"):
        with gr.Row():
            text_input_1 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_1 = gr.Number(label="Letter Count")
        count_btn_1 = gr.Button("Count Letters")
        count_btn_1.click(fn=count_letters, inputs=text_input_1, outputs=result_output_1)
    
    with gr.Tab("Word Counter"):
        with gr.Row():
            text_input_2 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_2 = gr.Number(label="Word Count")
        count_btn_2 = gr.Button("Count Words")
        count_btn_2.click(fn=count_words, inputs=text_input_2, outputs=result_output_2)
    
    with gr.Tab("Sentence Counter"):
        with gr.Row():
            text_input_3 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_3 = gr.Number(label="Sentence Count")
        count_btn_3 = gr.Button("Count Sentences")
        count_btn_3.click(fn=count_sentences, inputs=text_input_3, outputs=result_output_3)
    
    with gr.Tab("Paragraph Counter"):
        with gr.Row():
            text_input_4 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_4 = gr.Number(label="Paragraph Count")
        count_btn_4 = gr.Button("Count Paragraphs")
        count_btn_4.click(fn=count_paragraphs, inputs=text_input_4, outputs=result_output_4)
    
    with gr.Tab("Vowel Counter"):
        with gr.Row():
            text_input_5 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_5 = gr.Number(label="Vowel Count")
        count_btn_5 = gr.Button("Count Vowels")
        count_btn_5.click(fn=count_vowels, inputs=text_input_5, outputs=result_output_5)
    
    with gr.Tab("Consonant Counter"):
        with gr.Row():
            text_input_6 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_6 = gr.Number(label="Consonant Count")
        count_btn_6 = gr.Button("Count Consonants")
        count_btn_6.click(fn=count_consonants, inputs=text_input_6, outputs=result_output_6)
    
    with gr.Tab("Special Character Counter"):
        with gr.Row():
            text_input_7 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_7 = gr.Number(label="Special Character Count")
        count_btn_7 = gr.Button("Count Special Characters")
        count_btn_7.click(fn=count_special_characters, inputs=text_input_7, outputs=result_output_7)
    
    with gr.Tab("Digit Counter"):
        with gr.Row():
            text_input_8 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_8 = gr.Number(label="Digit Count")
        count_btn_8 = gr.Button("Count Digits")
        count_btn_8.click(fn=count_digits, inputs=text_input_8, outputs=result_output_8)
    
    with gr.Tab("Whitespace Counter"):
        with gr.Row():
            text_input_9 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_9 = gr.Number(label="Whitespace Count")
        count_btn_9 = gr.Button("Count Whitespaces")
        count_btn_9.click(fn=count_whitespaces, inputs=text_input_9, outputs=result_output_9)
    
    with gr.Tab("Uppercase Letter Counter"):
        with gr.Row():
            text_input_10 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_10 = gr.Number(label="Uppercase Letter Count")
        count_btn_10 = gr.Button("Count Uppercase Letters")
        count_btn_10.click(fn=count_uppercase_letters, inputs=text_input_10, outputs=result_output_10)
    
    with gr.Tab("Lowercase Letter Counter"):
        with gr.Row():
            text_input_11 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_11 = gr.Number(label="Lowercase Letter Count")
        count_btn_11 = gr.Button("Count Lowercase Letters")
        count_btn_11.click(fn=count_lowercase_letters, inputs=text_input_11, outputs=result_output_11)
    
    with gr.Tab("Unique Word Counter"):
        with gr.Row():
            text_input_12 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_12 = gr.Number(label="Unique Word Count")
        count_btn_12 = gr.Button("Count Unique Words")
        count_btn_12.click(fn=count_unique_words, inputs=text_input_12, outputs=result_output_12)
    
    with gr.Tab("Syllable Counter"):
        with gr.Row():
            text_input_13 = gr.Textbox(label="Enter text", placeholder="Type your text here...")
            result_output_13 = gr.Number(label="Syllable Count")
        count_btn_13 = gr.Button("Count Syllables")
        count_btn_13.click(fn=count_syllables, inputs=text_input_13, outputs=result_output_13)

if __name__ == "__main__":
    demo.launch()