Question: I have a Python script that loads search terms from a JSON file and processes a Pandas DataFrame to add new columns indicating whether certain terms are present in the text data. However, I would like to modify the script to use Polars instead of Pandas and possibly remove the JSON dependency. Here is my original code: import pandas as pd import json class SearchTermLoader: def __init__(self, json_file): self.json_file = json_file def load_terms(self): with open(self.json_file, 'r') as f: data = json.load(f) terms = {} for phase_name, phase_data in data.items(): terms[phase_name] = ( phase_data.get('words', []), phase_data.get('exact_phrases', []) ) return terms class DataFrameProcessor: def __init__(self, df: pd.DataFrame, col_name: str) -> None: self.df = df self.col_name = col_name def add_contains_columns(self, search_terms): columns_to_add = ["type1", "type2"] for column in columns_to_add: self.df[column] = self.df[self.col_name].apply( lambda text: any( term in text for term in search_terms.get(column, ([], []))[0] + search_terms.get(column, ([], []))[1] ) ) return self.df # Example Usage data = {'text_column': ['The apple is red', 'I like bananas', 'Cherries are tasty']} df = pd.DataFrame(data) term_loader = SearchTermLoader('word_list.json') search_terms = term_loader.load_terms() processor = DataFrameProcessor(df, 'text_column') new_df = processor.add_contains_columns(search_terms) new_df  Here is an example of the json file: { "type1": { "words": ["apple", "tasty"], "exact_phrases": ["soccer ball"] }, "type2": { "words": ["banana"], "exact_phrases": ["red apple"] } }  I understand that I can use the .str.contains() function, but I want to use it with specific words and exact phrases. Could you provide some guidance on how to get started with this?

Answer: For non-regex matching, .str.contains_any() is probably a better option. It seems like you want to concat both lists: word_list = pl.read_json("word_list.json") """ for older versions without struct "*" expansion type1 = pl.concat_list( pl.col("type1").struct.field("words", "exact_phrases") ) """ word_list = word_list.select( type1 = pl.concat_list(pl.col("type1").struct["*"]), type2 = pl.concat_list(pl.col("type2").struct["*"]) )  Shape: (1, 2) ┌───────────────────────────────────┬─────────────────────────┐ │ type1 ┆ type2 │ │ --- ┆ --- │ │ list[str] ┆ list[str] │ ╞═══════════════════════════════════╪═════════════════════════╡ │ ["apple", "tasty", "soccer ball"] ┆ ["banana", "red apple"] │ └───────────────────────────────────┴─────────────────────────┘  You could then .concat() them into your frame and run .contains_any() new_df = pl.concat([df, word_list], how="horizontal") new_df.with_columns( type1 = pl.col("text_column").str.contains_any(pl.col("type1").flatten()), type2 = pl.col("text_column").str.contains_any(pl.col("type2").flatten()) )  shape: (3, 3) ┌─────────────────────────────┬───────┬───────┐ │ text_column ┆ type1 ┆ type2 │ │ --- ┆ --- ┆ --- │ │ str ┆ bool ┆ bool │ ╞═════════════════════════════╪═══════╪═══════╡ │ The apple is red ┆ true ┆ false │ │ I like bananas ┆ false ┆ true │ │ Cherries are tasty ┆ true ┆ false │ └─────────────────────────────┴───────┴───────┘