A BKTree implementation

See add to insert a new word. See search to find matches from a key.

Introduced properties

fun add(key: String)

trees :: BKTree :: add

Add a key in the tree
fun default_tolerance: Int

trees :: BKTree :: default_tolerance

Default tolerance used to find matches
fun default_tolerance=(default_tolerance: Int)

trees :: BKTree :: default_tolerance=

Default tolerance used to find matches
fun search(key: String, tolerance: nullable Int): Array[BKMatch]

trees :: BKTree :: search

Search key with a distance of tolerance in self.

Redefined properties

redef type SELF: BKTree

trees $ BKTree :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun add(key: String)

trees :: BKTree :: add

Add a key in the tree
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun default_tolerance: Int

trees :: BKTree :: default_tolerance

Default tolerance used to find matches
fun default_tolerance=(default_tolerance: Int)

trees :: BKTree :: default_tolerance=

Default tolerance used to find matches
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun search(key: String, tolerance: nullable Int): Array[BKMatch]

trees :: BKTree :: search

Search key with a distance of tolerance in self.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram trees::BKTree BKTree core::Object Object trees::BKTree->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

trees $ BKTree
# A BKTree implementation
#
# See `add` to insert a new word.
# See `search` to find matches from a `key`.
class BKTree

	# Default tolerance used to find matches
	#
	# Default is `2`.
	var default_tolerance = 2 is writable

	# Tree root
	private var root: nullable BKNode = null

	# Add a `key` in the tree
	fun add(key: String) do
		var root = self.root
		if root == null then
			self.root = new BKNode(key)
			return
		end

		var node = root
		var dist = node.key.levenshtein_distance(key)

		while node.has_key(dist) do
			if dist == 0 then return
			node = node[dist]
			dist = node.key.levenshtein_distance(key)
		end
		node[dist] = new BKNode(key)
	end

	# Search `key` with a distance of `tolerance` in `self`.
	#
	# If `tolerance` is null, the use `default_tolerance` instead.
	fun search(key: String, tolerance: nullable Int): Array[BKMatch] do
		var res = new Array[BKMatch]
		var root = self.root
		if root != null then
			if tolerance == null then tolerance = self.default_tolerance
			search_recursive(root, res, key, tolerance)
		end
		default_comparator.sort(res)
		return res
	end

	private fun search_recursive(node: BKNode, res: Array[BKMatch], key: String, tolerance: Int) do
		var dist = node.key.levenshtein_distance(key)
		var min = dist - tolerance
		var max = dist + tolerance

		if dist < tolerance then
			res.add new BKMatch(dist, node.key)
		end

		for odist, child in node do
			if odist < min or odist > max then continue
			search_recursive(child, res, key, tolerance)
		end
	end
end
lib/trees/bktree.nit:50,1--111,3