DivYonko commited on
Commit
47614fa
·
1 Parent(s): 5a13d2c

Smart filter: scan full history for last N matching messages + keyword accuracy fixes

Browse files
Files changed (1) hide show
  1. pages/comments.py +52 -15
pages/comments.py CHANGED
@@ -104,24 +104,61 @@ with _cf3:
104
  with _cf4:
105
  _search_term = st.text_input("Search messages", placeholder="Filter by keyword...")
106
 
107
- _filtered = _feed_df.copy() if not _feed_df.empty else pd.DataFrame()
108
- if not _filtered.empty:
109
- if _sentiment_filter != "All":
110
- _filtered = _filtered[_filtered["sentiment"] == _sentiment_filter]
111
- if _topic_filter != "All":
112
- _filtered = _filtered[_filtered["topic"] == _topic_filter]
113
- if _action_type_filter != "All":
114
- if "action_type" in _filtered.columns:
115
- _filtered = _filtered[_filtered["action_type"] == _action_type_filter]
116
- if _search_term:
117
- _filtered = _filtered[_filtered["text"].str.contains(_search_term, case=False, na=False)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  _feed_hdr, _feed_dl = st.columns([3, 1])
120
  with _feed_hdr:
121
- st.markdown(
122
- f'<div style="font-size:0.78rem;color:var(--text-3);margin-bottom:12px;">Showing {len(_filtered)} of {len(_feed_df)} messages</div>',
123
- unsafe_allow_html=True
124
- )
 
 
 
 
 
 
 
 
125
  with _feed_dl:
126
  if not _filtered.empty:
127
  _export_cols = [c for c in ["author", "text", "sentiment", "confidence", "topic", "time"] if c in _filtered.columns]
 
104
  with _cf4:
105
  _search_term = st.text_input("Search messages", placeholder="Filter by keyword...")
106
 
107
+ # ── Smart filtering: when a filter is active, scan full history
108
+ # to find the last msg_limit matching messages instead of filtering
109
+ # only within the current window.
110
+ _any_filter = (
111
+ _sentiment_filter != "All"
112
+ or _topic_filter != "All"
113
+ or _action_type_filter != "All"
114
+ or bool(_search_term)
115
+ )
116
+
117
+ if _any_filter:
118
+ # Load full history for the feed key
119
+ _full_raw = load_stream_data(_feed_key)
120
+ if _full_raw:
121
+ _full_df = pd.DataFrame(_full_raw)
122
+ _full_df["sentiment"] = _full_df["sentiment"].apply(clean_sentiment)
123
+ _full_df["topic"] = _full_df["topic"].apply(clean_topic) if "topic" in _full_df.columns else "General"
124
+ # Apply filters on full history
125
+ _filtered = _full_df.copy()
126
+ if _sentiment_filter != "All":
127
+ _filtered = _filtered[_filtered["sentiment"] == _sentiment_filter]
128
+ if _topic_filter != "All":
129
+ _filtered = _filtered[_filtered["topic"] == _topic_filter]
130
+ if _action_type_filter != "All":
131
+ if "action_type" in _filtered.columns:
132
+ _filtered = _filtered[_filtered["action_type"] == _action_type_filter]
133
+ if _search_term:
134
+ _filtered = _filtered[_filtered["text"].str.contains(_search_term, case=False, na=False)]
135
+ # Cap to last msg_limit matching results
136
+ if len(_filtered) > msg_limit:
137
+ _filtered = _filtered.iloc[-msg_limit:]
138
+ else:
139
+ _filtered = pd.DataFrame()
140
+ _total_matching = len(_filtered)
141
+ _total_scanned = len(_full_raw) if _full_raw else 0
142
+ else:
143
+ # No filter — just use the window
144
+ _filtered = _feed_df.copy() if not _feed_df.empty else pd.DataFrame()
145
+ _total_matching = len(_filtered)
146
+ _total_scanned = len(_feed_df)
147
 
148
  _feed_hdr, _feed_dl = st.columns([3, 1])
149
  with _feed_hdr:
150
+ if _any_filter:
151
+ st.markdown(
152
+ f'<div style="font-size:0.78rem;color:var(--text-3);margin-bottom:12px;">'
153
+ f'Showing {len(_filtered)} matching (last {msg_limit} from {_total_scanned} total)</div>',
154
+ unsafe_allow_html=True
155
+ )
156
+ else:
157
+ st.markdown(
158
+ f'<div style="font-size:0.78rem;color:var(--text-3);margin-bottom:12px;">'
159
+ f'Showing {len(_filtered)} of {len(_feed_df)} messages</div>',
160
+ unsafe_allow_html=True
161
+ )
162
  with _feed_dl:
163
  if not _filtered.empty:
164
  _export_cols = [c for c in ["author", "text", "sentiment", "confidence", "topic", "time"] if c in _filtered.columns]