Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render_example_test.gno

2.18 Kb · 61 lines
 1package ahocorasickdemo
 2
 3// ExampleRender pins the realm's root Render output as a testable example.
 4// The `ushers` rows are the textbook automaton's published answer, so this
 5// example doubles as a conformance test for the library.
 6func ExampleRender() {
 7	print(Render(""))
 8	// Output:
 9	// # Aho-Corasick
10	//
11	// One automaton for the whole dictionary, one pass over the text, demoing the [`p/moul/x/daily/ahocorasick`](/p/moul/x/daily/ahocorasick/v0) library.
12	//
13	// ## The 1975 example
14	//
15	// Patterns `he`, `she`, `his`, `hers` over `ushers`. Three of the four overlap, and `hers` ends where nothing else starts.
16	//
17	// | start | end | pattern |
18	// | --- | --- | --- |
19	// | `1` | `4` | `she` |
20	// | `2` | `4` | `he` |
21	// | `2` | `6` | `hers` |
22	//
23	// ## One pass over a sentence
24	//
25	// Patterns `the`, `he`, `fox`, `over`, `lazy` over `the quick brown fox jumps over the lazy dog`.
26	//
27	// | start | end | pattern |
28	// | --- | --- | --- |
29	// | `0` | `3` | `the` |
30	// | `1` | `3` | `he` |
31	// | `16` | `19` | `fox` |
32	// | `26` | `30` | `over` |
33	// | `31` | `34` | `the` |
34	// | `32` | `34` | `he` |
35	// | `35` | `39` | `lazy` |
36	//
37	// `5` patterns, `17` automaton nodes, `43` bytes read once, `7` matches. Running a single-pattern scan per word would read those bytes `5` times instead.
38	//
39	// > Scan your own text: append it to the path, for instance [`/thermos`](/r/moul/x/daily/ahocorasickdemo/v0:/thermos), where `the` and `he` overlap on the same three letters.
40}
41
42// ExampleRenderScan pins the argument path: the visitor's text is scanned with
43// the same dictionary, and the table reports offsets and dictionary words only.
44func ExampleRenderScan() {
45	print(Render("/thermos"))
46	// Output:
47	// # Aho-Corasick
48	//
49	// One automaton for the whole dictionary, one pass over the text, demoing the [`p/moul/x/daily/ahocorasick`](/p/moul/x/daily/ahocorasick/v0) library.
50	//
51	// ## `thermos`
52	//
53	// Patterns `the`, `he`, `fox`, `over`, `lazy`.
54	//
55	// | start | end | pattern |
56	// | --- | --- | --- |
57	// | `0` | `3` | `the` |
58	// | `1` | `3` | `he` |
59	//
60	// `5` patterns, `17` automaton nodes, `7` bytes read once, `2` matches. Running a single-pattern scan per word would read those bytes `5` times instead.
61}