How to remove special characters from text like the one given below.
import asyncio
import re
from llama_index.core.llms import ChatMessage, MessageRole
dataset = []
Fetch 3000 documents from Supabase
res = supabase.table('sections').select('*').eq('act_id','856').execute().data
print(res[0])
Assistant
To remove special characters from text in Python, you can use the re (regular expressions) module to filter out unwanted characters. Below is an example of how you can modify your code to remove special characters from the fetched data.
python
import asyncio
import re
from llama_index.core.llms import ChatMessage, MessageRole
dataset = []
# Fetch 3000 documents from Supabase
res = supabase.table('sections').select('*').eq('act_id', '856').execute().data
def remove_special_characters(text):
# Define the pattern to keep only alphanumeric characters and spaces
pattern = r'[^A-Za-z0-9\s]+'
# Use re.sub() to replace unwanted characters with an empty string
clean_text = re.sub(pattern, '', text)
return clean_text
# Assuming res is a list of dictionaries and the text you want to clean is in a field called 'content'
for record in res:
original_text = record.get('content', '')
cleaned_text = remove_special_characters(original_text)
record['content'] = cleaned_text # Update the record with cleaned text
dataset.append(record) # Add the cleaned record to your dataset
print(dataset[0]) # Print the first cleaned record
Explanation:
Importing the re Module: You've already imported the re module, which provides support for regular expressions in Python.
Defining the remove_special_characters Function:
The regular expression pattern r'[^A-Za-z0-9\s]+' matches any character that is not an uppercase letter (A-Z), a lowercase letter (a-z), a digit (0-9), or a whitespace character (\s).
re.sub() replaces all occurrences of the pattern with an empty string '', effectively removing them from the text.
Processing Each Record:
Iterate over each record in the res list.
Use record.get('content', '') to safely extract the text content, defaulting to an empty string if the key doesn't exist.
Clean the text using remove_special_characters(original_text).
Update the record['content'] with the cleaned text.
Append the cleaned record to the dataset list.
Printing the Result:
print(dataset[0]) outputs the first cleaned record so you can verify the result.
Customizing the Regular Expression:
If you want to retain certain special characters (like punctuation), you can adjust the pattern. For example, to keep periods and commas, modify the pattern to r'[^A-Za-z0-9\s\.,]+'.
To remove digits as well, change the pattern to r'[^A-Za-z\s]+'.
Example Usage:
python
text = "Hello, World! This is a test. #Python3 @2021"
clean_text = remove_special_characters(text)
print(clean_text)
Output:
code
Hello World This is a test Python3 2021
Notes:
Regular expressions are powerful but can be complex. Ensure that the pattern matches exactly what you intend to remove.
Always test your regex patterns with sample data to confirm they work as expected.