Skip to content

81. Filter Low-Quality Training Text

Difficulty: Medium · Topics: Data Prep for AI, Strings, Null Handling

Before pre-training, a corpus pages (page_id, text) is filtered with simple quality rules. Drop a page when any of these is true:

Return the page_id, text of the pages that are kept.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

pages

page_idtext
1Spark runs jobs on a cluster of machines
2Too short
3Home | About | 2024 | 99 | 12 | 34 | 56
4Buy now!!!!! Limited offer for everyone

Expected output

page_idtext
1Spark runs jobs on a cluster of machines

Hints

Hint 1The share of other characters: remove letters and spaces with regexp_replace(text, "[A-Za-z ]", "") and compare its length with the length of the whole text.
Hint 2A character repeated 5 or more times is a backreference: (.)\1{4,} with rlike.
Hint 3A null text makes every condition null, and filter drops rows where the condition is null. Say what you mean anyway: check isNotNull() first.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice