rk404 commited on
Commit
07999ad
1 Parent(s): b3a1133

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -121,22 +121,24 @@ if uploaded_file is not None:
121
 
122
  # Step 9: Implement a search functionality on already extracted text
123
  if st.session_state.extracted_text:
124
- with st.form(key='search_form'):
125
- search_query = st.text_input("Enter keyword to search within the extracted text:")
126
- search_button = st.form_submit_button("Search")
127
-
128
- if search_button and search_query:
129
- # Perform case-insensitive search and highlight the matches
130
- extracted_text = st.session_state.extracted_text # Use already extracted text
131
- matches = re.finditer(re.escape(search_query), extracted_text, re.IGNORECASE)
132
-
133
- highlighted_text = extracted_text
134
- result = ''
135
- for match in matches:
136
- start, end = match.span()
137
- result = "**" + highlighted_text[start:end] + "**"
 
 
138
 
139
  st.subheader("Search Results:")
140
- if result == '':
141
  st.markdown('Not forund')
142
  st.markdown(result)
 
121
 
122
  # Step 9: Implement a search functionality on already extracted text
123
  if st.session_state.extracted_text:
124
+ with st.form(key='text_search_form'):
125
+ search_input = st.text_input("Enter a keyword to search within the extracted text:")
126
+ search_action = st.form_submit_button("Search")
127
+
128
+ if search_action and search_input:
129
+ # Split the extracted text into lines for searching
130
+ full_text = st.session_state.exttracted_text
131
+ lines = full_text.split('\n')
132
+
133
+ results = []
134
+ # Search for keyword in each line and collect lines that contain the keyword
135
+ for line in lines:
136
+ if re.search(re.escape(search_input), line, re.IGNORECASE):
137
+ # Highlight keyword in the line
138
+ highlighted_line = re.sub(f"({re.escape(search_input)})", r"*\1*", line, flags=re.IGNORECASE)
139
+ results.append(highlighted_line)
140
 
141
  st.subheader("Search Results:")
142
+ if result == []:
143
  st.markdown('Not forund')
144
  st.markdown(result)