import streamlit as st import json from thefuzz import process, fuzz # --- CONFIGURATION --- DATA_FILE = "manual_data.json" # --- LOAD DATA --- @st.cache_data def load_data(): try: with open(DATA_FILE, "r") as f: return json.load(f) except FileNotFoundError: return None data = load_data() # --- UI LAYOUT --- st.set_page_config(page_title="Navy Acronym Search", layout="centered") st.title("⚓ Navy Acronym Reference") if data is None: st.error(f"Error: Could not find '{DATA_FILE}'. Please ensure the file is uploaded.") else: st.markdown("Enter an acronym below. The system will look for exact matches and close approximations.") # Direct Search Input query = st.text_input("Acronym", placeholder="e.g., CENTRIXS").strip().upper() if query: acronyms_db = data.get('acronyms', {}) # 1. EXACT MATCH CHECK (Fastest/Best) exact_match = acronyms_db.get(query) if exact_match: st.success(f"**{query}**") st.info(exact_match) # 2. FUZZY SEARCH (If exact match is found, we still show these if they are relevant variants) # OR if exact match is NOT found, these become the primary results. # Get list of all acronym keys all_keys = list(acronyms_db.keys()) # Extract top 5 matches that have a similarity score > 60 # limit=5 ensures we don't flood the screen # scorer=fuzz.partial_ratio is great for "CENTRIXS" -> "CENTRIXS-M" logic matches = process.extract(query, all_keys, limit=5, scorer=fuzz.partial_ratio) # Filter matches: # We only want to show fuzzy results if they are RELEVANT (score > 80) # and if it's NOT the exact match we just showed above. relevant_matches = [ (match_key, score) for match_key, score in matches if score > 80 and match_key != query ] if relevant_matches: if exact_match: st.markdown("---") st.caption("Related variations found:") else: st.warning(f"No exact match for '{query}'. Did you mean one of these?") # Display the fuzzy options for match_key, score in relevant_matches: with st.expander(f"{match_key} (Match confidence: {score}%)"): st.write(acronyms_db[match_key]) elif not exact_match: st.error(f"No definitions found for '{query}'.") # --- FOOTER --- st.markdown("---") st.caption("PEO IWS 11.0 Reference Tool")