Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle numeric values with commas in rating processor #104

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Nykakin
Copy link
Contributor

@Nykakin Nykakin commented Aug 28, 2024

Ratting processor currently accept dictionaries with standard keys ratingValue, bestRating and reviewCount. It attempts to convert values using float and int calls, so that string values are handled:

>>> from zyte_common_items.processors import rating_processor
>>> 
>>> rating_processor({"bestRating": 5, "ratingValue": "3.3", "reviewCount": 12}, None)
AggregateRating(bestRating=5.0, ratingValue=3.3, reviewCount=12)

If the value of rating uses comma instead of a dot to separate decimal places, an exception is raised:

>>> rating_processor({"bestRating": 5, "ratingValue": "3,3", "reviewCount": 12}, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/zyte_common_items/processors.py", line 354, in rating_processor
    result.ratingValue = float(rating_value)
                         ^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: '3,3'

Similarly, number of reviews can have comma in string value (seen in Amazon pages), e.g. 12,070 ratings. Strings such as these also raise exceptions.

Because standard schema defines that ratingValue and bestRating use Number type and revewCount uses Integer type, decided to treat strings differently depending on used type:

  • If we expect real value, try replacing , with .
  • If we expect integer value, try removing ,

return value


def _to_float(value: Any) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should call it something different, like to_rating_float, to make it somewhat clear that it should not be used for values that could be greater than 999.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about moving these as local functions within rating_processor? Then it would be clear their usage is restricted to this function.

def rating_processor(value: Any, page: Any) -> Any:
    def _to_int(value: Any) -> Any:
        ...

    def _to_float(value: Any) -> Any:
        ...

    ...

That was my original idea but I've thought that perhaps we could re-use these in some other place so I left them at the top level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think renaming is better as we can still reuse the renamed ones if we want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wRAR what naming would you propose...? Because to_rating_float sounds like something that should be used only for, well, rating.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that these functions could be useful elsewhere. I suggested to_rating_float, but something more generic, like to_small_float, would also work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't guess from name alone what a function to_small_float does.

Copy link
Contributor

@Gallaecio Gallaecio Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m open to other names.

But we don’t really need for the name to make it obvious what the function can do, I think the most important thing is for the name to make people think twice before they use the function for a value that could surpass 999. The details can be explained in a docstring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, we also have https://github.com/zytedata/zyte-parsers/blob/8b161cf69df8caeb145e5244601ff93e41b287e6/zyte_parsers/aggregate_rating.py#L101 which does a similar thing for floats. I like how ints are handled in this PR though.

Copy link
Contributor

@kmike kmike Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function in zyte-parsers also swallows the exceptions, and here it's not; it seems not silencing them is better for the case processor is processing the value returned by human-written code (though I'm not 100% sure).

@Gallaecio Gallaecio requested review from kmike and wRAR August 28, 2024 19:11
Comment on lines +48 to +49
if "," in value:
value = value.replace(",", ".")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: why is if necesary? Is it an optimization, and if so - does it actually help (it can go both ways - it's 2 passes over string instead of one)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants