can you modify the function in order to also count how many rows are with 1 "other"…
- User
can you modify the function in order to also count how many rows are with 1 "other" count, how many with 2, how many with 3 and how many with 4 and 5 ?
def generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, category_keyword_counts, remove_duplicates=False): top_keywords_per_category = {category: set(counts.keys()) for category, counts in category_keyword_counts.items()}
# Initialize lists for categories and counters for "Other" categories = ['Object', 'Adjective', 'Activity', 'Place', 'Feeling'] category_keywords_list = {cat: [] for cat in categories} clustered_keywords_list = [] for idx, row in per_row_df.iterrows(): lemmatized_keywords = row['Lemmatized Keywords'].split(', ') clustered_keywords = [clustered_keyword_mapping.get(kw, kw) for kw in lemmatized_keywords] if remove_duplicates: # Remove duplicates while preserving order clustered_keywords = list(dict.fromkeys(clustered_keywords)) clustered_keywords_list.append(', '.join(clustered_keywords)) # Initialize "Other" counter for each row row_other_count = 0 # Assign keywords to categories based on positions for i, category in enumerate(categories): if i < len(clustered_keywords): keyword = clustered_keywords[i] # Map to 'Other' if not in top keywords if keyword not in top_keywords_per_category[category]: keyword = 'Other' row_other_count += 1 # Increase the row counter for "Other" category_keywords_list[category].append(keyword) else: category_keywords_list[category].append('') # or 'Other' if row_other_count > 0: print(f"{row['Image Name']}, {clustered_keywords}, {category_keywords_list['Object'][idx]} {category_keywords_list['Adjective'][idx]} {category_keywords_list['Activity'][idx]} {category_keywords_list['Feeling'][idx]} {category_keywords_list['Place'][idx]}, keywords: {row_other_count}") per_row_df['Clustered Keywords'] = clustered_keywords_list # Add the category columns to per_row_df for category in categories: per_row_df[category] = category_keywords_list[category] # Print summary of "Other" counts per category other_count = {cat: 0 for cat in categories} # Counter for "Other" per category print("\nSummary of 'Other' counts per category:") for category, keywords in category_keywords_list.items(): other_count[category] = sum(1 for keyword in keywords if keyword == 'Other') print(f"{category}: {other_count[category]}") return per_row_df - Assistant