I am trying to clean up text using a pre-processing function. I want to remove all non-alpha characters such as punctuation and digits, but I would like to retain compound words that use a dash without splitting them (e.g. pre-tender, pre-construction).
def preprocess(text):
#remove punctuation
text = re.sub('\b[A-Za-z]+(?:-+[A-Za-z]+)+\b', '-', text)
text = re.sub('[^a-zA-Z]', ' ', text)
text = text.split()
text = " ".join(text)
return text
For instance, the original text:
"Attended pre-tender meetings"
should be split into
['attended', 'pre-tender', 'meeting']
rather than
['attended', 'pre', 'tender', 'meeting']
Any help would be appreciated!