openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

1-time pass to fix ~200K Unicode encoding errors in Book & Author records

Open bicolino34 opened this issue 1 year ago • 15 comments

During import, Unicode text (e.g. Убезпецiз) was incorrectly encoded as HTML numeric character references (e.g. Убезпеціз). These should be remediated by running html.unescape(...) on these values.

@tfmorris says here: https://github.com/internetarchive/openlibrary/issues/10860#issuecomment-3534391446

This query should get us close to what we need: https://openlibrary.org/search?q=language%3Aukr+title%3A%22%23%22&mode=everything

I'm not sure why this is cast as a Ukrainian problem. That search currently returns 258 results, while removing the language filter produces over 200,000 hits and Asian titles demonstrate this problem frequently

This 1-liner

gzcat ol_dump_editions_2025-11-06.txt.gz | cut -f 5 | jq -r '[.key,.title,.subtitle?] | @tsv'  | grep -E '&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});' | gzip > edition-html-entities.tsv.gz

will produce a list 148.5K edition titles which need fixing (and there are another 72K work titles & 15K author names which need fixing too).

bicolino34 avatar Jun 09 '25 10:06 bicolino34

This issue requires a script/ to go through (preferably in bulk) and:

  • setup magic incantation
  • retrieve ~1k affected records at a time via web.ctx.site.get_many
  • fix the affected field with html.unescape(...)
  • write back the many records in a single commit with "unescaping incorrectly html escaped unicode"

Ideally, #10860 is solved first (i.e. preventing future escaping) so we don't have to remediate multiple times, however it seems worth doing a batch now to get 90%+ of the value) and then rerunning after #10860 is closed.

mekarpeles avatar Mar 16 '26 18:03 mekarpeles

Hey @mekarpeles , I've been looking into this and the adjacent issues, and would love to work on it. I've traced through the import pipeline and confirmed the corrupted data comes in pre-escaped from BWB with no escaping happening on OL's side. So, the remediation script is the right approach. I've also looked at update_stale_ocaid_references.py as a template for the batch fetch/modify/writeback pattern and have a clear picture of how to implement this. Could I please be assigned?

Saad259 avatar Mar 24 '26 13:03 Saad259

Hi @mekarpeles, I've been investigating this issue and would love to work on it, both as a standalone fix and as part of my GSoC proposal for the Tag Cleanup project.

Here are the things that I've done so far:

  1. I have confirmed the problem in real data - I ran the grep command from the issue against ol_dump_authors_latest.txt.gz and found broken records across multiple languages inclusing Russian/Cyrillic, Chiense, Greek, Hungarian, and Chinese.
Image
  1. I have verified that the fix works - html_unescape() correctly restores the original characters. For example Сергей gets returend as Сергей.
Image
  1. I also traced the code - I looked through openlibrary/catalog/add_book/load_book.py and confirmed there is no html_unescape() call anywhere in the import pipeline, which confirms the corrupted data comes in pre-escaped and stored as-is.

  2. I wrote a prototype cleanup scripts - I have a working dry-run script at scripts/fix_unicode_html_entities.py that reads the dump, detects broken records,, and shows the before/after.

Output result: the beginning part

Image

Output result: the ending part

Image

The script I wrote follows the batch fetch/modify/writeback pattern similar to update_stale_ocaid_references.py. Next steps would be writing it to the OL API to actually write back the fixes.

I would love to get a feedback on the approach and would be happy to open a PR if this is still available!

Chisomnwa avatar Mar 25 '26 14:03 Chisomnwa

Hi @Chisomnwa, thank you, this seems like the right approach to me. In addition to the script, we'll likely want to set up a bot that runs over and fixes them. The bot will likely require some form of discussion with @jimchamp and @cdrini -- do we edit books in bulk using save_many or do we create a separate commit for each item. In terms of data integrity, I'm thinking it may make sense to make an edit/commit for each individual author or work, though perhaps we deal with works and their editions in the same commit.

Thoughts from @jimchamp + @cdrini?

mekarpeles avatar Mar 29 '26 23:03 mekarpeles

Thank you for the assignment @mekarpeles . I will wait for feedback from @jimchamp and @cdrini on the commit strategy (individual commits vs save_many) before opening a PR, and will update the script accordingly.

Chisomnwa avatar Mar 30 '26 08:03 Chisomnwa

@Chisomnwa after discussing with @jimchamp, I think one approach that could work is:

Phase 1

Maintaining a dict of messy works & editions

map_works_editions = {}

# e.g.
{'OL123W': ['OL234M'] }

We loop through the complete openlibrary dump...

If a work.title is messy, we add to map_works_editions.setdefault(work_id, [])

If we hit an edition.title that is messy, we add its work_id to map_works_editions.setdefault(work_id, []).append(edition.id)

The output of this phase is the list of messy works and editions need to be fixed.

Phase 2

The second step is looping over each work, getting all its editions, fixing the work and editions, and then passing to save_many (with a bot account)

mekarpeles avatar Apr 01 '26 21:04 mekarpeles

Each of the phases (listed here) could take quite some time to complete, and the script can be interrupted before completion for any number of reasons (e.g. deployments, unhandled exceptions, etc.).

It's probably best if phase one persists all affected keys in a file which would be used as an input for the second phase. This way, we won't have to process a data dump multiple times if the script is interrupted before updating all of the affected records. Phase two should also periodically persist the last updated record, perhaps after a batch of affected records have been corrected. This could be done any number of ways. The important thing is that we want to avoid updating records that were already corrected.

jimchamp avatar Apr 01 '26 23:04 jimchamp

Thanks for the detailed breakdown, @mekarpeles and @jimchamp! I've noted the two-phase appproach and the need to track progress so the script can continue from where it stopped if interrupted. I'll update the PR accordingly.

Chisomnwa avatar Apr 02 '26 05:04 Chisomnwa

@jimchamp can we use https://github.com/internetarchive/openlibrary-client/blob/b7a68e31bc0f178c9e582edc7c58adf3ca61a086/olclient/openlibrary.py#L122-L136 the olclient to auth as some unicode-fixup-bot and then run @Chisomnwa's script to save in batches of 250? That would result in ~100 save_many

Can we try this by tweaking olclient to run against localhost?

We also likely want to try the save_many against production using just a single record (ensure it works).

mekarpeles avatar May 29 '26 20:05 mekarpeles

We ran a script to clean up about 150k of these. WAF modsecurity rules blocked a significant number from succeeding so we need to run another pass.

mekarpeles avatar Jun 08 '26 20:06 mekarpeles

Yes, we did on the 2nd of June! I'm looking forward to the second pass and I'm happy to help however I can.

Chisomnwa avatar Jun 08 '26 21:06 Chisomnwa

Yesterday, I updated our WAF rules and ran the script again. ~10K record updates were blocked by WAF.

jimchamp avatar Jun 11 '26 18:06 jimchamp

I think we've processed everything other than ~500 records (i.e. ~239k fixed)

We still haven't patched the root of this problem, I think? Which is #10860

So we'll want to run this next month as well... And also fix 10860.

mekarpeles avatar Jun 17 '26 17:06 mekarpeles

Excellent progress @mekarpeles and @jimchamp! Great to see the WAF rule updates helping. ~239K fixed is a huge win.

Also saw mek's latest on #10860 and PR #12953 — understand the importer fix still needs to land. Appreciate the update.

Chisomnwa avatar Jun 18 '26 01:06 Chisomnwa

I think this can be broadly closed now (maybe a few batches that got blocked by WAF rules, but otherwise most records fixed and the root cause fix got merged into the importer.

mekarpeles avatar Jun 24 '26 02:06 mekarpeles