Skip to content

Commit

Permalink
feat: update product_type (#11173)
Browse files Browse the repository at this point in the history
### What
Automatically change the product type of non-food categorized products

Copy pasted all non-*something* on top of each categories taxonomies.
Also copy pasted categories from food to other flavors (for example, en:
Dry Dog Food is moved from taxonomies/food/categories.txt to
taxonomies/petfood/categories.txt

### Remark
The following, I could not add, but would be a nice to have:
- rm tag from previous flavor/project after migration. For example,
after changing product_type from food to beauty we would like to remove
 1) 'en:non-food-products' and 'en:open-beauty-facts'
but also categories from food
2) "en:desserts", "en:pies", "en:sweet-pies", "en:apple-pies" for
example

I tried to do something with **remove_tag** function from **Tags.pm**.
It removes element of the **categories_tags** array, but **categories**
(string) and **categories_hierarchy** (array) remain. And tags are still
visible on the website. As I did not want to do some update of the
**categories** string, I decided not to include that in the present PR.


### Related issue(s) and discussion
- Fixes  #11094

---------

Co-authored-by: Stéphane Gigandet <[email protected]>
Co-authored-by: Pierre Slamich <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent 03b105a commit d1648b3
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 262 deletions.
1 change: 0 additions & 1 deletion lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ $options{categories_exempted_from_nutriscore} = [
en:spices
en:sugar-substitutes
en:vinegars
en:pet-food
en:non-food-products
)
];
Expand Down
59 changes: 59 additions & 0 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ BEGIN {
&compute_completeness_and_missing_tags
&compute_product_history_and_completeness
&compute_languages
&review_product_type
&compute_changes_diff_text
&compute_data_sources
&compute_sort_keys
Expand Down Expand Up @@ -3000,6 +3001,59 @@ 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;

my $expected_type;
if (has_tag($product_ref, "categories", "en:open-beauty-facts")) {
$expected_type = "beauty";
}
elsif (has_tag($product_ref, "categories", "en:open-food-facts")) {
$expected_type = "food";
}
elsif (has_tag($product_ref, "categories", "en:open-pet-food-facts")) {
$expected_type = "petfood";
}
elsif (has_tag($product_ref, "categories", "en:open-products-facts")) {
$expected_type = "product";
}

if ($expected_type and ($product_ref->{product_type} ne $expected_type)) {
$error = change_product_type($product_ref, $expected_type);
}

if ($error) {
$log->error("review_product_type - error", {error => $error, product_ref => $product_ref});
}
else {
# We remove the tag en:incorrect-product-type and its children before the product is stored on the server of the new type
remove_tag($product_ref, "categories", "en:incorrect-product-type");
remove_tag($product_ref, "categories", "en:open-beauty-facts");
remove_tag($product_ref, "categories", "en:open-food-facts");
remove_tag($product_ref, "categories", "en:open-pet-food-facts");
remove_tag($product_ref, "categories", "en:open-products-facts");
remove_tag($product_ref, "categories", "en:non-food-products");
remove_tag($product_ref, "categories", "en:non-pet-food-products");
remove_tag($product_ref, "categories", "en:non-beauty-products");
}

return;
}

=head2 process_product_edit_rules ($product_ref)
Process the edit_rules (see C<@edit_rules> in in Config file).
Expand Down Expand Up @@ -3723,6 +3777,11 @@ 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)
if (has_tag($product_ref, "categories", "en:incorrect-product-type")) {
review_product_type($product_ref);
}

# Run special analysis, score calculations that it specific to the product type

if (($options{product_type} eq "food")) {
Expand Down
6 changes: 6 additions & 0 deletions scripts/update_all_products.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,12 @@
assign_ciqual_codes($product_ref);
}

# if we have an old_product_type (if change_product_type() was called),
# we need to use store_product() so that the product is removed from the old MongoDB collection and added to the new one
if (defined $product_ref->{old_product_type}) {
$product_values_changed = 1;
}

my $any_change = $product_values_changed;
if (not $pretend) {
if (!$any_change) {
Expand Down
45 changes: 42 additions & 3 deletions taxonomies/beauty/categories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,48 @@ stopwords:fr: aux, au, de, le, du, la, a, et, avec, pour

synonyms:en: flavoured, flavored

en: Incorrect product type
bg: Неправилен product type
ca: Tipus de producte incorrecte
de: Falscher product type
es: Tipo de producto incorrecto
fi: Väärä product type
fr: Mauvais product type
hr: Pogrešan product type
it: Tipo di prodotto errato
nl: Verkeerd product type
pt: Tipo de produto incorreto
ru: Неправильный product type

< en: Incorrect product type
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, OPFF
xx: Open Pet Food Facts, OPFF

< en:Non beauty products
en: Open Products Facts, OPF
xx: Open Products Facts, OPF

# Can you add translations in all European, Asian languages (and more), following alphabetical order for language ordering, except for english which comes first ?


###### CATEGORIE HYGIENE

en: Hygiene
Expand Down Expand Up @@ -1295,7 +1335,6 @@ uz: Makiyaj
vi: Trang điểm
zh: 化妆品


#### Maquillage pour les yeux

< en:Makeup
Expand Down Expand Up @@ -1484,8 +1523,8 @@ en: Men perfumes, Men perfume, Men fragrances, Men fragrance, Male perfume, Male
fr: Parfums homme, Parfums hommes, Parfum homme, Parfum hommes

< en:Perfumes
en: Women perfumes, Women fragrances, Female perfumes
fr: Parfums femme
en: Women perfumes, Women perfume, Women fragrances, Women fragrance, Female perfume, Female perfumes
fr: Parfums femme, Parfum femmes, Parfum femme, Parfum femmes

< en:Perfumes
en: Kids perfumes, Kids fragrances
Expand Down
Loading

0 comments on commit d1648b3

Please sign in to comment.