Last active
June 26, 2017 04:13
-
-
Save MatrixManAtYrService/8da457e88c7125bb578b37ca3edd34cd to your computer and use it in GitHub Desktop.
Loosely coupling a semantic brushstroke to a sentence
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
import unittest | |
from token_primer import Tokens | |
import re | |
class TokenRef(unittest.TestCase): | |
def test_search_generators(self): | |
# two canvasses, both pointing to a name with a colon | |
canvas_A = "His hobby is: spinning gold. His name is: Rumplestiltskin" | |
canvas_B = "Hos hobby is: spinning gold. His name is kind of funny: Rumplestiltskin" | |
# semantic paint for either canvas: | |
word_mask = re.compile(r'\b\w+\b') | |
def is_name(candidate): | |
try: | |
if next(candidate.before()).suffix == ': ': | |
for preceeding_token in candidate.before(): | |
if preceeding_token.string == 'name': | |
return True | |
except StopIteration: | |
pass | |
return False | |
# semantic paint for canvas A: | |
A = Tokens(canvas_A, word_mask) | |
name_A = list(filter(is_name, A))[0] | |
# semantic paint for canvas B: | |
B = Tokens(canvas_B, word_mask) | |
name_B = list(filter(is_name, B))[0] | |
# test code: the above paint should indicate the same name, despite the canvas differences | |
self.assertEqual(name_A.string, "Rumplestiltskin") | |
self.assertEqual(name_B.string, "Rumplestiltskin") | |
def test_search_slices(self): | |
# two canvasses, both pointint to a name with a colon | |
canvas_A = "His hobby is: spinning gold. His name is: Rumplestiltskin" | |
canvas_B = "Hos hoppy is: spinning gold. His name is kind of funny: Rumplestiltskin" | |
word_mask = re.compile(r'\b\w+\b') | |
# semantic paint for canvas A: | |
A = Tokens(canvas_A, word_mask) | |
name_A = next((y for (x, y) in zip(A['name'::'i'], A['name'::'e']) if x.suffix[0] == ':')) | |
# semantic paint for canvas B: | |
B = Tokens(canvas_B, word_mask) | |
name_B = next((y for (x, y) in zip(A['name'::'i'], A['name'::'e']) if x.suffix[0] == ':')) | |
# test code: the above paint should indicate the same name, despite the canvas differences | |
self.assertEqual(name_A.string, "Rumplestiltskin") | |
self.assertEqual(name_B.string, "Rumplestiltskin") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment