fix(req): use correct requirements
Browse files- app.py +69 -36
- requirements.txt +0 -0
app.py
CHANGED
@@ -10,14 +10,17 @@ words = []
|
|
10 |
with open("words.txt") as file:
|
11 |
words = file.read().splitlines()
|
12 |
words = [word.strip() for word in words]
|
13 |
-
|
14 |
-
df = pd.DataFrame(
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
# DFA function call
|
19 |
dfa = generate_dfa(words)
|
20 |
|
|
|
21 |
# Generate examples || RandomSentence is not the best way to generate examples || Should be replaced with self-generated examples
|
22 |
def generateExamples():
|
23 |
s = RandomSentence()
|
@@ -26,6 +29,7 @@ def generateExamples():
|
|
26 |
examples.append(s.sentence())
|
27 |
return examples
|
28 |
|
|
|
29 |
# Color match function
|
30 |
def color_match(text):
|
31 |
colored_text = []
|
@@ -33,9 +37,13 @@ def color_match(text):
|
|
33 |
|
34 |
# Get the result of the DFA check on the input text
|
35 |
match_dict = dfa.check(text)
|
36 |
-
|
37 |
# Flatten the match_dict into a list of tuples and sort by the start index
|
38 |
-
matches = sorted(
|
|
|
|
|
|
|
|
|
39 |
|
40 |
for start, end, word in matches:
|
41 |
colored_text.append(text[pointer:start])
|
@@ -45,24 +53,25 @@ def color_match(text):
|
|
45 |
# print(f"Colored Text: {colored_text}")
|
46 |
# Move the pointer to the end of the match
|
47 |
pointer = end + 1
|
48 |
-
|
49 |
# Add remaining text
|
50 |
colored_text.append(text[pointer:])
|
51 |
# print(f"Text before merge: {colored_text}")
|
52 |
# Combine the strings
|
53 |
-
colored_text =
|
54 |
# print(f"Colored Text after merging: {colored_text}")
|
55 |
-
|
56 |
# Call getOccurrences function and get the DataFrame
|
57 |
occurrences_df = getOccurrences(text)
|
58 |
# print(f"Occurences_df: {occurrences_df}")
|
59 |
-
|
60 |
# Call getPositions function and get the DataFrame
|
61 |
positions_df = getPositions(text)
|
62 |
# print(f"Positions_df: {positions_df}")
|
63 |
-
|
64 |
return colored_text, occurrences_df, positions_df
|
65 |
|
|
|
66 |
# Get occurrences function
|
67 |
def getOccurrences(text):
|
68 |
match_dict = dfa.check(text)
|
@@ -73,40 +82,47 @@ def getOccurrences(text):
|
|
73 |
# Store the word and the number of occurrences in the wordCount dictionary
|
74 |
wordCount[word] = len(positions)
|
75 |
# print(f"Word Count: {wordCount}")
|
76 |
-
|
77 |
# Convert the wordCount dictionary to a DataFrame
|
78 |
-
occurrences_df = pd.DataFrame(
|
|
|
|
|
79 |
# print(occurences_df)
|
80 |
return occurrences_df
|
81 |
|
|
|
82 |
# Get positions function
|
83 |
def getPositions(text):
|
84 |
match_dict = dfa.check(text)
|
85 |
wordPositions = {}
|
86 |
for word, positions in match_dict.items():
|
87 |
# Convert the list of tuples to a string
|
88 |
-
positions_str =
|
89 |
print(f"Word: {word}, Positions: {positions_str}")
|
90 |
# Store the word and the positions string in the wordPositions dictionary
|
91 |
wordPositions[word] = positions_str
|
92 |
print(f"Word Positions: {wordPositions}")
|
93 |
|
94 |
# Convert the wordPositions dictionary to a DataFrame
|
95 |
-
positions_df = pd.DataFrame(
|
|
|
|
|
96 |
print(f"Positions_df: {positions_df}")
|
97 |
-
|
98 |
return positions_df
|
99 |
|
|
|
100 |
# Search and display function
|
101 |
def search_and_display(search_query):
|
102 |
# Filter the DataFrame based on the search query
|
103 |
-
filtered_df = df[df[
|
104 |
# print(f"Filtered text: {filtered_df}")
|
105 |
return filtered_df
|
106 |
|
|
|
107 |
# CSS styling
|
108 |
# css = """
|
109 |
-
#warning {background-color: #FFCCCB}
|
110 |
# .feedback textarea {font-size: 24px !important}
|
111 |
# """
|
112 |
|
@@ -117,52 +133,69 @@ def search_and_display(search_query):
|
|
117 |
|
118 |
# Gradio UI
|
119 |
with gr.Blocks() as demo:
|
120 |
-
|
121 |
# Title block
|
122 |
# Apply CSS styling to the title
|
123 |
-
title = gr.HTML(
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
126 |
# Search block
|
127 |
-
search = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
128 |
search_btn = gr.Button(value="Search")
|
129 |
resultSearch = gr.Dataframe(df, height=300)
|
130 |
|
131 |
search_btn.click(
|
132 |
search_and_display, inputs=[search], outputs=[resultSearch], api_name=False
|
133 |
)
|
134 |
-
|
135 |
# Adding a line break
|
136 |
line_break = gr.HTML("<br>")
|
137 |
-
|
138 |
# Text block for DFA and color match
|
139 |
textTitle = gr.HTML("<h2>Try it here!</h2>")
|
140 |
-
text = gr.Textbox(
|
|
|
|
|
|
|
|
|
141 |
submit_btn = gr.Button(value="Submit")
|
142 |
-
|
143 |
# Examples block
|
144 |
examples_data = generateExamples()
|
145 |
examples = gr.Examples(
|
146 |
examples=examples_data,
|
147 |
inputs=[text],
|
148 |
)
|
149 |
-
|
150 |
# Result block
|
151 |
-
resultTitle = gr.HTML("<h2 style='color: gold; margin-bottom: 5px'>Result</h2>")
|
152 |
result = gr.HTML("<p></p>")
|
153 |
-
|
154 |
# Occurrences block
|
155 |
-
occurrencesTitle = gr.HTML(
|
|
|
|
|
156 |
occurrences = gr.Dataframe()
|
157 |
|
158 |
# Position block
|
159 |
positionTitle = gr.HTML("<h2 style='color: gold;'>Position</h2>")
|
160 |
position = gr.Dataframe()
|
161 |
-
|
162 |
submit_btn.click(
|
163 |
-
color_match,
|
|
|
|
|
|
|
164 |
)
|
165 |
|
166 |
# Launch the app
|
167 |
-
demo.launch(
|
168 |
-
|
|
|
10 |
with open("words.txt") as file:
|
11 |
words = file.read().splitlines()
|
12 |
words = [word.strip() for word in words]
|
13 |
+
|
14 |
+
df = pd.DataFrame(
|
15 |
+
{
|
16 |
+
"Words": words,
|
17 |
+
}
|
18 |
+
)
|
19 |
+
|
20 |
# DFA function call
|
21 |
dfa = generate_dfa(words)
|
22 |
|
23 |
+
|
24 |
# Generate examples || RandomSentence is not the best way to generate examples || Should be replaced with self-generated examples
|
25 |
def generateExamples():
|
26 |
s = RandomSentence()
|
|
|
29 |
examples.append(s.sentence())
|
30 |
return examples
|
31 |
|
32 |
+
|
33 |
# Color match function
|
34 |
def color_match(text):
|
35 |
colored_text = []
|
|
|
37 |
|
38 |
# Get the result of the DFA check on the input text
|
39 |
match_dict = dfa.check(text)
|
40 |
+
|
41 |
# Flatten the match_dict into a list of tuples and sort by the start index
|
42 |
+
matches = sorted(
|
43 |
+
(start, end, word)
|
44 |
+
for word, indices in match_dict.items()
|
45 |
+
for start, end in indices
|
46 |
+
)
|
47 |
|
48 |
for start, end, word in matches:
|
49 |
colored_text.append(text[pointer:start])
|
|
|
53 |
# print(f"Colored Text: {colored_text}")
|
54 |
# Move the pointer to the end of the match
|
55 |
pointer = end + 1
|
56 |
+
|
57 |
# Add remaining text
|
58 |
colored_text.append(text[pointer:])
|
59 |
# print(f"Text before merge: {colored_text}")
|
60 |
# Combine the strings
|
61 |
+
colored_text = "".join(colored_text)
|
62 |
# print(f"Colored Text after merging: {colored_text}")
|
63 |
+
|
64 |
# Call getOccurrences function and get the DataFrame
|
65 |
occurrences_df = getOccurrences(text)
|
66 |
# print(f"Occurences_df: {occurrences_df}")
|
67 |
+
|
68 |
# Call getPositions function and get the DataFrame
|
69 |
positions_df = getPositions(text)
|
70 |
# print(f"Positions_df: {positions_df}")
|
71 |
+
|
72 |
return colored_text, occurrences_df, positions_df
|
73 |
|
74 |
+
|
75 |
# Get occurrences function
|
76 |
def getOccurrences(text):
|
77 |
match_dict = dfa.check(text)
|
|
|
82 |
# Store the word and the number of occurrences in the wordCount dictionary
|
83 |
wordCount[word] = len(positions)
|
84 |
# print(f"Word Count: {wordCount}")
|
85 |
+
|
86 |
# Convert the wordCount dictionary to a DataFrame
|
87 |
+
occurrences_df = pd.DataFrame(
|
88 |
+
list(wordCount.items()), columns=["Words", "Occurrences"]
|
89 |
+
)
|
90 |
# print(occurences_df)
|
91 |
return occurrences_df
|
92 |
|
93 |
+
|
94 |
# Get positions function
|
95 |
def getPositions(text):
|
96 |
match_dict = dfa.check(text)
|
97 |
wordPositions = {}
|
98 |
for word, positions in match_dict.items():
|
99 |
# Convert the list of tuples to a string
|
100 |
+
positions_str = ", ".join(map(str, positions))
|
101 |
print(f"Word: {word}, Positions: {positions_str}")
|
102 |
# Store the word and the positions string in the wordPositions dictionary
|
103 |
wordPositions[word] = positions_str
|
104 |
print(f"Word Positions: {wordPositions}")
|
105 |
|
106 |
# Convert the wordPositions dictionary to a DataFrame
|
107 |
+
positions_df = pd.DataFrame(
|
108 |
+
list(wordPositions.items()), columns=["Words", "Positions"]
|
109 |
+
)
|
110 |
print(f"Positions_df: {positions_df}")
|
111 |
+
|
112 |
return positions_df
|
113 |
|
114 |
+
|
115 |
# Search and display function
|
116 |
def search_and_display(search_query):
|
117 |
# Filter the DataFrame based on the search query
|
118 |
+
filtered_df = df[df["Words"].str.contains(search_query)]
|
119 |
# print(f"Filtered text: {filtered_df}")
|
120 |
return filtered_df
|
121 |
|
122 |
+
|
123 |
# CSS styling
|
124 |
# css = """
|
125 |
+
# warning {background-color: #FFCCCB}
|
126 |
# .feedback textarea {font-size: 24px !important}
|
127 |
# """
|
128 |
|
|
|
133 |
|
134 |
# Gradio UI
|
135 |
with gr.Blocks() as demo:
|
136 |
+
|
137 |
# Title block
|
138 |
# Apply CSS styling to the title
|
139 |
+
title = gr.HTML(
|
140 |
+
"<h1 style='color: gold; margin-bottom: 0px font-weight:bold'>English Conjuction Finder</h1>"
|
141 |
+
)
|
142 |
+
description = gr.HTML(
|
143 |
+
"<p style='color: #fef9c3;'>Enter a text and see the words that are accepted by the DFA highlighted in red.</p>"
|
144 |
+
)
|
145 |
+
|
146 |
# Search block
|
147 |
+
search = gr.Textbox(
|
148 |
+
label="Search",
|
149 |
+
placeholder="Search accepted words here",
|
150 |
+
lines=1,
|
151 |
+
info="List of accpetable words in DFA",
|
152 |
+
)
|
153 |
search_btn = gr.Button(value="Search")
|
154 |
resultSearch = gr.Dataframe(df, height=300)
|
155 |
|
156 |
search_btn.click(
|
157 |
search_and_display, inputs=[search], outputs=[resultSearch], api_name=False
|
158 |
)
|
159 |
+
|
160 |
# Adding a line break
|
161 |
line_break = gr.HTML("<br>")
|
162 |
+
|
163 |
# Text block for DFA and color match
|
164 |
textTitle = gr.HTML("<h2>Try it here!</h2>")
|
165 |
+
text = gr.Textbox(
|
166 |
+
label="Text",
|
167 |
+
placeholder="Enter text here",
|
168 |
+
info="Enter text to check for DFA match",
|
169 |
+
)
|
170 |
submit_btn = gr.Button(value="Submit")
|
171 |
+
|
172 |
# Examples block
|
173 |
examples_data = generateExamples()
|
174 |
examples = gr.Examples(
|
175 |
examples=examples_data,
|
176 |
inputs=[text],
|
177 |
)
|
178 |
+
|
179 |
# Result block
|
180 |
+
resultTitle = gr.HTML("<h2 style='color: gold; margin-bottom: 5px'>Result</h2>")
|
181 |
result = gr.HTML("<p></p>")
|
182 |
+
|
183 |
# Occurrences block
|
184 |
+
occurrencesTitle = gr.HTML(
|
185 |
+
"<h2 style='color: gold; margin-bottom: 5px'>Occurrences</h2>"
|
186 |
+
)
|
187 |
occurrences = gr.Dataframe()
|
188 |
|
189 |
# Position block
|
190 |
positionTitle = gr.HTML("<h2 style='color: gold;'>Position</h2>")
|
191 |
position = gr.Dataframe()
|
192 |
+
|
193 |
submit_btn.click(
|
194 |
+
color_match,
|
195 |
+
inputs=[text],
|
196 |
+
outputs=[result, occurrences, position],
|
197 |
+
api_name=False,
|
198 |
)
|
199 |
|
200 |
# Launch the app
|
201 |
+
demo.launch()
|
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|