基于词数与字符边界从谷歌抓取文本中提取合规人名
Extracting Valid Person Names from Scraped Text with Custom Boundaries
Hey there! Let's figure out how to extract that name correctly based on your rules. You've got a scraped string from Google:
Raveena Tandon (born 26 October 1974) is an Indian actress who works in Hindi films.
And you need to verify the opening text is a real name, using boundaries: the name ends at the first ( (matching Wikipedia's person entry format) OR caps at 4 words max. Here's a straightforward way to do this:
Core Approach
We'll combine two key checks to nail this:
- Prioritize the
(boundary first, since that's a strong signal of a Wikipedia-style name+birth detail structure - Fall back to a 4-word limit to avoid pulling in descriptive text if the
(is missing or misplaced
Example Implementation (Python)
Python is a common tool for web scraping tasks, so here's a quick script that does exactly what you need:
# Your scraped text scraped_string = "Raveena Tandon (born 26 October 1974) is an Indian actress who works in Hindi films." # Step 1: Grab everything before the first opening parenthesis, then clean up whitespace pre_parenthesis_text = scraped_string.split('(')[0].strip() # Step 2: Split into words and take up to the first 4 (in case there's no parenthesis) extracted_name = ' '.join(pre_parenthesis_text.split()[:4]) # Output the result print(extracted_name) # Prints: Raveena Tandon
How This Works
- Splitting on
(: This directly targets the pattern you referenced (name followed immediately by(age/birth info)). Thesplit('(')[0]grabs all text before the first(, andstrip()removes any accidental trailing spaces. - Capping at 4 words: Even if your scraped text didn't have a
((e.g., a messy entry likeJane Marie Lee Davis is an actress...), this ensures you only take up to 4 words—sticking to your rule that names don't exceed 4 words. - Edge case coverage: If a name has exactly 4 words before a
(, likeRobert John Smith Jr (born...), the script will correctly extract the full 4-word name.
内容的提问来源于stack exchange,提问作者srb633
相关产品推荐
相关产品推荐

