File size: 2,998 Bytes
33b10b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def validate_response(response_json):
    """
    Validates the response JSON for completeness and brand/product name uniqueness.
    
    Args:
        response_json (dict): The JSON response to validate
        
    Returns:
        bool: True if validation passes, False otherwise
    """
    try:
        # 1. Check if all required fields are present and not empty
        required_fields = [
            "productName", "brandName", "ingredients", "servingSize",
            "packagingSize", "servingsPerPack", "nutritionalInformation",
            "fssaiLicenseNumbers", "claims", "shelfLife"
        ]
        
        # Check if any required field is missing or empty
        for field in required_fields:
            if field not in response_json or not response_json[field]:
                return False
        
        # Type checking for specific fields
        if not isinstance(response_json["ingredients"], list):
            return False
        
        if not isinstance(response_json["servingSize"], dict):
            return False
        
        if not isinstance(response_json["packagingSize"], dict):
            return False
        
        if not isinstance(response_json["servingsPerPack"], (int, float)):
            return False
        
        if not isinstance(response_json["nutritionalInformation"], list):
            return False
        
        if not isinstance(response_json["fssaiLicenseNumbers"], list):
            return False
        
        if not isinstance(response_json["claims"], list):
            return False
        
        if not isinstance(response_json["shelfLife"], str):
            return False
            
        # Validate serving size and packaging size structure
        for size_field in ["servingSize", "packagingSize"]:
            if not all(key in response_json[size_field] for key in ["quantity", "unit"]):
                return False
            if not isinstance(response_json[size_field]["quantity"], (int, float)):
                return False
            if not isinstance(response_json[size_field]["unit"], str):
                return False
        
        # 2. Check if brand name and product name have common words
        # Convert to lowercase and split into words, removing any special characters
        import re
        
        def clean_and_split_text(text):
            # Remove special characters and convert to lowercase
            cleaned_text = re.sub(r'[^a-zA-Z0-9\s]', '', text.lower())
            # Split into words and remove empty strings
            return set(word for word in cleaned_text.split() if word)
        
        brand_words = clean_and_split_text(response_json["brandName"])
        product_words = clean_and_split_text(response_json["productName"])
        
        # Check for common words
        if brand_words.intersection(product_words):
            return False
        
        return True
        
    except (KeyError, TypeError, AttributeError):
        return False