Created
January 10, 2016 14:40
-
-
Save shuding/6a00e293f620e8b56e15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf-8 | |
import re | |
s = """It was a very cold evening, an old man was waiting for a ride across the river. He saw several horsemen pass by but he didn’t ask for any help. The wait seemed __1__(end).Then came another rider,the old man __2__(catch) his eye and said, “Sir, would you mind doing me a favor?” | |
Stopping his horse,he replied, “Of course.” Almost __3__(freeze), the old man could not get __4__ the ground. The horseman helped him onto his horse. He took the old man not just across the river,__5__ to his home. | |
“Sir, you didn’t even ask the other riders for help, why? What __6__ I had said ‘no’ and left you there?” the horseman asked. | |
The old man looked at him straight in the eyes and said, “I looked into their eyes, I found they didn’t care,__7__ told me it would be useless, but when I looked into __8__,I saw kindness.” | |
These words touched the rider deeply. “Thank you for __9__ you’ve said, I hope I will never be too busy to help others.” with that, Thomas Jefferson, the __10__(three) president of the US, turned his horse around and went away.""" | |
def parse(str): | |
ret = [] | |
words = str.replace(',', ',').replace('.', '.').replace('?', '?').replace(';',' ').replace(',',' ').replace('.', ' ').replace('“', ' ').replace('”', ' ').replace('"', ' ').replace('’', '\'').replace('‘', '\'').replace('?', ' ').replace('\'', ' ').replace('\n', ' ').replace('\t', ' ').split(' ') | |
key = 0 | |
for w in words: | |
if len(w): | |
if (w.startswith('_')): | |
key += 1 | |
if (w.endswith(')')): | |
ret.append([re.match(r".+(.+)", w).group(0)]) | |
else: | |
ret.append([w, key]) | |
else: | |
ret.append([w, 0]) | |
print ret | |
parse(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment