MK-316 commited on
Commit
da92bd2
1 Parent(s): 6e9c5a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from collections import Counter
3
+ import string
4
+
5
+ def process_text(text, sorting_option):
6
+ # Remove punctuation from the input text
7
+ translator = str.maketrans('', '', string.punctuation)
8
+ text = text.translate(translator)
9
+
10
+ words = text.split()
11
+
12
+ # Count word frequencies
13
+ word_counts = Counter(words)
14
+
15
+ # Sort words based on the selected option
16
+ if sorting_option == 'alphabetically':
17
+ sorted_words = sorted(word_counts.items(), key=lambda x: x[0])
18
+ elif sorting_option == 'by_frequency':
19
+ sorted_words = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
20
+ else:
21
+ sorted_words = word_counts.items()
22
+
23
+ # Get the top 5 words with their frequencies
24
+ top_5_words = sorted_words
25
+
26
+ # Format the top 5 words and frequencies as an HTML table
27
+ table_html = "<table style='width:100%'><tr><th>Word</th><th>Frequency</th></tr>"
28
+ for word, freq in top_5_words:
29
+ table_html += f"<tr><td>{word}</td><td>{freq}</td></tr>"
30
+ table_html += "</table>"
31
+
32
+ # Wrap the table in a div with a fixed height and scrolling
33
+ div_with_scroll = f"<div style='height: 200px; overflow-y: scroll;'>{table_html}</div>"
34
+
35
+ return div_with_scroll
36
+
37
+ iface = gr.Interface(
38
+ fn=process_text,
39
+ inputs=[gr.Textbox("text", label="Paste Text Here"),
40
+ gr.Radio(["alphabetically", "by_frequency", "none"], label="Select Sorting Option")],
41
+ outputs=gr.HTML(label="Top 5 Words with Frequencies")
42
+ )
43
+
44
+ iface.launch(share=True)