I found that code, but it does not work, as it modified a very old version and a lot of things have been changed since then.
I am not sure whether it is really needed, but according to google recommendations, URL should follow the rule shop/parentcategory/subcategory/product.html.
Your script is excellent, but all products are kind of in the same folder for google ...
Basically, only one replacement:
parentcategory- to parentcategory/
would do the job and I tried this, but slash is erased by something. Even the straightforward change of the parentcategory name to the one ending with '/' does not help as slashes are removed afterwards. Some (useless, in the product name) are not and this is puzzlng.