"""Analysis only: kcal distribution of drink categories in the OFF export, to pick plausibility bounds. Usage: python3 -I off_inspect_bev.py """ import gzip, sys, collections, random GROUPS = { 'water': {'en:waters'}, 'wine': {'en:wines'}, 'beer': {'en:beers', 'en:ciders'}, 'spirit': {'en:spirits', 'en:hard-liquors', 'en:distilled-beverages', 'en:liqueurs'}, 'soda': {'en:sodas', 'en:carbonated-drinks', 'en:soft-drinks', 'en:colas', 'en:iced-teas', 'en:energy-drinks'}, 'juice': {'en:fruit-juices', 'en:juices-and-nectars', 'en:nectars', 'en:smoothies'}, 'milkdrink': {'en:milks', 'en:plant-based-milk-alternatives', 'en:milk-substitutes', 'en:dairy-drinks', 'en:flavoured-milks', 'en:fermented-milk-drinks'}, } EXCL = {'en:syrups', 'en:beverages-preparations', 'en:beverage-preparations', 'en:instant-beverages', 'en:dehydrated-beverages', 'en:dried-products-to-be-rehydrated', 'en:powdered-milks', 'en:milk-powders', 'en:concentrated-milks', 'en:condensed-milks', 'en:sweetened-condensed-milks', 'en:evaporated-milks', 'en:coconut-milks-and-creams', 'en:coconut-milks', 'en:creams', 'en:fruit-juice-concentrates', 'en:concentrated-fruit-juices', 'en:fruit-syrups', 'en:cordials', 'en:squash'} BOUNDS = [50, 80, 100, 120, 150, 200, 250, 300, 400, 500] hist = {g: collections.Counter() for g in GROUPS} ex = {g: [] for g in GROUPS} random.seed(5) with gzip.open(sys.argv[1], 'rb') as f: header = f.readline().decode('utf-8').rstrip('\n').split('\t') H = {c: i for i, c in enumerate(header)} for raw in f: p = raw.decode('utf-8', 'replace').rstrip('\n').split('\t') if len(p) != len(header): continue tags = set(p[H['categories_tags']].split(',')) if not tags & set().union(*GROUPS.values()): continue try: k = float(p[H['energy-kcal_100g']]) except ValueError: continue for g, ts in GROUPS.items(): if tags & ts and not tags & EXCL: b = next((x for x in BOUNDS if k <= x), 9999) hist[g][b] += 1 if k > {'water': 80, 'wine': 200, 'beer': 200, 'spirit': 450, 'soda': 150, 'juice': 150, 'milkdrink': 150}[g] and len(ex[g]) < 400: ex[g].append((k, p[H['product_name']][:45], p[H['quantity']][:15], p[H['fat_100g']], p[H['carbohydrates_100g']], p[H['proteins_100g']], p[H['alcohol_100g']], p[H['main_category']][:30])) for g in GROUPS: print(g, sorted(hist[g].items())) for x in random.sample(ex[g], min(12, len(ex[g]))): print(' ', x)