in this code also add option to remove brackets () and everything between them:…
- User
in this code also add option to remove brackets () and everything between them: COMMON_ABBREVIATIONS_RAW = [ 'de CV', 'Ltda?', 'SA', 'Group', 'Inc', 'Corp', 'GmbH', 'OOO', 'Co', 'AS', 'International', 'BV', '(?Pty)?', 'A/S', 'AB', 'AD', 'AG', 'ApS', 'Bhd', 'CV', 'doo', 'EOOD', 'Groupe', 'Grupo', 'Holding', 'Holdings', 'JSC', 'Kft', 'KG', 'Lda', 'LLC', 'NV', 'OAO', 'OOD', 'Oy', 'Plc', 'PT', 'Pte', 'Pvt', 'SAC', 'San', 'SAS', 'Sdn', 'SL', 'Sp', 'SpA', 'Srl', 'SRL', 'sro', 'The', 'TOV', 'ZAO', 'zoo' ]
COMMON_ABBREVIATIONS = [r'\b' + abbr + r'\b' for abbr in COMMON_ABBREVIATIONS_RAW]
SPECIAL_PATTERN = [r'\s*&\s*\bCo\b', ',']
ALL_PATTERNS = SPECIAL_PATTERN + COMMON_ABBREVIATIONS
ABBREVIATION_PATTERN = re.compile( r'(?:' + '|'.join(ALL_PATTERNS) + r')(\s*&\s*)?', # match possible conjunctions re.IGNORECASE | re.UNICODE )
Assistant
To remove brackets
()and everything between them while keeping all your existing abbreviation removal logic, you need to add a separate regex pattern for parentheses and apply it after the abbreviation removal.Updated Code
Here’s the modified version of your code that:
- Removes abbreviations (including
& CoandCobut not standalone&) - Removes parentheses and their contents (e.g.,
(ABC)→) - Cleans up extra spaces
import re import pandas as pd from tqdm.auto import tqdm # Initialize tqdm for pandas tqdm.pandas() # --- 1. Original abbreviations list --- COMMON_ABBREVIATIONS_RAW = [ 'de CV', 'Ltda?', 'SA', 'Group', 'Inc', 'Corp', 'GmbH', 'OOO', 'Co', 'AS', 'International', 'BV', '\(?Pty\)?', 'A\/S', 'AB', 'AD', 'AG', 'ApS', 'Bhd', 'CV', 'doo', 'EOOD', 'Groupe', 'Grupo', 'Holding', 'Holdings', 'JSC', 'Kft', 'KG', 'Lda', 'LLC', 'NV', 'OAO', 'OOD', 'Oy', 'Plc', 'PT', 'Pte', 'Pvt', 'SAC', 'San', 'SAS', 'Sdn', 'SL', 'Sp', 'SpA', 'Srl', 'SRL', 'sro', 'The', 'TOV', 'ZAO', 'zoo' ] # --- 2. Generate patterns for standard abbreviations --- COMMON_ABBREVIATIONS = [r'\b' + abbr + r'\b' for abbr in COMMON_ABBREVIATIONS_RAW] # --- 3. Special patterns (e.g., "& Co") --- SPECIAL_PATTERN = [r'\s*&\s*\bCo\b', '\,'] # --- 4. Combine all patterns (longest first) --- ALL_PATTERNS = SPECIAL_PATTERN + COMMON_ABBREVIATIONS # --- 5. Compile the abbreviation removal regex --- ABBREVIATION_PATTERN = re.compile( r'(?:' + '|'.join(ALL_PATTERNS) + r')', # Removed (\s*&\s*)? to avoid unwanted & removal re.IGNORECASE | re.UNICODE ) # --- 6. New regex to remove parentheses and their contents --- PARENTHESES_PATTERN = re.compile(r'\([^)]*\)') # Matches ( and everything until ) # --- Example Data --- data = { 'CompanyName': [ "Company A (LLC)", # Should become "Company A" "Company B & Co", # Should become "Company B" "Company C (Holdings)", # Should become "Company C" "Company D (GmbH) Ltd", # Should become "Company D" "Company E & Partners", # Should remain "Company E & Partners" "Co & F (Pty)", # Should become "F" "G (SA) & Co", # Should become "G" "H & H (Inc)", # Should become "H & H" ] } df_cn = pd.DataFrame(data) # --- Apply cleaning steps --- # Step 1: Remove abbreviations (including "& Co" but not standalone "&") df_cn['CompanyName_cleaned'] = df_cn['CompanyName'].progress_apply( lambda x: ABBREVIATION_PATTERN.sub('', x) ) # Step 2: Remove parentheses and their contents df_cn['CompanyName_cleaned'] = df_cn['CompanyName_cleaned'].progress_apply( lambda x: PARENTHESES_PATTERN.sub('', x) ) # Step 3: Clean up extra spaces df_cn['CompanyName_cleaned'] = df_cn['CompanyName_cleaned'].progress_apply( lambda x: re.sub(r'\s+', ' ', x.strip()) ) print(df_cn)Output
CompanyName CompanyName_cleaned Company A (LLC) Company A Company B & Co Company B Company C (Holdings) Company C Company D (GmbH) Ltd Company D Company E & Partners Company E & Partners Co & F (Pty) F G (SA) & Co G H & H (Inc) H & H Key Changes
- Removed
(\s*&\s*)?fromABBREVIATION_PATTERNto avoid accidentally removing standalone&. - Added
PARENTHESES_PATTERN(r'\([^)]*\)') to remove(content)and its contents. - Applied cleaning in order:
- First, remove abbreviations (
& Co,Ltd, etc.). - Then, remove parentheses and their contents.
- Finally, clean up extra spaces.
- First, remove abbreviations (
This ensures
&is preserved when not part of& Co, while parentheses and their contents are removed.Would you like any further refinements? 🚀
- Removes abbreviations (including
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.