Home > database >  Need help to remove punctuation and replace numbers for an nlp task
Need help to remove punctuation and replace numbers for an nlp task

Time:04-01

For example, I have a string:

sentence = ['cracked $300 million','she\'s resolutely, smitten ', 'that\'s creative [r]', 'the market ( knowledge check : prices up!']

I want to remove the punctuation and replace numbers with the '£' symbol. I have tried this but can only replace one or the other when I try to run them both. my code is below

import re
s =([re.sub(r'[!":$()[]\',]',' ', word) for word in sentence]) 

s= [([re.sub(r'\d ','£', word) for word in s])]
s)

I think the problem could be in the square brackets?? thank you!

CodePudding user response:

you can try something like this I think that's easier

    sentence = "This is a sentence. This is another sentence."
    punctuations = [',', '.', '!', '?','"',"'"]
    for punctuation in punctuations:
        sentence = sentence.replace(punctuation, "£")

CodePudding user response:

Using your input and pattern:

>>> ([re.sub(r'[!":$()[]\',]',' ', word) for word in sentence]) 
['cracked $300 million', "she's resolutely, smitten ", "that's creative [r]", 'the market ( knowledge check : prices up!']
>>> 

The reason is because [!":$()[] is being treated as a character group, and \',] is a literal pattern, i.e. the engine is looking for ',] exactly.

With the closing bracket in the group escaped:

\]

>>> ([re.sub(r'[!":$()[\]\',]',' ', word) for word in sentence]) 
['cracked  300 million', 'she s resolutely  smitten ', 'that s creative  r ', 'the market   knowledge check   prices up ']
>>> 

Edit: If you're trying to stack multiple actions into a single list comprehension, then place your actions in a function and call the function:

def process_word(word):
  word = re.sub(r'[!":$()[\]\',]',' ', word)
  word = re.sub(r'\d ','£', word)
  return word

Results in:

>>> [process_word(word) for word in sentence]
['cracked  £ million', 'she s resolutely  smitten ', 'that s creative  r ', 'the market   knowledge check   prices up ']
  • Related