Data Types

This library contains various extra data types, based to a certain extend on MIT-licensed code from Peter Norvig, AI: A Modern Appproach :

class pynlpl.datatypes.FIFOQueue(data=[])

A First-In-First-Out Queue


Append all elements from items to the queue


Retrieve the next element in line, this will remove it from the queue

class pynlpl.datatypes.Pattern(data, classdecoder=None)
static fromstring(s, classencoder)
iterbytes(begin=0, end=0)
class pynlpl.datatypes.PatternMap(default=None)
class pynlpl.datatypes.PatternSet
class pynlpl.datatypes.PriorityQueue(data=[], f=<function PriorityQueue.<lambda>>, minimize=False, length=0, blockworse=False, blockequal=False, duplicates=True)

A queue in which the maximum (or minumum) element is returned first, as determined by either an external score function f (by default calling the objects score() method). If minimize=True, the item with minimum f(x) is returned first; otherwise is the item with maximum f(x) or x.score().

length can be set to an integer > 0. Items will only be added to the queue if they’re better or equal to the worst scoring item. If set to zero, length is unbounded. blockworse can be set to true if you want to prohibit adding worse-scoring items to the queue. Only items scoring better than the BEST one are added. blockequal can be set to false if you also want to prohibit adding equally-scoring items to the queue. (Both parameters default to False)


Adds an item to the priority queue (in the right place), returns True if successfull, False if the item was blocked (because of a bad score)


Retrieve the next element in line, this will remove it from the queue


prune all but the first (=best) n items

prunebyscore(score, retainequalscore=False)

Deletes all items below/above a certain score from the queue, depending on whether minimize is True or False. Note: It is recommended (more efficient) to use blockworse=True / blockequal=True instead! Preventing the addition of ‘worse’ items.


prune down to n items at random, disregarding their score


Return the score for item x (cheap lookup), Item 0 is always the best item


prune down to n items, chance of an item being pruned is reverse proportional to its score

class pynlpl.datatypes.Queue
Queue is an abstract class/interface. There are three types:
Python List: A Last In First Out Queue (no Queue object necessary). FIFOQueue(): A First In First Out Queue. PriorityQueue(lt): Queue where items are sorted by lt, (default <).
Each type supports the following methods and functions:
q.append(item) – add an item to the queue q.extend(items) – equivalent to: for item in items: q.append(item) q.pop() – return the top item from the queue len(q) – number of items in q (also q.__len()).

Append all elements from items to the queue

class pynlpl.datatypes.Tree(value=None, children=None)

Simple tree structure. Nodes are themselves trees.


Add an item to the Tree


Is this a leaf node or not?

class pynlpl.datatypes.Trie(sequence=None)

Simple trie structure. Nodes are themselves tries, values are stored on the edges, not the nodes.


Returns the depth of the current node


Is this a leaf node or not?


Returns the path to the current node


Returns True if this is the root of the Trie


Size is number of nodes under the trie, including the current node

walk(leavesonly=True, maxdepth=None, _depth=0)

Depth-first search, walking through trie, returning all encounterd nodes (by default only leaves)