Expanded search to multiple keywords
Browse files
app.py
CHANGED
|
@@ -3,19 +3,23 @@ from main_got import extract_text
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
|
| 6 |
-
def highlight_keywords(text: str,
|
| 7 |
# Split text into sentences
|
| 8 |
sentences = text.split('. ')
|
| 9 |
highlighted_text = ""
|
| 10 |
|
| 11 |
for sentence in sentences:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
highlighted_text += f'<span style="color: red">{highlighted_sentence}.</span> '
|
| 16 |
else:
|
| 17 |
-
highlighted_text += sentence +
|
| 18 |
-
|
| 19 |
return highlighted_text
|
| 20 |
|
| 21 |
|
|
@@ -35,5 +39,6 @@ if uploaded_image is not None:
|
|
| 35 |
# Search functionality
|
| 36 |
search_query = st.text_input("Enter a keyword to search within the text")
|
| 37 |
if search_query:
|
| 38 |
-
|
|
|
|
| 39 |
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
|
| 6 |
+
def highlight_keywords(text: str, keywords: str) -> str:
|
| 7 |
# Split text into sentences
|
| 8 |
sentences = text.split('. ')
|
| 9 |
highlighted_text = ""
|
| 10 |
|
| 11 |
for sentence in sentences:
|
| 12 |
+
highlighted_sentence = sentence
|
| 13 |
+
flag = False
|
| 14 |
+
for keyword in keywords.split():
|
| 15 |
+
if keyword.lower() in sentence.lower():
|
| 16 |
+
# Highlight keyword in yellow and set flag to True to highlight whole sentence after cycle
|
| 17 |
+
highlighted_sentence = highlighted_sentence.replace(keyword, f'<mark style="background-color: yellow">{keyword}</mark>')
|
| 18 |
+
flag = True
|
| 19 |
+
if flag:
|
| 20 |
highlighted_text += f'<span style="color: red">{highlighted_sentence}.</span> '
|
| 21 |
else:
|
| 22 |
+
highlighted_text += sentence + '. '
|
|
|
|
| 23 |
return highlighted_text
|
| 24 |
|
| 25 |
|
|
|
|
| 39 |
# Search functionality
|
| 40 |
search_query = st.text_input("Enter a keyword to search within the text")
|
| 41 |
if search_query:
|
| 42 |
+
for keyword in search_query.split():
|
| 43 |
+
st.markdown(highlight_keywords(extracted_text, search_query), unsafe_allow_html=True)
|
| 44 |
|