parser

package
v0.0.0-...-03ea0d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ItemType

func ItemType(s string) itemType

ItemType converts a string to itemType.

Types

type CFG

type CFG struct {
	// contains filtered or unexported fields
}

CFG defines the Context-Free Grammar, which is specified by its production rules. It implements the Grammar interface.

func NewCFGrammar

func NewCFGrammar(s string) *CFG

NewCFGrammar creates a new Context-Free Grammar. It loads the production rules from s.

func (*CFG) BuildTrees

func (g *CFG) BuildTrees(items Items) Trees

BuildTrees computes the parse trees from the input items using the Lange-Leiss implementation of the CYK algorithm. The grammar is assumed to be in the binary normal form. Lange and Leiss, "To CNF or not to CNF? An Efficient Yet Presentable Version of the CYK Algorithm", Informatica Didactica 8 (2009).

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element defines the element of the CYK state table.

func NewBinary

func NewBinary(l, r string) Element

NewBinary creates a binary element.

func NewUnary

func NewUnary(l string) Element

NewUnary creates an unary element.

func (Element) IsUnary

func (e Element) IsUnary() bool

IsUnary returns true if the element is unary.

func (Element) Set

func (e Element) Set(begin, split, end int) Element

Set sets the limits for the item spans that are used by the CYK algorithm.

type Grammar

type Grammar interface {
	BuildTrees(Items) Trees
}

Grammar defines the grammar interface to parse items into a parse tree.

type Interpreter

type Interpreter struct {
	// contains filtered or unexported fields
}

Interpreter defines the interpreter struct to convert unstructured criteria strings to structured relations.

func Get

func Get() *Interpreter

Get gets the interpreter to parse strings to relations.

func NewInterpreter

func NewInterpreter() *Interpreter

NewInterpreter creates a new interpreter.

func (*Interpreter) Interpret

func (i *Interpreter) Interpret(input string) (relation.Relations, relation.Relations)

Interpret interprets clinical trial criteria using parse trees and formal grammars.

type Item

type Item struct {
	// contains filtered or unexported fields
}

Item defines the lexical item that is syntactically constructed from the lexer token.

func NewItem

func NewItem(typ itemType, val string) *Item

NewItem creates a new item.

func UnknownItem

func UnknownItem() *Item

UnknownItem creates an unknown item.

func (*Item) Copy

func (i *Item) Copy(j *Item)

Copy copies the fields from the other item.

func (*Item) Equal

func (i *Item) Equal(j *Item) bool

Equal tests whether two items have the same fields.

func (*Item) Negate

func (i *Item) Negate() *Item

Negate applies 'not' operation to the comparison item.

func (*Item) Set

func (i *Item) Set(typ itemType, val string) *Item

Set sets the item fields.

func (*Item) String

func (i *Item) String() string

String returns the string representation of the item.

func (*Item) Valid

func (i *Item) Valid() bool

Valid tests whether the item is valid: the item type is not unknown and the value is not empty.

type Items

type Items []*Item

Items defines a slice of items.

func NewItems

func NewItems() Items

NewItems creates a slice of items.

func (*Items) Add

func (is *Items) Add(i *Item)

Add adds the item to the items.

func (Items) Empty

func (is Items) Empty() bool

Empty tests whether items has any items in it.

func (*Items) FixMissingVariable

func (is *Items) FixMissingVariable() bool

FixMissingVariable adds missing variable to the items, if it can be inferred from the unit.

func (Items) Get

func (is Items) Get(typ itemType) set.Set

Get gets the items of type 'typ'.

func (Items) LastType

func (is Items) LastType() itemType

func (Items) Len

func (is Items) Len() int

Len returns the length of the slice.

func (Items) String

func (is Items) String() string

String returns the string representation of the items.

func (*Items) TrimKnownItems

func (is *Items) TrimKnownItems()

TrimKnownItems merges consecutive variable, unit, and comparison items with the same name to one.

func (*Items) TrimRangeItems

func (is *Items) TrimRangeItems()

TrimRangeItems removes a range item if it precedes a comparison item.

func (*Items) TrimUnknownItems

func (is *Items) TrimUnknownItems()

TrimUnknownItems merges consecutive unknown items to one, removes the first item if it is an unknown item, and removes unknown items that precede or follow a variable or unit item.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer defines the struct that converts a string to tokens. Rob Pike's presentation has been a great inspiration for this design. https://www.youtube.com/watch?v=HxaD_trXwRE

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the input string.

func (*Lexer) Drain

func (l *Lexer) Drain() Tokens

Drain drains the output to the slice of tokens.

func (*Lexer) NextToken

func (l *Lexer) NextToken() *Token

NextToken returns the next token from the input string.

type List

type List []Items

List defines a slice of items.

func NewList

func NewList() List

NewList creates a new list.

func (List) FixMissingVariable

func (l List) FixMissingVariable()

FixMissingVariable adds missing variables to the list of items, if they can be inferred from the unit info.

func (List) Sort

func (l List) Sort()

Sort sorts the list elements by their length in descending order.

func (List) String

func (l List) String() string

String returns the string representation of the list.

func (List) TrimItems

func (l List) TrimItems()

TrimItems trims the unknown (typ = itemUnknown) and known items in the list.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node defines a node in the parse tree, which is constructed by applying grammar production rules to the parsed items.

func NewNode

func NewNode(val string) *Node

NewNode creates a new node.

func (*Node) Contains

func (n *Node) Contains(m *Node) bool

Contains returns true if n contains m and its left and right children.

func (*Node) EvalBound

func (n *Node) EvalBound() (*relation.Limit, bool)

EvalBound evaluates and returns the bound relation.

func (*Node) EvalNums

func (n *Node) EvalNums() []string

EvalNums evaluates and returns the list of numbers stored in the terminal leafs.

func (*Node) EvalRange

func (n *Node) EvalRange() *relation.Limit

EvalRange evaluates and returns the lower or upper bound of the range condition.

func (*Node) EvalRelation

func (n *Node) EvalRelation() (*relation.Relation, error)

EvalRelation evaluates and returns the relation stored in the parse node based on the production rules.

func (*Node) EvalRelations

func (n *Node) EvalRelations() (relation.Relations, relation.Relations)

EvalRelations evaluates and returns the 'or' and 'and' relations stored in the parse node.

func (*Node) EvalUnit

func (n *Node) EvalUnit() string

EvalUnit evaluates and returns the unit stored in the terminal leaf.

func (*Node) EvalVariable

func (n *Node) EvalVariable() string

EvalVariable evaluates and returns the variable name stored in the terminal leaf.

func (*Node) Size

func (n *Node) Size() int

Size calculates the number of leafs (terminals).

func (*Node) String

func (n *Node) String() string

String returns the string representation of the node.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser defines the parser logic for parsing clinical trial eligibility criteria.

func NewParser

func NewParser() *Parser

NewParser creates a new parser.

func (*Parser) Parse

func (p *Parser) Parse(input string) (criteria List)

Parse parses the input string to the list of criterion items.

type Pos

type Pos int

Pos is the rune position of the token in the string.

type RuleType

type RuleType int

RuleType defines the type of rules that are being loaded from the string.

type Rules

type Rules struct {
	// contains filtered or unexported fields
}

Rules define the grammar production rules.

func LoadRules

func LoadRules(s string) *Rules

LoadRules loads the grammar production rules from the string.

type Token

type Token struct {
	// contains filtered or unexported fields
}

Token defines a token or text string that is returned from the lexer.

func NewToken

func NewToken(typ tokenType, pos Pos, val string) *Token

NewToken creates a new token.

func (*Token) String

func (t *Token) String() string

String returns a string representation of the token.

type Tokens

type Tokens []*Token

Tokens defines a slice of tokens.

func NewTokens

func NewTokens() Tokens

NewTokens creates a slice of tokens.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree defines the parse tree.

func NewTree

func NewTree(root *Node, score float64) *Tree

NewTree creates a new parse tree.

func (*Tree) Contains

func (t *Tree) Contains(v *Tree) bool

Contains returns true if the tree t contains the tree v as a sub-tree or they are same.

func (*Tree) Relations

func (t *Tree) Relations() (relation.Relations, relation.Relations)

Relations converts the tree to 'or' and 'and' relations.

func (*Tree) Size

func (t *Tree) Size() int

Size calculates the number of leafs (terminals).

func (*Tree) String

func (t *Tree) String() string

String returns the string representation of the tree.

type Trees

type Trees []*Tree

Trees defines a slice of parse trees.

func NewTrees

func NewTrees() Trees

NewTrees creates a new slice of trees.

func (*Trees) Dedupe

func (ts *Trees) Dedupe()

Dedupe deduplicates trees by removing exact duplicates and stumps that are contained by other trees.

func (Trees) Empty

func (ts Trees) Empty() bool

Empty tests whether ts has any trees in it.

func (Trees) Relations

func (ts Trees) Relations() (relation.Relations, relation.Relations)

Relations converts the trees to 'or' and 'and' relations.

func (Trees) String

func (ts Trees) String() string

String returns the string representation of the trees.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL