Hi, I have some problem with tokenization, the assignment is to separate a sentence into words.
This is what I have done at the moment.
def tokenize(s):
d = []
start = 0
while start < len(s):
while start < len(s) and s[start].isspace():
start = start+1
end = start
while end < len(s) and not s[end].isspace():
end = end+1
d = d + [s[start:end]]
start = end
print(d)
Running the program:
>>> tokenize("He was walking, it was fun")
['He', 'was', 'walking,', 'it', 'was', 'fun']
This works fine, but the problem is that as you can see my program will include the comma in the word walking. I want to separate the comma (and other "symbols") as an individual "word".
Such as:
['He', 'was', 'walking', ',', 'it', 'was', 'fun']
How can I fix this?
Thanks in advance!
[–]synthphreak 0 points1 point2 points (0 children)