I want to extract tags - “Satisfied” or “Not Satisfied” from a string in python irrepective of tags
To extract tags like “Satisfied” or “Not Satisfied” from a string in Python, regardless of where these tags are situated within the string or how they are capitalized, you can use regular expressions. Python’s re module is perfect for this kind of pattern matching and extraction.

Below is an example function that searches for both “Satisfied” and “Not Satisfied” in the input text. It assumes that the tags can appear in any case (e.g., lower, upper, title).

import re

def extract_satisfaction_tags(text):
    # This regular expression looks for ‘satisfied’ or ‘not satisfied’ in a case-insensitive manner
    pattern = re.compile(r’\b(not\s)?satisfied\b’, re.IGNORECASE)
    
    matches = pattern.findall(text)
    # Each match is a tuple because of the capturing group, transform it into a more readable form
    tags = [’ '.join(match).strip() for match in matches]
    
    # Print or return the tags based on your preference
    return tags

# Example usage
text = “I am completely not satisfied with the service. However, later I felt somewhat satisfied.”
tags_found = extract_satisfaction_tags(text)
print(tags_found)


This function uses a regular expression with a basic pattern to find instances of “satisfied” or “not satisfied”, taking into account case-insensitivity (re.IGNORECASE). It returns these instances as a list. The \b in the pattern ensures that we are matching whole words only, thus avoiding partial matches like “dissatisfied” unless explicitly included in the pattern.

If there are more specific constraints (e.g., extracting only if the text appears within certain delimiters, or in a particular context), you’d need to adjust the regular expression accordingly.

This example combines both “Satisfied” and “Not Satisfied” into a single pattern for efficiency, and it showcases how to handle optional words in patterns (like “not” before “satisfied”) using (not\s)?, which makes "not " optional in the search.
there will be either satisfied or not satisfied in the string. Just check which one is present. I satisfied, make a flag - customer_satisfied = True else customer_satisfied = False
There will be either “Satisfied” tag or “Not Satisfied” tag present in the string not both. So, if you found “Satisfied” make a flag customer_satisfied and set it to True otherwise set it to False