James McCool
commited on
Commit
·
be8e70e
1
Parent(s):
507fe5f
Refactor name matching process in app.py: optimize the matching logic by processing names individually with extractOne for improved efficiency, while maintaining the handling of name-to-ID mapping and ensuring consistent debug output for better user feedback.
Browse files
app.py
CHANGED
@@ -145,56 +145,47 @@ with tab1:
|
|
145 |
|
146 |
# Update projections_df with any new matches
|
147 |
st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
match_dict = {}
|
186 |
-
for name, match_list in zip(names, matches):
|
187 |
-
if match_list and match_list[0][1] >= 85: # Check if we have a match and it meets threshold
|
188 |
-
match_dict[name] = name_id_map[match_list[0][0]] # Use the matched name to get the ID
|
189 |
-
else:
|
190 |
-
match_dict[name] = name # Keep original name if no good match
|
191 |
-
|
192 |
-
print(f"Number of entries in match_dict: {len(match_dict)}")
|
193 |
-
print("Sample of match_dict:", list(match_dict.items())[:3])
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
|
199 |
st.write("Export Dictionary Contents:")
|
200 |
st.write(st.session_state['export_dict'])
|
|
|
145 |
|
146 |
# Update projections_df with any new matches
|
147 |
st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
|
148 |
+
if 'export_dict' not in st.session_state and csv_file is not None:
|
149 |
+
try:
|
150 |
+
name_id_map = dict(zip(
|
151 |
+
st.session_state['csv_file']['Name'],
|
152 |
+
st.session_state['csv_file']['Name + ID']
|
153 |
+
))
|
154 |
+
print("Using Name + ID mapping")
|
155 |
+
except:
|
156 |
+
name_id_map = dict(zip(
|
157 |
+
st.session_state['csv_file']['Nickname'],
|
158 |
+
st.session_state['csv_file']['Id']
|
159 |
+
))
|
160 |
+
print("Using Nickname + Id mapping")
|
161 |
+
|
162 |
+
# Get all names at once
|
163 |
+
names = projections['player_names'].tolist()
|
164 |
+
choices = list(name_id_map.keys())
|
165 |
+
|
166 |
+
# Create a dictionary to store matches
|
167 |
+
match_dict = {}
|
168 |
+
|
169 |
+
# Process each name individually but more efficiently
|
170 |
+
for name in names:
|
171 |
+
# Use extractOne with score_cutoff for efficiency
|
172 |
+
match = process.extractOne(
|
173 |
+
name,
|
174 |
+
choices,
|
175 |
+
score_cutoff=85
|
176 |
+
)
|
177 |
+
|
178 |
+
if match:
|
179 |
+
match_dict[name] = name_id_map[match[0]]
|
180 |
+
else:
|
181 |
+
match_dict[name] = name
|
182 |
+
|
183 |
+
print(f"Number of entries in match_dict: {len(match_dict)}")
|
184 |
+
print("Sample of match_dict:", list(match_dict.items())[:3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
+
# Apply the matches
|
187 |
+
projections['upload_match'] = projections['player_names'].map(match_dict)
|
188 |
+
st.session_state['export_dict'] = match_dict
|
189 |
|
190 |
st.write("Export Dictionary Contents:")
|
191 |
st.write(st.session_state['export_dict'])
|