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
Files changed (1) hide show
  1. app.py +39 -39
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
- 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(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()