""" Utility functions for the Emoji Mashup application. """ import logging import os import pickle # Configure logging def setup_logging(): """Configure application logging.""" logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) return logging.getLogger(__name__) # Initialize logger logger = setup_logging() def kitchen_txt_to_dict(filepath): """Convert emoji kitchen text file to dictionary. Args: filepath: Path to the emoji kitchen text file Returns: Dictionary mapping emojis to descriptions """ emoji_dict = {} try: with open(filepath, 'r', encoding='utf-8') as f: for line in f: parts = line.strip().split(' ', 1) if len(parts) == 2: emoji, desc = parts emoji_dict[emoji] = desc return emoji_dict except Exception as e: logger.error(f"Error loading emoji dictionary from {filepath}: {e}") return {} def save_embeddings_to_pickle(embeddings, filepath): """Save embeddings dictionary to a pickle file. Args: embeddings: Dictionary of embeddings to save filepath: Path to save the pickle file to Returns: True if successful, False otherwise """ try: os.makedirs(os.path.dirname(filepath), exist_ok=True) with open(filepath, 'wb') as f: pickle.dump(embeddings, f) logger.info(f"Saved embeddings to {filepath}") return True except Exception as e: logger.error(f"Error saving embeddings to {filepath}: {e}") return False def load_embeddings_from_pickle(filepath): """Load embeddings dictionary from a pickle file. Args: filepath: Path to load the pickle file from Returns: Dictionary of embeddings if successful, None otherwise """ if not os.path.exists(filepath): logger.info(f"Pickle file {filepath} does not exist") return None try: with open(filepath, 'rb') as f: embeddings = pickle.load(f) logger.info(f"Loaded embeddings from {filepath}") return embeddings except Exception as e: logger.error(f"Error loading embeddings from {filepath}: {e}") return None def get_embeddings_pickle_path(model_id, emoji_type): """Generate the path for an embeddings pickle file. Args: model_id: ID of the embedding model emoji_type: Type of emoji ('emotion' or 'event') Returns: Path to the embeddings pickle file """ # Create a safe filename from the model ID safe_model_id = model_id.replace('/', '_').replace('\\', '_') return os.path.join('embeddings', f"{safe_model_id}_{emoji_type}.pkl")