-
-
Save alexanderk23/b55064a337f0e96109d6 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
#!/usr/bin/env python | |
from __future__ import print_function | |
def find_ranges(text): | |
s = 0 | |
q = pc = '' | |
start = None | |
ranges = [] | |
seps = " .,:;-\r\n" | |
quotes = "'\"" | |
for i, c in enumerate(text + "\n"): | |
if s == 0 and c in quotes and pc in seps: | |
start = i | |
s = 1 | |
q = c | |
elif s == 1 and c==q: | |
s = 2 | |
elif s == 2: | |
if c in seps: | |
ranges.append((start+1, i-1)) | |
start = None | |
s = 0 | |
else: | |
s = 1 | |
pc = c | |
return ranges | |
def pos_in_ranges(ranges, pos): | |
for start,end in ranges: | |
if start<=pos<end: return True | |
return False | |
if __name__ == '__main__': | |
TESTCASES = ( | |
"The dog's bark was loud", | |
"He was 6' tall", | |
"There's something funny smelling in John's sisters' apartment.", | |
'This sentence has a "quotation", if you can even "oops" call it that.', | |
"John's sister said, \"The funny smell might be John's 'friend' Jake.\"", | |
"John said, \"He told me that yesterday.\n\"But now I've forgotten.\n\"I asked him to tell me again.\"\nHello" | |
) | |
for text in TESTCASES: | |
print(text) | |
ranges = find_ranges(text) | |
for i, c in enumerate(text): | |
r = 'T' if pos_in_ranges(ranges, i) else 'F' | |
e = c if c == "\n" else '' | |
print(r, end=e) | |
print("\n") | |
Author
alexanderk23
commented
Feb 28, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment