ashok2216 commited on
Commit
b4b6994
·
verified ·
1 Parent(s): 419a0a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -149,12 +149,38 @@ def query_flight_data(geo_df, question):
149
  if not identifier:
150
  return "Please specify a flight identifier (callsign or ICAO code) in your question."
151
 
152
- # Try to find the flight by callsign or icao
 
 
 
153
  flight_data = None
154
- if identifier in geo_df['callsign'].values:
155
- flight_data = geo_df[geo_df['callsign'] == identifier]
156
- elif identifier in geo_df['icao24'].values:
157
- flight_data = geo_df[geo_df['icao24'] == identifier]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  if flight_data is None or flight_data.empty:
160
  return f"Could not find flight information for {identifier}. Please check the flight identifier and try again."
 
149
  if not identifier:
150
  return "Please specify a flight identifier (callsign or ICAO code) in your question."
151
 
152
+ # Clean and normalize the identifier
153
+ identifier = identifier.strip().upper()
154
+
155
+ # Try to find the flight by callsign or icao (case-insensitive)
156
  flight_data = None
157
+
158
+ # First try exact match
159
+ if identifier in geo_df['callsign'].str.upper().values:
160
+ flight_data = geo_df[geo_df['callsign'].str.upper() == identifier]
161
+ elif identifier in geo_df['icao24'].str.upper().values:
162
+ flight_data = geo_df[geo_df['icao24'].str.upper() == identifier]
163
+
164
+ # If no exact match, try partial match
165
+ if flight_data is None or flight_data.empty:
166
+ # Try matching without spaces or special characters
167
+ clean_identifier = ''.join(filter(str.isalnum, identifier))
168
+ if not geo_df['callsign'].empty:
169
+ clean_callsigns = geo_df['callsign'].fillna('').apply(lambda x: ''.join(filter(str.isalnum, str(x).upper())))
170
+ matches = clean_callsigns == clean_identifier
171
+ if matches.any():
172
+ flight_data = geo_df[matches]
173
+
174
+ # If still no match, try fuzzy matching
175
+ if flight_data is None or flight_data.empty:
176
+ try:
177
+ from difflib import get_close_matches
178
+ all_callsigns = geo_df['callsign'].fillna('').str.upper().unique()
179
+ close_matches = get_close_matches(identifier, all_callsigns, n=1, cutoff=0.8)
180
+ if close_matches:
181
+ flight_data = geo_df[geo_df['callsign'].str.upper() == close_matches[0]]
182
+ except:
183
+ pass
184
 
185
  if flight_data is None or flight_data.empty:
186
  return f"Could not find flight information for {identifier}. Please check the flight identifier and try again."