<<

NAME

ProductOpener::KnowledgePanels - Generate product knowledge panels that can be requested through the API

SYNOPSIS

Apps can request through the API knowledge panels for one product. They are returned in the same structured format for all panels.

DESCRIPTION

See https://docs.google.com/document/d/1vJ9gatmv8pCXxyOERmYD16jOKRWJpz1RaQQ5MEcTxms/edit

FUNCTIONS

initialize_knowledge_panels_options( $knowledge_panels_options_ref, $request_ref )

Initialize the options for knowledge panels from parameters.

create_knowledge_panels( $product_ref, $target_lc, $target_cc, $options_ref, $request_ref)

Create all knowledge panels for a product, with strings (descriptions, recommendations etc.) in a specific language, and return them in an array of panels.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc (or "data")

Returned panels contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

If $target_lc is equal to "data", no strings are returned.

country code $target_cc

Needed for some country specific panels like the Eco-Score.

options $options_ref

Defines how some panels should be created (or not created)

- deactivate_[panel_id] : do not create a default panel -- currently unimplemented - activate_[panel_id] : create an on demand panel -- currently only for physical_activities panel

request reference $request_ref

Contains the request parameters, including the API request parameters.

Return values

Panels are returned in the "knowledge_panels_[$target_lc]" hash of the product reference passed as input.

convert_multiline_string_to_singleline($line)

Helper function to allow to enter multiline strings in JSON templates. The function converts the multiline string into a single line string.

create_panel_from_json_template ( $panel_id, $panel_template, $panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel from a JSON template. The template is passed both the full product data + optional panel specific data. The template is thus responsible for all the display logic (what to display and how to display it).

Some special features that are not included in the JSON format are supported:

1. Relative links are converted to absolute links using the requested country / language subdomain

2. Multiline strings can be included using backticks ` at the start and end of the multiline strings. - The multiline strings will be converted to a single string. - Quotes " are automatically escaped unless they are already escaped

3. Comments can be included by starting a line with // - Comments will be removed in the resulting JSON, they are only intended to make the source template easier to understand.

4. Trailing commas are removed - For each loops in templates can result in trailing commas when separating items in a list with a comma (e.g. if want to generate a list of labels)

Arguments

panel id $panel_id

panel template $panel_template

Relative path to the the template panel file, from the "/templates" directory. e.g. "api/knowledge-panels/environment/ecoscore/agribalyse.tt.json"

panel data reference $panel_data_ref (optional, can be an empty hash)

Used to pass data that is necessary for the panel but is not contained in the product data.

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

extract_data_from_impact_estimator_best_recipe ($product_ref, $panel_data_ref)

The impact estimator adds a lot of data to products. This function extracts the data we need to display knowledge panels.

compare_impact_estimator_data_to_category_average ($product_ref, $panel_data_ref, $target_cc)

gen_top_tags_per_country.pl computes stats for categories for nutrients, and now also for the extended ecoscore impacts computed by the impact estimator.

For a specific product, this function finds the most specific category for which we have impact stats to compare with.

create_ecoscore_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel to describe the Eco-Score, including sub-panels for the different components of the Eco-Score.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

create_environment_card_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel card that contains all knowledge panels related to the environment.

Created for all products (with at least a packaging panel).

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

create_manufacturing_place_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel when we know the location of the manufacturing place, usually through a packaging code.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)

create_health_card_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel card that contains all knowledge panels related to health.

This panel card is created for food, pet food, and beauty products.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

We may display country specific recommendations from health authorities, or country specific scores.

options reference $options_ref

create_nutriscore_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates knowledge panels to describe the Nutri-Score.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_nutrient_levels_panels ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates knowledge panels for nutrient levels for fat, saturated fat, sugars and salt.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_nutrition_facts_table_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel with the nutrition facts table.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_serving_size_panel( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel with portion size.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_physical_activities_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel to indicate how much time is needed to burn the calories of a product for various activities.

Description: https://en.wikipedia.org/wiki/Metabolic_equivalent_of_task

1 MET = (kJ / 4.2) / (kg * hour)

Data: https://sites.google.com/site/compendiumofphysicalactivities/

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

Data

Activity energy equivalent of task

Data: https://sites.google.com/site/compendiumofphysicalactivities/

create_ingredients_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel with the list of ingredients.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_additives_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates knowledge panels for additives.

Arguments

product reference $product_ref

language code $target_lc

country code $target_cc

create_ingredients_analysis_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates a knowledge panel with the results of ingredients analysis.

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

add_taxonomy_properties_in_target_languages_to_object ( $object_ref, $tagtype, $tagid, $properties_ref, $target_lcs_ref )

This function adds to the hash ref $object_ref (for instance a data structure passed to a template) the values of selected properties, if they exist in one of the target languages.

For instance for the panel for an additive, we can include a Wikipedia abstract in the requested language if available, or in English if not.

Arguments

object reference $object_ref

taxonomy $tagtype

tag id $tagoid

list of properties $properties_ref

Properties to add to the resulting object.

language codes $target_lcs

Reference to an array of preferred languages, with the preferred language first.

create_recommendation_panels ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates knowledge panels with recommendations (e.g. related to health or the environment). Recommendations can depend on product properties (e.g. categories or ingredients) and user properties (e.g. country and language to get country specific recommendations, but possibly also user preferences regarding trusted sources of information).

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

create_nova_panel ( $product_ref, $target_lc, $target_cc, $options_ref )

Creates knowledge panels to describe the NOVA groups / processing / ultra-processing

Arguments

product reference $product_ref

Loaded from the MongoDB database, Storable files, or the OFF API.

language code $target_lc

Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.

country code $target_cc

<<