Case Studies
Text Index
A tiny text index records which documents contain an exact word.
exact-word search
Splitting text into words makes an exact membership test visible in the trace.
Text Index
text_index.py
Replay: real traced execution (multi-file project)
documents = [
"trace source code",
"source replay view",
"static trace page",
]
term = "trace"
matches = []
for number, text in enumerate(documents, start=1):
words = text.split()
if term in words:
matches.append(str(number))
print("matches=" + ",".join(matches))
documents = [
"trace source code",
"source replay view",
"static trace page",
]
term = "source"
matches = []
for number, text in enumerate(documents, start=1):
words = text.split()
if term in words:
matches.append(str(number))
print("matches=" + ",".join(matches))
documents ← ['trace source code', 'source replay view', 'static trace page']
1documents→ ['trace source code', 'source replay view', 'static trace page'] = [2 "trace source code",3 "source replay view",4 "static trace page",5]67term→ trace = "trace" #@term="source"89matches→ [] = []10for number, text in enumerate(documents, start=1):words ← ['trace', 'source', 'code']
pass 1 of 39matches = []10for number1, texttrace source code in enumerate(documents['trace source code', 'source replay view', 'static trace page'], start=1):11 words→ ['trace', 'source', 'code'] = texttrace source code.split()12 if term in words:All 3 passes — pass 1 is the card above pass numbertexttermwordsmatches1 1 trace source code trace ['trace', 'source', 'code'] [] → ['1'] 2 2 source replay view — ['source', 'replay', 'view'] — 3 3 static trace page trace ['static', 'trace', 'page'] ['1'] → ['1', '3'] matches ← ['1']
pass 1 of 211words = text.split()12if termtrace in words['trace', 'source', 'code']:13 matches→ ['1'].append(str(number1))matches ← ['1', '3']
pass 2 of 211words = text.split()12if termtrace in words['static', 'trace', 'page']:13 matches→ ['1', '3'].append(str(number3))print("matches=" + ",".join(matches))
15print("matches=" + ",".join(matches['1', '3']))outputmatches=1,3
documents ← ['trace source code', 'source replay view', 'static trace page']
1documents→ ['trace source code', 'source replay view', 'static trace page'] = [2 "trace source code",3 "source replay view",4 "static trace page",5]67term→ source = "source"89matches→ [] = []10for number, text in enumerate(documents, start=1):words ← ['trace', 'source', 'code']
pass 1 of 39matches = []10for number1, texttrace source code in enumerate(documents['trace source code', 'source replay view', 'static trace page'], start=1):11 words→ ['trace', 'source', 'code'] = texttrace source code.split()12 if term in words:All 3 passes — pass 1 is the card above pass numbertexttermwordsmatches1 1 trace source code source ['trace', 'source', 'code'] [] → ['1'] 2 2 source replay view source ['source', 'replay', 'view'] ['1'] → ['1', '2'] 3 3 static trace page — ['static', 'trace', 'page'] — matches ← ['1']
pass 1 of 211words = text.split()12if termsource in words['trace', 'source', 'code']:13 matches→ ['1'].append(str(number1))matches ← ['1', '2']
pass 2 of 211words = text.split()12if termsource in words['source', 'replay', 'view']:13 matches→ ['1', '2'].append(str(number2))print("matches=" + ",".join(matches))
15print("matches=" + ",".join(matches['1', '2']))outputmatches=1,2