Skip to content

Commit 602c8ef

Browse files
authored
README update (#13)
1 parent ef6e4fd commit 602c8ef

File tree

2 files changed

+18
-56
lines changed

2 files changed

+18
-56
lines changed

README.md

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ and if the search will be case sensitive or not.
3232
There are 3 "implementations" of `SubstringEngine` that uses the libraries from
3333
https://github.com/cloudflare/ahocorasick,
3434
https://github.com/anknown/ahocorasick and
35-
https://github.com/petar-dambovaliev/aho-corasick and a regexp implementation for the `RegexEngine`.
35+
https://github.com/pedroegsilva/ahocorasick (fork from cloudflare with the addition for matching positions) and a regexp implementation for the `RegexEngine`.
3636
But any other library can be used as long as it "implements" the `SubstringEngine` or `RegexEngine` interface.
3737
```go
3838
subEng := &finder.CloudflareForkEngine{}
@@ -41,7 +41,7 @@ But any other library can be used as long as it "implements" the `SubstringEngin
4141
findthem := finder.NewFinder(subEng, rgxEng, caseSensitive)
4242
```
4343

44-
Then you need to add the expressions that need to be solved.
44+
Then you need to add the expressions that need to be solved and a tag for that expression.
4545
```go
4646
if err := findthem.AddExpressionWithTag(`r"Lorem" and "ipsum"`, "test"); err != nil {
4747
log.Fatal(err)
@@ -64,7 +64,7 @@ Then you need to add the expressions that need to be solved.
6464
}
6565
```
6666

67-
And finally you can check which expressions match on each text.
67+
And finally you can check which expressions were match on each text.
6868
```go
6969
for i, text := range texts {
7070
resp, err := findthem.ProcessText(text)
@@ -143,58 +143,25 @@ You can also get a pretty format to see the created Abstract Syntax Tree (AST).
143143
fmt.Printf("pretty format:\n%s\n", expression.PrettyFormat())
144144
```
145145

146-
There are two ways to solve the expression.
146+
To solve the expression we need a map of the terms that were matched and the position of each match. The positions are needed to solve the INORD operator and must be sorted to work properly, if there is no INORD operator in the expression, an empty array or a nil value will suffice.
147147

148-
Recursively:
148+
solving example:
149149
```go
150-
matches := map[string]dsl.PatternResult{
151-
"foo": dsl.PatternResult{
152-
Val: true,
153-
SortedMatchPos: []int{0, 2, 5},
154-
},
155-
"bar": dsl.PatternResult{
156-
Val: true,
157-
SortedMatchPos: []int{3},
158-
},
159-
"dolor": dsl.PatternResult{
160-
Val: true,
161-
SortedMatchPos: []int{1, 7},
162-
},
163-
}
150+
matches := map[string][]int{
151+
"foo": {0, 2, 5},
152+
"bar": {3},
153+
"dolor": {1, 7},
154+
}
164155

165-
responseRecursive, err := expression.Solve(matches, false)
166-
if err != nil {
167-
log.Fatal(err)
168-
}
169-
fmt.Println("recursive eval ", responseRecursive)
170-
```
171-
Iteratively:
172-
```go
173-
solverArr := expression.CreateSolverOrder()
174-
responseIter, err := solverArr.Solve(matches, false)
175-
if err != nil {
176-
log.Fatal(err)
177-
}
178-
fmt.Println("iterative eval ", responseIter)
156+
response, err := expression.Solve(matches)
157+
if err != nil {
158+
log.Fatal(err)
159+
}
160+
fmt.Println("eval ", response)
179161
```
180-
The Iterative solution needs to create an array with the order in which the expressions need to be solved.
181-
It is faster than the recursive if you need to solve the expression more than 8 times (the gain in performance is around 13% from the benchmark results)
182-
183-
The solvers also need to know if the map of matches is complete or not. If it is complete it will have the term as a key even if it was a no match.
184-
The incomplete option will assume that if a key is not present the term was not found.
185-
If an incomplete map is provided and the key is not found an error will be returned.
186-
187-
```go
188-
// should return an error
189-
_, err = expression.Solve(matches, true)
190-
if err != nil {
191162

192-
log.Fatal(err)
193-
}
194-
}
195-
196-
```
197163
The complete example can be found at `/examples/dsl/main.go`
164+
198165
## Run Locally
199166
This project uses Bazel to build and test the code.
200167
You can run this project using go as well.

examples/dsl/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,10 @@ func main() {
3030
"dolor": {1, 7},
3131
}
3232

33-
responseRecursive, err := expression.Solve(matches)
33+
response, err := expression.Solve(matches)
3434
if err != nil {
3535
log.Fatal(err)
3636
}
37-
fmt.Println("recursive eval ", responseRecursive)
37+
fmt.Println("eval ", response)
3838

39-
// should return an error
40-
_, err = expression.Solve(matches)
41-
if err != nil {
42-
log.Fatal(err)
43-
}
4439
}

0 commit comments

Comments
 (0)