From fbfda8d07f301e9a3782ab9bcd7993c783f6a618 Mon Sep 17 00:00:00 2001 From: benbenben2 <110821832+benbenben2@users.noreply.github.com> Date: Fri, 27 Dec 2024 17:11:15 +0100 Subject: [PATCH] feat-update-product-type --- lib/ProductOpener/Products.pm | 80 ++ taxonomies/beauty/categories.txt | 1254 +++++++++++++++-------------- taxonomies/food/categories.txt | 302 ++----- taxonomies/petfood/categories.txt | 121 ++- taxonomies/product/categories.txt | 25 + tests/unit/products.t | 30 + 6 files changed, 938 insertions(+), 874 deletions(-) diff --git a/lib/ProductOpener/Products.pm b/lib/ProductOpener/Products.pm index f441518b62509..75a6c63ee9c0c 100644 --- a/lib/ProductOpener/Products.pm +++ b/lib/ProductOpener/Products.pm @@ -2955,6 +2955,83 @@ sub compute_languages ($product_ref) { return; } +=head2 review_product_type ( $product_ref ) + +Reviews the product type based on the presence of specific tags in the categories field. +Updates the product type if necessary. + +=head3 Arguments + +=head4 Product reference $product_ref + +A reference to a hash containing the product details. + +=cut + +sub review_product_type ($product_ref) { + + my $error; + + if ($product_ref->{product_type} eq "beauty") { + if (has_tag($product_ref, "categories", "en:non-beauty-products")) { + if (has_tag($product_ref, "categories", "en:open-food-facts")) { + $error = change_product_type($product_ref, "food"); + } + elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) { + $error = change_product_type($product_ref, "petfood"); + } + elsif (has_tag($product_ref, "categories", "en:open-products-facts")) { + $error = change_product_type($product_ref, "product"); + } + } + } + elsif ($product_ref->{product_type} eq "food") { + if (has_tag($product_ref, "categories", "en:non-food-products")) { + if (has_tag($product_ref, "categories", "en:open-beauty-facts")) { + $error = change_product_type($product_ref, "beauty"); + } + elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) { + $error = change_product_type($product_ref, "petfood"); + } + elsif (has_tag($product_ref, "categories", "en:open-products-facts")) { + $error = change_product_type($product_ref, "product"); + } + } + } + elsif ($product_ref->{product_type} eq "petfood") { + if (has_tag($product_ref, "categories", "en:non-pet-food-products")) { + if (has_tag($product_ref, "categories", "en:open-beauty-facts")) { + $error = change_product_type($product_ref, "beauty"); + } + elsif (has_tag($product_ref, "categories", "en:open-food-facts")) { + $error = change_product_type($product_ref, "food"); + } + elsif (has_tag($product_ref, "categories", "en:open-products-facts")) { + $error = change_product_type($product_ref, "product"); + } + } + } + elsif ($product_ref->{product_type} eq "product") { + if (has_tag($product_ref, "categories", "en:non-food-products")) { + if (has_tag($product_ref, "categories", "en:open-beauty-facts")) { + $error = change_product_type($product_ref, "beauty"); + } + elsif (has_tag($product_ref, "categories", "en:open-food-facts")) { + $error = change_product_type($product_ref, "food"); + } + elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) { + $error = change_product_type($product_ref, "petfood"); + } + } + } + + if ($error) { + $log->error("review_product_type - error", {error => $error, product_ref => $product_ref}); + } + + return; +} + =head2 process_product_edit_rules ($product_ref) Process the edit_rules (see C<@edit_rules> in in Config file). @@ -3678,6 +3755,9 @@ sub analyze_and_enrich_product_data ($product_ref, $response_ref) { compute_languages($product_ref); # need languages for allergens detection and cleaning ingredients + # change the product type of non-food categorized products (issue #11094) + review_product_type($product_ref); + # Run special analysis, score calculations that it specific to the product type if (($options{product_type} eq "food")) { diff --git a/taxonomies/beauty/categories.txt b/taxonomies/beauty/categories.txt index 1f215a51b4837..8ed1145eec6e3 100644 --- a/taxonomies/beauty/categories.txt +++ b/taxonomies/beauty/categories.txt @@ -1,810 +1,830 @@ -stopwords:fr:aux,au,de,le,du,la,a,et,avec,pour - -synonyms:en:flavoured,flavored +stopwords:fr: aux, au, de, le, du, la, a, et, avec, pour + +synonyms:en: flavoured, flavored + +en: Non beauty products +bg: Некозметични продукти +ca: Productes no de bellesa +de: Non-Beauty-Produkte, Non Beauty Produkte +es: Productos no de belleza +fi: ei kauneustuote +fr: Non beauté +hr: Neprehrambeni proizvodi za ljepotu +it: Prodotti non di bellezza +nl: Non-beauty +pt: Produtos não de beleza +ru: Непродукты для красоты, Не косметические продукты + +< en:Non beauty products +en: Open Food Facts, OFF +xx: Open Food Facts, OFF + +< en:Non beauty products +en: Open Pet Food Facts, OFF +xx: Open Pet Food Facts, OFF + +< en:Non beauty products +en: Open Products Facts, OPF +xx: Open Products Facts, OPF ###### CATEGORIE HYGIENE -en:Hygiene -fr:Hygiène -nl:Hygiëne +en: Hygiene +fr: Hygiène +nl: Hygiëne - ['en:non-food-products', 'en:open-pet-food-facts'], + product_type => 'food' +}; +ProductOpener::Products::review_product_type($product_ref); +is($product_ref->{product_type}, 'petfood'); +# beauty to product +$product_ref = { + categories_tags => ['en:non-beauty-products', 'en:open-products-facts'], + product_type => 'beauty' +}; +ProductOpener::Products::review_product_type($product_ref); +is($product_ref->{product_type}, 'product'); +# food to beauty AND product -> move to beauty (handled by alphabetical order) +$product_ref = { + categories_tags => ['en:non-food-products', 'en:open-beauty-facts', 'en:open-products-facts'], + product_type => 'food' +}; +ProductOpener::Products::review_product_type($product_ref); +is($product_ref->{product_type}, 'beauty'); +# rerun same test based on result of previous test should not do anything (missing tag "en:non-beauty-products") +$product_ref = { + categories_tags => ['en:non-food-products', 'en:open-beauty-facts', 'en:open-products-facts'], + product_type => 'beauty' +}; +ProductOpener::Products::review_product_type($product_ref); +is($product_ref->{product_type}, 'beauty'); + done_testing();