Documentation
¶
Overview ¶
Package rules parses, validates and explains match rules, a small pattern language for short texts such as job titles, and matches a set of them against one text at a time. The textindex and ruleindex packages match them at scale with the same results. README.md describes the syntax.
Index ¶
- Constants
- func Compile(set Set) (*Compiled, []Problem)
- func Explain(rule string) string
- func Inflections(word string) []string
- func Messages(err error) []string
- func Words(text string) []string
- type Compiled
- type Form
- type Kind
- type Node
- type NodeType
- type Normalizer
- type Pattern
- type Problem
- type Rule
- type Set
- type SyntaxError
- type Term
Examples ¶
Constants ¶
const ( // MinWildcardLetters is the least number of letters a word with * or ~ may have. MinWildcardLetters = 4 // MaxJoinedForms caps the joined forms one term may expand to. They are the cartesian product of a // compound's groups; beyond this a rule is a word list in disguise and should be split. MaxJoinedForms = 2000 )
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
Compile parses every include and exclude. The result is always usable: the problems are the entries left out of it, and it is up to the caller whether those make the whole set unacceptable.
Example ¶
package main
import (
"fmt"
"github.com/Back-to-code/f2f-match-utils/rules"
)
func main() {
set, problems := rules.Compile(rules.Set{
Includes: []string{"(magazijn|warehouse)+medewerker~", "*er"},
Excludes: []string{"*manager"},
})
for _, problem := range problems {
fmt.Println(problem)
}
fmt.Println(set.Match("Magazijn-medewerkers"))
fmt.Println(set.Match("magazijnmanager"))
}
Output: includes[1] '*er': a word with * or ~ needs at least 4 letters, 'er' has 2 0 -1
func Explain ¶
Explain describes in plain text what a rule covers: per term its alternatives, the joined and apart forms of compounds and the words every ~ stands for. For an invalid rule it lists the problems.
Example ¶
package main
import (
"fmt"
"github.com/Back-to-code/f2f-match-utils/rules"
)
func main() {
fmt.Println(rules.Explain("magazijn+medewerker~ -hoofd*"))
}
Output: rule: magazijn+medewerker~ -hoofd* term 1 must match: magazijn+medewerker~ one word: magazijnmedewerker~ or apart, each part some word, any order: magazijn & medewerker~ term 2 must NOT match: hoofd* (prefix) ~ forms (a joined form ending in a ~ word takes the same endings): medewerker~ = medewerker, medewerkers, medewerkere, medewerkeren, medewerkeres, medewerkern, medewerkster, medewerksters
func Inflections ¶
Inflections returns the forms `word~` stands for, word itself first.
func Messages ¶
Messages returns the problems of a *SyntaxError, or the error's text for any other error.
func Words ¶
Words returns the word set a rule is matched against: the words of sanitize.SplitWords plus every two adjacent words joined into one, without duplicates, real words first.
Example ¶
package main
import (
"fmt"
"github.com/Back-to-code/f2f-match-utils/rules"
)
func main() {
fmt.Println(rules.Words("Magazijn-medewerker (o.a. heftruck)"))
}
Output: [magazijn medewerker oa heftruck magazijnmedewerker medewerkeroa oaheftruck]
Types ¶
type Compiled ¶
Compiled is a Set with its includes and excludes parsed. An invalid entry is nil, so every index still points into the Set's lists.
func MustCompile ¶
MustCompile is Compile for sets known to be valid; it panics on the first problem.
func (*Compiled) MatchWords ¶
MatchWords returns the index in Includes of the first include that matches the word set, or -1 when none does or an exclude matches. words is a word set as returned by Words.
This evaluates every rule on its own, which is fine for a few texts and rules; for many of either, use the textindex or ruleindex packages.
type Form ¶
Form is a folded literal, or the joined form of a compound, with its wildcards.
type Node ¶
type Node struct {
Type NodeType
// Raw, Lead, Trail and Tilde are the literal as written; Form is it folded.
Raw string
Lead bool
Trail bool
Tilde bool
Form Form
Children []*Node
// Forms are a compound's joined forms.
Forms []Form
// Patterns are a literal's patterns, or the patterns of a compound's joined forms.
Patterns []Pattern
}
Node is a literal, a group of alternatives (Children are the options) or a compound (Children are the parts).
func (*Node) MatchWords ¶
MatchWords reports whether the node matches a word set as returned by Words.
type Normalizer ¶
type Normalizer struct {
// RealWords is how many of the last Words result are real words; the joined pairs follow them.
RealWords int
// contains filtered or unexported fields
}
Normalizer turns texts into word sets. It reuses its buffers between calls, so it is not safe for concurrent use; the zero value is ready to use.
func (*Normalizer) Words ¶
func (n *Normalizer) Words(text string) []string
Words is the package level Words without the copy: the returned slice is only valid until the next call, the strings in it stay valid.
type Rule ¶
type Rule struct {
// Source is the rule as written, without surrounding whitespace.
Source string
Terms []*Term
}
Rule matches a text when every positive term matches it and no negative term does.
func MustParseRule ¶
func ParseRule ¶
ParseRule parses one rule. On failure the error is a *SyntaxError that lists every problem found, worded so it can be shown to whoever wrote the rule.
func (*Rule) MatchWords ¶
MatchWords reports whether the rule matches a word set as returned by Words.
func (*Rule) NormalizedSource ¶
NormalizedSource prints the rule in normalised syntax: folded literals, single spaces between terms and only the parentheses that are needed. Rules that print the same match the same.
type Set ¶
Set is the rules for one kind of text: a text matches when one of the includes matches it and none of the excludes does.
func (Set) Clean ¶
Clean trims every include and exclude and drops the empty ones. The lists are never nil, so a cleaned set marshals as [] instead of null.
func (Set) PruneIncludes ¶
PruneIncludes returns the set with every include blanked whose matches the other includes already have, and with the alternatives left out of an include that only bring such matches, so it matches the same texts. The int is how many includes it blanked or rewrote.
Every index keeps pointing into the original lists, as with Compile; Clean drops the blanks. A rewritten include comes back in normalised syntax, the others as written. Invalid includes and the excludes are passed through.
It reads the rules alone, never texts, so it only prunes what it can prove: an include stays when it takes several others together to cover it.
Example ¶
package main
import (
"fmt"
"github.com/Back-to-code/f2f-match-utils/rules"
)
func main() {
set := rules.Set{Includes: []string{"*bouw*", "bouw+techniek", "(bouw|installatie)+techniek"}}
pruned, changed := set.PruneIncludes()
fmt.Printf("%q %d\n", pruned.Includes, changed)
}
Output: ["*bouw*" "" "installatie+techniek"] 2
type SyntaxError ¶
SyntaxError lists every problem found in one rule or exclude.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type Term ¶
Term is one space separated part of a rule, or an exclude.
func ParseExclude ¶
ParseExclude parses an exclude: one term, without spaces and without a leading '-'. On failure the error is a *SyntaxError.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
synth
Package synth generates made-up texts and rule sets for tests and benchmarks.
|
Package synth generates made-up texts and rule sets for tests and benchmarks. |
|
Package ruleindex matches texts against many rule sets at once: the sets are indexed up front, the texts are not.
|
Package ruleindex matches texts against many rule sets at once: the sets are indexed up front, the texts are not. |
|
Package textindex matches rule sets against many texts at once: the texts are indexed up front, the rule sets are not.
|
Package textindex matches rule sets against many texts at once: the texts are indexed up front, the rule sets are not. |