File size: 2,872 Bytes
cf957e4
 
 
 
 
cfb0d15
 
cf957e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb0d15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
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")