James McCool
commited on
Commit
·
d209a5b
1
Parent(s):
51d04e4
Refactor name matching logic in app.py: streamline the process of creating the name ID mapping and matching player names, improving efficiency and clarity in data handling.
Browse files
app.py
CHANGED
|
@@ -145,47 +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 |
-
if match:
|
| 179 |
-
match_dict[name] = name_id_map[match[0]]
|
| 180 |
-
else:
|
| 181 |
-
match_dict[name] = name
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 189 |
|
| 190 |
st.write(st.session_state['export_dict'])
|
| 191 |
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
|
|
|
|
| 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 |
+
try:
|
| 149 |
+
name_id_map = dict(zip(
|
| 150 |
+
st.session_state['csv_file']['Name'],
|
| 151 |
+
st.session_state['csv_file']['Name + ID']
|
| 152 |
+
))
|
| 153 |
+
print("Using Name + ID mapping")
|
| 154 |
+
except:
|
| 155 |
+
name_id_map = dict(zip(
|
| 156 |
+
st.session_state['csv_file']['Nickname'],
|
| 157 |
+
st.session_state['csv_file']['Id']
|
| 158 |
+
))
|
| 159 |
+
print("Using Nickname + Id mapping")
|
| 160 |
+
|
| 161 |
+
# Get all names at once
|
| 162 |
+
names = projections['player_names'].tolist()
|
| 163 |
+
choices = list(name_id_map.keys())
|
| 164 |
+
|
| 165 |
+
# Create a dictionary to store matches
|
| 166 |
+
match_dict = {}
|
| 167 |
+
|
| 168 |
+
# Process each name individually but more efficiently
|
| 169 |
+
for name in names:
|
| 170 |
+
# Use extractOne with score_cutoff for efficiency
|
| 171 |
+
match = process.extractOne(
|
| 172 |
+
name,
|
| 173 |
+
choices,
|
| 174 |
+
score_cutoff=85
|
| 175 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
if match:
|
| 178 |
+
match_dict[name] = name_id_map[match[0]]
|
| 179 |
+
else:
|
| 180 |
+
match_dict[name] = name
|
| 181 |
+
|
| 182 |
+
print(f"Number of entries in match_dict: {len(match_dict)}")
|
| 183 |
+
print("Sample of match_dict:", list(match_dict.items())[:3])
|
| 184 |
|
| 185 |
+
# Apply the matches
|
| 186 |
+
projections['upload_match'] = projections['player_names'].map(match_dict)
|
| 187 |
+
st.session_state['export_dict'] = match_dict
|
| 188 |
+
|
| 189 |
|
| 190 |
st.write(st.session_state['export_dict'])
|
| 191 |
st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
|