I was wondering how I might go about removing punctuation from user input and creating a set from the words in the input. So far I have this.
input_set = set(self.entry.get().lower().split(' '))
I was wondering how I might go about removing punctuation from user input and creating a set from the words in the input. So far I have this.
input_set = set(self.entry.get().lower().split(' '))
Use str.translate
:
Python2:
>>> from string import punctuation
>>> s = 'sfda%$$sdafd dasf564%^%^, hgghg%#56'
>>> set(s.translate(None, punctuation).split())
set(['hgghg56', 'dasf564', 'sfdasdafd'])
Python3:
from string import punctuation
s = 'sfda%$$sdafd dasf564%^%^, hgghg%#56'
tab = dict.fromkeys(map(ord, punctuation))
print (set(s.translate(tab).split()))
This is an excellent place to use regular expressions:
import re
re.split(r'\W+',str)
depending on what you consider punctuation you may want to change '\W' to a different character class or character group.
Remove punctuation: see Best way to strip punctuation from a string in Python
Create a set of words: set(sentence.split(' '))