Skip to main content

PyPI

KeywordTree

A Python data structure that maps arbitrary text to values by using a decision-tree-like algorithm based on counting keyword occurrences.

KeywordTree

How it works

  • Each node contains a list of keywords and a value if it's a leaf.
  • A query is given as a string and is broken into tokens by a tokenizer.
  • Starting from the root, the query follows the child with the most keywords present in the query token list.
  • When a leaf is reached, its value is returned.
  • If there are multiple nodes with the most keywords in the token list, the query follows all of them and the results are merged at the end into a list.

Example

Suppose we have the above tree and the query "My pug is so young it can barely woof".

The default tokenizer converts the query to lowercase and splits at whitespaces, so the tokens will be ['my', 'pug', 'is', 'so', 'young', 'it', 'can', 'barely','woof'].

The query will follow the left child of the root, since it has one match for 'woof' and will then match the child with 'pug' in its keywords. Since it's a leaf, its value will be in the final result: ['Pug'].

Usage

KeywordTree instances are constructed from tuples of the form (list of keywords, list of children described as tuples, value).

Non-leaf nodes must have value equal to None.

from keywordtree import KeywordTree

kt = KeywordTree([], [
    (['dog', 'woof'], [
        (['bulldog'], [], 'Bulldog'),
        (['pug'], [], 'Pug'),
        (['boxer'], [], 'Boxer'),
    ], None),
    (['cat', 'meow'], [
        (['persian'], [], 'Persian'),
        (['bengal'], [], 'Bengal'),
        (['siamese'], [], 'Siamese'),
    ], None),
], None)

kt.query('My pug is so young it can barely woof')
# ['Pug']

Other tokenizers can be used as well:

from keywordtree import KeywordTree
from nltk.tokenize.stanford import StanfordTokenizer

stftk = StanfordTokenizer()

def tokenizer(s):
    return stftk.tokenize(s)

kt = KeywordTree([], [
    # list of children
], None, tokenizer=tokenizer)

License

Copyright 2019 Theodoros Diamantidis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for keywordtree 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keywordtree 0.1.6
File Size Uploaded
keywordtree-0.1.6.tar.gz 4.5 kB Details

Release files / keywordtree-0.1.6.tar.gz

Download URL keywordtree-0.1.6.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
33dba5830c8b7d0e3907e26ba826a12c35dbd7e7c39f3a8ff3645c7e6d9b2881
BLAKE2b-256 checksum
How to use checksums
8187fca908c5f190626d06a25dae039dc27dd0d58d7dffe38b2807ccebfb9caf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.1

Release history Release notifications | RSS feed

This release

0.1.6 This release

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page