Skip to content

Instantly share code, notes, and snippets.

@alexanderk23
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderk23/b55064a337f0e96109d6 to your computer and use it in GitHub Desktop.
Save alexanderk23/b55064a337f0e96109d6 to your computer and use it in GitHub Desktop.
#!/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")
@alexanderk23
Copy link
Author

$ ./quotes.py 
The dog's bark was loud
FFFFFFFFFFFFFFFFFFFFFFF

He was 6' tall
FFFFFFFFFFFFFF

There's something funny smelling in John's sisters' apartment.
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

This sentence has a "quotation", if you can even "oops" call it that.
FFFFFFFFFFFFFFFFFFFFFTTTTTTTTTFFFFFFFFFFFFFFFFFFFFTTTTFFFFFFFFFFFFFFF

John's sister said, "The funny smell might be John's 'friend' Jake."
FFFFFFFFFFFFFFFFFFFFFTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTF

John said, "He told me that yesterday.
"But now I've forgotten.
"I asked him to tell me again."
Hello
FFFFFFFFFFFFTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTFF
FFFFF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment